APP3 / Zigbee_communication

Dependents:   Coordinator_node Router_node

Committer:
ShaolinPoutine
Date:
Tue Feb 14 20:34:41 2017 +0000
Revision:
14:b8d037ec02d3
Parent:
8:ba349a2eeb37
Added web comm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ShaolinPoutine 2:0000443a78fe 1 #include "xbee.h"
ShaolinPoutine 2:0000443a78fe 2
ShaolinPoutine 14:b8d037ec02d3 3 XBee::XBee(PinName reset, PinName transfer, PinName receive, Mail<char, 250>* m, Mail<char[256], 16>* w) :
EmileArseneault 5:9b4d93bd6725 4 rst(reset), comm(transfer, receive)
EmileArseneault 5:9b4d93bd6725 5 {
EmileArseneault 5:9b4d93bd6725 6 // Constructor
EmileArseneault 5:9b4d93bd6725 7 mail = m;
ShaolinPoutine 14:b8d037ec02d3 8 webmail = w;
EmileArseneault 5:9b4d93bd6725 9 rst = 0;
EmileArseneault 5:9b4d93bd6725 10 wait(0.4);
EmileArseneault 5:9b4d93bd6725 11 rst = 1;
EmileArseneault 5:9b4d93bd6725 12 wait(3); // waiting for initiation
ShaolinPoutine 14:b8d037ec02d3 13 buff[0] = '\0';
ShaolinPoutine 14:b8d037ec02d3 14 }
ShaolinPoutine 14:b8d037ec02d3 15
ShaolinPoutine 14:b8d037ec02d3 16 void XBee::sendBuffer()
ShaolinPoutine 14:b8d037ec02d3 17 {
ShaolinPoutine 14:b8d037ec02d3 18 char *c = (char *) webmail->alloc();
ShaolinPoutine 14:b8d037ec02d3 19 int i = 0;
ShaolinPoutine 14:b8d037ec02d3 20 while(buff[i] != '\0' || i == 255)
ShaolinPoutine 14:b8d037ec02d3 21 {
ShaolinPoutine 14:b8d037ec02d3 22 c[i] = buff[i];
ShaolinPoutine 14:b8d037ec02d3 23 i++;
ShaolinPoutine 14:b8d037ec02d3 24 }
ShaolinPoutine 14:b8d037ec02d3 25 c[i] = '\0';
ShaolinPoutine 14:b8d037ec02d3 26 webmail->put((char(*)[256]) c);
ShaolinPoutine 14:b8d037ec02d3 27
ShaolinPoutine 14:b8d037ec02d3 28 buff[0] = '\0';
ShaolinPoutine 14:b8d037ec02d3 29 }
ShaolinPoutine 14:b8d037ec02d3 30
ShaolinPoutine 14:b8d037ec02d3 31 void XBee::appendBuffer(char* c)
ShaolinPoutine 14:b8d037ec02d3 32 {
ShaolinPoutine 14:b8d037ec02d3 33 int i = 0;
ShaolinPoutine 14:b8d037ec02d3 34 char* ptr = c;
ShaolinPoutine 14:b8d037ec02d3 35 //find end
ShaolinPoutine 14:b8d037ec02d3 36 while (buff[i] != '\0') i++;
ShaolinPoutine 14:b8d037ec02d3 37
ShaolinPoutine 14:b8d037ec02d3 38 while (*ptr != '\0')
ShaolinPoutine 14:b8d037ec02d3 39 {
ShaolinPoutine 14:b8d037ec02d3 40 buff[i] = *ptr;
ShaolinPoutine 14:b8d037ec02d3 41 ptr++;
ShaolinPoutine 14:b8d037ec02d3 42 i++;
ShaolinPoutine 14:b8d037ec02d3 43 }
ShaolinPoutine 14:b8d037ec02d3 44 buff[i] = '\0';
EmileArseneault 5:9b4d93bd6725 45 }
EmileArseneault 5:9b4d93bd6725 46
ShaolinPoutine 2:0000443a78fe 47 void XBee::pcPrint(char* c)
ShaolinPoutine 2:0000443a78fe 48 {
ShaolinPoutine 2:0000443a78fe 49 int i = 0;
ShaolinPoutine 2:0000443a78fe 50 while( (c)[i] != '\0')
ShaolinPoutine 2:0000443a78fe 51 {
ShaolinPoutine 2:0000443a78fe 52 mail->put(&(c[i]));
ShaolinPoutine 2:0000443a78fe 53 i++;
ShaolinPoutine 2:0000443a78fe 54 }
ShaolinPoutine 2:0000443a78fe 55 }
ShaolinPoutine 2:0000443a78fe 56
EmileArseneault 5:9b4d93bd6725 57 void XBee::printInt(int i)
EmileArseneault 5:9b4d93bd6725 58 {
EmileArseneault 5:9b4d93bd6725 59 bool signe = i > 0;
EmileArseneault 5:9b4d93bd6725 60 char *c = mail->alloc();
EmileArseneault 5:9b4d93bd6725 61 if (signe)
EmileArseneault 5:9b4d93bd6725 62 {
EmileArseneault 5:9b4d93bd6725 63 *c = '-';
EmileArseneault 5:9b4d93bd6725 64 mail->put(c);
ShaolinPoutine 8:ba349a2eeb37 65 i *= -1;
EmileArseneault 5:9b4d93bd6725 66 }
EmileArseneault 5:9b4d93bd6725 67
EmileArseneault 5:9b4d93bd6725 68 int diviseur = 1;
EmileArseneault 5:9b4d93bd6725 69 int modulo = 10;
EmileArseneault 5:9b4d93bd6725 70 int j = 9;
EmileArseneault 5:9b4d93bd6725 71 char chiffre[10];
EmileArseneault 5:9b4d93bd6725 72
EmileArseneault 5:9b4d93bd6725 73 while (i / diviseur > 0 && j >= 0)
EmileArseneault 5:9b4d93bd6725 74 {
ShaolinPoutine 8:ba349a2eeb37 75 chiffre[j] = (char) (i % modulo)/diviseur;
EmileArseneault 5:9b4d93bd6725 76 modulo *= 10;
EmileArseneault 5:9b4d93bd6725 77 diviseur *= 10;
EmileArseneault 5:9b4d93bd6725 78 j--;
EmileArseneault 5:9b4d93bd6725 79 }
EmileArseneault 5:9b4d93bd6725 80
EmileArseneault 5:9b4d93bd6725 81 j = 0;
EmileArseneault 5:9b4d93bd6725 82 bool numberHasStarted = false;
EmileArseneault 5:9b4d93bd6725 83 while (j < 10)
EmileArseneault 5:9b4d93bd6725 84 {
EmileArseneault 5:9b4d93bd6725 85 if (chiffre[j] != 0 || numberHasStarted || j == 9)
EmileArseneault 5:9b4d93bd6725 86 {
EmileArseneault 5:9b4d93bd6725 87 numberHasStarted = true;
EmileArseneault 5:9b4d93bd6725 88 c = mail->alloc();
ShaolinPoutine 8:ba349a2eeb37 89 *c = chiffre[j] + 0x30;
EmileArseneault 5:9b4d93bd6725 90 mail->put(c);
EmileArseneault 5:9b4d93bd6725 91 }
EmileArseneault 5:9b4d93bd6725 92 j++;
EmileArseneault 5:9b4d93bd6725 93 }
EmileArseneault 5:9b4d93bd6725 94 }
EmileArseneault 5:9b4d93bd6725 95
ShaolinPoutine 2:0000443a78fe 96 void XBee::printHexa(char c)
ShaolinPoutine 2:0000443a78fe 97 {
ShaolinPoutine 2:0000443a78fe 98 char *msb = mail->alloc();
ShaolinPoutine 2:0000443a78fe 99 *msb = c >> 4;
ShaolinPoutine 2:0000443a78fe 100 char *lsb = mail->alloc();
ShaolinPoutine 2:0000443a78fe 101 *lsb = c & 0xF;
ShaolinPoutine 2:0000443a78fe 102
ShaolinPoutine 2:0000443a78fe 103 if (*msb < 10)
ShaolinPoutine 2:0000443a78fe 104 *msb += 0x30;
ShaolinPoutine 2:0000443a78fe 105 else
ShaolinPoutine 2:0000443a78fe 106 *msb += 0x37;
ShaolinPoutine 2:0000443a78fe 107
ShaolinPoutine 2:0000443a78fe 108 if (*lsb < 10)
ShaolinPoutine 2:0000443a78fe 109 *lsb += 0x30;
ShaolinPoutine 2:0000443a78fe 110 else
ShaolinPoutine 2:0000443a78fe 111 *lsb += 0x37;
ShaolinPoutine 2:0000443a78fe 112
ShaolinPoutine 2:0000443a78fe 113 char * str = "0x";
ShaolinPoutine 2:0000443a78fe 114 pcPrint(str);
ShaolinPoutine 2:0000443a78fe 115 mail->put(msb);
ShaolinPoutine 2:0000443a78fe 116 mail->put(lsb);
ShaolinPoutine 2:0000443a78fe 117 str = " ";
ShaolinPoutine 2:0000443a78fe 118 pcPrint(str);
ShaolinPoutine 2:0000443a78fe 119 }
ShaolinPoutine 2:0000443a78fe 120
ShaolinPoutine 2:0000443a78fe 121 void XBee::SendATCommand(char firstChar, char secondChar, char *optionalParam, int paramLen)
ShaolinPoutine 2:0000443a78fe 122 {
ShaolinPoutine 2:0000443a78fe 123 // Frame Type 0x08
ShaolinPoutine 2:0000443a78fe 124 // Two char as parameters
ShaolinPoutine 2:0000443a78fe 125
ShaolinPoutine 2:0000443a78fe 126 char cmdtosend[10];
ShaolinPoutine 2:0000443a78fe 127 char sum = 0;
ShaolinPoutine 2:0000443a78fe 128 int cmdlength = 8;
ShaolinPoutine 2:0000443a78fe 129 int i = 0;
ShaolinPoutine 2:0000443a78fe 130
ShaolinPoutine 2:0000443a78fe 131 cmdtosend[0] = FRAMEDELIMITER;
ShaolinPoutine 2:0000443a78fe 132 cmdtosend[1] = 0x00;
ShaolinPoutine 2:0000443a78fe 133 cmdtosend[2] = 0x04 + paramLen;
ShaolinPoutine 2:0000443a78fe 134 cmdtosend[3] = 0x08;
ShaolinPoutine 2:0000443a78fe 135 cmdtosend[4] = 0x52;
ShaolinPoutine 2:0000443a78fe 136 cmdtosend[5] = firstChar;
ShaolinPoutine 2:0000443a78fe 137 cmdtosend[6] = secondChar;
ShaolinPoutine 2:0000443a78fe 138
ShaolinPoutine 2:0000443a78fe 139 // Ajouter les parametres au message
ShaolinPoutine 2:0000443a78fe 140 if(optionalParam != NULL)
ShaolinPoutine 2:0000443a78fe 141 {
ShaolinPoutine 2:0000443a78fe 142 i = 0;
ShaolinPoutine 2:0000443a78fe 143 cmdlength += paramLen;
ShaolinPoutine 2:0000443a78fe 144
ShaolinPoutine 2:0000443a78fe 145 while (i < paramLen)
ShaolinPoutine 2:0000443a78fe 146 {
ShaolinPoutine 2:0000443a78fe 147 cmdtosend[7 + i] = (optionalParam)[i];
ShaolinPoutine 2:0000443a78fe 148 i++;
ShaolinPoutine 2:0000443a78fe 149 }
ShaolinPoutine 2:0000443a78fe 150 pcPrint("\r\n\0");
ShaolinPoutine 2:0000443a78fe 151 }
ShaolinPoutine 2:0000443a78fe 152
ShaolinPoutine 2:0000443a78fe 153 // Calculate checksum
ShaolinPoutine 2:0000443a78fe 154 i = 3;
ShaolinPoutine 2:0000443a78fe 155 while (i < (cmdlength - 1))
ShaolinPoutine 2:0000443a78fe 156 {
ShaolinPoutine 2:0000443a78fe 157 sum += cmdtosend[i];
ShaolinPoutine 2:0000443a78fe 158 i++;
ShaolinPoutine 2:0000443a78fe 159 }
ShaolinPoutine 2:0000443a78fe 160 cmdtosend[cmdlength - 1] = 0xFF - sum;
ShaolinPoutine 2:0000443a78fe 161
ShaolinPoutine 2:0000443a78fe 162 // Envoyer la commande sur UART
ShaolinPoutine 2:0000443a78fe 163 i = 0;
ShaolinPoutine 2:0000443a78fe 164 while (i < cmdlength)
ShaolinPoutine 2:0000443a78fe 165 {
ShaolinPoutine 2:0000443a78fe 166 comm.putc(cmdtosend[i]);
ShaolinPoutine 2:0000443a78fe 167 i++;
ShaolinPoutine 2:0000443a78fe 168 }
ShaolinPoutine 2:0000443a78fe 169
ShaolinPoutine 2:0000443a78fe 170 wait(0.1);
ShaolinPoutine 2:0000443a78fe 171 }
ShaolinPoutine 2:0000443a78fe 172
ShaolinPoutine 2:0000443a78fe 173 char* XBee::InterpretMessage()
ShaolinPoutine 2:0000443a78fe 174 {
ShaolinPoutine 2:0000443a78fe 175 char *response = "\0";
ShaolinPoutine 2:0000443a78fe 176
ShaolinPoutine 2:0000443a78fe 177 if (comm.readable())
ShaolinPoutine 2:0000443a78fe 178 {
ShaolinPoutine 2:0000443a78fe 179
ShaolinPoutine 2:0000443a78fe 180 char start = comm.getc(); // = FRAMEDELIMITER
ShaolinPoutine 2:0000443a78fe 181 //assert
ShaolinPoutine 2:0000443a78fe 182 char len_msb = comm.getc();
ShaolinPoutine 2:0000443a78fe 183 char len_lsb = comm.getc();
ShaolinPoutine 2:0000443a78fe 184
ShaolinPoutine 2:0000443a78fe 185 int len = ((int) len_msb << 4) + (int) len_lsb;
ShaolinPoutine 2:0000443a78fe 186 char frame_data[len];
ShaolinPoutine 2:0000443a78fe 187
ShaolinPoutine 2:0000443a78fe 188 // Resolving frame type
ShaolinPoutine 2:0000443a78fe 189 char type = comm.getc();
ShaolinPoutine 2:0000443a78fe 190 len--;
ShaolinPoutine 2:0000443a78fe 191
ShaolinPoutine 2:0000443a78fe 192 switch (type){
ShaolinPoutine 2:0000443a78fe 193 case 0x88: ATCommandResponse(len);
ShaolinPoutine 2:0000443a78fe 194 break;
ShaolinPoutine 2:0000443a78fe 195 case 0x8A: ModemStatus(len);
ShaolinPoutine 2:0000443a78fe 196 break;
ShaolinPoutine 3:4c1dec78117b 197 case 0x8B: ZigBeeTransmitStatus(len);
ShaolinPoutine 3:4c1dec78117b 198 break;
ShaolinPoutine 3:4c1dec78117b 199 case 0x90: ZigBeeReceivePacket(len);
ShaolinPoutine 3:4c1dec78117b 200 break;
ShaolinPoutine 14:b8d037ec02d3 201 case 0xff:
ShaolinPoutine 14:b8d037ec02d3 202 pcPrint("Tonight at 11. Doooom!");
ShaolinPoutine 14:b8d037ec02d3 203 while(comm.readable())
ShaolinPoutine 14:b8d037ec02d3 204 comm.getc();
ShaolinPoutine 14:b8d037ec02d3 205 break;
EmileArseneault 5:9b4d93bd6725 206 default: pcPrint("Please implement response of type ");
EmileArseneault 5:9b4d93bd6725 207 printHexa(type);
ShaolinPoutine 2:0000443a78fe 208 pcPrint("\r\n\0");
EmileArseneault 5:9b4d93bd6725 209 for (int i = 0; i <len; i++)
EmileArseneault 5:9b4d93bd6725 210 {
EmileArseneault 5:9b4d93bd6725 211 comm.getc();
EmileArseneault 5:9b4d93bd6725 212 }
ShaolinPoutine 2:0000443a78fe 213 }
ShaolinPoutine 2:0000443a78fe 214 }
ShaolinPoutine 2:0000443a78fe 215 return response;
ShaolinPoutine 2:0000443a78fe 216 }
ShaolinPoutine 2:0000443a78fe 217
ShaolinPoutine 2:0000443a78fe 218 void XBee::ATCommandResponse(int len)
ShaolinPoutine 2:0000443a78fe 219 {
ShaolinPoutine 2:0000443a78fe 220 char total = 0x88;
ShaolinPoutine 2:0000443a78fe 221 char id = comm.getc();
ShaolinPoutine 2:0000443a78fe 222 total += id;
ShaolinPoutine 2:0000443a78fe 223 char* command0 = mail->alloc();
ShaolinPoutine 2:0000443a78fe 224 char* command1 = mail->alloc();
ShaolinPoutine 2:0000443a78fe 225 *command0 = comm.getc();
ShaolinPoutine 2:0000443a78fe 226 total += *command0;
ShaolinPoutine 2:0000443a78fe 227 *command1 = comm.getc();
ShaolinPoutine 2:0000443a78fe 228 total += *command1;
ShaolinPoutine 2:0000443a78fe 229 char status = comm.getc();
ShaolinPoutine 2:0000443a78fe 230 total += status;
ShaolinPoutine 2:0000443a78fe 231 int i = 0;
ShaolinPoutine 2:0000443a78fe 232 len-= 4;
ShaolinPoutine 2:0000443a78fe 233 char data[len];
ShaolinPoutine 2:0000443a78fe 234
ShaolinPoutine 2:0000443a78fe 235 pcPrint("response to command \0");
ShaolinPoutine 2:0000443a78fe 236 mail->put(command0);
ShaolinPoutine 2:0000443a78fe 237 mail->put(command1);
ShaolinPoutine 2:0000443a78fe 238 pcPrint(" is \0");
ShaolinPoutine 2:0000443a78fe 239
ShaolinPoutine 4:e8f4d41c0fbc 240 if (len == 0)
ShaolinPoutine 4:e8f4d41c0fbc 241 {
ShaolinPoutine 4:e8f4d41c0fbc 242 switch (status)
ShaolinPoutine 4:e8f4d41c0fbc 243 {
ShaolinPoutine 4:e8f4d41c0fbc 244 case 0 : pcPrint("OK"); break;
ShaolinPoutine 4:e8f4d41c0fbc 245 case 1 : pcPrint("ERROR"); break;
ShaolinPoutine 4:e8f4d41c0fbc 246 case 2 : pcPrint("Invalid Command"); break;
ShaolinPoutine 4:e8f4d41c0fbc 247 case 3 : pcPrint("Invalid Parameter"); break;
ShaolinPoutine 4:e8f4d41c0fbc 248 case 4 : pcPrint("Tx Failure"); break;
ShaolinPoutine 4:e8f4d41c0fbc 249 default : pcPrint("Unknow error ..."); break;
ShaolinPoutine 4:e8f4d41c0fbc 250 }
ShaolinPoutine 4:e8f4d41c0fbc 251 }
ShaolinPoutine 4:e8f4d41c0fbc 252
ShaolinPoutine 2:0000443a78fe 253 while (i < len)
ShaolinPoutine 2:0000443a78fe 254 {
ShaolinPoutine 2:0000443a78fe 255 if (comm.readable())
ShaolinPoutine 2:0000443a78fe 256 {
ShaolinPoutine 2:0000443a78fe 257 data[i] = comm.getc();
ShaolinPoutine 2:0000443a78fe 258 total += data[i];
ShaolinPoutine 2:0000443a78fe 259 printHexa(data[i]);
ShaolinPoutine 2:0000443a78fe 260 i++;
ShaolinPoutine 2:0000443a78fe 261 }
ShaolinPoutine 2:0000443a78fe 262 }
ShaolinPoutine 2:0000443a78fe 263
ShaolinPoutine 2:0000443a78fe 264 char checksum = comm.getc();
ShaolinPoutine 2:0000443a78fe 265 total += checksum;
ShaolinPoutine 2:0000443a78fe 266 // Verify checksum
ShaolinPoutine 2:0000443a78fe 267 if (total != 0xFF)
ShaolinPoutine 2:0000443a78fe 268 {
ShaolinPoutine 2:0000443a78fe 269 pcPrint("Checksum is wrong\0");
ShaolinPoutine 2:0000443a78fe 270 }
ShaolinPoutine 2:0000443a78fe 271 pcPrint("\r\n\0");
ShaolinPoutine 2:0000443a78fe 272 }
ShaolinPoutine 2:0000443a78fe 273
ShaolinPoutine 2:0000443a78fe 274 void XBee::ModemStatus(int len)
ShaolinPoutine 2:0000443a78fe 275 {
ShaolinPoutine 2:0000443a78fe 276 char status = comm.getc();
ShaolinPoutine 2:0000443a78fe 277
ShaolinPoutine 2:0000443a78fe 278 switch (status){
ShaolinPoutine 2:0000443a78fe 279 case 0 : pcPrint("Hardware reset\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 280 case 1 : pcPrint("Watchdog timer reset\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 281 case 2 : pcPrint("Joined network (routers and end devices)\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 282 case 3 : pcPrint("Disassociated\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 283 case 6 : pcPrint("Coordinator started\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 284 case 7 : pcPrint("Network security key was updated\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 285 case 0x0D : pcPrint("Voltage supply limit exceeded\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 286 case 0x11 : pcPrint("Modem configuration changed while join in progress\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 287 default : pcPrint("stack error\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 288 }
ShaolinPoutine 2:0000443a78fe 289
ShaolinPoutine 2:0000443a78fe 290 char checksum = comm.getc();
ShaolinPoutine 2:0000443a78fe 291
ShaolinPoutine 2:0000443a78fe 292 checksum += 0x8A + status;
ShaolinPoutine 2:0000443a78fe 293
ShaolinPoutine 2:0000443a78fe 294 if (checksum != 0xFF)
ShaolinPoutine 2:0000443a78fe 295 {
ShaolinPoutine 2:0000443a78fe 296 pcPrint("Checksum is wrong\r\n\0");
ShaolinPoutine 2:0000443a78fe 297 }
ShaolinPoutine 3:4c1dec78117b 298 }
ShaolinPoutine 3:4c1dec78117b 299
ShaolinPoutine 3:4c1dec78117b 300 void XBee::ZigBeeTransmitStatus(int len)
ShaolinPoutine 3:4c1dec78117b 301 {
ShaolinPoutine 3:4c1dec78117b 302 char id = comm.getc();
ShaolinPoutine 3:4c1dec78117b 303 char msb = comm.getc();
ShaolinPoutine 3:4c1dec78117b 304 char lsb = comm.getc();
ShaolinPoutine 3:4c1dec78117b 305 char retry = comm.getc();
ShaolinPoutine 3:4c1dec78117b 306 char status = comm.getc();
ShaolinPoutine 3:4c1dec78117b 307 char discovery = comm.getc();
EmileArseneault 5:9b4d93bd6725 308 char checksum;
ShaolinPoutine 3:4c1dec78117b 309
ShaolinPoutine 8:ba349a2eeb37 310 pcPrint("Response to transmit #");
ShaolinPoutine 8:ba349a2eeb37 311 printHexa(id);
EmileArseneault 5:9b4d93bd6725 312 pcPrint(" is : ");
ShaolinPoutine 3:4c1dec78117b 313
ShaolinPoutine 3:4c1dec78117b 314 if (status == 0)
ShaolinPoutine 3:4c1dec78117b 315 {
ShaolinPoutine 3:4c1dec78117b 316 pcPrint("Success\r\n");
ShaolinPoutine 3:4c1dec78117b 317 }
ShaolinPoutine 3:4c1dec78117b 318 else
ShaolinPoutine 3:4c1dec78117b 319 {
ShaolinPoutine 3:4c1dec78117b 320 switch (status){
ShaolinPoutine 3:4c1dec78117b 321 case 0x01 : pcPrint("MAC ACK Failure\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 322 case 0x02 : pcPrint("CCA Failure\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 323 case 0x15 : pcPrint("Invalid destination endpoint\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 324 case 0x21 : pcPrint("Network ACK Failure\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 325 case 0x22 : pcPrint("Not Joined to Network\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 326 case 0x23 : pcPrint("Self-addressed\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 327 case 0x24 : pcPrint("Address Not Found\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 328 case 0x25 : pcPrint("Route Not Found\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 329 case 0x26 : pcPrint("Broadcast source failed to hear a neighbor relay the message\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 330 case 0x2B : pcPrint("Invalid binding table index\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 331 case 0x2C : pcPrint("Resource error lack of free buffers, timers, etc.\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 332 case 0x2D : pcPrint("Attempted broadcast with APS transmission\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 333 case 0x2E : pcPrint("Attempted unicast with APS transmission, but EE=0\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 334 case 0x32 : pcPrint("Resource error lack of free buffers, timers, etc.\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 335 case 0x74 : pcPrint("Data payload too large\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 336 default : pcPrint("Unknow error ...\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 337 }
ShaolinPoutine 3:4c1dec78117b 338 }
EmileArseneault 5:9b4d93bd6725 339
EmileArseneault 5:9b4d93bd6725 340 checksum = comm.getc();
EmileArseneault 5:9b4d93bd6725 341 // Validate checksum TODO
ShaolinPoutine 3:4c1dec78117b 342 }
ShaolinPoutine 3:4c1dec78117b 343
ShaolinPoutine 3:4c1dec78117b 344 void XBee::ZigBeeReceivePacket(int len)
ShaolinPoutine 3:4c1dec78117b 345 {
ShaolinPoutine 3:4c1dec78117b 346 int i = 0;
ShaolinPoutine 3:4c1dec78117b 347 char adresse64bit[8];
ShaolinPoutine 3:4c1dec78117b 348 char adresse16bit[2];
ShaolinPoutine 3:4c1dec78117b 349 char receiveOptions;
EmileArseneault 5:9b4d93bd6725 350 char checksum;
ShaolinPoutine 3:4c1dec78117b 351
ShaolinPoutine 3:4c1dec78117b 352 while(i < 8)
ShaolinPoutine 3:4c1dec78117b 353 {
ShaolinPoutine 3:4c1dec78117b 354 adresse64bit[i] = comm.getc();
ShaolinPoutine 3:4c1dec78117b 355 i++;
ShaolinPoutine 3:4c1dec78117b 356 }
ShaolinPoutine 3:4c1dec78117b 357
ShaolinPoutine 3:4c1dec78117b 358 adresse16bit[0] = comm.getc();
ShaolinPoutine 3:4c1dec78117b 359 adresse16bit[1] = comm.getc();
ShaolinPoutine 3:4c1dec78117b 360
ShaolinPoutine 3:4c1dec78117b 361 receiveOptions = comm.getc();
ShaolinPoutine 3:4c1dec78117b 362
ShaolinPoutine 3:4c1dec78117b 363 pcPrint("Data received : ");
ShaolinPoutine 3:4c1dec78117b 364
ShaolinPoutine 3:4c1dec78117b 365 i = 11;
ShaolinPoutine 3:4c1dec78117b 366 while (i < len)
ShaolinPoutine 3:4c1dec78117b 367 {
ShaolinPoutine 3:4c1dec78117b 368 printHexa(comm.getc());
ShaolinPoutine 3:4c1dec78117b 369 i++;
ShaolinPoutine 3:4c1dec78117b 370 }
EmileArseneault 5:9b4d93bd6725 371 pcPrint("\r\n");
ShaolinPoutine 3:4c1dec78117b 372
EmileArseneault 5:9b4d93bd6725 373 checksum = comm.getc();
ShaolinPoutine 3:4c1dec78117b 374 // Validate checksum TODO
ShaolinPoutine 6:3b97770f30e6 375 }
ShaolinPoutine 6:3b97770f30e6 376
ShaolinPoutine 6:3b97770f30e6 377 void XBee::ZigBeeTransmit(int adresse16, int adresse64, char *data, int dataLength)
ShaolinPoutine 6:3b97770f30e6 378 {
ShaolinPoutine 6:3b97770f30e6 379 // Frame Type 0x10
ShaolinPoutine 6:3b97770f30e6 380 // 0x0000000000000000 - Reserved 64-bit address for the coordinator
ShaolinPoutine 6:3b97770f30e6 381 // 0x000000000000FFFF - Broadcast address
ShaolinPoutine 6:3b97770f30e6 382
ShaolinPoutine 6:3b97770f30e6 383 // The Transmit Status frame (0x8B) est la reponse
ShaolinPoutine 6:3b97770f30e6 384 char cmdtosend[25];
ShaolinPoutine 6:3b97770f30e6 385 char checksum = 0x00;
ShaolinPoutine 6:3b97770f30e6 386 int cmdlength = 18;
ShaolinPoutine 6:3b97770f30e6 387 int i = 3;
ShaolinPoutine 6:3b97770f30e6 388
ShaolinPoutine 6:3b97770f30e6 389 //ID command to set/read operating 64 bit PAN ID
ShaolinPoutine 6:3b97770f30e6 390 //WR command to set operating 64 bit PAN ID across reboot
ShaolinPoutine 6:3b97770f30e6 391 //OI command to read operating 16 bit PAN ID
ShaolinPoutine 6:3b97770f30e6 392 //II command to set operating 16 bit PAN ID
ShaolinPoutine 6:3b97770f30e6 393
ShaolinPoutine 6:3b97770f30e6 394 cmdtosend[0] = FRAMEDELIMITER;
ShaolinPoutine 6:3b97770f30e6 395 cmdtosend[1] = 0x00;
ShaolinPoutine 6:3b97770f30e6 396 cmdtosend[2] = 0x0E + dataLength;
ShaolinPoutine 6:3b97770f30e6 397 cmdtosend[3] = 0x10; // Frame type
ShaolinPoutine 6:3b97770f30e6 398 cmdtosend[4] = 0x01; // Frame number
ShaolinPoutine 6:3b97770f30e6 399 cmdtosend[5] = 0x00; // MSB adresse 64-bit
ShaolinPoutine 6:3b97770f30e6 400 cmdtosend[6] = 0x00;
ShaolinPoutine 6:3b97770f30e6 401 cmdtosend[7] = 0x00;
ShaolinPoutine 6:3b97770f30e6 402 cmdtosend[8] = 0x00;
ShaolinPoutine 6:3b97770f30e6 403 cmdtosend[9] = 0x00;
ShaolinPoutine 6:3b97770f30e6 404 cmdtosend[10] = 0x00;
ShaolinPoutine 6:3b97770f30e6 405 cmdtosend[11] = 0xFF;
ShaolinPoutine 6:3b97770f30e6 406 cmdtosend[12] = 0xFF; // LSB adresse 64-bit
ShaolinPoutine 6:3b97770f30e6 407 cmdtosend[13] = adresse16 >> 16; // MSB adresse 16-bit
ShaolinPoutine 6:3b97770f30e6 408 cmdtosend[14] = adresse16 && 0b0000000011111111; // LSB adresse 16-bit
ShaolinPoutine 6:3b97770f30e6 409 cmdtosend[15] = 0x00; // Broadcast Radius
ShaolinPoutine 6:3b97770f30e6 410 cmdtosend[16] = 0x00; // Options
ShaolinPoutine 6:3b97770f30e6 411
ShaolinPoutine 6:3b97770f30e6 412 // Set RF DATA
ShaolinPoutine 6:3b97770f30e6 413 if(data != NULL)
ShaolinPoutine 6:3b97770f30e6 414 {
ShaolinPoutine 6:3b97770f30e6 415 i = 0;
ShaolinPoutine 6:3b97770f30e6 416 cmdlength += dataLength;
ShaolinPoutine 6:3b97770f30e6 417
ShaolinPoutine 6:3b97770f30e6 418 while (i < dataLength)
ShaolinPoutine 6:3b97770f30e6 419 {
ShaolinPoutine 6:3b97770f30e6 420 cmdtosend[17 + i] = (data)[i];
ShaolinPoutine 6:3b97770f30e6 421 i++;
ShaolinPoutine 6:3b97770f30e6 422 }
ShaolinPoutine 6:3b97770f30e6 423 }
ShaolinPoutine 6:3b97770f30e6 424
ShaolinPoutine 6:3b97770f30e6 425 //cmdtosend[17] = 0x00; // Start RF DATA
ShaolinPoutine 6:3b97770f30e6 426 //cmdtosend[18] = 0x00;
ShaolinPoutine 6:3b97770f30e6 427 //cmdtosend[19] = 0x00;
ShaolinPoutine 6:3b97770f30e6 428 //cmdtosend[20] = 0x00;
ShaolinPoutine 6:3b97770f30e6 429 //cmdtosend[21] = 0x00;
ShaolinPoutine 6:3b97770f30e6 430 //cmdtosend[22] = 0x00;
ShaolinPoutine 6:3b97770f30e6 431 //cmdtosend[23] = 0x00;
ShaolinPoutine 6:3b97770f30e6 432 //cmdtosend[24] = 0x00; // End RF DATA
ShaolinPoutine 6:3b97770f30e6 433
ShaolinPoutine 6:3b97770f30e6 434 // Calculate checksum
ShaolinPoutine 6:3b97770f30e6 435 i = 3;
ShaolinPoutine 6:3b97770f30e6 436 while (i < (cmdlength - 1))
ShaolinPoutine 6:3b97770f30e6 437 {
ShaolinPoutine 6:3b97770f30e6 438 checksum += cmdtosend[i];
ShaolinPoutine 6:3b97770f30e6 439 i++;
ShaolinPoutine 6:3b97770f30e6 440 }
ShaolinPoutine 6:3b97770f30e6 441 cmdtosend[cmdlength - 1] = 0xFF - checksum;
ShaolinPoutine 6:3b97770f30e6 442
ShaolinPoutine 6:3b97770f30e6 443 // Envoyer la commande sur UART
ShaolinPoutine 6:3b97770f30e6 444 i = 0;
ShaolinPoutine 6:3b97770f30e6 445 while (i < cmdlength)
ShaolinPoutine 6:3b97770f30e6 446 {
ShaolinPoutine 6:3b97770f30e6 447 comm.putc(cmdtosend[i]);
ShaolinPoutine 6:3b97770f30e6 448 i++;
ShaolinPoutine 6:3b97770f30e6 449 }
ShaolinPoutine 6:3b97770f30e6 450 wait(0.1);
ShaolinPoutine 2:0000443a78fe 451 }