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)

ssWi.cpp

Committer:
mariob
Date:
2020-04-21
Revision:
25:83172a067b57
Parent:
24:80345e511574

File content as of revision 25:83172a067b57:

/** \file ssWi.cpp
 *
 *  \brief implementation of the internal functions for mananing the protocol
 *
 */
 
#include "ssWiChannel.hpp"
#include "ssWiPort.hpp"
#include "ssWi.hpp"

#include "mbed.h"
#include "rtos.h"

#include <map>


/** \brief first byte of the header
 *
 * The header is composed of 3 bytes: START_0 START_1 START_2
 */
#define START_0 255
#define START_1 130
#define START_2 255


/** 
 * dimension of the buffers used by the sender and receiver procedure
 */ 
#define INTERNAL_BUFFER_SIZE 100


/** \brief channel abstraction
 *
 * This object represent the communication channel. It can be different means
 */
ssWiChannel* channel = NULL;

/** \brief serialize read and write operations
 *
 * simultaneous multiple accesses to the mean are avoided
 */
Mutex mutexChannel;

/** \brief set of registered communication ports
 *
 */
std::map<int, ssWiPort> ports;

/** \brief transmission rate
 *
 */
int TXRate;

/** \brief reading rate
 *
 */
int RXRate;


void functionSender();
void functionReceiver();

static Thread readingThread;
static Thread writingThread;

bool ssWi_init (ssWiChannel* c, int rateTX, int rateRX)
{
    if (channel!=NULL)
        return false;
    
    // nothing else is using the channel
    channel = c;
    TXRate = rateTX > 0 ? 1000/rateTX : 0;
    RXRate = rateRX > 0 ? 1000/rateRX : 0;
    // start txer and rxer
    readingThread.start(functionReceiver);
    writingThread.start(functionSender);

    return true;
}

void functionSender() {
    static char buffer[INTERNAL_BUFFER_SIZE];

    while (true) {
        int n = 3;
        int numFrames = 0;
        
        // copy data
        for (std::map<int, ssWiPort>::iterator it = ports.begin();
                                                      it != ports.end(); it++) {
            if ((n + sizeof(PortValue) + sizeof(PortID)) > INTERNAL_BUFFER_SIZE)
                break;
            if ((*it).second.isModified()) {
                buffer[n++] = (*it).first;
                PortValue tmp = (*it).second.getTXValue();
                memcpy(&buffer[n], &tmp, sizeof(PortValue));
                n += sizeof(PortValue);
                numFrames++;
            }
        }
        // add header and send
        if (numFrames > 0) {
            buffer[0] = START_0;
            buffer[1] = START_1;
            buffer[2] = START_2;
            mutexChannel.lock();
            channel->write(buffer, n);
            mutexChannel.unlock();
        }
        
        thread_sleep_for(TXRate);
    }
}

void functionReceiver ()
{
    static char buffer[INTERNAL_BUFFER_SIZE];
    int offset = 0;

    while(1) {
        thread_sleep_for(RXRate);

        // read buffer
        mutexChannel.lock();
        int n = channel->read(buffer + offset);
        mutexChannel.unlock();
        // not enough to find the header, merge with next message as the buffer 
        // can contain part of the header
        if (n < 3) {
            offset = n;
            continue;
        }
        // find header
        int index = -1;
        for (int i=0; i<(n-2); i++) {
            if (buffer[i]==START_0 && buffer[i+1]==START_1 && buffer[i+2]==START_2) {
                index = i + 3;
                break;
            }
        }
        // header not found, discard everthing
        offset = 0;
        if (index < 0)
            continue;
        // read samples
        while((index + sizeof(PortID) + sizeof(PortValue)) <= n) {
            PortID port = buffer[index++];
            PortValue value = 0;
            memcpy(&value, &buffer[index], sizeof(PortValue));
            index += sizeof(PortValue);
            if (ports.find(port)!=ports.end())
                ports[port].setRXValue(value);
        }
    }
}


inline bool ssWi_isActive (PortID port)
{
    return channel != NULL && ports.find(port) != ports.end();
}


bool ssWi_setPort (PortID port)
{
    if (channel == NULL)
        return false;
        
    ports[port];
    
    return true;
}


bool ssWi_unsetPort (PortID port)
{
    if (!ssWi_isActive(port))
        return false;

    ports.erase(port);

    return true;
}