Class containing functions usefull to communication between PC and Xbee device
Dependents: Coordinator_node Router_node
xbee.cpp@15:04e892ae9361, 2017-02-14 (annotated)
- Committer:
- ShaolinPoutine
- Date:
- Tue Feb 14 21:05:16 2017 +0000
- Revision:
- 15:04e892ae9361
- Parent:
- 9:902d0f74333c
- Child:
- 16:13ab66e244c3
Added web comm
Who changed what in which revision?
User | Revision | Line number | New 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; |
ShaolinPoutine | 3:4c1dec78117b | 358 | |
ShaolinPoutine | 3:4c1dec78117b | 359 | while(i < 8) |
ShaolinPoutine | 3:4c1dec78117b | 360 | { |
EmileArseneault | 9:902d0f74333c | 361 | adresse64bit[i] = getChar(); |
ShaolinPoutine | 3:4c1dec78117b | 362 | i++; |
ShaolinPoutine | 3:4c1dec78117b | 363 | } |
ShaolinPoutine | 3:4c1dec78117b | 364 | |
EmileArseneault | 9:902d0f74333c | 365 | adresse16bit[0] = getChar(); |
EmileArseneault | 9:902d0f74333c | 366 | adresse16bit[1] = getChar(); |
ShaolinPoutine | 3:4c1dec78117b | 367 | |
EmileArseneault | 9:902d0f74333c | 368 | receiveOptions = getChar(); |
ShaolinPoutine | 3:4c1dec78117b | 369 | |
ShaolinPoutine | 3:4c1dec78117b | 370 | pcPrint("Data received : "); |
ShaolinPoutine | 3:4c1dec78117b | 371 | |
ShaolinPoutine | 3:4c1dec78117b | 372 | i = 11; |
ShaolinPoutine | 3:4c1dec78117b | 373 | while (i < len) |
ShaolinPoutine | 3:4c1dec78117b | 374 | { |
EmileArseneault | 9:902d0f74333c | 375 | printHexa(getChar()); |
ShaolinPoutine | 3:4c1dec78117b | 376 | i++; |
ShaolinPoutine | 3:4c1dec78117b | 377 | } |
EmileArseneault | 5:9b4d93bd6725 | 378 | pcPrint("\r\n"); |
ShaolinPoutine | 3:4c1dec78117b | 379 | |
EmileArseneault | 9:902d0f74333c | 380 | checksum = getChar(); |
ShaolinPoutine | 3:4c1dec78117b | 381 | // Validate checksum TODO |
ShaolinPoutine | 6:3b97770f30e6 | 382 | } |
ShaolinPoutine | 6:3b97770f30e6 | 383 | |
EmileArseneault | 9:902d0f74333c | 384 | void XBee::ZigBeeTransmit(int adresse16, int adresse64msb, int adresse64lsb, char *data, int dataLength) |
ShaolinPoutine | 6:3b97770f30e6 | 385 | { |
ShaolinPoutine | 6:3b97770f30e6 | 386 | // Frame Type 0x10 |
ShaolinPoutine | 6:3b97770f30e6 | 387 | // 0x0000000000000000 - Reserved 64-bit address for the coordinator |
ShaolinPoutine | 6:3b97770f30e6 | 388 | // 0x000000000000FFFF - Broadcast address |
ShaolinPoutine | 6:3b97770f30e6 | 389 | |
ShaolinPoutine | 6:3b97770f30e6 | 390 | // The Transmit Status frame (0x8B) est la reponse |
ShaolinPoutine | 6:3b97770f30e6 | 391 | char cmdtosend[25]; |
ShaolinPoutine | 6:3b97770f30e6 | 392 | char checksum = 0x00; |
ShaolinPoutine | 6:3b97770f30e6 | 393 | int cmdlength = 18; |
ShaolinPoutine | 6:3b97770f30e6 | 394 | int i = 3; |
ShaolinPoutine | 6:3b97770f30e6 | 395 | |
ShaolinPoutine | 6:3b97770f30e6 | 396 | //ID command to set/read operating 64 bit PAN ID |
ShaolinPoutine | 6:3b97770f30e6 | 397 | //WR command to set operating 64 bit PAN ID across reboot |
ShaolinPoutine | 6:3b97770f30e6 | 398 | //OI command to read operating 16 bit PAN ID |
ShaolinPoutine | 6:3b97770f30e6 | 399 | //II command to set operating 16 bit PAN ID |
ShaolinPoutine | 6:3b97770f30e6 | 400 | |
ShaolinPoutine | 6:3b97770f30e6 | 401 | cmdtosend[0] = FRAMEDELIMITER; |
ShaolinPoutine | 6:3b97770f30e6 | 402 | cmdtosend[1] = 0x00; |
ShaolinPoutine | 6:3b97770f30e6 | 403 | cmdtosend[2] = 0x0E + dataLength; |
ShaolinPoutine | 6:3b97770f30e6 | 404 | cmdtosend[3] = 0x10; // Frame type |
ShaolinPoutine | 6:3b97770f30e6 | 405 | cmdtosend[4] = 0x01; // Frame number |
EmileArseneault | 9:902d0f74333c | 406 | cmdtosend[5] = adresse64msb & 0xFF000000; // MSB adresse 64-bit |
EmileArseneault | 9:902d0f74333c | 407 | cmdtosend[6] = adresse64msb & 0x00FF0000; |
EmileArseneault | 9:902d0f74333c | 408 | cmdtosend[7] = adresse64msb & 0x0000FF00; |
EmileArseneault | 9:902d0f74333c | 409 | cmdtosend[8] = adresse64msb & 0x000000FF; |
EmileArseneault | 9:902d0f74333c | 410 | cmdtosend[9] = adresse64msb & 0xFF000000; |
EmileArseneault | 9:902d0f74333c | 411 | cmdtosend[10] = adresse64msb & 0x00FF0000; |
EmileArseneault | 9:902d0f74333c | 412 | cmdtosend[11] = adresse64msb & 0x0000FF00; |
EmileArseneault | 9:902d0f74333c | 413 | cmdtosend[12] = adresse64msb & 0x000000FF; // LSB adresse 64-bit |
ShaolinPoutine | 6:3b97770f30e6 | 414 | cmdtosend[13] = adresse16 >> 16; // MSB adresse 16-bit |
ShaolinPoutine | 6:3b97770f30e6 | 415 | cmdtosend[14] = adresse16 && 0b0000000011111111; // LSB adresse 16-bit |
ShaolinPoutine | 6:3b97770f30e6 | 416 | cmdtosend[15] = 0x00; // Broadcast Radius |
ShaolinPoutine | 6:3b97770f30e6 | 417 | cmdtosend[16] = 0x00; // Options |
ShaolinPoutine | 6:3b97770f30e6 | 418 | |
ShaolinPoutine | 6:3b97770f30e6 | 419 | // Set RF DATA |
ShaolinPoutine | 6:3b97770f30e6 | 420 | if(data != NULL) |
ShaolinPoutine | 6:3b97770f30e6 | 421 | { |
ShaolinPoutine | 6:3b97770f30e6 | 422 | i = 0; |
ShaolinPoutine | 6:3b97770f30e6 | 423 | cmdlength += dataLength; |
ShaolinPoutine | 6:3b97770f30e6 | 424 | |
ShaolinPoutine | 6:3b97770f30e6 | 425 | while (i < dataLength) |
ShaolinPoutine | 6:3b97770f30e6 | 426 | { |
ShaolinPoutine | 6:3b97770f30e6 | 427 | cmdtosend[17 + i] = (data)[i]; |
ShaolinPoutine | 6:3b97770f30e6 | 428 | i++; |
ShaolinPoutine | 6:3b97770f30e6 | 429 | } |
ShaolinPoutine | 6:3b97770f30e6 | 430 | } |
ShaolinPoutine | 6:3b97770f30e6 | 431 | |
ShaolinPoutine | 6:3b97770f30e6 | 432 | // Calculate checksum |
ShaolinPoutine | 6:3b97770f30e6 | 433 | i = 3; |
ShaolinPoutine | 6:3b97770f30e6 | 434 | while (i < (cmdlength - 1)) |
ShaolinPoutine | 6:3b97770f30e6 | 435 | { |
ShaolinPoutine | 6:3b97770f30e6 | 436 | checksum += cmdtosend[i]; |
ShaolinPoutine | 6:3b97770f30e6 | 437 | i++; |
ShaolinPoutine | 6:3b97770f30e6 | 438 | } |
ShaolinPoutine | 6:3b97770f30e6 | 439 | cmdtosend[cmdlength - 1] = 0xFF - checksum; |
ShaolinPoutine | 6:3b97770f30e6 | 440 | |
ShaolinPoutine | 6:3b97770f30e6 | 441 | // Envoyer la commande sur UART |
ShaolinPoutine | 6:3b97770f30e6 | 442 | i = 0; |
ShaolinPoutine | 6:3b97770f30e6 | 443 | while (i < cmdlength) |
ShaolinPoutine | 6:3b97770f30e6 | 444 | { |
ShaolinPoutine | 6:3b97770f30e6 | 445 | comm.putc(cmdtosend[i]); |
ShaolinPoutine | 6:3b97770f30e6 | 446 | i++; |
ShaolinPoutine | 6:3b97770f30e6 | 447 | } |
ShaolinPoutine | 6:3b97770f30e6 | 448 | wait(0.1); |
EmileArseneault | 9:902d0f74333c | 449 | } |
EmileArseneault | 9:902d0f74333c | 450 | |
EmileArseneault | 9:902d0f74333c | 451 | void XBee::BroadcastHelloWorld() |
EmileArseneault | 9:902d0f74333c | 452 | { |
EmileArseneault | 9:902d0f74333c | 453 | char hello[5] = {'H', 'e', 'l', 'l', 'o'}; |
EmileArseneault | 9:902d0f74333c | 454 | char world[5] = {'w', 'o', 'r', 'l', 'd'}; |
EmileArseneault | 9:902d0f74333c | 455 | |
EmileArseneault | 9:902d0f74333c | 456 | while (1) |
EmileArseneault | 9:902d0f74333c | 457 | { |
EmileArseneault | 9:902d0f74333c | 458 | ZigBeeTransmit(0x0000, 0x00000000, 0x00000000, &hello[0], 5); |
EmileArseneault | 9:902d0f74333c | 459 | ZigBeeTransmit(0x0000, 0x00000000, 0x00000000, &world[0], 5); |
EmileArseneault | 9:902d0f74333c | 460 | wait(2); |
EmileArseneault | 9:902d0f74333c | 461 | } |
ShaolinPoutine | 2:0000443a78fe | 462 | } |