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.
TODO:
- improve the parsing of the received messages
- communication tests with many nodes (so far, only 2 nodes have been tested)
Revision 24:80345e511574, committed 2020-04-20
- Comitter:
- mariob
- Date:
- Mon Apr 20 20:27:01 2020 +0000
- Parent:
- 23:2d57c3ea08dc
- Child:
- 25:83172a067b57
- Commit message:
- few more comments
Changed in this revision
--- a/ssWi.cpp Sun Apr 19 17:59:59 2020 +0000 +++ b/ssWi.cpp Mon Apr 20 20:27:01 2020 +0000 @@ -1,4 +1,5 @@ /** \file ssWi.cpp + * * \brief implementation of the internal functions for mananing the protocol * */ @@ -13,23 +14,12 @@ #include <map> - /** \brief first byte of the header * - * The header is composed of 3 bytes + * The header is composed of 3 bytes: START_0 START_1 START_2 */ #define START_0 255 - -/** \brief second byte of the header - * - * The header is composed of 3 bytes - */ #define START_1 130 - -/** \brief third byte of the header - * - * The header is composed of 3 bytes - */ #define START_2 255 @@ -41,8 +31,7 @@ /** \brief channel abstraction * - * This object represent the communication channel that can be - * different means + * This object represent the communication channel. It can be different means */ ssWiChannel* channel = NULL; @@ -78,12 +67,12 @@ { if (channel!=NULL) return false; - //mutexChannel.lock(); + + // nothing else is using the channel channel = c; - //mutexChannel.unlock(); TXRate = rateTX > 0 ? 1000/rateTX : 0; RXRate = rateRX > 0 ? 1000/rateRX : 0; - + // start txer and rxer readingThread.start(functionReceiver); writingThread.start(functionSender); @@ -93,11 +82,15 @@ void functionSender() { static char buffer[INTERNAL_BUFFER_SIZE]; - while (1) { + while (true) { int n = 3; int numFrames = 0; + + // copy data for (std::map<int, ssWiPort>::iterator it = ports.begin(); - it != ports.end(); it++) { + it != ports.end(); it++) { + if ((n + sizeof(PortValue) + sizeof(PortID)) > INTERNAL_BUFFER_SIZE) + break; if ((*it).second.isModified()) { buffer[n++] = (*it).first; PortValue tmp = (*it).second.getTXValue(); @@ -106,15 +99,16 @@ numFrames++; } } + // add header and send if (numFrames > 0) { buffer[0] = START_0; buffer[1] = START_1; buffer[2] = START_2; - if (!mutexChannel.trylock()) - return; + mutexChannel.lock(); channel->write(buffer, n); mutexChannel.unlock(); } + thread_sleep_for(TXRate); } } @@ -127,16 +121,17 @@ while(1) { thread_sleep_for(RXRate); - if (!mutexChannel.trylock()) - return; + // read buffer + mutexChannel.lock(); int n = channel->read(buffer + offset); mutexChannel.unlock(); - + // not enough to find the header, merge with next message as the buffer + // can contain part of the header if (n < 3) { offset = n; continue; } - + // find header int index = -1; for (int i=0; i<(n-2); i++) { if (buffer[i]==START_0 && buffer[i+1]==START_1 && buffer[i+2]==START_2) { @@ -144,13 +139,11 @@ break; } } - - if (index < 0) { - offset = n; + // header not found, discard everthing + offset = 0; + if (index < 0) continue; - } - offset = 0; - + // read samples while((index + sizeof(PortID) + sizeof(PortValue)) <= n) { PortID port = buffer[index++]; PortValue value = 0; @@ -165,15 +158,17 @@ inline bool ssWi_isActive (PortID port) { - return channel!=NULL && ports.find(port)!=ports.end(); + return channel != NULL && ports.find(port) != ports.end(); } bool ssWi_setPort (PortID port) { - if (channel==NULL) + if (channel == NULL) return false; + ports[port]; + return true; } @@ -182,6 +177,8 @@ { if (!ssWi_isActive(port)) return false; + ports.erase(port); + return true; }
--- a/ssWi.hpp Sun Apr 19 17:59:59 2020 +0000 +++ b/ssWi.hpp Mon Apr 20 20:27:01 2020 +0000 @@ -1,4 +1,5 @@ /** \file ssWi.hpp + * * \brief internal functions for mananing the protocol * */ @@ -9,9 +10,9 @@ #include "ssWiTypes.hpp" -/** \brief number of provided ports +/** \brief max number of provided ports */ -#define N_PORTS 256 +#define N_PORTS 255 class ssWiChannel;
--- a/ssWiPort.cpp Sun Apr 19 17:59:59 2020 +0000 +++ b/ssWiPort.cpp Mon Apr 20 20:27:01 2020 +0000 @@ -5,45 +5,31 @@ #include "ssWiPort.hpp" + PortValue ssWiPort::getTXValue() { - PortValue tmp; - mutexTX.lock(); - tmp = valueTX; + PortValue tmp = valueTX; modified = false; - mutexTX.unlock(); return tmp; } void ssWiPort::setTXValue(PortValue tmp) { - mutexTX.lock(); valueTX = tmp; modified = true; - mutexTX.unlock(); } bool ssWiPort::isModified() { - bool tmp; - mutexTX.lock(); - tmp = modified; - mutexTX.unlock(); - return tmp; + return modified; } PortValue ssWiPort::getRXValue() { - PortValue tmp; - mutexRX.lock(); - tmp = valueRX; - mutexRX.unlock(); - return tmp; + return valueRX; } void ssWiPort::setRXValue(PortValue tmp) { - mutexRX.lock(); valueRX = tmp; - mutexRX.unlock(); }
--- a/ssWiPort.hpp Sun Apr 19 17:59:59 2020 +0000 +++ b/ssWiPort.hpp Mon Apr 20 20:27:01 2020 +0000 @@ -10,6 +10,7 @@ #include "ssWiTypes.hpp" +#include <atomic> /** \brief Internal type which represents a logical flow * @@ -21,17 +22,13 @@ class ssWiPort { /** \brief receiving buffer */ - PortValue valueRX; - /** \brief receiving mutex */ - Mutex mutexRX; + std::atomic<PortValue> valueRX; /** \brief transmission buffer */ - PortValue valueTX; - /** \brief transmission mutex */ - Mutex mutexTX; + std::atomic<PortValue> valueTX; /** \brief modification flag (if true a new value has to be sent) */ - bool modified; + std::atomic<bool> modified; public:
--- a/ssWiSocket.cpp Sun Apr 19 17:59:59 2020 +0000 +++ b/ssWiSocket.cpp Mon Apr 20 20:27:01 2020 +0000 @@ -25,5 +25,6 @@ { if (!ssWi_setPort(id)) return NULL; + return new ssWiSocket(id); }
--- a/ssWiSocket.hpp Sun Apr 19 17:59:59 2020 +0000 +++ b/ssWiSocket.hpp Mon Apr 20 20:27:01 2020 +0000 @@ -1,4 +1,5 @@ /** \file ssWiSocket.hpp + * * \brief Header for the communication socket * */
--- a/xbee/xbee.cpp Sun Apr 19 17:59:59 2020 +0000 +++ b/xbee/xbee.cpp Mon Apr 20 20:27:01 2020 +0000 @@ -12,7 +12,9 @@ { char c = 0; int i = 0; - while (c!='\r') { + + // read until it sends \r + while (c != '\r') { if (serial_xbee.readable()) { c = serial_xbee.getc(); msg[i++] = c; @@ -23,21 +25,23 @@ bool XBeeModule::executeWithOk (const char* cmd) { char msg[5]; - + + // send command serial_xbee.printf("%s", cmd); - //printf("command %s\r\n", cmd); + // read response readResponse(msg); - //printf("response %s\r\n", msg); - if (strncmp(msg, "OK\r", 3)!=0) + // check response + if (strncmp(msg, "OK\r", 3) != 0) return false; - + return true; } - void XBeeModule::executeWithRes (const char* cmd, char* res) { + // send command serial_xbee.printf("%s", cmd); + // read response readResponse(res); } @@ -54,19 +58,9 @@ return true; } -XBeeModule::XBeeModule (PinName tx, PinName rx, int panID, int channel) : serial_xbee(tx, rx, 9600) -{ - status = false; - // read address - if (!_getLocalAddr()) - return; - // set channel - if (!_setChannel(channel)) - return; - // set pan ID - if (!_setPanID(panID)) - return; - status = true; +XBeeModule::XBeeModule (PinName tx, PinName rx, int panID, int channel) + : serial_xbee(tx, rx, 9600) { + status = _getLocalAddr() && _setChannel(channel) && _setPanID(panID); } bool XBeeModule::getDstAddress (XBeeAddress &addr) @@ -114,7 +108,7 @@ // terminate if (!executeWithOk("ATCN \r")) return false; - ThisThread::sleep_for(1000); + ThisThread::sleep_for(1000); return true; } @@ -158,6 +152,7 @@ return false; if (!executeWithOk("ATCN \r")) return false; + return true; } @@ -174,6 +169,7 @@ if (!executeWithOk("ATCN \r")) return -1; ThisThread::sleep_for(1000); + return channel; } @@ -211,17 +207,16 @@ } - int XBeeModule::read (char* msg) { int i = 0; + while (serial_xbee.readable()) msg[i++] = serial_xbee.getc(); + return i; } - - void XBeeModule::write (const char* msg, int n) { for (int i=0; i<n; i++) {
--- a/xbee/xbee.hpp Sun Apr 19 17:59:59 2020 +0000 +++ b/xbee/xbee.hpp Mon Apr 20 20:27:01 2020 +0000 @@ -2,7 +2,7 @@ * \brief header of the ssWi channel using the XBee module * */ - + #ifndef __XBEE_MODULE_HPP__ #define __XBEE_MODULE_HPP__