Class containing functions usefull to communication between PC and Xbee device

Dependents:   Coordinator_node Router_node

Committer:
ShaolinPoutine
Date:
Tue Feb 14 02:47:56 2017 +0000
Revision:
4:e8f4d41c0fbc
Parent:
3:4c1dec78117b
Child:
5:9b4d93bd6725
Child:
6:3b97770f30e6
Added switch case when no data sent.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ShaolinPoutine 2:0000443a78fe 1 #include "xbee.h"
ShaolinPoutine 2:0000443a78fe 2
ShaolinPoutine 2:0000443a78fe 3 void XBee::pcPrint(char* c)
ShaolinPoutine 2:0000443a78fe 4 {
ShaolinPoutine 2:0000443a78fe 5 int i = 0;
ShaolinPoutine 2:0000443a78fe 6 while( (c)[i] != '\0')
ShaolinPoutine 2:0000443a78fe 7 {
ShaolinPoutine 2:0000443a78fe 8 mail->put(&(c[i]));
ShaolinPoutine 2:0000443a78fe 9 i++;
ShaolinPoutine 2:0000443a78fe 10 }
ShaolinPoutine 2:0000443a78fe 11 }
ShaolinPoutine 2:0000443a78fe 12
ShaolinPoutine 2:0000443a78fe 13 void XBee::printHexa(char c)
ShaolinPoutine 2:0000443a78fe 14 {
ShaolinPoutine 2:0000443a78fe 15 char *msb = mail->alloc();
ShaolinPoutine 2:0000443a78fe 16 *msb = c >> 4;
ShaolinPoutine 2:0000443a78fe 17 char *lsb = mail->alloc();
ShaolinPoutine 2:0000443a78fe 18 *lsb = c & 0xF;
ShaolinPoutine 2:0000443a78fe 19
ShaolinPoutine 2:0000443a78fe 20 if (*msb < 10)
ShaolinPoutine 2:0000443a78fe 21 *msb += 0x30;
ShaolinPoutine 2:0000443a78fe 22 else
ShaolinPoutine 2:0000443a78fe 23 *msb += 0x37;
ShaolinPoutine 2:0000443a78fe 24
ShaolinPoutine 2:0000443a78fe 25 if (*lsb < 10)
ShaolinPoutine 2:0000443a78fe 26 *lsb += 0x30;
ShaolinPoutine 2:0000443a78fe 27 else
ShaolinPoutine 2:0000443a78fe 28 *lsb += 0x37;
ShaolinPoutine 2:0000443a78fe 29
ShaolinPoutine 2:0000443a78fe 30 char * str = "0x";
ShaolinPoutine 2:0000443a78fe 31 pcPrint(str);
ShaolinPoutine 2:0000443a78fe 32 mail->put(msb);
ShaolinPoutine 2:0000443a78fe 33 mail->put(lsb);
ShaolinPoutine 2:0000443a78fe 34 str = " ";
ShaolinPoutine 2:0000443a78fe 35 pcPrint(str);
ShaolinPoutine 2:0000443a78fe 36 }
ShaolinPoutine 2:0000443a78fe 37
ShaolinPoutine 2:0000443a78fe 38 XBee::XBee(PinName reset, PinName transfer, PinName receive, Mail<char, 250>* m) :
ShaolinPoutine 2:0000443a78fe 39 rst(reset), comm(transfer, receive)
ShaolinPoutine 2:0000443a78fe 40 {
ShaolinPoutine 2:0000443a78fe 41 // Constructor
ShaolinPoutine 2:0000443a78fe 42 mail = m;
ShaolinPoutine 2:0000443a78fe 43 rst = 0;
ShaolinPoutine 2:0000443a78fe 44 wait(0.4);
ShaolinPoutine 2:0000443a78fe 45 rst = 1;
ShaolinPoutine 2:0000443a78fe 46 wait(3); // waiting for initiation
ShaolinPoutine 2:0000443a78fe 47 }
ShaolinPoutine 2:0000443a78fe 48
ShaolinPoutine 2:0000443a78fe 49 void XBee::SendATCommand(char firstChar, char secondChar, char *optionalParam, int paramLen)
ShaolinPoutine 2:0000443a78fe 50 {
ShaolinPoutine 2:0000443a78fe 51 // Frame Type 0x08
ShaolinPoutine 2:0000443a78fe 52 // Two char as parameters
ShaolinPoutine 2:0000443a78fe 53
ShaolinPoutine 2:0000443a78fe 54 char cmdtosend[10];
ShaolinPoutine 2:0000443a78fe 55 char sum = 0;
ShaolinPoutine 2:0000443a78fe 56 int cmdlength = 8;
ShaolinPoutine 2:0000443a78fe 57 int i = 0;
ShaolinPoutine 2:0000443a78fe 58
ShaolinPoutine 2:0000443a78fe 59 cmdtosend[0] = FRAMEDELIMITER;
ShaolinPoutine 2:0000443a78fe 60 cmdtosend[1] = 0x00;
ShaolinPoutine 2:0000443a78fe 61 cmdtosend[2] = 0x04 + paramLen;
ShaolinPoutine 2:0000443a78fe 62 cmdtosend[3] = 0x08;
ShaolinPoutine 2:0000443a78fe 63 cmdtosend[4] = 0x52;
ShaolinPoutine 2:0000443a78fe 64 cmdtosend[5] = firstChar;
ShaolinPoutine 2:0000443a78fe 65 cmdtosend[6] = secondChar;
ShaolinPoutine 2:0000443a78fe 66
ShaolinPoutine 2:0000443a78fe 67 // Ajouter les parametres au message
ShaolinPoutine 2:0000443a78fe 68 if(optionalParam != NULL)
ShaolinPoutine 2:0000443a78fe 69 {
ShaolinPoutine 2:0000443a78fe 70 i = 0;
ShaolinPoutine 2:0000443a78fe 71 cmdlength += paramLen;
ShaolinPoutine 2:0000443a78fe 72
ShaolinPoutine 2:0000443a78fe 73 while (i < paramLen)
ShaolinPoutine 2:0000443a78fe 74 {
ShaolinPoutine 2:0000443a78fe 75 mail->put(&(optionalParam)[i]);
ShaolinPoutine 2:0000443a78fe 76 cmdtosend[7 + i] = (optionalParam)[i];
ShaolinPoutine 2:0000443a78fe 77 i++;
ShaolinPoutine 2:0000443a78fe 78 }
ShaolinPoutine 2:0000443a78fe 79 pcPrint("\r\n\0");
ShaolinPoutine 2:0000443a78fe 80 }
ShaolinPoutine 2:0000443a78fe 81
ShaolinPoutine 2:0000443a78fe 82 // Calculate checksum
ShaolinPoutine 2:0000443a78fe 83 i = 3;
ShaolinPoutine 2:0000443a78fe 84 while (i < (cmdlength - 1))
ShaolinPoutine 2:0000443a78fe 85 {
ShaolinPoutine 2:0000443a78fe 86 sum += cmdtosend[i];
ShaolinPoutine 2:0000443a78fe 87 i++;
ShaolinPoutine 2:0000443a78fe 88 }
ShaolinPoutine 2:0000443a78fe 89 cmdtosend[cmdlength - 1] = 0xFF - sum;
ShaolinPoutine 2:0000443a78fe 90
ShaolinPoutine 2:0000443a78fe 91 // Envoyer la commande sur UART
ShaolinPoutine 2:0000443a78fe 92 i = 0;
ShaolinPoutine 2:0000443a78fe 93 while (i < cmdlength)
ShaolinPoutine 2:0000443a78fe 94 {
ShaolinPoutine 2:0000443a78fe 95 comm.putc(cmdtosend[i]);
ShaolinPoutine 2:0000443a78fe 96 i++;
ShaolinPoutine 2:0000443a78fe 97 }
ShaolinPoutine 2:0000443a78fe 98
ShaolinPoutine 2:0000443a78fe 99 wait(0.1);
ShaolinPoutine 2:0000443a78fe 100 }
ShaolinPoutine 2:0000443a78fe 101
ShaolinPoutine 2:0000443a78fe 102 char* XBee::InterpretMessage()
ShaolinPoutine 2:0000443a78fe 103 {
ShaolinPoutine 2:0000443a78fe 104 char *response = "\0";
ShaolinPoutine 2:0000443a78fe 105
ShaolinPoutine 2:0000443a78fe 106 if (comm.readable())
ShaolinPoutine 2:0000443a78fe 107 {
ShaolinPoutine 2:0000443a78fe 108
ShaolinPoutine 2:0000443a78fe 109 char start = comm.getc(); // = FRAMEDELIMITER
ShaolinPoutine 2:0000443a78fe 110 //assert
ShaolinPoutine 2:0000443a78fe 111 char len_msb = comm.getc();
ShaolinPoutine 2:0000443a78fe 112 char len_lsb = comm.getc();
ShaolinPoutine 2:0000443a78fe 113
ShaolinPoutine 2:0000443a78fe 114 int len = ((int) len_msb << 4) + (int) len_lsb;
ShaolinPoutine 2:0000443a78fe 115 char frame_data[len];
ShaolinPoutine 2:0000443a78fe 116
ShaolinPoutine 2:0000443a78fe 117 // Resolving frame type
ShaolinPoutine 2:0000443a78fe 118 char type = comm.getc();
ShaolinPoutine 2:0000443a78fe 119 len--;
ShaolinPoutine 2:0000443a78fe 120
ShaolinPoutine 2:0000443a78fe 121 switch (type){
ShaolinPoutine 2:0000443a78fe 122 case 0x88: ATCommandResponse(len);
ShaolinPoutine 2:0000443a78fe 123 break;
ShaolinPoutine 2:0000443a78fe 124 case 0x8A: ModemStatus(len);
ShaolinPoutine 2:0000443a78fe 125 break;
ShaolinPoutine 3:4c1dec78117b 126 case 0x8B: ZigBeeTransmitStatus(len);
ShaolinPoutine 3:4c1dec78117b 127 break;
ShaolinPoutine 3:4c1dec78117b 128 case 0x90: ZigBeeReceivePacket(len);
ShaolinPoutine 3:4c1dec78117b 129 break;
ShaolinPoutine 2:0000443a78fe 130 default: pcPrint("Please implement response of type\0");
ShaolinPoutine 2:0000443a78fe 131 //printsHexa(type);
ShaolinPoutine 2:0000443a78fe 132 pcPrint("\r\n\0");
ShaolinPoutine 2:0000443a78fe 133 for (int i = -1; i <len; i++) comm.getc();
ShaolinPoutine 2:0000443a78fe 134 }
ShaolinPoutine 2:0000443a78fe 135 }
ShaolinPoutine 2:0000443a78fe 136 return response;
ShaolinPoutine 2:0000443a78fe 137 }
ShaolinPoutine 2:0000443a78fe 138
ShaolinPoutine 2:0000443a78fe 139 void XBee::ATCommandResponse(int len)
ShaolinPoutine 2:0000443a78fe 140 {
ShaolinPoutine 2:0000443a78fe 141 char total = 0x88;
ShaolinPoutine 2:0000443a78fe 142 char id = comm.getc();
ShaolinPoutine 2:0000443a78fe 143 total += id;
ShaolinPoutine 2:0000443a78fe 144 char* command0 = mail->alloc();
ShaolinPoutine 2:0000443a78fe 145 char* command1 = mail->alloc();
ShaolinPoutine 2:0000443a78fe 146 *command0 = comm.getc();
ShaolinPoutine 2:0000443a78fe 147 total += *command0;
ShaolinPoutine 2:0000443a78fe 148 *command1 = comm.getc();
ShaolinPoutine 2:0000443a78fe 149 total += *command1;
ShaolinPoutine 2:0000443a78fe 150 char status = comm.getc();
ShaolinPoutine 2:0000443a78fe 151 total += status;
ShaolinPoutine 2:0000443a78fe 152 int i = 0;
ShaolinPoutine 2:0000443a78fe 153 len-= 4;
ShaolinPoutine 2:0000443a78fe 154 char data[len];
ShaolinPoutine 2:0000443a78fe 155
ShaolinPoutine 2:0000443a78fe 156 pcPrint("response to command \0");
ShaolinPoutine 2:0000443a78fe 157 mail->put(command0);
ShaolinPoutine 2:0000443a78fe 158 mail->put(command1);
ShaolinPoutine 2:0000443a78fe 159 pcPrint(" is \0");
ShaolinPoutine 2:0000443a78fe 160
ShaolinPoutine 4:e8f4d41c0fbc 161 if (len == 0)
ShaolinPoutine 4:e8f4d41c0fbc 162 {
ShaolinPoutine 4:e8f4d41c0fbc 163 switch (status)
ShaolinPoutine 4:e8f4d41c0fbc 164 {
ShaolinPoutine 4:e8f4d41c0fbc 165 case 0 : pcPrint("OK"); break;
ShaolinPoutine 4:e8f4d41c0fbc 166 case 1 : pcPrint("ERROR"); break;
ShaolinPoutine 4:e8f4d41c0fbc 167 case 2 : pcPrint("Invalid Command"); break;
ShaolinPoutine 4:e8f4d41c0fbc 168 case 3 : pcPrint("Invalid Parameter"); break;
ShaolinPoutine 4:e8f4d41c0fbc 169 case 4 : pcPrint("Tx Failure"); break;
ShaolinPoutine 4:e8f4d41c0fbc 170 default : pcPrint("Unknow error ..."); break;
ShaolinPoutine 4:e8f4d41c0fbc 171 }
ShaolinPoutine 4:e8f4d41c0fbc 172 }
ShaolinPoutine 4:e8f4d41c0fbc 173
ShaolinPoutine 2:0000443a78fe 174 while (i < len)
ShaolinPoutine 2:0000443a78fe 175 {
ShaolinPoutine 2:0000443a78fe 176 if (comm.readable())
ShaolinPoutine 2:0000443a78fe 177 {
ShaolinPoutine 2:0000443a78fe 178 data[i] = comm.getc();
ShaolinPoutine 2:0000443a78fe 179 total += data[i];
ShaolinPoutine 2:0000443a78fe 180 printHexa(data[i]);
ShaolinPoutine 2:0000443a78fe 181 i++;
ShaolinPoutine 2:0000443a78fe 182 }
ShaolinPoutine 2:0000443a78fe 183 }
ShaolinPoutine 2:0000443a78fe 184
ShaolinPoutine 2:0000443a78fe 185 char checksum = comm.getc();
ShaolinPoutine 2:0000443a78fe 186 total += checksum;
ShaolinPoutine 2:0000443a78fe 187 // Verify checksum
ShaolinPoutine 2:0000443a78fe 188 if (total != 0xFF)
ShaolinPoutine 2:0000443a78fe 189 {
ShaolinPoutine 2:0000443a78fe 190 pcPrint("Checksum is wrong\0");
ShaolinPoutine 2:0000443a78fe 191 }
ShaolinPoutine 2:0000443a78fe 192 pcPrint("\r\n\0");
ShaolinPoutine 2:0000443a78fe 193 }
ShaolinPoutine 2:0000443a78fe 194
ShaolinPoutine 2:0000443a78fe 195 void XBee::ModemStatus(int len)
ShaolinPoutine 2:0000443a78fe 196 {
ShaolinPoutine 2:0000443a78fe 197 char status = comm.getc();
ShaolinPoutine 2:0000443a78fe 198
ShaolinPoutine 2:0000443a78fe 199 switch (status){
ShaolinPoutine 2:0000443a78fe 200 case 0 : pcPrint("Hardware reset\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 201 case 1 : pcPrint("Watchdog timer reset\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 202 case 2 : pcPrint("Joined network (routers and end devices)\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 203 case 3 : pcPrint("Disassociated\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 204 case 6 : pcPrint("Coordinator started\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 205 case 7 : pcPrint("Network security key was updated\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 206 case 0x0D : pcPrint("Voltage supply limit exceeded\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 207 case 0x11 : pcPrint("Modem configuration changed while join in progress\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 208 default : pcPrint("stack error\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 209 }
ShaolinPoutine 2:0000443a78fe 210
ShaolinPoutine 2:0000443a78fe 211 char checksum = comm.getc();
ShaolinPoutine 2:0000443a78fe 212
ShaolinPoutine 2:0000443a78fe 213 checksum += 0x8A + status;
ShaolinPoutine 2:0000443a78fe 214
ShaolinPoutine 2:0000443a78fe 215 if (checksum != 0xFF)
ShaolinPoutine 2:0000443a78fe 216 {
ShaolinPoutine 2:0000443a78fe 217 pcPrint("Checksum is wrong\r\n\0");
ShaolinPoutine 2:0000443a78fe 218 }
ShaolinPoutine 3:4c1dec78117b 219 }
ShaolinPoutine 3:4c1dec78117b 220
ShaolinPoutine 3:4c1dec78117b 221 void XBee::ZigBeeTransmitStatus(int len)
ShaolinPoutine 3:4c1dec78117b 222 {
ShaolinPoutine 3:4c1dec78117b 223 char id = comm.getc();
ShaolinPoutine 3:4c1dec78117b 224 char msb = comm.getc();
ShaolinPoutine 3:4c1dec78117b 225 char lsb = comm.getc();
ShaolinPoutine 3:4c1dec78117b 226 char retry = comm.getc();
ShaolinPoutine 3:4c1dec78117b 227 char status = comm.getc();
ShaolinPoutine 3:4c1dec78117b 228 char discovery = comm.getc();
ShaolinPoutine 3:4c1dec78117b 229
ShaolinPoutine 3:4c1dec78117b 230 //pcPrint("Response to transmit id %d is : ", id);
ShaolinPoutine 3:4c1dec78117b 231 pcPrint("Response to transmit id is : ");
ShaolinPoutine 3:4c1dec78117b 232
ShaolinPoutine 3:4c1dec78117b 233 if (status == 0)
ShaolinPoutine 3:4c1dec78117b 234 {
ShaolinPoutine 3:4c1dec78117b 235 pcPrint("Success\r\n");
ShaolinPoutine 3:4c1dec78117b 236 }
ShaolinPoutine 3:4c1dec78117b 237 else
ShaolinPoutine 3:4c1dec78117b 238 {
ShaolinPoutine 3:4c1dec78117b 239 switch (status){
ShaolinPoutine 3:4c1dec78117b 240 case 0x01 : pcPrint("MAC ACK Failure\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 241 case 0x02 : pcPrint("CCA Failure\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 242 case 0x15 : pcPrint("Invalid destination endpoint\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 243 case 0x21 : pcPrint("Network ACK Failure\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 244 case 0x22 : pcPrint("Not Joined to Network\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 245 case 0x23 : pcPrint("Self-addressed\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 246 case 0x24 : pcPrint("Address Not Found\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 247 case 0x25 : pcPrint("Route Not Found\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 248 case 0x26 : pcPrint("Broadcast source failed to hear a neighbor relay the message\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 249 case 0x2B : pcPrint("Invalid binding table index\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 250 case 0x2C : pcPrint("Resource error lack of free buffers, timers, etc.\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 251 case 0x2D : pcPrint("Attempted broadcast with APS transmission\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 252 case 0x2E : pcPrint("Attempted unicast with APS transmission, but EE=0\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 253 case 0x32 : pcPrint("Resource error lack of free buffers, timers, etc.\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 254 case 0x74 : pcPrint("Data payload too large\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 255 default : pcPrint("Unknow error ...\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 256 }
ShaolinPoutine 3:4c1dec78117b 257 }
ShaolinPoutine 3:4c1dec78117b 258 }
ShaolinPoutine 3:4c1dec78117b 259
ShaolinPoutine 3:4c1dec78117b 260 void XBee::ZigBeeReceivePacket(int len)
ShaolinPoutine 3:4c1dec78117b 261 {
ShaolinPoutine 3:4c1dec78117b 262 int i = 0;
ShaolinPoutine 3:4c1dec78117b 263 char adresse64bit[8];
ShaolinPoutine 3:4c1dec78117b 264 char adresse16bit[2];
ShaolinPoutine 3:4c1dec78117b 265 char receiveOptions;
ShaolinPoutine 3:4c1dec78117b 266
ShaolinPoutine 3:4c1dec78117b 267 while(i < 8)
ShaolinPoutine 3:4c1dec78117b 268 {
ShaolinPoutine 3:4c1dec78117b 269 adresse64bit[i] = comm.getc();
ShaolinPoutine 3:4c1dec78117b 270 i++;
ShaolinPoutine 3:4c1dec78117b 271 }
ShaolinPoutine 3:4c1dec78117b 272
ShaolinPoutine 3:4c1dec78117b 273 adresse16bit[0] = comm.getc();
ShaolinPoutine 3:4c1dec78117b 274 adresse16bit[1] = comm.getc();
ShaolinPoutine 3:4c1dec78117b 275
ShaolinPoutine 3:4c1dec78117b 276 receiveOptions = comm.getc();
ShaolinPoutine 3:4c1dec78117b 277
ShaolinPoutine 3:4c1dec78117b 278 pcPrint("Data received : ");
ShaolinPoutine 3:4c1dec78117b 279
ShaolinPoutine 3:4c1dec78117b 280 i = 11;
ShaolinPoutine 3:4c1dec78117b 281 while (i < len)
ShaolinPoutine 3:4c1dec78117b 282 {
ShaolinPoutine 3:4c1dec78117b 283 printHexa(comm.getc());
ShaolinPoutine 3:4c1dec78117b 284 i++;
ShaolinPoutine 3:4c1dec78117b 285 }
ShaolinPoutine 3:4c1dec78117b 286
ShaolinPoutine 3:4c1dec78117b 287 // Validate checksum TODO
ShaolinPoutine 2:0000443a78fe 288 }