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:
Fri Sep 07 23:02:04 2012 +0000
Revision:
4:dbbf82c966c2
Parent:
0:cc7218c5e5f7
Child:
5:0b0ca40aeb81
new version

Who changed what in which revision?

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