forked from mic159/ArduinoMesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeshBase.cpp
199 lines (178 loc) · 4.71 KB
/
MeshBase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <Arduino.h>
#include <RF24.h>
#include "MeshBase.h"
#define MAX_PACKET_SIZE 32
#define MAX_PAYLOAD_SIZE (MAX_PACKET_SIZE - sizeof(Message))
// -- Broadcast addresses --
#define PEER_DISCOVERY 1
// -- Helpers --
#define TO_BROADCAST(x) (0xBB00000000LL + x)
#define TO_ADDRESS(x) (0xAA00000000LL + x)
#define PEER_DISCOVERY_TIME 3000
#define PEER_CHECK_TIME 4000
#define PEER_TIMEOUT 2
MeshBase::MeshBase(uint8_t ce, uint8_t cs)
: radio(ce, cs)
, address(0)
, last_broadcast_time(0)
, last_peer_check_time(0)
{}
void MeshBase::Begin()
{
radio.begin();
radio.enableDynamicPayloads();
radio.setRetries(2,1);
//radio.openReadingPipe(0, TO_ADDRESS(address));
radio.openReadingPipe(1, TO_BROADCAST(PEER_DISCOVERY));
radio.setAutoAck(0, true);
radio.setAutoAck(1, false);
radio.startListening();
}
void MeshBase::Update()
{
// Periodic sends
if (millis() - last_broadcast_time > PEER_DISCOVERY_TIME)
{
if (!IsReady())
ChooseAddress();
SendPeerDiscovery();
}
// Recieve
uint8_t pipe_num;
if (radio.available(&pipe_num))
{
bool done = false;
do {
uint8_t len = radio.getDynamicPayloadSize();
byte buff[MAX_PAYLOAD_SIZE];
done = radio.read(buff, len);
HandlePacket(buff, len);
} while (!done);
}
// Update peers
if (millis() - last_peer_check_time > PEER_CHECK_TIME)
{
LinkedList<Peer>::Node* current = peers.first;
while(current != NULL)
{
current->item->time += 1;
if (current->item->time >= PEER_TIMEOUT)
{
Serial.print("Lost Peer: ");
Serial.println(current->item->address, DEC);
current = peers.Remove(current);
} else {
current = current->next;
}
}
last_peer_check_time = millis();
}
}
void MeshBase::HandlePacket(const byte* data, uint8_t len)
{
if (len < sizeof(Message))
return;
const MeshBase::Message* msg = (struct MeshBase::Message*)data;
uint8_t payload_length = len - sizeof(Message);
const byte* payload = data + sizeof(Message);
if (msg->split_more || msg->split_part != 0)
{
// Re-assembly needed
// TODO: Re-assemble packets
} else {
switch(msg->type) {
case type_peer_discovery:
HandlePeerDiscovery(msg, payload, payload_length);
break;
default:
OnMessage(msg, payload, payload_length);
break;
}
delete data;
}
}
void MeshBase::HandlePeerDiscovery(const MeshBase::Message* msg, const void* buff, uint8_t length)
{
if (length != sizeof(PeerDiscoveryMessage))
return;
const PeerDiscoveryMessage* pd = (struct PeerDiscoveryMessage*)buff;
if (pd->protocol_version != 1)
return;
Peer* peer = GetPeer(msg->address_from);
if (peer == NULL)
{
// Found a new peer
Serial.print("New Peer. Address=");
Serial.print(msg->address_from, DEC);
Serial.print(" uptime=");
Serial.print(pd->uptime, DEC);
Serial.print(" num_peers=");
Serial.println(pd->num_peers, DEC);
Peer* p = new Peer(msg->address_from);
peers.Add(p);
OnNewPeer(p);
} else {
// Existing peer, reset timer
peer->time = 0;
}
}
void MeshBase::SendPeerDiscovery()
{
last_broadcast_time = millis();
MeshBase::PeerDiscoveryMessage payload;
payload.protocol_version = 1;
payload.network_capabilities = 0;
payload.application_capabilities = 0;
payload.num_peers = peers.length;
payload.uptime = millis() / 1000;
SendMessage(PEER_DISCOVERY, type_peer_discovery, &payload, sizeof(payload), true);
}
void MeshBase::SendMessage(uint32_t to, uint8_t type, const void* data, uint8_t length, bool is_broadcast)
{
byte buff[MAX_PACKET_SIZE];
Message* msg = (struct Message*)buff;
msg->protocol_version = 1;
msg->ttl = 0;
msg->type = type;
msg->address_from = address;
uint8_t num_pkts = (length / MAX_PAYLOAD_SIZE) + 1;
for (uint8_t num = 0; num < num_pkts; ++num)
{
uint8_t remaining_length = length - (num * MAX_PAYLOAD_SIZE);
msg->split_part = num;
msg->split_more = remaining_length > MAX_PAYLOAD_SIZE;
memcpy(buff + sizeof(Message), (const byte*)data + (num * MAX_PAYLOAD_SIZE), min(remaining_length, MAX_PAYLOAD_SIZE));
radio.stopListening();
if (is_broadcast)
radio.openWritingPipe(TO_BROADCAST(to));
else
radio.openWritingPipe(TO_ADDRESS(to));
radio.write(buff, min(remaining_length, MAX_PAYLOAD_SIZE));
radio.startListening();
}
}
void MeshBase::SendMessage(uint32_t to, uint8_t type, const void* data, uint8_t length)
{
SendMessage(to, type, data, length, false);
}
void MeshBase::ChooseAddress()
{
do {
address = random(0xFFFF);
} while(GetPeer(address) != NULL);
radio.openReadingPipe(0, TO_ADDRESS(address));
Serial.print("Chose address: ");
Serial.println(address, DEC);
}
MeshBase::Peer* MeshBase::GetPeer(uint32_t a)
{
LinkedList<Peer>::Node* current = peers.first;
while(current != NULL)
{
if (current->item->address == a)
return current->item;
current = current->next;
}
// Could not find..
return NULL;
}