APP3 / Zigbee_communication

Dependents:   Coordinator_node Router_node

Committer:
ShaolinPoutine
Date:
Tue Feb 14 21:05:44 2017 +0000
Revision:
16:13ab66e244c3
Parent:
11:7b5b9c1ab757
Parent:
15:04e892ae9361
Child:
17:cb6b423b45cd
Pull back

Who changed what in which revision?

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