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