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 0:cc7218c5e5f7, committed 2012-09-06
- Comitter:
- mariob
- Date:
- Thu Sep 06 10:37:03 2012 +0000
- Child:
- 1:b3dcde512252
- Commit message:
- ssWi
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ssWi.cpp Thu Sep 06 10:37:03 2012 +0000 @@ -0,0 +1,116 @@ +#include "ssWi.hpp" + +#include "mbed.h" +#include "rtos.h" + + +void threadSender (void const* arg); +void threadReceiver (void const* arg); + + +ssWi::ssWi (ssWiChannel* c, int rateTX, int rateRX) +{ + msTXSleep = 1000/rateTX; + msRXSleep = 1000/rateRX; + mutexChannel.lock(); + channel = c; + sender = new Thread(threadSender, this); + receiver = new Thread(threadReceiver, this); + mutexChannel.unlock(); +} + +ssWi::~ssWi () +{ + delete sender; + delete receiver; +} + +ssWiSocket* ssWi::createSocket (PortID port) +{ + setPort(port); + return new ssWiSocket(&ports[port]); +} + +bool ssWi::setPort (PortID port) +{ + if (isActive(port)) + return false; + ports[port]; + return true; +} + +bool ssWi::unsetPort (PortID port) +{ + if (!isActive(port)) + return false; + ports.erase(port); + return true; +} + +void ssWi::readFromChannel (char* msg, int& len) +{ + mutexChannel.lock(); + len = channel->read(msg); + mutexChannel.unlock(); +} + +void ssWi::writeOnChannel (char* msg, int len) +{ + mutexChannel.lock(); + channel->write(msg, len); + mutexChannel.unlock(); +} + +#define INTERNAL_BUFFER_SIZE 100 + +void threadSender (void const* arg) +{ + ssWi* s = (ssWi*)arg; + char buffer[INTERNAL_BUFFER_SIZE]; + while(true) { + int n = 0; + std::map<int, ssWiPort>::iterator it = s->ports.begin(); + + buffer[n++] = 255; + for (; it!=s->ports.end(); it++) { + if ((*it).second.isModified()) { + buffer[n++] = (*it).first; + PortValue tmp = (*it).second.getTXValue(); + for (int i=0; i<sizeof(PortValue); i++) { + buffer[n++] = tmp & 0x00ff; + tmp = tmp>>8; + } + } + } + if (n>1) + s->writeOnChannel(buffer, n); + Thread::wait(s->getTXSleep()); + } +} + +void threadReceiver (void const* arg) +{ + ssWi* s = (ssWi*)arg; + char buffer[INTERNAL_BUFFER_SIZE]; + while(true) { + int n; + s->readFromChannel(buffer, n); + + int i = 0; + for (; buffer[i]!=255 && i<n; i++); + i++; + + for (; i<n;) { + PortID port = buffer[i++]; + PortValue value = 0; + for (int j=sizeof(PortValue)-1; j>=0; j--) { + value = value<<8; + value += buffer[i+j]; + } + i += sizeof(PortValue); + if (s->ports.find(port)!=s->ports.end()) + s->ports[port].setRXValue(value); + } + Thread::wait(s->getRXSleep()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ssWi.hpp Thu Sep 06 10:37:03 2012 +0000 @@ -0,0 +1,63 @@ +#ifndef __SHARED_SLOTTED_WIRELESS_HPP__ +#define __SHARED_SLOTTED_WIRELESS_HPP__ + +#include "ssWiTypes.hpp" +#include "ssWiChannel.hpp" +#include "ssWiPort.hpp" +#include "ssWiSocket.hpp" + +#include "rtos.h" + +#include <map> + + + +#define N_PORTS 256 + + + +class ssWi +{ + + ssWiChannel* channel; + Mutex mutexChannel; + + Thread* sender; + Thread* receiver; + + int msTXSleep; + int msRXSleep; + + std::map<int, ssWiPort> ports; + + friend void threadReceiver (void const* arg); + friend void threadSender (void const* arg); + + bool setPort (PortID port); + bool isActive (PortID port) { + return ports.find(port)!=ports.end(); + } + bool unsetPort (PortID port); + + void readFromChannel (char* msg, int& len); + void writeOnChannel (char* msg, int len); + + int getTXSleep () { + return msTXSleep; + } + int getRXSleep () { + return msRXSleep; + } + +public: + + ssWi (ssWiChannel* channel, int rateTX=10, int rateRX=20); + ~ssWi (); + + ssWiSocket* createSocket (PortID port); + + + +}; + +#endif //__SHARED_SLOTTED_WIRELESS_HPP__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ssWiChannel.hpp Thu Sep 06 10:37:03 2012 +0000 @@ -0,0 +1,11 @@ +#ifndef __SHARED_SLOTTED_WIRELESS_CHANNEL_HPP__ +#define __SHARED_SLOTTED_WIRELESS_CHANNEL_HPP__ + +class ssWiChannel +{ +public: + virtual int read (char* msg) = 0; + virtual void write (const char* msg, int n) = 0; +}; + +#endif //__SHARED_SLOTTED_WIRELESS_CHANNEL_HPP__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ssWiPort.cpp Thu Sep 06 10:37:03 2012 +0000 @@ -0,0 +1,44 @@ +#include "ssWiPort.hpp" + +PortValue ssWiPort::getTXValue() +{ + PortValue tmp; + mutexTX.lock(); + 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; +} + +PortValue ssWiPort::getRXValue() +{ + PortValue tmp; + mutexRX.lock(); + tmp = valueRX; + mutexRX.unlock(); + return tmp; +} + +void ssWiPort::setRXValue(PortValue tmp) +{ + mutexRX.lock(); + valueRX = tmp; + mutexRX.unlock(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ssWiPort.hpp Thu Sep 06 10:37:03 2012 +0000 @@ -0,0 +1,40 @@ +#ifndef __SHARED_SLOTTED_WIRELESS_PORT_HPP__ +#define __SHARED_SLOTTED_WIRELESS_PORT_HPP__ + +#include "ssWiTypes.hpp" + +#include "rtos.h" + +class ssWiPort +{ + //buffer for the receaving + PortValue valueRX; + Mutex mutexRX; + + //buffer for the transmission + PortValue valueTX; + Mutex mutexTX; + + bool modified; + + +public: + + ssWiPort () { + modified = false; + valueTX = 0; + valueRX = 0; + } + + //transmission + PortValue getTXValue(); + void setTXValue(PortValue tmp); + bool isModified(); + + //reception + PortValue getRXValue(); + void setRXValue(PortValue tmp); + +}; + +#endif //__SHARED_SLOTTED_WIRELESS_PORT_HPP__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ssWiSocket.cpp Thu Sep 06 10:37:03 2012 +0000 @@ -0,0 +1,168 @@ +/* +//#include "ssWi.hpp" +#include "ssWiSocket.hpp" +#include "ssWiPort.hpp" + + +ssWiChannel* ssWiSocket::channel = NULL; +Mutex ssWiSocket::chMutex = Mutex(); + +static Thread* ssWiSocket::sender = NULL; +static Thread* ssWiSocket::receiver = NULL; + + +static bool ssWiSocket::setChannel (ssWiChannel* c) +{ + if (sender!=NULL) + delete sender; + if (receiver!=NULL) + delete receiver; + chMutex.lock(); + channel = c; + sender = new Thread(threadSender); + receiver = new Thread(threadReceiver); + chMutex.unlock(); +} + +ssWiSocket::ssWiSocket(PortID port) +{ + this->port = port; + setPort (port); +} + +bool ssWiSocket::valid () +{ + return isActive(port); +} + +bool ssWiSocket::close() +{ + return unsetPort(port); +} + +bool ssWiSocket::read (PortValue &value) +{ + return readPort (port, value); +} + +bool ssWiSocket::write (PortValue value) +{ + return writePort (port, value); +} + + +#include "ssWi.hpp" + + + + +#include "mbed.h" +#include "rtos.h" + + + + +bool setPort (PortID port) +{ + if (ports.find(port)!=ports.end()) + return false; + ports[port]; + return true; +} + +bool isActive (PortID port) +{ + return ports.find(port)!=ports.end(); +} + +bool unsetPort (PortID port) +{ + if (ports.find(port)==ports.end()) + return false; + ports.erase(port); + return true; +} + +bool readPort (PortID port, PortValue &value) +{ + if (ports.find(port)==ports.end()) + return false; + value = ports[port].getRXValue(); + return true; +} + +bool writePort (PortID port, PortValue value) +{ + if (ports.find(port)==ports.end()) + return false; + ports[port].setTXValue(value); + return true; +} + + +#define INTERNAL_BUFFER_SIZE 100 + +void threadSender (void const* arg) +{ + char buffer[INTERNAL_BUFFER_SIZE]; + while(true) { + int n = 0; + std::map<int, ssWiPort>::iterator it = ports.begin(); + + buffer[n++] = 255; + // && n<(INTERNAL_BUFFER_SIZE-sizeof(PortID)-sizeof(PortValue)-1) + for (; it!=ports.end(); it++) { + if ((*it).second.isModified()) { + buffer[n++] = (*it).first; + PortValue tmp = (*it).second.getTXValue(); + for (int i=0; i<sizeof(PortValue); i++) { + buffer[n++] = tmp & 0x00ff; + tmp = tmp>>8; + } + } + } + //buffer[n++] = '\r'; + if (n>1) { + mutexChannel.lock(); + channel->write(buffer, n); + mutexChannel.unlock(); + } + //printf("Totale byte: %d\n\r", n); + //for (int i=0; i<n; i++) + // printf("%d ", buffer[i]); + //printf("\n\r"); + Thread::wait(200); + } +} + +void threadReceiver (void const* arg) +{ + char buffer[INTERNAL_BUFFER_SIZE]; + while(true) { + mutexChannel.lock(); + int n = channel->read(buffer); + //printf("Byte letti: %d\n\r", n); + //for (int i=0; i<n; i++) + // printf("%d ", buffer[i]); + //printf("\n\r"); + mutexChannel.unlock(); + + int i = 0; + for (; buffer[i]!=255 && i<n; i++); + i++; + + for (; i<n;) { + PortID port = buffer[i++]; + PortValue value = 0; + for (int j=sizeof(PortValue)-1; j>=0; j--) { + value = value<<8; + value += buffer[i+j]; + } + i += sizeof(PortValue); + if (ports.find(port)!=ports.end()) + ports[port].setRXValue(value); + } + Thread::wait(100); + } +} +*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ssWiSocket.hpp Thu Sep 06 10:37:03 2012 +0000 @@ -0,0 +1,37 @@ +#ifndef __SHARED_SLOTTED_WIRELESS_SOCKET_HPP__ +#define __SHARED_SLOTTED_WIRELESS_SOCKET_HPP__ + +#include "ssWiPort.hpp" + +class ssWiSocket +{ + + friend class ssWi; + + //ssWi* w; + ssWiPort* p; + + //ssWiSocket(ssWi* w, ssWiPort* p) { + ssWiSocket(ssWiPort* p) { + //this->w = w; + this->p = p; + } + +public: + + PortValue read () { + return p->getRXValue(); + } + void write (PortValue value) { + p->setTXValue(value); + } + +/* + bool close() { + w->unsetPort(); + } +*/ +}; + + +#endif //__SHARED_SLOTTED_WIRELESS_SOCKET_HPP__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ssWiTypes.hpp Thu Sep 06 10:37:03 2012 +0000 @@ -0,0 +1,16 @@ +/** \file ssWiTypes.hpp + * \biref type definition + */ + +#ifndef __SHARED_SLOTTED_TYPES_HPP__ +#define __SHARED_SLOTTED_TYPES_HPP__ + +/** \brief Port Identifier + */ +typedef char PortID; + +/** \brief Value exchanged through the port + */ +typedef int PortValue; + +#endif //__SHARED_SLOTTED_TYPES_HPP__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xbee.lib Thu Sep 06 10:37:03 2012 +0000 @@ -0,0 +1,1 @@ +xbee#1c7f8dfcde17
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xbee/xbee.cpp Thu Sep 06 10:37:03 2012 +0000 @@ -0,0 +1,215 @@ +#include "xbee.hpp" + +#include <sstream> + + +void XBeeModule::readResponse (char* msg) +{ + char c = 0; + int i = 0; + while (c!='\r') + if (xbee.readable()) { + c = xbee.getc(); + msg[i++] = c; + } +} + + +bool XBeeModule::executeWithOk (const char* cmd) +{ + char msg[5]; + xbee.printf("%s", cmd); + readResponse(msg); + //printf("%s -> %s\n\r", cmd, msg); + if (strncmp(msg, "OK\r", 3)!=0) + return false; + return true; +} + + +void XBeeModule::executeWithRes (const char* cmd, char* res) +{ + xbee.printf("%s", cmd); + readResponse(res); + //printf("\n\r%s -> %s\n\r", cmd, res); +} + + + + + + +XBeeModule::XBeeModule (PinName tx, PinName rx, int panID, int channel) : xbee(tx, rx) +{ + status = false; + wait(1); + if (!_getLocalAddr()) + return; + wait(1); + if (!_setChannel(channel)) + return; + wait(1); + if (!_setPanID(panID)) + return; + wait(1); + status = true; +} + + + + + + + +XBeeAddress XBeeModule::getDstAddress () +{ + char tmp[10]; + std::stringstream s1, s2; + string high, low; + XBeeAddress addr; +wait(1); + if (!executeWithOk("+++")) + return addr; + +wait(1); + + executeWithRes("ATDH \r", tmp); + s1<<std::hex<<tmp; + s1>>high; + + executeWithRes("ATDL \r", tmp); + s2<<std::hex<<tmp; + s2>>low; + + if (!executeWithOk("ATCN \r")) + return addr; + + wait(1); + return XBeeAddress(low, high); +} + + +bool XBeeModule::setDstAddress (XBeeAddress addr) +{ + char s[10]; + string low, high; +wait(1); + if (!executeWithOk("+++")) + return false; +wait(1); + sprintf(s, "ATDH%s \r", addr.getHighAddr().c_str()); + //printf("%s\n\r", addr.getHighAddr().c_str()); + if (!executeWithOk(s)) + return false; + + sprintf(s, "ATDL%s \r", addr.getLowAddr().c_str()); + //printf("%s\n\r", addr.getLowAddr().c_str()); + if (!executeWithOk(s)) + return false; + + if (!executeWithOk("ATCN \r")) + return false; + + return true; +} + + +bool XBeeModule::_getLocalAddr () +{ + char tmp[10]; + string high, low; + std::stringstream s1, s2; + + if (!executeWithOk("+++")) + return false; + executeWithRes("ATSH \r", tmp); + s1<<std::hex<<tmp; + s1>>high; + executeWithRes("ATSL \r", tmp); + s2<<std::hex<<tmp; + s2>>low; + if (!executeWithOk("ATCN \r")) + return false; + local = XBeeAddress(low, high); + return true; +} + + +bool XBeeModule::_setChannel (int channel) +{ + char s[10]; + + if (!executeWithOk("+++")) + return false; +wait(1); + sprintf(s, "ATCH%d \r", channel); + if (!executeWithOk(s)) + return false; + if (!executeWithOk("ATCN \r")) + return false; + return true; +} + +int XBeeModule::getChannel () +{ + int channel; + char s[10]; + + if (!executeWithOk("+++")) + return -1; +wait(1); + executeWithRes("ATCH \r", s); + channel = atoi(s); + if (!executeWithOk("ATCN \r")) + return -1; + wait(1); + return channel; +} + + +bool XBeeModule::_setPanID (int panID) +{ + char s[10]; + if (!executeWithOk("+++")) + return false; + sprintf(s, "ATID%d \r", panID); + if (!executeWithOk(s)) + return false; + if (!executeWithOk("ATCN \r")) + return false; + return true; +} + +int XBeeModule::getPanID () +{ + int id; + char s[10]; + + if (!executeWithOk("+++")) + return -1; + executeWithRes("ATID \r", s); + id = atoi(s); + if (!executeWithOk("ATCN \r")) + return -1; + wait(1); + return id; +} + + + +int XBeeModule::read (char* msg) +{ + int i = 0; + while (xbee.readable() && i<100) + msg[i++] = xbee.getc(); + return i; +} + + +void XBeeModule::write (const char* msg, int n) +{ + for (int i=0; i<n; i++) { + while(!xbee.writeable()); + xbee.putc(msg[i]); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xbee/xbee.hpp Thu Sep 06 10:37:03 2012 +0000 @@ -0,0 +1,79 @@ +#ifndef __XBEE_MODULE_HPP__ +#define __XBEE_MODULE_HPP__ + +#include "mbed.h" + +#include <ssWiChannel.hpp> + +#include "string" + + +class XBeeAddress +{ + string low; + string high; + +public: + + XBeeAddress () : low(""), high("") {} + + XBeeAddress (string low, string high) { + this->low = low; + this->high = high; + } + + string getLowAddr () { + return low; + } + + string getHighAddr () { + return high; + } +}; + + +class XBeeBroadcastAddress: public XBeeAddress +{ +public: + XBeeBroadcastAddress () : XBeeAddress("00FFFF", "0") {} +}; + + +class XBeeModule: public ssWiChannel +{ + + Serial xbee; + XBeeAddress local; + + bool status; + + bool executeWithOk (const char* cmd); + void executeWithRes (const char* cmd, char* res); + void readResponse (char* msg); + + bool _getLocalAddr (); + bool _setChannel (int channel); + bool _setPanID (int id); + + public: + + XBeeModule (PinName tx, PinName rx, int panID, int channel); + + XBeeAddress getLocalAddress () { + return local; + } + + bool setDstAddress (XBeeAddress addr); + + XBeeAddress getDstAddress (); + + + int getChannel (); + int getPanID (); + + virtual int read (char* msg); + virtual void write (const char* msg, int n); + +}; + +#endif //__XBEE_MODULE_HPP__