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:
Tue Oct 16 11:24:49 2012 +0000
Revision:
12:f35f9195d598
Parent:
11:4ad44d62d510
Child:
13:69ff47a83260
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 #define INTERNAL_BUFFER_SIZE 100
mariob 4:dbbf82c966c2 11
mariob 4:dbbf82c966c2 12 Thread* sender;
mariob 4:dbbf82c966c2 13 Thread* receiver;
mariob 4:dbbf82c966c2 14
mariob 4:dbbf82c966c2 15 ssWiChannel* channel = NULL;
mariob 4:dbbf82c966c2 16 Mutex mutexChannel;
mariob 4:dbbf82c966c2 17
mariob 4:dbbf82c966c2 18 std::map<int, ssWiPort> ports;
mariob 4:dbbf82c966c2 19
mariob 4:dbbf82c966c2 20 int TXRate;
mariob 4:dbbf82c966c2 21 int RXRate;
mariob 4:dbbf82c966c2 22
mariob 4:dbbf82c966c2 23
mariob 4:dbbf82c966c2 24 void threadSender (void const* arg);
mariob 4:dbbf82c966c2 25 void threadReceiver (void const* arg);
mariob 4:dbbf82c966c2 26
mariob 4:dbbf82c966c2 27
mariob 4:dbbf82c966c2 28 bool ssWi_init (ssWiChannel* c, int rateTX, int rateRX)
mariob 4:dbbf82c966c2 29 {
mariob 4:dbbf82c966c2 30 if (channel!=NULL)
mariob 4:dbbf82c966c2 31 return false;
mariob 8:354a0e3087c1 32 TXRate = 1000.0/rateTX;
mariob 8:354a0e3087c1 33 RXRate = 1000.0/rateRX;
mariob 4:dbbf82c966c2 34 mutexChannel.lock();
mariob 4:dbbf82c966c2 35 channel = c;
mariob 4:dbbf82c966c2 36 sender = new Thread(threadSender);
mariob 4:dbbf82c966c2 37 receiver = new Thread(threadReceiver);
mariob 4:dbbf82c966c2 38 mutexChannel.unlock();
mariob 4:dbbf82c966c2 39 return true;
mariob 4:dbbf82c966c2 40 }
mariob 4:dbbf82c966c2 41
mariob 4:dbbf82c966c2 42 bool ssWi_isActive (PortID port)
mariob 4:dbbf82c966c2 43 {
mariob 4:dbbf82c966c2 44 return channel!=NULL && ports.find(port)!=ports.end();
mariob 4:dbbf82c966c2 45 }
mariob 4:dbbf82c966c2 46
mariob 4:dbbf82c966c2 47 bool ssWi_setPort (PortID port)
mariob 4:dbbf82c966c2 48 {
mariob 4:dbbf82c966c2 49 if (channel==NULL)
mariob 4:dbbf82c966c2 50 return false;
mariob 4:dbbf82c966c2 51 ports[port];
mariob 4:dbbf82c966c2 52 return true;
mariob 4:dbbf82c966c2 53 }
mariob 4:dbbf82c966c2 54
mariob 4:dbbf82c966c2 55 bool ssWi_unsetPort (PortID port)
mariob 4:dbbf82c966c2 56 {
mariob 4:dbbf82c966c2 57 if (!ssWi_isActive(port))
mariob 4:dbbf82c966c2 58 return false;
mariob 4:dbbf82c966c2 59 ports.erase(port);
mariob 4:dbbf82c966c2 60 return true;
mariob 4:dbbf82c966c2 61 }
mariob 4:dbbf82c966c2 62
mariob 9:b5b5d0533fa6 63
mariob 4:dbbf82c966c2 64 void threadSender (void const* arg)
mariob 4:dbbf82c966c2 65 {
mariob 9:b5b5d0533fa6 66 char buffer[50];
mariob 4:dbbf82c966c2 67 while(true) {
mariob 9:b5b5d0533fa6 68 int n = 3;
mariob 9:b5b5d0533fa6 69 int numFrames = 0;
mariob 9:b5b5d0533fa6 70 for (std::map<int, ssWiPort>::iterator it = ports.begin(); it!=ports.end(); it++) {
mariob 4:dbbf82c966c2 71 if ((*it).second.isModified()) {
mariob 4:dbbf82c966c2 72 buffer[n++] = (*it).first;
mariob 4:dbbf82c966c2 73 PortValue tmp = (*it).second.getTXValue();
mariob 5:0b0ca40aeb81 74 memcpy(&buffer[n], &tmp, sizeof(PortValue));
mariob 5:0b0ca40aeb81 75 n += sizeof(PortValue);
mariob 9:b5b5d0533fa6 76 //printf("Write[%d] = %d\n\r", (*it).first, tmp);
mariob 9:b5b5d0533fa6 77 numFrames++;
mariob 4:dbbf82c966c2 78 }
mariob 4:dbbf82c966c2 79 }
mariob 9:b5b5d0533fa6 80 if (numFrames>0) {
mariob 9:b5b5d0533fa6 81 buffer[0] = START_0;
mariob 9:b5b5d0533fa6 82 buffer[1] = START_1;
mariob 9:b5b5d0533fa6 83 buffer[2] = START_2;
mariob 4:dbbf82c966c2 84 mutexChannel.lock();
mariob 4:dbbf82c966c2 85 channel->write(buffer, n);
mariob 4:dbbf82c966c2 86 mutexChannel.unlock();
mariob 11:4ad44d62d510 87 /*
mariob 9:b5b5d0533fa6 88 printf("W(%d): ", n);
mariob 8:354a0e3087c1 89 for(int k=0; k<n; k++)
mariob 8:354a0e3087c1 90 printf("%d ", buffer[k]);
mariob 8:354a0e3087c1 91 printf("\n\r");
mariob 11:4ad44d62d510 92 */
mariob 4:dbbf82c966c2 93 }
mariob 4:dbbf82c966c2 94 Thread::wait(TXRate);
mariob 4:dbbf82c966c2 95 }
mariob 4:dbbf82c966c2 96 }
mariob 4:dbbf82c966c2 97
mariob 4:dbbf82c966c2 98 void threadReceiver (void const* arg)
mariob 4:dbbf82c966c2 99 {
mariob 9:b5b5d0533fa6 100 char buffer[50];
mariob 9:b5b5d0533fa6 101
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 9:b5b5d0533fa6 106
mariob 11:4ad44d62d510 107 /*
mariob 11:4ad44d62d510 108 if (n>0) {
mariob 11:4ad44d62d510 109 printf("R(%d): ", n);
mariob 11:4ad44d62d510 110 for(int k=0; k<n; k++)
mariob 11:4ad44d62d510 111 printf("%d ", buffer[k]);
mariob 11:4ad44d62d510 112 printf("\n\r");
mariob 11:4ad44d62d510 113 }
mariob 11:4ad44d62d510 114 */
mariob 9:b5b5d0533fa6 115 bool alreadyIn = false;
mariob 9:b5b5d0533fa6 116 for (int i=0; i<(n-2);) {
mariob 9:b5b5d0533fa6 117 bool found = false;
mariob 9:b5b5d0533fa6 118 if (buffer[i]==START_0 && buffer[i+1]==START_1 && buffer[i+2]==START_2) {
mariob 9:b5b5d0533fa6 119 found = true;
mariob 9:b5b5d0533fa6 120 alreadyIn = true;
mariob 9:b5b5d0533fa6 121 i += 3;
mariob 4:dbbf82c966c2 122 }
mariob 9:b5b5d0533fa6 123 if ((found || alreadyIn) && (n-i)>=(sizeof(PortID)+sizeof(PortValue))) {
mariob 9:b5b5d0533fa6 124 PortID port = buffer[i++];
mariob 9:b5b5d0533fa6 125 PortValue value = 0;
mariob 9:b5b5d0533fa6 126 memcpy(&value, &buffer[i], sizeof(PortValue));
mariob 11:4ad44d62d510 127 // printf("Read[%d] = %d\n\r", port, value);
mariob 9:b5b5d0533fa6 128 i += sizeof(PortValue);
mariob 9:b5b5d0533fa6 129 if (ports.find(port)!=ports.end())
mariob 9:b5b5d0533fa6 130 ports[port].setRXValue(value);
mariob 9:b5b5d0533fa6 131 } else
mariob 9:b5b5d0533fa6 132 i++;
mariob 4:dbbf82c966c2 133 }
mariob 4:dbbf82c966c2 134 Thread::wait(RXRate);
mariob 4:dbbf82c966c2 135 }
mariob 4:dbbf82c966c2 136 }