desloges-libioulle / Mbed 2 deprecated app3_coordinator

Dependencies:   mbed-rtos mbed EthernetInterface

Committer:
libv2001
Date:
Mon Feb 13 02:17:10 2017 +0000
Revision:
3:05ba7be59773
Parent:
2:ff0b74e5e62c
Ethernet connection added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
libv2001 0:671a7b7e4673 1 #include "mbed.h"
libv2001 0:671a7b7e4673 2 #include "rtos.h"
libv2001 0:671a7b7e4673 3 #include "xbee.h"
libv2001 3:05ba7be59773 4 #include "EthernetInterface.h"
libv2001 0:671a7b7e4673 5
libv2001 0:671a7b7e4673 6 #define nullptr 0
libv2001 0:671a7b7e4673 7
libv2001 0:671a7b7e4673 8 DigitalOut myled(LED1);
libv2001 0:671a7b7e4673 9
libv2001 0:671a7b7e4673 10 Serial pc(USBTX, USBRX);
libv2001 0:671a7b7e4673 11
libv2001 0:671a7b7e4673 12 Serial *xbee;
libv2001 0:671a7b7e4673 13 DigitalOut *xbeeRst;
libv2001 0:671a7b7e4673 14
libv2001 0:671a7b7e4673 15 const int HARDWARE_RESET_SIGNAL = 0x10;
libv2001 0:671a7b7e4673 16 const int COORDINATOR_STARTED_SIGNAL = 0x20;
libv2001 0:671a7b7e4673 17 const int TICKER_SIGNAL = 0x40;
libv2001 0:671a7b7e4673 18 const int RESPONSE_SIGNAL = 0x80;
libv2001 0:671a7b7e4673 19
libv2001 0:671a7b7e4673 20 Thread * XBeeConsumer;
libv2001 0:671a7b7e4673 21 Thread * XBeeProducer;
libv2001 0:671a7b7e4673 22 Thread * EventConsumer;
libv2001 3:05ba7be59773 23 Thread * EthernetConsumer;
libv2001 0:671a7b7e4673 24 Ticker timer;
libv2001 0:671a7b7e4673 25
libv2001 0:671a7b7e4673 26 int responseStatus;
libv2001 0:671a7b7e4673 27
libv2001 0:671a7b7e4673 28 char * BTN_ID = "BTN";
libv2001 0:671a7b7e4673 29 char * ACC_ID = "ACC";
libv2001 0:671a7b7e4673 30
libv2001 0:671a7b7e4673 31 struct ButtonEvent{
libv2001 0:671a7b7e4673 32 char id[3];
libv2001 0:671a7b7e4673 33 bool state;
libv2001 0:671a7b7e4673 34 };
libv2001 0:671a7b7e4673 35
libv2001 0:671a7b7e4673 36 struct AccelerometerEvent {
libv2001 0:671a7b7e4673 37 char id[3];
libv2001 0:671a7b7e4673 38 char x[2], y[2], z[2];
libv2001 0:671a7b7e4673 39 };
libv2001 0:671a7b7e4673 40
libv2001 0:671a7b7e4673 41 MemoryPool<ButtonEvent, 32> btnPool;
libv2001 0:671a7b7e4673 42 MemoryPool<AccelerometerEvent, 32> accPool;
libv2001 0:671a7b7e4673 43 Queue<void, 64> event;
libv2001 0:671a7b7e4673 44
libv2001 0:671a7b7e4673 45 struct Rooter{
libv2001 3:05ba7be59773 46 char addr64[8];
libv2001 3:05ba7be59773 47 char addr16[2];
libv2001 0:671a7b7e4673 48
libv2001 0:671a7b7e4673 49 bool operator==(const Rooter & rhs){
libv2001 3:05ba7be59773 50 bool same = true;
libv2001 3:05ba7be59773 51
libv2001 3:05ba7be59773 52 for (int i = 0; i < 8; ++i){
libv2001 3:05ba7be59773 53 if (addr64[i] != rhs.addr64[i]){
libv2001 3:05ba7be59773 54 return false;
libv2001 3:05ba7be59773 55 }
libv2001 3:05ba7be59773 56 }
libv2001 3:05ba7be59773 57
libv2001 3:05ba7be59773 58 for (int i = 0; i < 2; ++i){
libv2001 3:05ba7be59773 59 if (addr16[i] != rhs.addr16[i]){
libv2001 3:05ba7be59773 60 return false;
libv2001 3:05ba7be59773 61 }
libv2001 3:05ba7be59773 62 }
libv2001 3:05ba7be59773 63
libv2001 3:05ba7be59773 64 return same;
libv2001 0:671a7b7e4673 65 }
libv2001 0:671a7b7e4673 66 };
libv2001 0:671a7b7e4673 67
libv2001 0:671a7b7e4673 68 #define ROOTER_MAX 2
libv2001 0:671a7b7e4673 69 Rooter rooters[ROOTER_MAX];
libv2001 0:671a7b7e4673 70 int rooterCount = 0;
libv2001 2:ff0b74e5e62c 71
libv2001 2:ff0b74e5e62c 72 Mutex rooterMutex;
libv2001 3:05ba7be59773 73
libv2001 3:05ba7be59773 74 TCPSocketConnection socket;
libv2001 3:05ba7be59773 75
libv2001 0:671a7b7e4673 76 /*******************************************************/
libv2001 0:671a7b7e4673 77 /**********************UTILITIES************************/
libv2001 0:671a7b7e4673 78 /*******************************************************/
libv2001 0:671a7b7e4673 79
libv2001 0:671a7b7e4673 80 PinName GetPinName(const int p){
libv2001 0:671a7b7e4673 81 switch(p){
libv2001 0:671a7b7e4673 82 case 5: return p5;
libv2001 0:671a7b7e4673 83 case 6: return p6;
libv2001 0:671a7b7e4673 84 case 7: return p7;
libv2001 0:671a7b7e4673 85 case 8: return p8;
libv2001 0:671a7b7e4673 86 case 9: return p9;
libv2001 0:671a7b7e4673 87 case 10: return p10;
libv2001 0:671a7b7e4673 88 case 11: return p11;
libv2001 0:671a7b7e4673 89 case 12: return p12;
libv2001 0:671a7b7e4673 90 case 13: return p13;
libv2001 0:671a7b7e4673 91 case 14: return p14;
libv2001 0:671a7b7e4673 92 case 15: return p15;
libv2001 0:671a7b7e4673 93 case 16: return p16;
libv2001 0:671a7b7e4673 94 case 17: return p17;
libv2001 0:671a7b7e4673 95 case 18: return p18;
libv2001 0:671a7b7e4673 96 case 19: return p19;
libv2001 0:671a7b7e4673 97 case 20: return p20;
libv2001 0:671a7b7e4673 98 case 21: return p21;
libv2001 0:671a7b7e4673 99 case 22: return p22;
libv2001 0:671a7b7e4673 100 case 23: return p23;
libv2001 0:671a7b7e4673 101 case 24: return p24;
libv2001 0:671a7b7e4673 102 case 25: return p25;
libv2001 0:671a7b7e4673 103 case 26: return p26;
libv2001 0:671a7b7e4673 104 case 27: return p27;
libv2001 0:671a7b7e4673 105 case 28: return p28;
libv2001 0:671a7b7e4673 106 case 29: return p29;
libv2001 0:671a7b7e4673 107 case 30: return p30;
libv2001 0:671a7b7e4673 108 }
libv2001 0:671a7b7e4673 109 pc.printf("Numero de pin invalid");
libv2001 0:671a7b7e4673 110 return NC;
libv2001 0:671a7b7e4673 111 }
libv2001 0:671a7b7e4673 112
libv2001 0:671a7b7e4673 113 /*******************************************************/
libv2001 0:671a7b7e4673 114 /***********************CONFIG**************************/
libv2001 0:671a7b7e4673 115 /*******************************************************/
libv2001 0:671a7b7e4673 116 int panID;
libv2001 0:671a7b7e4673 117 int xbeeTxPin;
libv2001 0:671a7b7e4673 118 int xbeeRxPin;
libv2001 0:671a7b7e4673 119 int xbeeRstPin;
libv2001 0:671a7b7e4673 120 char server[16];
libv2001 0:671a7b7e4673 121
libv2001 0:671a7b7e4673 122 char key[10];
libv2001 0:671a7b7e4673 123
libv2001 0:671a7b7e4673 124 LocalFileSystem local("local");
libv2001 0:671a7b7e4673 125
libv2001 0:671a7b7e4673 126 void ReadConfig(){
libv2001 0:671a7b7e4673 127 memset(server, 0x00, 16);
libv2001 0:671a7b7e4673 128 FILE * f = fopen("/local/coord.cfg", "r");
libv2001 0:671a7b7e4673 129 fscanf(f,"%s %x", key, &panID);
libv2001 3:05ba7be59773 130 pc.printf("Lecture de la config %s : %04x\r\n", key, panID);
libv2001 0:671a7b7e4673 131 fscanf(f,"%s %d %d %d", key, &xbeeTxPin, &xbeeRxPin, &xbeeRstPin);
libv2001 0:671a7b7e4673 132 pc.printf("Lecture de la config %s : %d %d %d\r\n", key, xbeeTxPin, xbeeRxPin, xbeeRstPin);
libv2001 0:671a7b7e4673 133 fscanf(f,"%s %s", key, server);
libv2001 0:671a7b7e4673 134 pc.printf("Lecture de la config %s : %s\r\n", key, server);
libv2001 0:671a7b7e4673 135 fclose(f);
libv2001 0:671a7b7e4673 136 }
libv2001 0:671a7b7e4673 137
libv2001 0:671a7b7e4673 138 /*******************************************************/
libv2001 0:671a7b7e4673 139 /**********************XBEE SEND************************/
libv2001 0:671a7b7e4673 140 /*******************************************************/
libv2001 0:671a7b7e4673 141
libv2001 0:671a7b7e4673 142 char frameID = 0;
libv2001 0:671a7b7e4673 143
libv2001 0:671a7b7e4673 144 inline char GetFrameID(){
libv2001 0:671a7b7e4673 145 ++frameID;
libv2001 0:671a7b7e4673 146 if (frameID == 0){
libv2001 0:671a7b7e4673 147 frameID = 1;
libv2001 0:671a7b7e4673 148 }
libv2001 0:671a7b7e4673 149 return frameID;
libv2001 0:671a7b7e4673 150 }
libv2001 0:671a7b7e4673 151
libv2001 0:671a7b7e4673 152 void SetCheckSum(char * buffer){
libv2001 0:671a7b7e4673 153 uint16_t length = GetFrameLength(buffer);
libv2001 0:671a7b7e4673 154
libv2001 0:671a7b7e4673 155 char sum = 0;
libv2001 0:671a7b7e4673 156
libv2001 0:671a7b7e4673 157 int max = length + 3;
libv2001 0:671a7b7e4673 158
libv2001 0:671a7b7e4673 159 for(int i = 3; i < max; ++i){
libv2001 0:671a7b7e4673 160 sum += buffer[i];
libv2001 0:671a7b7e4673 161 }
libv2001 0:671a7b7e4673 162
libv2001 0:671a7b7e4673 163 buffer[max] = 0xff - sum;
libv2001 0:671a7b7e4673 164 }
libv2001 0:671a7b7e4673 165
libv2001 0:671a7b7e4673 166 void XBeeSend(char * buffer, int count){
libv2001 0:671a7b7e4673 167 for ( int i = 0; i < count; ++i ){
libv2001 0:671a7b7e4673 168 xbee->putc(buffer[i]);
libv2001 3:05ba7be59773 169 wait_us(25);
libv2001 0:671a7b7e4673 170 }
libv2001 0:671a7b7e4673 171 }
libv2001 0:671a7b7e4673 172
libv2001 0:671a7b7e4673 173 void XBeeSendATCommand(bool queue, char * type, char * data, int dataLength){
libv2001 0:671a7b7e4673 174 char buffer[128];
libv2001 0:671a7b7e4673 175 buffer[START_IDX] = START;
libv2001 0:671a7b7e4673 176 buffer[LENGTH_MSB_IDX] = (dataLength + AT_MIN_SIZE) >> 8;
libv2001 0:671a7b7e4673 177 buffer[LENGTH_LSB_IDX] = (dataLength + AT_MIN_SIZE) & 0xff;
libv2001 0:671a7b7e4673 178 buffer[API_ID_IDX] = queue ? API_ID_AT_CMD_QUEUE : API_ID_AT_CMD;
libv2001 0:671a7b7e4673 179 buffer[FRAME_ID_IDX] = GetFrameID();
libv2001 0:671a7b7e4673 180 memcpy(&buffer[AT_CMD_ID_IDX], type, AT_CMD_ID_SIZE);
libv2001 0:671a7b7e4673 181 memcpy(&buffer[AT_PARAM_IDX], data, dataLength);
libv2001 0:671a7b7e4673 182
libv2001 0:671a7b7e4673 183 SetCheckSum(buffer);
libv2001 0:671a7b7e4673 184
libv2001 0:671a7b7e4673 185 while(true){
libv2001 0:671a7b7e4673 186 XBeeSend(buffer, dataLength + AT_MIN_SIZE + FRAME_MIN_SIZE);
libv2001 0:671a7b7e4673 187
libv2001 0:671a7b7e4673 188 Thread::signal_wait(RESPONSE_SIGNAL);
libv2001 0:671a7b7e4673 189
libv2001 0:671a7b7e4673 190 switch (responseStatus){
libv2001 0:671a7b7e4673 191 case AT_CMD_RSP_STATUS_OK:
libv2001 0:671a7b7e4673 192 return;
libv2001 0:671a7b7e4673 193 case AT_CMD_RSP_STATUS_ERROR:
libv2001 0:671a7b7e4673 194 case AT_CMD_RSP_STATUS_INVALID_CMD:
libv2001 0:671a7b7e4673 195 case AT_CMD_RSP_STATUS_INVALID_PARAM:
libv2001 0:671a7b7e4673 196 case AT_CMD_RSP_STATUS_TX_FAILURE:
libv2001 0:671a7b7e4673 197 default:
libv2001 0:671a7b7e4673 198 pc.printf("This AT error occured : %02x\r\n", responseStatus);
libv2001 0:671a7b7e4673 199 break;
libv2001 0:671a7b7e4673 200 }
libv2001 0:671a7b7e4673 201 }
libv2001 0:671a7b7e4673 202 }
libv2001 0:671a7b7e4673 203
libv2001 0:671a7b7e4673 204 inline void XBeeSendATID(){
libv2001 0:671a7b7e4673 205 char idBuf[8];
libv2001 0:671a7b7e4673 206 for (int i = 0; i < 8; ++i){
libv2001 0:671a7b7e4673 207 idBuf[i] = (panID >> (56 - 8 * i)) & 0xff;
libv2001 0:671a7b7e4673 208 }
libv2001 0:671a7b7e4673 209 XBeeSendATCommand(true, "ID", idBuf, 8);
libv2001 0:671a7b7e4673 210 }
libv2001 0:671a7b7e4673 211
libv2001 0:671a7b7e4673 212 inline void XBeeSendATWR(){
libv2001 0:671a7b7e4673 213 XBeeSendATCommand(true, "WR", nullptr, 0);
libv2001 0:671a7b7e4673 214 }
libv2001 0:671a7b7e4673 215
libv2001 0:671a7b7e4673 216 inline void XBeeSendATAC(){
libv2001 0:671a7b7e4673 217 XBeeSendATCommand(true, "AC", nullptr, 0);
libv2001 0:671a7b7e4673 218 }
libv2001 0:671a7b7e4673 219
libv2001 3:05ba7be59773 220 void XbeeSendRemoteAtCommand(char * addr64, char * addr16, char opt, char * type, char * data, int dataLength){
libv2001 2:ff0b74e5e62c 221 char buffer[128];
libv2001 2:ff0b74e5e62c 222 buffer[START_IDX] = START;
libv2001 2:ff0b74e5e62c 223 buffer[LENGTH_MSB_IDX] = (dataLength + REMOTE_AT_RQST_MIN_SIZE) >> 8;
libv2001 2:ff0b74e5e62c 224 buffer[LENGTH_LSB_IDX] = (dataLength + REMOTE_AT_RQST_MIN_SIZE) & 0xff;
libv2001 2:ff0b74e5e62c 225 buffer[API_ID_IDX] = API_ID_REMOTE_AT_RQST;
libv2001 2:ff0b74e5e62c 226 buffer[FRAME_ID_IDX] = GetFrameID();
libv2001 3:05ba7be59773 227 memcpy(&buffer[REMOTE_AT_RQST_64BIT_MSB_IDX], addr64, ADDR_64BIT_SIZE);
libv2001 3:05ba7be59773 228 memcpy(&buffer[REMOTE_AT_RQST_16BIT_MSB_IDX], addr16, ADDR_16BIT_SIZE);
libv2001 2:ff0b74e5e62c 229 buffer[REMOTE_AT_RQST_OPT_IDX] = opt;
libv2001 2:ff0b74e5e62c 230 memcpy(&buffer[REMOTE_AT_RQST_AT_CMD1_IDX], type, AT_CMD_ID_SIZE);
libv2001 2:ff0b74e5e62c 231 memcpy(&buffer[REMOTE_AT_RQST_AT_PARAM_IDX], data, dataLength);
libv2001 2:ff0b74e5e62c 232
libv2001 2:ff0b74e5e62c 233 SetCheckSum(buffer);
libv2001 2:ff0b74e5e62c 234
libv2001 2:ff0b74e5e62c 235 while(true){
libv2001 2:ff0b74e5e62c 236 XBeeSend(buffer, dataLength + REMOTE_AT_RQST_MIN_SIZE + FRAME_MIN_SIZE);
libv2001 2:ff0b74e5e62c 237 Thread::signal_wait(RESPONSE_SIGNAL);
libv2001 2:ff0b74e5e62c 238 switch (responseStatus){
libv2001 2:ff0b74e5e62c 239 case REMOTE_AT_CMD_RSP_STATUS_OK:
libv2001 2:ff0b74e5e62c 240 return;
libv2001 2:ff0b74e5e62c 241 default:
libv2001 2:ff0b74e5e62c 242 pc.printf("This AT error occured : %02x\r\n", responseStatus);
libv2001 2:ff0b74e5e62c 243 break;
libv2001 2:ff0b74e5e62c 244 }
libv2001 2:ff0b74e5e62c 245 }
libv2001 2:ff0b74e5e62c 246 }
libv2001 2:ff0b74e5e62c 247
libv2001 3:05ba7be59773 248 inline void SendRemoteD0Command(char* addr64, char* addr16, bool on){
libv2001 3:05ba7be59773 249 char data[1] = {on ? 0x05 : 0x04};
libv2001 3:05ba7be59773 250 XbeeSendRemoteAtCommand(addr64, addr16, 0x02, "D0", data, 1);
libv2001 3:05ba7be59773 251 }
libv2001 3:05ba7be59773 252
libv2001 0:671a7b7e4673 253 /*******************************************************/
libv2001 0:671a7b7e4673 254 /**********************XBEE READ************************/
libv2001 0:671a7b7e4673 255 /*******************************************************/
libv2001 0:671a7b7e4673 256
libv2001 2:ff0b74e5e62c 257 void HandleAtRemoteCommandResponse(char * cmd){
libv2001 2:ff0b74e5e62c 258 responseStatus = cmd[REMOTE_CMD_RSP_STATUS_IDX];
libv2001 2:ff0b74e5e62c 259 XBeeProducer->signal_set(RESPONSE_SIGNAL);
libv2001 2:ff0b74e5e62c 260 }
libv2001 2:ff0b74e5e62c 261
libv2001 0:671a7b7e4673 262 void HandleBtnPacket(char* cmd){
libv2001 0:671a7b7e4673 263 ButtonEvent* evt = btnPool.alloc();
libv2001 0:671a7b7e4673 264 memcpy(evt->id, BTN_ID, 3);
libv2001 0:671a7b7e4673 265 evt->state = cmd[RECEIVED_PACKET_DATA_IDX + 3] == 0x01;
libv2001 0:671a7b7e4673 266 event.put((void*)evt);
libv2001 0:671a7b7e4673 267 }
libv2001 0:671a7b7e4673 268
libv2001 0:671a7b7e4673 269 void HandleAccPacket(char* cmd){
libv2001 0:671a7b7e4673 270 AccelerometerEvent* evt = accPool.alloc();
libv2001 0:671a7b7e4673 271 memcpy(evt->id, ACC_ID, 3);
libv2001 0:671a7b7e4673 272 memcpy(evt->x, &cmd[RECEIVED_PACKET_DATA_IDX + 3], 2);
libv2001 0:671a7b7e4673 273 memcpy(evt->y, &cmd[RECEIVED_PACKET_DATA_IDX + 5], 2);
libv2001 0:671a7b7e4673 274 memcpy(evt->z, &cmd[RECEIVED_PACKET_DATA_IDX + 7], 2);
libv2001 0:671a7b7e4673 275 event.put((void*)evt);
libv2001 0:671a7b7e4673 276 }
libv2001 0:671a7b7e4673 277
libv2001 0:671a7b7e4673 278 void HandleXbeeReceivedPacket(char * cmd){
libv2001 2:ff0b74e5e62c 279 if (rooterCount < ROOTER_MAX){
libv2001 2:ff0b74e5e62c 280 Rooter r;
libv2001 3:05ba7be59773 281 memcpy(r.addr64, &cmd[RECEIVED_PACKET_64BIT_MSB_IDX], 8);
libv2001 3:05ba7be59773 282 memcpy(r.addr16, &cmd[RECEIVED_PACKET_16BIT_MSB_IDX], 2);
libv2001 2:ff0b74e5e62c 283
libv2001 2:ff0b74e5e62c 284 bool found = false;
libv2001 2:ff0b74e5e62c 285 for (int i = 0; i < rooterCount; ++i){
libv2001 2:ff0b74e5e62c 286 if (rooters[i] == r){
libv2001 2:ff0b74e5e62c 287 found = true;
libv2001 2:ff0b74e5e62c 288 break;
libv2001 2:ff0b74e5e62c 289 }
libv2001 2:ff0b74e5e62c 290 }
libv2001 2:ff0b74e5e62c 291 if (!found){
libv2001 2:ff0b74e5e62c 292 rooterMutex.lock();
libv2001 2:ff0b74e5e62c 293 rooters[rooterCount] = r;
libv2001 2:ff0b74e5e62c 294 ++rooterCount;
libv2001 2:ff0b74e5e62c 295 rooterMutex.unlock();
libv2001 2:ff0b74e5e62c 296 }
libv2001 2:ff0b74e5e62c 297 }
libv2001 2:ff0b74e5e62c 298
libv2001 0:671a7b7e4673 299 if (cmd[RECEIVED_PACKET_DATA_IDX] == BTN_ID[0] &&
libv2001 0:671a7b7e4673 300 cmd[RECEIVED_PACKET_DATA_IDX + 1] == BTN_ID[1] &&
libv2001 0:671a7b7e4673 301 cmd[RECEIVED_PACKET_DATA_IDX + 2] == BTN_ID[2]){
libv2001 0:671a7b7e4673 302 HandleBtnPacket(cmd);
libv2001 0:671a7b7e4673 303 }
libv2001 0:671a7b7e4673 304
libv2001 0:671a7b7e4673 305 if (cmd[RECEIVED_PACKET_DATA_IDX] == ACC_ID[0] &&
libv2001 0:671a7b7e4673 306 cmd[RECEIVED_PACKET_DATA_IDX + 1] == ACC_ID[1] &&
libv2001 0:671a7b7e4673 307 cmd[RECEIVED_PACKET_DATA_IDX + 2] == ACC_ID[2]){
libv2001 0:671a7b7e4673 308 HandleAccPacket(cmd);
libv2001 0:671a7b7e4673 309 }
libv2001 0:671a7b7e4673 310 }
libv2001 0:671a7b7e4673 311
libv2001 0:671a7b7e4673 312 void HandleXbeeModemStatus(char * cmd){
libv2001 0:671a7b7e4673 313 switch(cmd[MODEM_STATUS_STATUS_IDX]){
libv2001 0:671a7b7e4673 314 case MODEM_STATUS_HARDWARE_RST:
libv2001 0:671a7b7e4673 315 XBeeProducer->signal_set(HARDWARE_RESET_SIGNAL);
libv2001 0:671a7b7e4673 316 break;
libv2001 0:671a7b7e4673 317 case MODEM_STATUS_COORDINATOR_STARTED:
libv2001 0:671a7b7e4673 318 XBeeProducer->signal_set(COORDINATOR_STARTED_SIGNAL);
libv2001 0:671a7b7e4673 319 break;
libv2001 0:671a7b7e4673 320 default:
libv2001 0:671a7b7e4673 321 pc.printf("Unhandled modem status received : %02x\r\n", cmd[MODEM_STATUS_STATUS_IDX]);
libv2001 0:671a7b7e4673 322 break;
libv2001 0:671a7b7e4673 323 }
libv2001 0:671a7b7e4673 324 }
libv2001 0:671a7b7e4673 325
libv2001 0:671a7b7e4673 326 void HandleXBeeATCommandResponse(char * cmd){
libv2001 0:671a7b7e4673 327 responseStatus = cmd[AT_CMD_RSP_STATUS_IDX];
libv2001 0:671a7b7e4673 328 XBeeProducer->signal_set(RESPONSE_SIGNAL);
libv2001 0:671a7b7e4673 329 }
libv2001 0:671a7b7e4673 330
libv2001 0:671a7b7e4673 331 void HandleXbeeReceivedCommand(char * cmd){
libv2001 0:671a7b7e4673 332 switch(cmd[API_ID_IDX]){
libv2001 0:671a7b7e4673 333 case API_ID_AT_CMD_RSP:
libv2001 0:671a7b7e4673 334 HandleXBeeATCommandResponse(cmd);
libv2001 0:671a7b7e4673 335 break;
libv2001 0:671a7b7e4673 336 case API_ID_MODEM_STATUS:
libv2001 0:671a7b7e4673 337 HandleXbeeModemStatus(cmd);
libv2001 0:671a7b7e4673 338 break;
libv2001 2:ff0b74e5e62c 339 case API_ID_RECEIVED_PACKET:
libv2001 2:ff0b74e5e62c 340 HandleXbeeReceivedPacket(cmd);
libv2001 2:ff0b74e5e62c 341 break;
libv2001 2:ff0b74e5e62c 342 case API_ID_REMOTE_CMD_RSP:
libv2001 2:ff0b74e5e62c 343 HandleAtRemoteCommandResponse(cmd);
libv2001 2:ff0b74e5e62c 344 break;
libv2001 0:671a7b7e4673 345 default:
libv2001 0:671a7b7e4673 346 pc.printf("Unhandle XBee Command received : %02x\r\n", cmd[API_ID_IDX]);
libv2001 3:05ba7be59773 347 break;
libv2001 0:671a7b7e4673 348 }
libv2001 0:671a7b7e4673 349 }
libv2001 0:671a7b7e4673 350
libv2001 0:671a7b7e4673 351 /*******************************************************/
libv2001 3:05ba7be59773 352 /************************EVENT**************************/
libv2001 3:05ba7be59773 353 /*******************************************************/
libv2001 3:05ba7be59773 354
libv2001 3:05ba7be59773 355 void HandleBtnEvent(ButtonEvent* data){
libv2001 3:05ba7be59773 356 char out[24];
libv2001 3:05ba7be59773 357 out[23] = 0x00;
libv2001 3:05ba7be59773 358 sprintf(out, "Event BTN: %s", data->state ? "Pressed" : "Released");
libv2001 3:05ba7be59773 359
libv2001 3:05ba7be59773 360 pc.printf("Sending to Server : %s\r\n", out);
libv2001 3:05ba7be59773 361 socket.send_all(out, data->state ? 20 : 21);
libv2001 3:05ba7be59773 362 }
libv2001 3:05ba7be59773 363
libv2001 3:05ba7be59773 364 #define NEGATIVE_PADDING 0xfffff000;
libv2001 3:05ba7be59773 365 int AccDataToInt(char* data)
libv2001 3:05ba7be59773 366 {
libv2001 3:05ba7be59773 367 int x = ((int)data[0])<<4;
libv2001 3:05ba7be59773 368 x |= data[1]>>4;
libv2001 3:05ba7be59773 369 if ((data[0] & 0x80) != 0) {
libv2001 3:05ba7be59773 370 x |= NEGATIVE_PADDING;
libv2001 3:05ba7be59773 371 }
libv2001 3:05ba7be59773 372 return x;
libv2001 3:05ba7be59773 373 }
libv2001 3:05ba7be59773 374
libv2001 3:05ba7be59773 375 void HandleAccEvent(AccelerometerEvent* data){
libv2001 3:05ba7be59773 376 char out[40];
libv2001 3:05ba7be59773 377 out[39] = 0;
libv2001 3:05ba7be59773 378 int x = AccDataToInt(data->x);
libv2001 3:05ba7be59773 379 int y = AccDataToInt(data->y);
libv2001 3:05ba7be59773 380 int z = AccDataToInt(data->z);
libv2001 3:05ba7be59773 381
libv2001 3:05ba7be59773 382 float x_g = (float)x/1024.0f;
libv2001 3:05ba7be59773 383 float y_g = (float)y/1024.0f;
libv2001 3:05ba7be59773 384 float z_g = (float)z/1024.0f;
libv2001 3:05ba7be59773 385 sprintf(out, "Event ACC: x=%01.02fg, y=%01.02fg, z=%01.02fg", x_g, y_g, z_g);
libv2001 3:05ba7be59773 386
libv2001 3:05ba7be59773 387 pc.printf("Sending to Server : %s\r\n", out);
libv2001 3:05ba7be59773 388 socket.send_all(out, 40);
libv2001 3:05ba7be59773 389 }
libv2001 3:05ba7be59773 390
libv2001 3:05ba7be59773 391 /*******************************************************/
libv2001 0:671a7b7e4673 392 /************************INIT***************************/
libv2001 0:671a7b7e4673 393 /*******************************************************/
libv2001 0:671a7b7e4673 394
libv2001 0:671a7b7e4673 395 bool InitXBee(){
libv2001 0:671a7b7e4673 396 xbeeRst->write(0);
libv2001 0:671a7b7e4673 397 wait(0.4);
libv2001 0:671a7b7e4673 398 xbeeRst->write(1);
libv2001 0:671a7b7e4673 399
libv2001 0:671a7b7e4673 400 Thread::signal_wait(HARDWARE_RESET_SIGNAL);
libv2001 0:671a7b7e4673 401
libv2001 0:671a7b7e4673 402 XBeeSendATID();
libv2001 0:671a7b7e4673 403 XBeeSendATWR();
libv2001 0:671a7b7e4673 404 XBeeSendATAC();
libv2001 0:671a7b7e4673 405
libv2001 0:671a7b7e4673 406 Thread::signal_wait(COORDINATOR_STARTED_SIGNAL);
libv2001 0:671a7b7e4673 407
libv2001 0:671a7b7e4673 408 pc.printf("XBee configured\r\n");
libv2001 0:671a7b7e4673 409
libv2001 0:671a7b7e4673 410 return true;
libv2001 0:671a7b7e4673 411 }
libv2001 0:671a7b7e4673 412
libv2001 3:05ba7be59773 413 bool InitEthernet(){
libv2001 3:05ba7be59773 414 EthernetInterface eth;
libv2001 3:05ba7be59773 415 // No DHCP
libv2001 3:05ba7be59773 416 eth.init("192.168.2.3", "255.255.255.0", server);
libv2001 3:05ba7be59773 417 // DHCP
libv2001 3:05ba7be59773 418 //eth.init();
libv2001 3:05ba7be59773 419 eth.connect();
libv2001 3:05ba7be59773 420 printf("\nClient IP Address is %s\r\n", eth.getIPAddress());
libv2001 3:05ba7be59773 421
libv2001 3:05ba7be59773 422 // Connect to Server
libv2001 3:05ba7be59773 423 while (socket.connect(server, 7) < 0) {
libv2001 3:05ba7be59773 424 printf("Unable to connect to (%s) on port (%d)\r\n", server, 7);
libv2001 3:05ba7be59773 425 wait(1);
libv2001 3:05ba7be59773 426 }
libv2001 3:05ba7be59773 427
libv2001 3:05ba7be59773 428 printf("Connected to Server at %s\r\n", server);
libv2001 3:05ba7be59773 429
libv2001 3:05ba7be59773 430 return true;
libv2001 3:05ba7be59773 431 }
libv2001 3:05ba7be59773 432
libv2001 3:05ba7be59773 433
libv2001 3:05ba7be59773 434 /*******************************************************/
libv2001 3:05ba7be59773 435 /************************MAIN***************************/
libv2001 3:05ba7be59773 436 /*******************************************************/
libv2001 3:05ba7be59773 437
libv2001 3:05ba7be59773 438 inline char XbeeReadChar(){
libv2001 3:05ba7be59773 439 while(!xbee->readable()){
libv2001 3:05ba7be59773 440 }
libv2001 3:05ba7be59773 441 return xbee->getc();
libv2001 3:05ba7be59773 442 }
libv2001 3:05ba7be59773 443
libv2001 0:671a7b7e4673 444 void ConsumerMain(){
libv2001 0:671a7b7e4673 445 char buffer[128];
libv2001 0:671a7b7e4673 446 while(true){
libv2001 3:05ba7be59773 447 buffer[START_IDX] = XbeeReadChar();
libv2001 0:671a7b7e4673 448 if (buffer[START_IDX] != START){
libv2001 0:671a7b7e4673 449 pc.printf("Wrong start byte received : %02x\r\n", buffer[START_IDX]);
libv2001 0:671a7b7e4673 450 continue;
libv2001 0:671a7b7e4673 451 }
libv2001 3:05ba7be59773 452 buffer[LENGTH_MSB_IDX] = XbeeReadChar();
libv2001 3:05ba7be59773 453 buffer[LENGTH_LSB_IDX] = XbeeReadChar();
libv2001 0:671a7b7e4673 454 int length = GetFrameLength(buffer);
libv2001 0:671a7b7e4673 455
libv2001 0:671a7b7e4673 456 for (int i = 0; i <= length; ++i){
libv2001 3:05ba7be59773 457 buffer[i + API_ID_IDX] = XbeeReadChar();
libv2001 0:671a7b7e4673 458 }
libv2001 0:671a7b7e4673 459 if (!ValidateCheckSum(buffer)){
libv2001 3:05ba7be59773 460 pc.printf("Bad CheckSum\r\n");
libv2001 3:05ba7be59773 461
libv2001 3:05ba7be59773 462 for (int i = 0; i < length + FRAME_MIN_SIZE; ++i){
libv2001 3:05ba7be59773 463 pc.printf("%02x ", buffer[i]);
libv2001 3:05ba7be59773 464 }
libv2001 3:05ba7be59773 465 pc.printf("\r\n");
libv2001 0:671a7b7e4673 466 continue;
libv2001 0:671a7b7e4673 467 }
libv2001 0:671a7b7e4673 468
libv2001 0:671a7b7e4673 469 HandleXbeeReceivedCommand(buffer);
libv2001 0:671a7b7e4673 470 }
libv2001 0:671a7b7e4673 471 }
libv2001 0:671a7b7e4673 472
libv2001 3:05ba7be59773 473 void ToggleRemoteRooters(bool on){
libv2001 2:ff0b74e5e62c 474 rooterMutex.lock();
libv2001 2:ff0b74e5e62c 475 for(int i = 0; i < rooterCount; ++i){
libv2001 3:05ba7be59773 476 SendRemoteD0Command(rooters[i].addr64, rooters[i].addr16, on);
libv2001 2:ff0b74e5e62c 477 }
libv2001 2:ff0b74e5e62c 478 rooterMutex.unlock();
libv2001 2:ff0b74e5e62c 479 }
libv2001 2:ff0b74e5e62c 480
libv2001 0:671a7b7e4673 481 bool ProducerInit(){
libv2001 3:05ba7be59773 482 if (!InitEthernet()){
libv2001 3:05ba7be59773 483 pc.printf("Connection problem with the Ethernet\r\n");
libv2001 3:05ba7be59773 484 return false;
libv2001 3:05ba7be59773 485 }
libv2001 3:05ba7be59773 486
libv2001 0:671a7b7e4673 487 if (!InitXBee()){
libv2001 0:671a7b7e4673 488 pc.printf("Connection problem with the XBee\r\n");
libv2001 0:671a7b7e4673 489 return false;
libv2001 0:671a7b7e4673 490 }
libv2001 0:671a7b7e4673 491
libv2001 0:671a7b7e4673 492 return true;
libv2001 0:671a7b7e4673 493 }
libv2001 0:671a7b7e4673 494
libv2001 0:671a7b7e4673 495 void Tick(){
libv2001 0:671a7b7e4673 496 XBeeProducer->signal_set(TICKER_SIGNAL);
libv2001 0:671a7b7e4673 497 }
libv2001 0:671a7b7e4673 498
libv2001 3:05ba7be59773 499 bool initDone = false;
libv2001 3:05ba7be59773 500
libv2001 0:671a7b7e4673 501 void ProducerMain(const void*){
libv2001 0:671a7b7e4673 502 if (!ProducerInit()){
libv2001 0:671a7b7e4673 503 pc.printf("Initialization problem\r\n");
libv2001 0:671a7b7e4673 504 return;
libv2001 3:05ba7be59773 505 } else {
libv2001 3:05ba7be59773 506 initDone = true;
libv2001 0:671a7b7e4673 507 }
libv2001 0:671a7b7e4673 508
libv2001 0:671a7b7e4673 509 timer.attach(&Tick, 1);
libv2001 3:05ba7be59773 510 bool on = true;
libv2001 0:671a7b7e4673 511 while(true){
libv2001 0:671a7b7e4673 512 Thread::signal_wait(TICKER_SIGNAL);
libv2001 3:05ba7be59773 513 ToggleRemoteRooters(on);
libv2001 3:05ba7be59773 514 on = !on;
libv2001 3:05ba7be59773 515 }
libv2001 3:05ba7be59773 516 }
libv2001 3:05ba7be59773 517
libv2001 3:05ba7be59773 518 void EventConsumerMain(const void*){
libv2001 3:05ba7be59773 519 while(true){
libv2001 3:05ba7be59773 520 void* ptr = event.get().value.p;
libv2001 3:05ba7be59773 521
libv2001 3:05ba7be59773 522 char * id = (char*) ptr;
libv2001 3:05ba7be59773 523 // Compare l'ID pour trouver le type d'évènement
libv2001 3:05ba7be59773 524 if (id[0] == BTN_ID[0] && id[1] == BTN_ID[1] && id[2] == BTN_ID[2]){
libv2001 3:05ba7be59773 525 ButtonEvent* BtnEvt = (ButtonEvent*)ptr;
libv2001 3:05ba7be59773 526 HandleBtnEvent(BtnEvt);
libv2001 3:05ba7be59773 527 btnPool.free(BtnEvt);
libv2001 3:05ba7be59773 528 } else if (id[0] == ACC_ID[0] && id[1] == ACC_ID[1] && id[2] == ACC_ID[2]){
libv2001 3:05ba7be59773 529 AccelerometerEvent* AccEvt = (AccelerometerEvent*)ptr;
libv2001 3:05ba7be59773 530 HandleAccEvent(AccEvt);
libv2001 3:05ba7be59773 531 accPool.free(AccEvt);
libv2001 3:05ba7be59773 532 } else {
libv2001 3:05ba7be59773 533 pc.printf("Unknown event : %c%c%c\r\n", id[0], id[1], id[2]);
libv2001 3:05ba7be59773 534 }
libv2001 3:05ba7be59773 535 }
libv2001 3:05ba7be59773 536 }
libv2001 3:05ba7be59773 537
libv2001 3:05ba7be59773 538 void EthernetConsumerMain(const void *){
libv2001 3:05ba7be59773 539 // Attend que l'initialisation soit terminée avant de commencer
libv2001 3:05ba7be59773 540 while (!initDone){
libv2001 3:05ba7be59773 541 Thread::yield();
libv2001 3:05ba7be59773 542 }
libv2001 3:05ba7be59773 543 // Receive message from server
libv2001 3:05ba7be59773 544 char buf[256];
libv2001 3:05ba7be59773 545 while(true){
libv2001 3:05ba7be59773 546 int n = socket.receive(buf, 256);
libv2001 3:05ba7be59773 547 buf[n] = 0x00;
libv2001 3:05ba7be59773 548 printf("Received from Server : %s\r\n", buf);
libv2001 0:671a7b7e4673 549 }
libv2001 0:671a7b7e4673 550 }
libv2001 0:671a7b7e4673 551
libv2001 0:671a7b7e4673 552 int main() {
libv2001 0:671a7b7e4673 553 // Lecture de la configuration.
libv2001 0:671a7b7e4673 554 ReadConfig();
libv2001 0:671a7b7e4673 555
libv2001 0:671a7b7e4673 556 //Créer les interfaces de communication des capteurs avec les données de la config.
libv2001 0:671a7b7e4673 557 Serial mainXbee(GetPinName(xbeeTxPin), GetPinName(xbeeRxPin));
libv2001 0:671a7b7e4673 558 DigitalOut mainXbeeRst(GetPinName(xbeeRstPin));
libv2001 0:671a7b7e4673 559
libv2001 0:671a7b7e4673 560 //Rendre les interfaces de communication globaux.
libv2001 0:671a7b7e4673 561 xbee = &mainXbee;
libv2001 0:671a7b7e4673 562 xbeeRst = &mainXbeeRst;
libv2001 0:671a7b7e4673 563
libv2001 3:05ba7be59773 564 Thread ethernet(EthernetConsumerMain);
libv2001 3:05ba7be59773 565 EthernetConsumer = &ethernet;
libv2001 3:05ba7be59773 566
libv2001 3:05ba7be59773 567 Thread event(EventConsumerMain);
libv2001 3:05ba7be59773 568 EventConsumer = &event;
libv2001 3:05ba7be59773 569
libv2001 0:671a7b7e4673 570 Thread consumer(ConsumerMain);
libv2001 0:671a7b7e4673 571 XBeeConsumer = &consumer;
libv2001 0:671a7b7e4673 572
libv2001 0:671a7b7e4673 573 Thread producer(ProducerMain);
libv2001 0:671a7b7e4673 574 XBeeProducer = &producer;
libv2001 0:671a7b7e4673 575
libv2001 0:671a7b7e4673 576 // Mettre la thread principale dans un état de waiting à l'infinie
libv2001 0:671a7b7e4673 577 Thread::signal_wait(0x1);
libv2001 0:671a7b7e4673 578 }