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.
TODO:
- improve the parsing of the received messages
- communication tests with many nodes (so far, only 2 nodes have been tested)
ssWiSocket.cpp
- Committer:
- mariob
- Date:
- 2012-09-06
- Revision:
- 0:cc7218c5e5f7
- Child:
- 4:dbbf82c966c2
File content as of revision 0:cc7218c5e5f7:
/* //#include "ssWi.hpp" #include "ssWiSocket.hpp" #include "ssWiPort.hpp" ssWiChannel* ssWiSocket::channel = NULL; Mutex ssWiSocket::chMutex = Mutex(); static Thread* ssWiSocket::sender = NULL; static Thread* ssWiSocket::receiver = NULL; static bool ssWiSocket::setChannel (ssWiChannel* c) { if (sender!=NULL) delete sender; if (receiver!=NULL) delete receiver; chMutex.lock(); channel = c; sender = new Thread(threadSender); receiver = new Thread(threadReceiver); chMutex.unlock(); } ssWiSocket::ssWiSocket(PortID port) { this->port = port; setPort (port); } bool ssWiSocket::valid () { return isActive(port); } bool ssWiSocket::close() { return unsetPort(port); } bool ssWiSocket::read (PortValue &value) { return readPort (port, value); } bool ssWiSocket::write (PortValue value) { return writePort (port, value); } #include "ssWi.hpp" #include "mbed.h" #include "rtos.h" bool setPort (PortID port) { if (ports.find(port)!=ports.end()) return false; ports[port]; return true; } bool isActive (PortID port) { return ports.find(port)!=ports.end(); } bool unsetPort (PortID port) { if (ports.find(port)==ports.end()) return false; ports.erase(port); return true; } bool readPort (PortID port, PortValue &value) { if (ports.find(port)==ports.end()) return false; value = ports[port].getRXValue(); return true; } bool writePort (PortID port, PortValue value) { if (ports.find(port)==ports.end()) return false; ports[port].setTXValue(value); return true; } #define INTERNAL_BUFFER_SIZE 100 void threadSender (void const* arg) { char buffer[INTERNAL_BUFFER_SIZE]; while(true) { int n = 0; std::map<int, ssWiPort>::iterator it = ports.begin(); buffer[n++] = 255; // && n<(INTERNAL_BUFFER_SIZE-sizeof(PortID)-sizeof(PortValue)-1) for (; it!=ports.end(); it++) { if ((*it).second.isModified()) { buffer[n++] = (*it).first; PortValue tmp = (*it).second.getTXValue(); for (int i=0; i<sizeof(PortValue); i++) { buffer[n++] = tmp & 0x00ff; tmp = tmp>>8; } } } //buffer[n++] = '\r'; if (n>1) { mutexChannel.lock(); channel->write(buffer, n); mutexChannel.unlock(); } //printf("Totale byte: %d\n\r", n); //for (int i=0; i<n; i++) // printf("%d ", buffer[i]); //printf("\n\r"); Thread::wait(200); } } void threadReceiver (void const* arg) { char buffer[INTERNAL_BUFFER_SIZE]; while(true) { mutexChannel.lock(); int n = channel->read(buffer); //printf("Byte letti: %d\n\r", n); //for (int i=0; i<n; i++) // printf("%d ", buffer[i]); //printf("\n\r"); mutexChannel.unlock(); int i = 0; for (; buffer[i]!=255 && i<n; i++); i++; for (; i<n;) { PortID port = buffer[i++]; PortValue value = 0; for (int j=sizeof(PortValue)-1; j>=0; j--) { value = value<<8; value += buffer[i+j]; } i += sizeof(PortValue); if (ports.find(port)!=ports.end()) ports[port].setRXValue(value); } Thread::wait(100); } } */