A simple wireless protocol to let my examples communicate each other. ssWi stands for Shared Slotted Wireless protocol

Dependents:   rover_car rover_pc supervisor watering_unit ... more

This library aims at implementing a simple communication protocol among nodes, abstracting from the hardware. The name ssWi stands for Shared Slotted Wireless. Wireless is part of the name, even though the library abstracts from the hardware, as the first version was entirely focused on the XBee modules and then the name has not been changed.

The communication channel is represented by ssWiChannel, an abstract class which models the channel that the transceivers access to. The concrete classes must implement the functions: init, read and write. The protocol automatically sends and receives data through the selected channel, exploiting the operting system timers. Addresses are not considered as the communication lays on broadcast transmissions.

The protocol provides the ssWiPort abstraction which is like memory areas shared among all the connected nodes. Reading from one port lets the node retrieve the last written value from the other nodes. Writing on one port means sending such value to other nodes.

Objects instantiated from ssWiSocket are the interface for allowing nodes to access the protocol ports.

/media/uploads/mariob/scheme.png

TODO:

  • improve the parsing of the received messages
  • communication tests with many nodes (so far, only 2 nodes have been tested)
Committer:
mariob
Date:
Wed Mar 06 08:21:41 2013 +0000
Revision:
13:69ff47a83260
Parent:
11:4ad44d62d510
Child:
15:fb0f6cbc0ed5
added comments; MB

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mariob 13:69ff47a83260 1 /** \file xbee.cpp
mariob 13:69ff47a83260 2 * \brief implementation of the ssWi channel using the XBee module
mariob 13:69ff47a83260 3 *
mariob 13:69ff47a83260 4 */
mariob 13:69ff47a83260 5
mariob 0:cc7218c5e5f7 6 #include "xbee.hpp"
mariob 0:cc7218c5e5f7 7
mariob 0:cc7218c5e5f7 8 #include <sstream>
mariob 0:cc7218c5e5f7 9
mariob 0:cc7218c5e5f7 10
mariob 0:cc7218c5e5f7 11 void XBeeModule::readResponse (char* msg)
mariob 0:cc7218c5e5f7 12 {
mariob 0:cc7218c5e5f7 13 char c = 0;
mariob 0:cc7218c5e5f7 14 int i = 0;
mariob 0:cc7218c5e5f7 15 while (c!='\r')
mariob 0:cc7218c5e5f7 16 if (xbee.readable()) {
mariob 0:cc7218c5e5f7 17 c = xbee.getc();
mariob 0:cc7218c5e5f7 18 msg[i++] = c;
mariob 0:cc7218c5e5f7 19 }
mariob 0:cc7218c5e5f7 20 }
mariob 0:cc7218c5e5f7 21
mariob 0:cc7218c5e5f7 22
mariob 0:cc7218c5e5f7 23 bool XBeeModule::executeWithOk (const char* cmd)
mariob 0:cc7218c5e5f7 24 {
mariob 0:cc7218c5e5f7 25 char msg[5];
mariob 0:cc7218c5e5f7 26 xbee.printf("%s", cmd);
mariob 0:cc7218c5e5f7 27 readResponse(msg);
mariob 0:cc7218c5e5f7 28 //printf("%s -> %s\n\r", cmd, msg);
mariob 0:cc7218c5e5f7 29 if (strncmp(msg, "OK\r", 3)!=0)
mariob 0:cc7218c5e5f7 30 return false;
mariob 0:cc7218c5e5f7 31 return true;
mariob 0:cc7218c5e5f7 32 }
mariob 0:cc7218c5e5f7 33
mariob 0:cc7218c5e5f7 34
mariob 0:cc7218c5e5f7 35 void XBeeModule::executeWithRes (const char* cmd, char* res)
mariob 0:cc7218c5e5f7 36 {
mariob 0:cc7218c5e5f7 37 xbee.printf("%s", cmd);
mariob 0:cc7218c5e5f7 38 readResponse(res);
mariob 0:cc7218c5e5f7 39 //printf("\n\r%s -> %s\n\r", cmd, res);
mariob 0:cc7218c5e5f7 40 }
mariob 0:cc7218c5e5f7 41
mariob 0:cc7218c5e5f7 42
mariob 0:cc7218c5e5f7 43
mariob 0:cc7218c5e5f7 44
mariob 0:cc7218c5e5f7 45
mariob 0:cc7218c5e5f7 46
mariob 11:4ad44d62d510 47 XBeeModule::XBeeModule (PinName tx, PinName rx, int panID, int channel) : xbee(tx, rx, 64)
mariob 0:cc7218c5e5f7 48 {
mariob 0:cc7218c5e5f7 49 status = false;
mariob 0:cc7218c5e5f7 50 wait(1);
mariob 0:cc7218c5e5f7 51 if (!_getLocalAddr())
mariob 0:cc7218c5e5f7 52 return;
mariob 0:cc7218c5e5f7 53 wait(1);
mariob 0:cc7218c5e5f7 54 if (!_setChannel(channel))
mariob 0:cc7218c5e5f7 55 return;
mariob 0:cc7218c5e5f7 56 wait(1);
mariob 0:cc7218c5e5f7 57 if (!_setPanID(panID))
mariob 0:cc7218c5e5f7 58 return;
mariob 0:cc7218c5e5f7 59 wait(1);
mariob 0:cc7218c5e5f7 60 status = true;
mariob 0:cc7218c5e5f7 61 }
mariob 0:cc7218c5e5f7 62
mariob 0:cc7218c5e5f7 63
mariob 0:cc7218c5e5f7 64
mariob 0:cc7218c5e5f7 65
mariob 0:cc7218c5e5f7 66
mariob 0:cc7218c5e5f7 67
mariob 0:cc7218c5e5f7 68
mariob 0:cc7218c5e5f7 69 XBeeAddress XBeeModule::getDstAddress ()
mariob 0:cc7218c5e5f7 70 {
mariob 0:cc7218c5e5f7 71 char tmp[10];
mariob 0:cc7218c5e5f7 72 std::stringstream s1, s2;
mariob 0:cc7218c5e5f7 73 string high, low;
mariob 0:cc7218c5e5f7 74 XBeeAddress addr;
mariob 13:69ff47a83260 75 wait(1);
mariob 0:cc7218c5e5f7 76 if (!executeWithOk("+++"))
mariob 0:cc7218c5e5f7 77 return addr;
mariob 0:cc7218c5e5f7 78
mariob 13:69ff47a83260 79 wait(1);
mariob 0:cc7218c5e5f7 80
mariob 0:cc7218c5e5f7 81 executeWithRes("ATDH \r", tmp);
mariob 0:cc7218c5e5f7 82 s1<<std::hex<<tmp;
mariob 0:cc7218c5e5f7 83 s1>>high;
mariob 0:cc7218c5e5f7 84
mariob 0:cc7218c5e5f7 85 executeWithRes("ATDL \r", tmp);
mariob 0:cc7218c5e5f7 86 s2<<std::hex<<tmp;
mariob 0:cc7218c5e5f7 87 s2>>low;
mariob 0:cc7218c5e5f7 88
mariob 0:cc7218c5e5f7 89 if (!executeWithOk("ATCN \r"))
mariob 0:cc7218c5e5f7 90 return addr;
mariob 0:cc7218c5e5f7 91
mariob 0:cc7218c5e5f7 92 wait(1);
mariob 0:cc7218c5e5f7 93 return XBeeAddress(low, high);
mariob 0:cc7218c5e5f7 94 }
mariob 0:cc7218c5e5f7 95
mariob 0:cc7218c5e5f7 96
mariob 0:cc7218c5e5f7 97 bool XBeeModule::setDstAddress (XBeeAddress addr)
mariob 0:cc7218c5e5f7 98 {
mariob 0:cc7218c5e5f7 99 char s[10];
mariob 0:cc7218c5e5f7 100 string low, high;
mariob 13:69ff47a83260 101 wait(1);
mariob 0:cc7218c5e5f7 102 if (!executeWithOk("+++"))
mariob 0:cc7218c5e5f7 103 return false;
mariob 13:69ff47a83260 104 wait(1);
mariob 0:cc7218c5e5f7 105 sprintf(s, "ATDH%s \r", addr.getHighAddr().c_str());
mariob 0:cc7218c5e5f7 106 //printf("%s\n\r", addr.getHighAddr().c_str());
mariob 0:cc7218c5e5f7 107 if (!executeWithOk(s))
mariob 0:cc7218c5e5f7 108 return false;
mariob 0:cc7218c5e5f7 109
mariob 0:cc7218c5e5f7 110 sprintf(s, "ATDL%s \r", addr.getLowAddr().c_str());
mariob 0:cc7218c5e5f7 111 //printf("%s\n\r", addr.getLowAddr().c_str());
mariob 0:cc7218c5e5f7 112 if (!executeWithOk(s))
mariob 0:cc7218c5e5f7 113 return false;
mariob 0:cc7218c5e5f7 114
mariob 0:cc7218c5e5f7 115 if (!executeWithOk("ATCN \r"))
mariob 0:cc7218c5e5f7 116 return false;
mariob 0:cc7218c5e5f7 117
mariob 0:cc7218c5e5f7 118 return true;
mariob 0:cc7218c5e5f7 119 }
mariob 0:cc7218c5e5f7 120
mariob 0:cc7218c5e5f7 121
mariob 0:cc7218c5e5f7 122 bool XBeeModule::_getLocalAddr ()
mariob 0:cc7218c5e5f7 123 {
mariob 0:cc7218c5e5f7 124 char tmp[10];
mariob 0:cc7218c5e5f7 125 string high, low;
mariob 0:cc7218c5e5f7 126 std::stringstream s1, s2;
mariob 0:cc7218c5e5f7 127
mariob 0:cc7218c5e5f7 128 if (!executeWithOk("+++"))
mariob 0:cc7218c5e5f7 129 return false;
mariob 0:cc7218c5e5f7 130 executeWithRes("ATSH \r", tmp);
mariob 0:cc7218c5e5f7 131 s1<<std::hex<<tmp;
mariob 0:cc7218c5e5f7 132 s1>>high;
mariob 0:cc7218c5e5f7 133 executeWithRes("ATSL \r", tmp);
mariob 0:cc7218c5e5f7 134 s2<<std::hex<<tmp;
mariob 0:cc7218c5e5f7 135 s2>>low;
mariob 0:cc7218c5e5f7 136 if (!executeWithOk("ATCN \r"))
mariob 0:cc7218c5e5f7 137 return false;
mariob 0:cc7218c5e5f7 138 local = XBeeAddress(low, high);
mariob 0:cc7218c5e5f7 139 return true;
mariob 0:cc7218c5e5f7 140 }
mariob 0:cc7218c5e5f7 141
mariob 0:cc7218c5e5f7 142
mariob 0:cc7218c5e5f7 143 bool XBeeModule::_setChannel (int channel)
mariob 0:cc7218c5e5f7 144 {
mariob 0:cc7218c5e5f7 145 char s[10];
mariob 0:cc7218c5e5f7 146
mariob 0:cc7218c5e5f7 147 if (!executeWithOk("+++"))
mariob 0:cc7218c5e5f7 148 return false;
mariob 13:69ff47a83260 149 wait(1);
mariob 0:cc7218c5e5f7 150 sprintf(s, "ATCH%d \r", channel);
mariob 0:cc7218c5e5f7 151 if (!executeWithOk(s))
mariob 0:cc7218c5e5f7 152 return false;
mariob 0:cc7218c5e5f7 153 if (!executeWithOk("ATCN \r"))
mariob 0:cc7218c5e5f7 154 return false;
mariob 0:cc7218c5e5f7 155 return true;
mariob 0:cc7218c5e5f7 156 }
mariob 0:cc7218c5e5f7 157
mariob 0:cc7218c5e5f7 158 int XBeeModule::getChannel ()
mariob 0:cc7218c5e5f7 159 {
mariob 0:cc7218c5e5f7 160 int channel;
mariob 0:cc7218c5e5f7 161 char s[10];
mariob 0:cc7218c5e5f7 162
mariob 0:cc7218c5e5f7 163 if (!executeWithOk("+++"))
mariob 0:cc7218c5e5f7 164 return -1;
mariob 13:69ff47a83260 165 wait(1);
mariob 0:cc7218c5e5f7 166 executeWithRes("ATCH \r", s);
mariob 0:cc7218c5e5f7 167 channel = atoi(s);
mariob 0:cc7218c5e5f7 168 if (!executeWithOk("ATCN \r"))
mariob 0:cc7218c5e5f7 169 return -1;
mariob 0:cc7218c5e5f7 170 wait(1);
mariob 0:cc7218c5e5f7 171 return channel;
mariob 0:cc7218c5e5f7 172 }
mariob 0:cc7218c5e5f7 173
mariob 0:cc7218c5e5f7 174
mariob 0:cc7218c5e5f7 175 bool XBeeModule::_setPanID (int panID)
mariob 0:cc7218c5e5f7 176 {
mariob 0:cc7218c5e5f7 177 char s[10];
mariob 0:cc7218c5e5f7 178 if (!executeWithOk("+++"))
mariob 0:cc7218c5e5f7 179 return false;
mariob 0:cc7218c5e5f7 180 sprintf(s, "ATID%d \r", panID);
mariob 0:cc7218c5e5f7 181 if (!executeWithOk(s))
mariob 0:cc7218c5e5f7 182 return false;
mariob 0:cc7218c5e5f7 183 if (!executeWithOk("ATCN \r"))
mariob 0:cc7218c5e5f7 184 return false;
mariob 0:cc7218c5e5f7 185 return true;
mariob 0:cc7218c5e5f7 186 }
mariob 0:cc7218c5e5f7 187
mariob 0:cc7218c5e5f7 188 int XBeeModule::getPanID ()
mariob 0:cc7218c5e5f7 189 {
mariob 0:cc7218c5e5f7 190 int id;
mariob 0:cc7218c5e5f7 191 char s[10];
mariob 0:cc7218c5e5f7 192
mariob 0:cc7218c5e5f7 193 if (!executeWithOk("+++"))
mariob 0:cc7218c5e5f7 194 return -1;
mariob 0:cc7218c5e5f7 195 executeWithRes("ATID \r", s);
mariob 0:cc7218c5e5f7 196 id = atoi(s);
mariob 0:cc7218c5e5f7 197 if (!executeWithOk("ATCN \r"))
mariob 0:cc7218c5e5f7 198 return -1;
mariob 0:cc7218c5e5f7 199 wait(1);
mariob 0:cc7218c5e5f7 200 return id;
mariob 0:cc7218c5e5f7 201 }
mariob 0:cc7218c5e5f7 202
mariob 0:cc7218c5e5f7 203
mariob 0:cc7218c5e5f7 204
mariob 0:cc7218c5e5f7 205 int XBeeModule::read (char* msg)
mariob 0:cc7218c5e5f7 206 {
mariob 0:cc7218c5e5f7 207 int i = 0;
mariob 11:4ad44d62d510 208 while (xbee.readable())
mariob 0:cc7218c5e5f7 209 msg[i++] = xbee.getc();
mariob 0:cc7218c5e5f7 210 return i;
mariob 0:cc7218c5e5f7 211 }
mariob 0:cc7218c5e5f7 212
mariob 0:cc7218c5e5f7 213
mariob 11:4ad44d62d510 214
mariob 0:cc7218c5e5f7 215 void XBeeModule::write (const char* msg, int n)
mariob 0:cc7218c5e5f7 216 {
mariob 0:cc7218c5e5f7 217 for (int i=0; i<n; i++) {
mariob 11:4ad44d62d510 218 //while(!xbee.writeable());
mariob 0:cc7218c5e5f7 219 xbee.putc(msg[i]);
mariob 0:cc7218c5e5f7 220 }
mariob 0:cc7218c5e5f7 221 }