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:
4:dbbf82c966c2
ssWi

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mariob 0:cc7218c5e5f7 1 #include "ssWi.hpp"
mariob 0:cc7218c5e5f7 2
mariob 0:cc7218c5e5f7 3 #include "mbed.h"
mariob 0:cc7218c5e5f7 4 #include "rtos.h"
mariob 0:cc7218c5e5f7 5
mariob 0:cc7218c5e5f7 6
mariob 0:cc7218c5e5f7 7 void threadSender (void const* arg);
mariob 0:cc7218c5e5f7 8 void threadReceiver (void const* arg);
mariob 0:cc7218c5e5f7 9
mariob 0:cc7218c5e5f7 10
mariob 0:cc7218c5e5f7 11 ssWi::ssWi (ssWiChannel* c, int rateTX, int rateRX)
mariob 0:cc7218c5e5f7 12 {
mariob 0:cc7218c5e5f7 13 msTXSleep = 1000/rateTX;
mariob 0:cc7218c5e5f7 14 msRXSleep = 1000/rateRX;
mariob 0:cc7218c5e5f7 15 mutexChannel.lock();
mariob 0:cc7218c5e5f7 16 channel = c;
mariob 0:cc7218c5e5f7 17 sender = new Thread(threadSender, this);
mariob 0:cc7218c5e5f7 18 receiver = new Thread(threadReceiver, this);
mariob 0:cc7218c5e5f7 19 mutexChannel.unlock();
mariob 0:cc7218c5e5f7 20 }
mariob 0:cc7218c5e5f7 21
mariob 0:cc7218c5e5f7 22 ssWi::~ssWi ()
mariob 0:cc7218c5e5f7 23 {
mariob 0:cc7218c5e5f7 24 delete sender;
mariob 0:cc7218c5e5f7 25 delete receiver;
mariob 0:cc7218c5e5f7 26 }
mariob 0:cc7218c5e5f7 27
mariob 0:cc7218c5e5f7 28 ssWiSocket* ssWi::createSocket (PortID port)
mariob 0:cc7218c5e5f7 29 {
mariob 0:cc7218c5e5f7 30 setPort(port);
mariob 0:cc7218c5e5f7 31 return new ssWiSocket(&ports[port]);
mariob 0:cc7218c5e5f7 32 }
mariob 0:cc7218c5e5f7 33
mariob 0:cc7218c5e5f7 34 bool ssWi::setPort (PortID port)
mariob 0:cc7218c5e5f7 35 {
mariob 0:cc7218c5e5f7 36 if (isActive(port))
mariob 0:cc7218c5e5f7 37 return false;
mariob 0:cc7218c5e5f7 38 ports[port];
mariob 0:cc7218c5e5f7 39 return true;
mariob 0:cc7218c5e5f7 40 }
mariob 0:cc7218c5e5f7 41
mariob 0:cc7218c5e5f7 42 bool ssWi::unsetPort (PortID port)
mariob 0:cc7218c5e5f7 43 {
mariob 0:cc7218c5e5f7 44 if (!isActive(port))
mariob 0:cc7218c5e5f7 45 return false;
mariob 0:cc7218c5e5f7 46 ports.erase(port);
mariob 0:cc7218c5e5f7 47 return true;
mariob 0:cc7218c5e5f7 48 }
mariob 0:cc7218c5e5f7 49
mariob 0:cc7218c5e5f7 50 void ssWi::readFromChannel (char* msg, int& len)
mariob 0:cc7218c5e5f7 51 {
mariob 0:cc7218c5e5f7 52 mutexChannel.lock();
mariob 0:cc7218c5e5f7 53 len = channel->read(msg);
mariob 0:cc7218c5e5f7 54 mutexChannel.unlock();
mariob 0:cc7218c5e5f7 55 }
mariob 0:cc7218c5e5f7 56
mariob 0:cc7218c5e5f7 57 void ssWi::writeOnChannel (char* msg, int len)
mariob 0:cc7218c5e5f7 58 {
mariob 0:cc7218c5e5f7 59 mutexChannel.lock();
mariob 0:cc7218c5e5f7 60 channel->write(msg, len);
mariob 0:cc7218c5e5f7 61 mutexChannel.unlock();
mariob 0:cc7218c5e5f7 62 }
mariob 0:cc7218c5e5f7 63
mariob 0:cc7218c5e5f7 64 #define INTERNAL_BUFFER_SIZE 100
mariob 0:cc7218c5e5f7 65
mariob 0:cc7218c5e5f7 66 void threadSender (void const* arg)
mariob 0:cc7218c5e5f7 67 {
mariob 0:cc7218c5e5f7 68 ssWi* s = (ssWi*)arg;
mariob 0:cc7218c5e5f7 69 char buffer[INTERNAL_BUFFER_SIZE];
mariob 0:cc7218c5e5f7 70 while(true) {
mariob 0:cc7218c5e5f7 71 int n = 0;
mariob 0:cc7218c5e5f7 72 std::map<int, ssWiPort>::iterator it = s->ports.begin();
mariob 0:cc7218c5e5f7 73
mariob 0:cc7218c5e5f7 74 buffer[n++] = 255;
mariob 0:cc7218c5e5f7 75 for (; it!=s->ports.end(); it++) {
mariob 0:cc7218c5e5f7 76 if ((*it).second.isModified()) {
mariob 0:cc7218c5e5f7 77 buffer[n++] = (*it).first;
mariob 0:cc7218c5e5f7 78 PortValue tmp = (*it).second.getTXValue();
mariob 0:cc7218c5e5f7 79 for (int i=0; i<sizeof(PortValue); i++) {
mariob 0:cc7218c5e5f7 80 buffer[n++] = tmp & 0x00ff;
mariob 0:cc7218c5e5f7 81 tmp = tmp>>8;
mariob 0:cc7218c5e5f7 82 }
mariob 0:cc7218c5e5f7 83 }
mariob 0:cc7218c5e5f7 84 }
mariob 0:cc7218c5e5f7 85 if (n>1)
mariob 0:cc7218c5e5f7 86 s->writeOnChannel(buffer, n);
mariob 0:cc7218c5e5f7 87 Thread::wait(s->getTXSleep());
mariob 0:cc7218c5e5f7 88 }
mariob 0:cc7218c5e5f7 89 }
mariob 0:cc7218c5e5f7 90
mariob 0:cc7218c5e5f7 91 void threadReceiver (void const* arg)
mariob 0:cc7218c5e5f7 92 {
mariob 0:cc7218c5e5f7 93 ssWi* s = (ssWi*)arg;
mariob 0:cc7218c5e5f7 94 char buffer[INTERNAL_BUFFER_SIZE];
mariob 0:cc7218c5e5f7 95 while(true) {
mariob 0:cc7218c5e5f7 96 int n;
mariob 0:cc7218c5e5f7 97 s->readFromChannel(buffer, n);
mariob 0:cc7218c5e5f7 98
mariob 0:cc7218c5e5f7 99 int i = 0;
mariob 0:cc7218c5e5f7 100 for (; buffer[i]!=255 && i<n; i++);
mariob 0:cc7218c5e5f7 101 i++;
mariob 0:cc7218c5e5f7 102
mariob 0:cc7218c5e5f7 103 for (; i<n;) {
mariob 0:cc7218c5e5f7 104 PortID port = buffer[i++];
mariob 0:cc7218c5e5f7 105 PortValue value = 0;
mariob 0:cc7218c5e5f7 106 for (int j=sizeof(PortValue)-1; j>=0; j--) {
mariob 0:cc7218c5e5f7 107 value = value<<8;
mariob 0:cc7218c5e5f7 108 value += buffer[i+j];
mariob 0:cc7218c5e5f7 109 }
mariob 0:cc7218c5e5f7 110 i += sizeof(PortValue);
mariob 0:cc7218c5e5f7 111 if (s->ports.find(port)!=s->ports.end())
mariob 0:cc7218c5e5f7 112 s->ports[port].setRXValue(value);
mariob 0:cc7218c5e5f7 113 }
mariob 0:cc7218c5e5f7 114 Thread::wait(s->getRXSleep());
mariob 0:cc7218c5e5f7 115 }
mariob 0:cc7218c5e5f7 116 }