MoteProtocol.h
1/********************************************************************
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 *
17 ********************************************************************/
18/*********************************************************************
19 * TinyOS data structures.
20 * Portions borrowed from the TinyOS project (http://www.tinyos.net),
21 * distributed according to the Intel Open Source License.
22 *********************************************************************/
23/***************************************************************************
24 * Desc: Library for generic Crossbow WSN nodes communication
25 * Author: Jose Manuel Sanchez Matamoros, Adrian Jimenez Gonzalez
26 * Date: 15 Aug 2011
27 **************************************************************************/
28
29#ifndef MOTEPROTOCOL_H
30#define MOTEPROTOCOL_H
31
32#include <stdint.h>
33#include <memory.h>
34#include <cstdio>
35#include <iostream>
36#include <assert.h>
37
38using std::istream;
39using std::ostream;
40using std::endl;
41
42#include "MoteException.h"
43
44//#define debug
45
46namespace mote {
47
48
49// message_t type dispatch
50
51enum {
52 TOS_SERIAL_ACTIVE_MESSAGE_ID = 0,
53 TOS_SERIAL_CC1000_ID = 1,
54 TOS_SERIAL_802_15_4_ID = 2,
55 TOS_SERIAL_UNKNOWN_ID = 255,
56};
57
58enum { OS_TRANSPARENT = 0, TOS1 = 1, TOS2 = 2, CONTIKI = 3 };
59
60#define LIMIT ((char)0x7E)
61#define ESCAPE ((char)0x7D)
62
63#define IN_FRAME_SIZE 100
64#define OUT_FRAME_SIZE 100
65
66// packet types
67#define P_ACK ((char)0x40)
68
69#define P_TOS1_PACKET_NO_ACK ((char)0x42)
70#define P_TOS1_PACKET_ACK ((char)0x41)
71#define P_TOS2_ACK ((char)0x43)
72#define P_TOS2_PACKET_NO_ACK ((char)0x45)
73#define P_TOS2_PACKET_ACK ((char)0x44)
74#define P_UNKNOWN ((char)0xFF)
75
76// Maximum TinyOS message data size TODO revisarlo, me lo he inventao
77#define MAX_TOS_SIZE 100
78#define TOS_BROADCAST 0xFFFF
79#define TOS1_DEFAULT_GROUP 0x7D
80#define TOS2_DEFAULT_GROUP 0x00
81#define TOS_DEFAULT_GROUP 0x7D
82// #define TOS2_DEFAULT_GROUP 0x22
83// #define TOS2_DEFAULT_GROUP 0x66
84
85#define MP_LITTLE_ENDIAN 1
86#define MP_BIG_ENDIAN 0
87
89
90// Plot output before sending to the serial port
91// #define DEBUG_OUTPUT
92
93// Plot input when read from serial port
94// #define DEBUG_INPUT
95
96// Include SEQN field in the TinyOS 2 packet
97// #define PUT_SEQN
99
100struct Dumpable;
101struct Packet;
102struct ACKMessage;
103struct TOSMessage;
104
148
149public:
150 MoteProtocol();
152
153 void bind( istream& is, ostream& os );
154
155 void getMessage( TOSMessage& message );
156 void sendMessage(TOSMessage& message, uint8_t type );
157
162 void setOS(int v);
167 int getOS();
168protected:
169
170 void sendPacket( Packet& packet );
171
172 enum Status { ST_LOST, ST_SYNC };
173
174 Status status;
175
176 char *bufferIn;
177 char *bufferOut;
178 istream *ins;
179 ostream *outs;
180
181 int os;
182
183};
184
190struct Dumpable {
191 virtual uint8_t *dump( uint8_t *dst , int os = 1) = 0;
192 virtual uint8_t *undump( uint8_t *src, int os = 1 ) = 0;
193};
194
195
196
197struct ACKMessage: public Dumpable {
198 uint8_t *dump( uint8_t *dst , int os = 1);
199 uint8_t *undump( uint8_t *src, int os = 1 );
200};
201
202
203
205struct TOSMessage: public Dumpable {
206
207 uint16_t addr;
208 uint8_t type;
209 uint8_t group;
210 uint8_t length ;
211 uint8_t data[MAX_TOS_SIZE];
212
213 // Version of tinyos
214 uint8_t os;
215 uint16_t lnk_src; // Link source (only TOS 2.x)
216 uint8_t n_msg; // Message number (only TOS 2.x)
217 uint8_t zero; // Unknown field up to now (only TOS 2.x)
218
219 uint8_t msg_count; // Message count
220
221 TOSMessage(){
222 addr = TOS_BROADCAST;
223 type = 0;
224 group = TOS_DEFAULT_GROUP;
225 length = 0;
226 os = TOS1;
227 lnk_src = 0; // Link source (only TOS 2.x)
228 n_msg = 0; // Message number (only TOS 2.x)
229 zero = 0;
230 msg_count = 0; //0x0e
231
232 }
233
234 void compose( uint8_t type,
235 void *data,
236 uint8_t length,
237 uint16_t addr = TOS_BROADCAST,
238 uint16_t lnk_src = 0,
239 uint8_t group = TOS_DEFAULT_GROUP );
240
241 void compose( uint8_t type,
242 void *data,
243 uint8_t length,
244 const char *def,
245 uint16_t addr = TOS_BROADCAST,
246 uint16_t lnk_src = 0,
247 uint8_t group = TOS_DEFAULT_GROUP );
248
249 void compose( uint8_t type,
250 void *data,
251 const char *def,
252 uint16_t addr = TOS_BROADCAST,
253 uint16_t lnk_src = 0,
254 uint8_t group = TOS_DEFAULT_GROUP );
255
256 void getData ( void *dst, int size, const char *def );
257 void getData ( void *dst, int size);
258 void getData ( void *dst, const char *def );
259
260 uint8_t *dump( uint8_t *dst , int os = 1);
261 uint8_t *undump( uint8_t *src , int os = 1);
262
263 void setOS(int v);
264};
265
266
267
274struct Packet: public Dumpable {
275
276 friend class MoteProtocol;
277
278 uint8_t type;
279 Dumpable *payload;
280
281 void compose( uint8_t type, TOSMessage& message );
282
283 uint8_t *dump( uint8_t *dst , int os = 1);
284 uint8_t *undump( uint8_t *src, int os = 1 );
285
286protected:
287 void composeAck();
288 void getTOSMessage( TOSMessage &message );
289 uint16_t computeCRCByte(uint16_t crc, uint8_t b);
290 uint16_t computeCRC(uint8_t *packet, int index, int count);
291
292
293 // possible payloads
294 TOSMessage payloadTOSMessage;
295 ACKMessage payloadACK;
296
297 uint16_t crc;
298};
299
300
301
302
303inline uint8_t *dump(uint8_t d, uint8_t *dst) { *(dst++) = d; return dst; }
304inline uint8_t *dump(int8_t d, uint8_t *dst) { *(dst++) = d; return dst; }
305inline uint8_t *dump(uint16_t d, uint8_t *dst) { *((uint16_t *)dst) = d; return dst+2; }
306inline uint8_t *dump(int16_t d, uint8_t *dst) { *((int16_t *)dst) = d; return dst+2; }
307
308inline uint8_t *undump(uint8_t& d, uint8_t *dst) { d = *(dst++); return dst; }
309inline uint8_t *undump(int8_t& d, uint8_t *dst) { d = *(dst++); return dst; }
310inline uint8_t *undump(uint16_t& d, uint8_t *dst) { d = *((uint16_t *)dst); return dst+2; }
311inline uint8_t *undump(int16_t& d, uint8_t *dst) { d = *((int16_t *)dst); return dst+2; }
312
313inline ostream& operator<<(ostream& os, TOSMessage& message ) {
314
315 os << "addr " << (int)message.addr << endl;
316 os << "type " << (int)message.type << endl;
317 os << "group " << (int)message.group << endl;
318 os << "data length " << (int)message.length << endl;
319
320 for (int i = 0; i< message.length; i++ ) {
321 os << " " << (unsigned int)message.data[i];
322 }
323
324 return os;
325}
326
327
328// Function to reverse endianness
329template <typename T>
330T reverseEndian(T v){
331 int s = sizeof(T);
332 T r = 0;
333 uint8_t *pv = (uint8_t *)&v;
334 uint8_t *pr = (uint8_t *)&r;
335
336 for(int i=0;i<s;i++){
337 pr[i] = pv[s-i-1];
338 }
339 return r;
340}
341
354template <typename T>
355char *defineStruct(const T& obj) {return NULL;}
356
360int checkNativeEndianness();
361
362}
363
364
365#endif
Implements the basic sending and receiving capabilities on iostreams.
Definition MoteProtocol.h:147
int getOS()
Get current TinyOS version.
Definition MoteProtocol.cpp:669
void setOS(int v)
Set TinyOS version.
Definition MoteProtocol.cpp:662
Definition MoteProtocol.h:197
Interface that ensures serialization from and to a memory region.
Definition MoteProtocol.h:190
Data packet as described in the TinyOS serial protocol specifications.
Definition MoteProtocol.h:274
TinyOS generic message.
Definition MoteProtocol.h:205