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:
Thu Sep 06 10:37:03 2012 +0000
Revision:
0:cc7218c5e5f7
Child:
11:4ad44d62d510
ssWi

Who changed what in which revision?

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