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)
Revision:
20:36931ee9af85
Parent:
16:b50c3b3b241c
Child:
21:2db8d5db6c57
--- a/ssWi.cpp	Thu Mar 07 15:44:05 2013 +0000
+++ b/ssWi.cpp	Fri Apr 17 15:19:40 2020 +0000
@@ -68,73 +68,68 @@
 int RXRate;
 
 
-/*
-Thread* sender;
-Thread* receiver;
+void functionSender();
+void functionReceiver();
 
-void threadSender (void const* arg);
-void threadReceiver (void const* arg);
+static Thread readingThread;
+static Thread writingThread;
 
 bool ssWi_init (ssWiChannel* c, int rateTX, int rateRX)
 {
     if (channel!=NULL)
         return false;
-    TXRate = 1000.0/rateTX;
-    RXRate = 1000.0/rateRX;
-    mutexChannel.lock();
+    //mutexChannel.lock();
     channel = c;
-    mutexChannel.unlock();
-    sender = new Thread(threadSender);
-    receiver = new Thread(threadReceiver);
+    //mutexChannel.unlock();
+    TXRate = rateTX > 0 ? 1000/rateTX : 0;
+    RXRate = rateRX > 0 ? 1000/rateRX : 0;
+    
+    readingThread.start(functionReceiver);
+    writingThread.start(functionSender);
+
     return true;
 }
 
-void threadSender (void const* arg)
-{
+void functionSender() {
+    static DigitalOut led4(LED4);
     static char buffer[INTERNAL_BUFFER_SIZE];
-    while(true) {
+
+    while (1) {
+        led4 = !led4;
         int n = 3;
         int numFrames = 0;
-        for (std::map<int, ssWiPort>::iterator it = ports.begin(); it!=ports.end(); it++) {
+        for (std::map<int, ssWiPort>::iterator it = ports.begin();
+            it != ports.end(); it++) {
             if ((*it).second.isModified()) {
                 buffer[n++] = (*it).first;
                 PortValue tmp = (*it).second.getTXValue();
                 memcpy(&buffer[n], &tmp, sizeof(PortValue));
                 n += sizeof(PortValue);
-                //printf("Write[%d] = %d\n\r", (*it).first, tmp);
                 numFrames++;
             }
         }
-        if (numFrames>0) {
+        if (numFrames > 0) {
             buffer[0] = START_0;
             buffer[1] = START_1;
             buffer[2] = START_2;
-            mutexChannel.lock();
+            if (!mutexChannel.trylock())
+                return;
             channel->write(buffer, n);
             mutexChannel.unlock();
-//            printf("W(%d): ", n);
-//            for(int k=0; k<n; k++)
-//                printf("%d ", buffer[k]);
-//            printf("\n\r");
         }
-        Thread::wait(TXRate);
+        thread_sleep_for(TXRate);
     }
 }
 
-void threadReceiver (void const* arg)
+void functionReceiver ()
 {
     static char buffer[INTERNAL_BUFFER_SIZE];
-    while(true) {
-        printf("...read...\n\r");
-        mutexChannel.lock();
+
+    while(1) {
+        if (!mutexChannel.trylock())
+            return;
         int n = channel->read(buffer);
         mutexChannel.unlock();
-//        if (n>0) {
-//            printf("R(%d): ", n);
-//              for(int k=0; k<n; k++)
-//                   printf("%d ", buffer[k]);
-//              printf("\n\r");
-//        }
         bool alreadyIn = false;
         for (int i=0; i<(n-2);) {
             bool found = false;
@@ -147,98 +142,14 @@
                 PortID port = buffer[i++];
                 PortValue value = 0;
                 memcpy(&value, &buffer[i], sizeof(PortValue));
-//                printf("Read[%d] = %d\n\r", port, value);
                 i += sizeof(PortValue);
                 if (ports.find(port)!=ports.end())
                     ports[port].setRXValue(value);
             }
         }
-        Thread::wait(RXRate);
+        thread_sleep_for(RXRate);
     }
 }
-*/
-
-
-void functionSender (void const* arg);
-void functionReceiver (void const* arg);
-
-RtosTimer *senderTimer;
-RtosTimer *receiverTimer;
-
-bool ssWi_init (ssWiChannel* c, int rateTX, int rateRX)
-{
-    if (channel!=NULL)
-        return false;
-    mutexChannel.lock();
-    channel = c;
-    mutexChannel.unlock();
-    senderTimer = new RtosTimer(functionSender, osTimerPeriodic, NULL);
-    receiverTimer = new RtosTimer(functionReceiver, osTimerPeriodic, NULL);
-    if (rateTX>0) {
-        TXRate = 1000/rateTX;
-        senderTimer->start(TXRate);
-    } else
-       TXRate = 0;
-    if (rateRX>0) {
-        RXRate = 1000/rateRX;
-        receiverTimer->start(RXRate);
-    } else
-       RXRate = 0;
-    return true;
-}
-
-void functionSender (void const* arg)
-{
-    static char buffer[INTERNAL_BUFFER_SIZE];
-    int n = 3;
-    int numFrames = 0;
-    for (std::map<int, ssWiPort>::iterator it = ports.begin(); it!=ports.end(); it++) {
-        if ((*it).second.isModified()) {
-            buffer[n++] = (*it).first;
-            PortValue tmp = (*it).second.getTXValue();
-            memcpy(&buffer[n], &tmp, sizeof(PortValue));
-            n += sizeof(PortValue);
-            numFrames++;
-        }
-    }
-    if (numFrames>0) {
-        buffer[0] = START_0;
-        buffer[1] = START_1;
-        buffer[2] = START_2;
-        if (!mutexChannel.trylock())
-            return;
-        channel->write(buffer, n);
-        mutexChannel.unlock();
-    }
-}
-
-void functionReceiver (void const* arg)
-{
-    static char buffer[INTERNAL_BUFFER_SIZE];
-    if (!mutexChannel.trylock())
-        return;
-    int n = channel->read(buffer);
-    mutexChannel.unlock();
-    bool alreadyIn = false;
-    for (int i=0; i<(n-2);) {
-        bool found = false;
-        if (buffer[i]==START_0 && buffer[i+1]==START_1 && buffer[i+2]==START_2) {
-            found = true;
-            alreadyIn = true;
-            i += 3;
-        }
-        if ((found || alreadyIn) && (n-i)>=(sizeof(PortID)+sizeof(PortValue))) {
-            PortID port = buffer[i++];
-            PortValue value = 0;
-            memcpy(&value, &buffer[i], sizeof(PortValue));
-            i += sizeof(PortValue);
-            if (ports.find(port)!=ports.end())
-                ports[port].setRXValue(value);
-        }
-    }
-}
-
-
 
 
 inline bool ssWi_isActive (PortID port)