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:
Sun Sep 09 20:02:36 2012 +0000
Revision:
8:354a0e3087c1
Parent:
6:f98855efa37e
Child:
9:b5b5d0533fa6
ok; ; MB

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 8:354a0e3087c1 34 TXRate = 1000.0/rateTX;
mariob 8:354a0e3087c1 35 RXRate = 1000.0/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 5:0b0ca40aeb81 70 buffer[n++] = 255;
mariob 4:dbbf82c966c2 71 std::map<int, ssWiPort>::iterator it = ports.begin();
mariob 4:dbbf82c966c2 72 for (; it!=ports.end(); it++) {
mariob 4:dbbf82c966c2 73 if ((*it).second.isModified()) {
mariob 4:dbbf82c966c2 74 buffer[n++] = (*it).first;
mariob 4:dbbf82c966c2 75 PortValue tmp = (*it).second.getTXValue();
mariob 5:0b0ca40aeb81 76 memcpy(&buffer[n], &tmp, sizeof(PortValue));
mariob 5:0b0ca40aeb81 77 n += sizeof(PortValue);
mariob 5:0b0ca40aeb81 78 //for (int i=0; i<sizeof(PortValue); i++) {
mariob 5:0b0ca40aeb81 79 // buffer[n++] = tmp & 0x00ff;
mariob 5:0b0ca40aeb81 80 // tmp = tmp>>8;
mariob 5:0b0ca40aeb81 81 //}
mariob 4:dbbf82c966c2 82 }
mariob 4:dbbf82c966c2 83 }
mariob 4:dbbf82c966c2 84 if (n>1) {
mariob 4:dbbf82c966c2 85 mutexChannel.lock();
mariob 4:dbbf82c966c2 86 channel->write(buffer, n);
mariob 4:dbbf82c966c2 87 mutexChannel.unlock();
mariob 8:354a0e3087c1 88 /*
mariob 8:354a0e3087c1 89 printf("W: ");
mariob 8:354a0e3087c1 90 for(int k=0; k<n; k++)
mariob 8:354a0e3087c1 91 printf("%d ", buffer[k]);
mariob 8:354a0e3087c1 92 printf("\n\r");
mariob 8:354a0e3087c1 93 */
mariob 4:dbbf82c966c2 94 }
mariob 4:dbbf82c966c2 95 Thread::wait(TXRate);
mariob 4:dbbf82c966c2 96 }
mariob 4:dbbf82c966c2 97 }
mariob 4:dbbf82c966c2 98
mariob 4:dbbf82c966c2 99 void threadReceiver (void const* arg)
mariob 4:dbbf82c966c2 100 {
mariob 4:dbbf82c966c2 101 char buffer[INTERNAL_BUFFER_SIZE];
mariob 4:dbbf82c966c2 102 while(true) {
mariob 4:dbbf82c966c2 103 mutexChannel.lock();
mariob 5:0b0ca40aeb81 104 int n = channel->read(buffer);
mariob 4:dbbf82c966c2 105 mutexChannel.unlock();
mariob 8:354a0e3087c1 106 /*
mariob 8:354a0e3087c1 107 if (n>0) {
mariob 8:354a0e3087c1 108 printf("R: ");
mariob 8:354a0e3087c1 109 for(int k=0; k<n; k++)
mariob 8:354a0e3087c1 110 printf("%d ", buffer[k]);
mariob 8:354a0e3087c1 111 printf("\n\r");
mariob 8:354a0e3087c1 112 }
mariob 8:354a0e3087c1 113 */
mariob 4:dbbf82c966c2 114 int i = 0;
mariob 4:dbbf82c966c2 115 for (; buffer[i]!=255 && i<n; i++);
mariob 4:dbbf82c966c2 116 i++;
mariob 4:dbbf82c966c2 117
mariob 4:dbbf82c966c2 118 for (; i<n;) {
mariob 4:dbbf82c966c2 119 PortID port = buffer[i++];
mariob 4:dbbf82c966c2 120 PortValue value = 0;
mariob 5:0b0ca40aeb81 121 memcpy(&value, &buffer[i], sizeof(PortValue));
mariob 5:0b0ca40aeb81 122 /*
mariob 4:dbbf82c966c2 123 for (int j=sizeof(PortValue)-1; j>=0; j--) {
mariob 4:dbbf82c966c2 124 value = value<<8;
mariob 4:dbbf82c966c2 125 value += buffer[i+j];
mariob 4:dbbf82c966c2 126 }
mariob 5:0b0ca40aeb81 127 */
mariob 4:dbbf82c966c2 128 i += sizeof(PortValue);
mariob 4:dbbf82c966c2 129 if (ports.find(port)!=ports.end())
mariob 4:dbbf82c966c2 130 ports[port].setRXValue(value);
mariob 4:dbbf82c966c2 131 }
mariob 4:dbbf82c966c2 132 Thread::wait(RXRate);
mariob 4:dbbf82c966c2 133 }
mariob 4:dbbf82c966c2 134 }