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