3
3
#include " MeshBase.h"
4
4
5
5
#define MAX_PACKET_SIZE 32
6
- #define MAX_PAYLOAD_SIZE (MAX_PACKET_SIZE - sizeof (Message ))
6
+ #define MAX_PAYLOAD_SIZE (MAX_PACKET_SIZE - sizeof (MeshBase::MessageHeader ))
7
7
8
8
// -- Broadcast addresses --
9
9
#define PEER_DISCOVERY 1
@@ -28,7 +28,6 @@ void MeshBase::Begin()
28
28
radio.begin ();
29
29
radio.enableDynamicPayloads ();
30
30
radio.setRetries (2 ,1 );
31
- // radio.openReadingPipe(0, TO_ADDRESS(address));
32
31
radio.openReadingPipe (1 , TO_BROADCAST (PEER_DISCOVERY));
33
32
radio.setAutoAck (0 , true );
34
33
radio.setAutoAck (1 , false );
@@ -46,8 +45,7 @@ void MeshBase::Update()
46
45
}
47
46
48
47
// Recieve
49
- uint8_t pipe_num;
50
- if (radio.available (&pipe_num))
48
+ if (radio.available ())
51
49
{
52
50
bool done = false ;
53
51
do {
@@ -78,31 +76,93 @@ void MeshBase::Update()
78
76
}
79
77
}
80
78
79
+ bool FindStream (const MeshBase::Message* current, const MeshBase::MessageHeader* find)
80
+ {
81
+ if (current->header .address_from != find->address_from )
82
+ return false ;
83
+ if (current->header .msg_id != find->msg_id )
84
+ return false ;
85
+ return true ;
86
+ }
87
+
88
+ void MeshBase::Message::AddPart (const void * payload, uint8_t len, uint8_t part_num, bool more_parts)
89
+ {
90
+ uint8_t start_pos = part_num * MAX_PAYLOAD_SIZE;
91
+ uint8_t end_pos = len + (part_num * MAX_PAYLOAD_SIZE);
92
+ Serial.print (" R AddPart() : Adding part. start_pos=" );
93
+ Serial.print (start_pos);
94
+ Serial.print (" end_pos=" );
95
+ Serial.print (end_pos);
96
+ Serial.print (" len=" );
97
+ Serial.print (len);
98
+ Serial.print (" part_num=" );
99
+ Serial.print (part_num);
100
+ Serial.print (" more_parts=" );
101
+ Serial.println (more_parts);
102
+ if (data == NULL )
103
+ data = malloc (end_pos);
104
+ if (end_pos > data_used)
105
+ data = realloc (data, end_pos);
106
+ memcpy (&static_cast <byte*>(data)[start_pos], payload, len);
107
+ if (end_pos > data_used)
108
+ data_used = end_pos;
109
+ blocks_recieved += 1 ;
110
+ if (!more_parts) {
111
+ header.split_more = false ;
112
+ header.split_part = part_num;
113
+ }
114
+ }
115
+
116
+ bool MeshBase::Message::IsDone () const
117
+ {
118
+ // We set the split_more to false if we recieved the last packet
119
+ // in the stream, and split_part to total number of blocks in the stream.
120
+ // So if split_more is false, and we have the right number of blocks_recieved
121
+ // we are good to go.
122
+ Serial.print (" R IsDone() : split_more=" );
123
+ Serial.print (header.split_more );
124
+ Serial.print (" split_part=" );
125
+ Serial.print (header.split_part );
126
+ Serial.print (" blocks_recieved=" );
127
+ Serial.println (blocks_recieved);
128
+ if (!header.split_more && blocks_recieved >= header.split_part )
129
+ return true ;
130
+ Serial.println (" R IsDone() : False" );
131
+ return false ;
132
+ }
133
+
134
+ MeshBase::Message::~Message () {
135
+ free (data);
136
+ }
137
+
81
138
void MeshBase::HandlePacket (const byte* data, uint8_t len)
82
139
{
83
- if (len < sizeof (Message ))
140
+ if (len < sizeof (MessageHeader ))
84
141
return ;
85
- const MeshBase::Message* msg = (struct MeshBase ::Message*)data;
86
- uint8_t payload_length = len - sizeof (Message);
87
- const byte* payload = data + sizeof (Message);
88
- if (msg->split_more || msg->split_part != 0 )
89
- {
90
- // Re-assembly needed
91
- // TODO: Re-assemble packets
92
- } else {
93
- switch (msg->type ) {
142
+ const MeshBase::MessageHeader* header = (struct MeshBase ::MessageHeader*)data;
143
+ uint8_t payload_length = len - sizeof (MessageHeader);
144
+ const byte* payload = data + sizeof (MessageHeader);
145
+ Message* s = assembly_list.Find <const MessageHeader*>(header, &FindStream);
146
+ if (s == NULL ) {
147
+ s = new Message (*header);
148
+ assembly_list.Add (s);
149
+ }
150
+ s->AddPart (payload, payload_length, header->split_part , header->split_more );
151
+ if (s->IsDone ()) {
152
+ Serial.println (" R IsDone() : true!!" );
153
+ switch (header->type ) {
94
154
case type_peer_discovery:
95
- HandlePeerDiscovery (msg, payload, payload_length );
155
+ HandlePeerDiscovery (&(s-> header ), s-> data , s-> data_used );
96
156
break ;
97
157
default :
98
- OnMessage (msg, payload, payload_length );
158
+ OnMessage (&(s-> header ), s-> data , s-> data_used );
99
159
break ;
100
160
}
101
- delete data ;
161
+ assembly_list. Remove (s) ;
102
162
}
103
163
}
104
164
105
- void MeshBase::HandlePeerDiscovery (const MeshBase::Message * msg, const void * buff, uint8_t length)
165
+ void MeshBase::HandlePeerDiscovery (const MeshBase::MessageHeader * msg, const void * buff, uint8_t length)
106
166
{
107
167
if (length != sizeof (PeerDiscoveryMessage))
108
168
return ;
@@ -143,28 +203,36 @@ void MeshBase::SendPeerDiscovery()
143
203
144
204
void MeshBase::SendMessage (uint32_t to, uint8_t type, const void * data, uint8_t length, bool is_broadcast)
145
205
{
206
+ static uint8_t current_msg_id = 0 ;
146
207
byte buff[MAX_PACKET_SIZE];
147
- Message * msg = (struct Message *)buff;
208
+ MessageHeader * msg = (struct MessageHeader *)buff;
148
209
msg->protocol_version = 1 ;
149
210
msg->ttl = 0 ;
150
211
msg->type = type;
151
212
msg->address_from = address;
213
+ msg->msg_id = current_msg_id++;
152
214
153
215
uint8_t num_pkts = (length / MAX_PAYLOAD_SIZE) + 1 ;
154
216
for (uint8_t num = 0 ; num < num_pkts; ++num)
155
217
{
156
218
uint8_t remaining_length = length - (num * MAX_PAYLOAD_SIZE);
157
219
msg->split_part = num;
158
220
msg->split_more = remaining_length > MAX_PAYLOAD_SIZE;
159
- memcpy (buff + sizeof (Message ), (const byte*)data + (num * MAX_PAYLOAD_SIZE), min (remaining_length, MAX_PAYLOAD_SIZE));
221
+ memcpy (buff + sizeof (MessageHeader ), (const byte*)data + (num * MAX_PAYLOAD_SIZE), min (remaining_length, MAX_PAYLOAD_SIZE));
160
222
161
223
radio.stopListening ();
162
224
if (is_broadcast)
163
225
radio.openWritingPipe (TO_BROADCAST (to));
164
226
else
165
227
radio.openWritingPipe (TO_ADDRESS (to));
166
- radio.write (buff, min (remaining_length, MAX_PAYLOAD_SIZE));
228
+ radio.write (buff, min (remaining_length + sizeof (MessageHeader) , MAX_PAYLOAD_SIZE));
167
229
radio.startListening ();
230
+ Serial.print (" T Sending pkt split_part=" );
231
+ Serial.print (msg->split_part );
232
+ Serial.print (" split_more=" );
233
+ Serial.print (msg->split_more );
234
+ Serial.print (" length=" );
235
+ Serial.println (min (remaining_length, MAX_PAYLOAD_SIZE));
168
236
}
169
237
}
170
238
0 commit comments