A libery to connect to telegesis zigbee module. Bassed on implemtation of XBEE
Fork of xbee_lib by
telegesis.cpp@31:c59bc92a047e, 2014-11-28 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Fri Nov 28 10:53:09 2014 +0000
- Revision:
- 31:c59bc92a047e
- Parent:
- 30:f9cdb6f62586
- Child:
- 32:e44448cee1e1
?ndret struktur med ekstra funktioner
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gert_lauritsen | 7:45511c3d2950 | 1 | #include "telegesis.h" |
gert_lauritsen | 14:dcf2390f89e2 | 2 | #define CR 0x0D |
gert_lauritsen | 14:dcf2390f89e2 | 3 | #define LF 0x0A |
gert_lauritsen | 18:10fcddf238c9 | 4 | //#define IRQCtrl |
tristanjph | 0:2656fb225c5d | 5 | |
gert_lauritsen | 18:10fcddf238c9 | 6 | zigbee::zigbee(PinName tx, PinName rx): _zbee(tx, rx) |
gert_lauritsen | 18:10fcddf238c9 | 7 | { |
tristanjph | 0:2656fb225c5d | 8 | _tx = tx; |
tristanjph | 0:2656fb225c5d | 9 | _rx = rx; |
gert_lauritsen | 18:10fcddf238c9 | 10 | _zbee.baud(19200); |
gert_lauritsen | 28:c724a8921f01 | 11 | Ok=1; PacketAck=1; |
gert_lauritsen | 18:10fcddf238c9 | 12 | #ifdef IRQCtrl |
gert_lauritsen | 18:10fcddf238c9 | 13 | _zbee.attach(this, &zigbee::SeePacket, Serial::RxIrq); |
gert_lauritsen | 18:10fcddf238c9 | 14 | #endif |
tristanjph | 0:2656fb225c5d | 15 | } |
tristanjph | 0:2656fb225c5d | 16 | |
gert_lauritsen | 30:f9cdb6f62586 | 17 | size_t zigbee::comma_parse ( char *line, char *list[], size_t size ) |
gert_lauritsen | 30:f9cdb6f62586 | 18 | { |
gert_lauritsen | 30:f9cdb6f62586 | 19 | char *p; |
gert_lauritsen | 30:f9cdb6f62586 | 20 | size_t n; |
gert_lauritsen | 30:f9cdb6f62586 | 21 | |
gert_lauritsen | 30:f9cdb6f62586 | 22 | p = line; |
gert_lauritsen | 30:f9cdb6f62586 | 23 | n = 0; |
gert_lauritsen | 30:f9cdb6f62586 | 24 | for ( ; ; ) { |
gert_lauritsen | 30:f9cdb6f62586 | 25 | /* Ditch leading commas */ |
gert_lauritsen | 30:f9cdb6f62586 | 26 | while ( *p == ',' ) |
gert_lauritsen | 30:f9cdb6f62586 | 27 | p++; |
gert_lauritsen | 30:f9cdb6f62586 | 28 | /* Nothing of use */ |
gert_lauritsen | 30:f9cdb6f62586 | 29 | if ( *p == '\0' ) |
gert_lauritsen | 30:f9cdb6f62586 | 30 | return n; |
gert_lauritsen | 30:f9cdb6f62586 | 31 | /* Save the string */ |
gert_lauritsen | 30:f9cdb6f62586 | 32 | list[n++] = p; |
gert_lauritsen | 30:f9cdb6f62586 | 33 | /* Find the next field */ |
gert_lauritsen | 30:f9cdb6f62586 | 34 | while ( *p != ',' && *p != '\0' ) |
gert_lauritsen | 30:f9cdb6f62586 | 35 | p++; |
gert_lauritsen | 30:f9cdb6f62586 | 36 | /* Nothing else of use or too many fields */ |
gert_lauritsen | 30:f9cdb6f62586 | 37 | if ( *p == '\0' || n >= size ) |
gert_lauritsen | 30:f9cdb6f62586 | 38 | return n; |
gert_lauritsen | 30:f9cdb6f62586 | 39 | /* Split the field */ |
gert_lauritsen | 30:f9cdb6f62586 | 40 | *p++ = '\0'; |
gert_lauritsen | 30:f9cdb6f62586 | 41 | } |
gert_lauritsen | 30:f9cdb6f62586 | 42 | } |
gert_lauritsen | 30:f9cdb6f62586 | 43 | |
gert_lauritsen | 30:f9cdb6f62586 | 44 | |
gert_lauritsen | 7:45511c3d2950 | 45 | zigbee::~zigbee() |
tristanjph | 0:2656fb225c5d | 46 | { |
tristanjph | 0:2656fb225c5d | 47 | } |
tristanjph | 0:2656fb225c5d | 48 | |
gert_lauritsen | 26:2a85af491d92 | 49 | bool zigbee::Work() |
gert_lauritsen | 26:2a85af491d92 | 50 | { |
gert_lauritsen | 24:5ed5311266b0 | 51 | //read and waits for the packet |
gert_lauritsen | 26:2a85af491d92 | 52 | bool datain=false; |
gert_lauritsen | 24:5ed5311266b0 | 53 | b=0; |
gert_lauritsen | 28:c724a8921f01 | 54 | if (_zbee.readable()) { |
gert_lauritsen | 26:2a85af491d92 | 55 | datain=true; |
gert_lauritsen | 26:2a85af491d92 | 56 | SeePacket(); |
gert_lauritsen | 28:c724a8921f01 | 57 | } |
gert_lauritsen | 26:2a85af491d92 | 58 | return datain; |
gert_lauritsen | 18:10fcddf238c9 | 59 | } |
gert_lauritsen | 18:10fcddf238c9 | 60 | |
gert_lauritsen | 20:070dc2050052 | 61 | bool zigbee::wait4str(char *p) //joinpan |
gert_lauritsen | 20:070dc2050052 | 62 | { |
gert_lauritsen | 20:070dc2050052 | 63 | Timer t; |
gert_lauritsen | 20:070dc2050052 | 64 | t.reset(); |
gert_lauritsen | 20:070dc2050052 | 65 | t.start(); |
gert_lauritsen | 25:31120552e53c | 66 | while ((strstr(_responseFrameString,p)==0) && (t.read_ms() < 5000)) readPacket(); |
gert_lauritsen | 26:2a85af491d92 | 67 | t.stop(); |
gert_lauritsen | 26:2a85af491d92 | 68 | return (strstr(_responseFrameString,p)>0); |
gert_lauritsen | 20:070dc2050052 | 69 | } |
gert_lauritsen | 20:070dc2050052 | 70 | |
gert_lauritsen | 20:070dc2050052 | 71 | |
gert_lauritsen | 20:070dc2050052 | 72 | bool zigbee::wait4JPAN() //joinpan |
gert_lauritsen | 20:070dc2050052 | 73 | { |
gert_lauritsen | 20:070dc2050052 | 74 | Timer t; |
gert_lauritsen | 20:070dc2050052 | 75 | t.reset(); |
gert_lauritsen | 20:070dc2050052 | 76 | t.start(); |
gert_lauritsen | 20:070dc2050052 | 77 | readPacket(); |
gert_lauritsen | 25:31120552e53c | 78 | while ((strstr(_responseFrameString,"JPAN")==0) && (t.read_ms() < 5000)) readPacket(); |
gert_lauritsen | 26:2a85af491d92 | 79 | t.stop(); |
gert_lauritsen | 26:2a85af491d92 | 80 | return (strstr(_responseFrameString,"JPAN")>0); |
gert_lauritsen | 20:070dc2050052 | 81 | } |
gert_lauritsen | 20:070dc2050052 | 82 | |
gert_lauritsen | 20:070dc2050052 | 83 | |
gert_lauritsen | 20:070dc2050052 | 84 | bool zigbee::wait4Offline() |
gert_lauritsen | 20:070dc2050052 | 85 | { |
gert_lauritsen | 20:070dc2050052 | 86 | Timer t; |
gert_lauritsen | 20:070dc2050052 | 87 | t.reset(); |
gert_lauritsen | 20:070dc2050052 | 88 | t.start(); |
gert_lauritsen | 20:070dc2050052 | 89 | readPacket(); |
gert_lauritsen | 25:31120552e53c | 90 | while ((PanOnline>0) && (t.read_ms() < 3000)) readPacket(); |
gert_lauritsen | 26:2a85af491d92 | 91 | t.stop(); |
gert_lauritsen | 26:2a85af491d92 | 92 | return PanOnline; |
gert_lauritsen | 20:070dc2050052 | 93 | } |
gert_lauritsen | 20:070dc2050052 | 94 | |
gert_lauritsen | 20:070dc2050052 | 95 | |
gert_lauritsen | 18:10fcddf238c9 | 96 | bool zigbee::wait4OK() |
gert_lauritsen | 18:10fcddf238c9 | 97 | { |
gert_lauritsen | 12:debf76f0c0bf | 98 | Timer t; |
gert_lauritsen | 12:debf76f0c0bf | 99 | t.reset(); |
gert_lauritsen | 18:10fcddf238c9 | 100 | t.start(); |
gert_lauritsen | 11:18ff088287ea | 101 | readPacket(); |
gert_lauritsen | 25:31120552e53c | 102 | while ((strstr(_responseFrameString,"OK")==0) && (t.read_ms() < 500)) readPacket(); |
gert_lauritsen | 26:2a85af491d92 | 103 | t.stop(); |
gert_lauritsen | 26:2a85af491d92 | 104 | return (strstr(_responseFrameString,"OK")>0); |
gert_lauritsen | 11:18ff088287ea | 105 | } |
tristanjph | 0:2656fb225c5d | 106 | |
gert_lauritsen | 11:18ff088287ea | 107 | int zigbee::GetSerial() |
gert_lauritsen | 18:10fcddf238c9 | 108 | { |
gert_lauritsen | 11:18ff088287ea | 109 | /** comes with something like this |
gert_lauritsen | 18:10fcddf238c9 | 110 | Telegesis ETRX357-LRS |
gert_lauritsen | 18:10fcddf238c9 | 111 | R305C |
gert_lauritsen | 18:10fcddf238c9 | 112 | 000D6F0000D5F06A |
gert_lauritsen | 18:10fcddf238c9 | 113 | OK |
gert_lauritsen | 18:10fcddf238c9 | 114 | */ |
gert_lauritsen | 11:18ff088287ea | 115 | _zbee.printf("ATI\r"); |
gert_lauritsen | 22:e8a8edf8b45c | 116 | wait4str("Telegesis"); |
gert_lauritsen | 11:18ff088287ea | 117 | sscanf(_responseFrameString,"Telegesis %s",HWType); |
gert_lauritsen | 27:a38b67f5be8f | 118 | readPacket(); //Typen |
gert_lauritsen | 27:a38b67f5be8f | 119 | readPacket(); //ID |
gert_lauritsen | 18:10fcddf238c9 | 120 | strcpy(LocalID,_responseFrameString); //vi lægger den bare over i stringformat. Så kan man altid senere convertere |
gert_lauritsen | 26:2a85af491d92 | 121 | wait4OK(); |
gert_lauritsen | 18:10fcddf238c9 | 122 | // LocalID=hextoint(_responseFrameString); |
gert_lauritsen | 18:10fcddf238c9 | 123 | // wait_ms(5); |
tristanjph | 0:2656fb225c5d | 124 | return 1; |
tristanjph | 0:2656fb225c5d | 125 | } |
tristanjph | 0:2656fb225c5d | 126 | |
gert_lauritsen | 18:10fcddf238c9 | 127 | int zigbee::SetKey(char* key) |
gert_lauritsen | 18:10fcddf238c9 | 128 | { |
gert_lauritsen | 18:10fcddf238c9 | 129 | /** |
gert_lauritsen | 18:10fcddf238c9 | 130 | S09: the link key. Write the same 128-bit number (32 hexadecimal characters) into every device |
gert_lauritsen | 15:8d4990362a80 | 131 | |
gert_lauritsen | 18:10fcddf238c9 | 132 | S0A: security configuration. You must set bit 4 in the coordinator, and set bit 8 in all other devices. |
gert_lauritsen | 18:10fcddf238c9 | 133 | For more security set bit 2 also in the coordinator. The devices ignore the bits that are not relevant |
gert_lauritsen | 18:10fcddf238c9 | 134 | for them, so it is easiest to just set bits 8, 4, and 2 in all devices which gives S0A=0114. |
gert_lauritsen | 18:10fcddf238c9 | 135 | */ |
gert_lauritsen | 18:10fcddf238c9 | 136 | char psw[50]; |
gert_lauritsen | 15:8d4990362a80 | 137 | sprintf(psw,"ATS09=%s;password\r",key); |
gert_lauritsen | 15:8d4990362a80 | 138 | _zbee.printf(psw); |
gert_lauritsen | 18:10fcddf238c9 | 139 | wait4OK(); |
gert_lauritsen | 15:8d4990362a80 | 140 | _zbee.printf("ATS0A=0114;password\r"); |
tristanjph | 0:2656fb225c5d | 141 | return 1; |
tristanjph | 0:2656fb225c5d | 142 | } |
tristanjph | 0:2656fb225c5d | 143 | |
tristanjph | 0:2656fb225c5d | 144 | |
tristanjph | 0:2656fb225c5d | 145 | |
gert_lauritsen | 7:45511c3d2950 | 146 | void zigbee::RecieveData(char *data_buf, int numchar) |
tristanjph | 1:c3d9bdcb0b03 | 147 | { |
tristanjph | 1:c3d9bdcb0b03 | 148 | int count=0; |
tristanjph | 2:cb627ea9b817 | 149 | if(numchar == 0) { |
tristanjph | 2:cb627ea9b817 | 150 | numchar = sizeof(data_buf); |
tristanjph | 2:cb627ea9b817 | 151 | } |
gert_lauritsen | 18:10fcddf238c9 | 152 | |
tristanjph | 1:c3d9bdcb0b03 | 153 | while(numchar!=count) { |
gert_lauritsen | 11:18ff088287ea | 154 | if(_zbee.readable()) { |
gert_lauritsen | 11:18ff088287ea | 155 | *data_buf = _zbee.getc(); |
tristanjph | 3:682615a0717e | 156 | data_buf+=1; |
tristanjph | 3:682615a0717e | 157 | count++; |
tristanjph | 1:c3d9bdcb0b03 | 158 | } |
tristanjph | 1:c3d9bdcb0b03 | 159 | |
tristanjph | 1:c3d9bdcb0b03 | 160 | } |
tristanjph | 2:cb627ea9b817 | 161 | } |
tristanjph | 1:c3d9bdcb0b03 | 162 | |
gert_lauritsen | 12:debf76f0c0bf | 163 | int zigbee::Reset() |
gert_lauritsen | 12:debf76f0c0bf | 164 | { |
gert_lauritsen | 28:c724a8921f01 | 165 | PacketAck=0; |
gert_lauritsen | 12:debf76f0c0bf | 166 | _zbee.printf("ATZ\r"); |
gert_lauritsen | 12:debf76f0c0bf | 167 | wait4OK(); |
gert_lauritsen | 28:c724a8921f01 | 168 | |
gert_lauritsen | 12:debf76f0c0bf | 169 | return 1; |
gert_lauritsen | 12:debf76f0c0bf | 170 | } |
gert_lauritsen | 12:debf76f0c0bf | 171 | |
gert_lauritsen | 7:45511c3d2950 | 172 | int zigbee::ATI() |
gert_lauritsen | 7:45511c3d2950 | 173 | { |
gert_lauritsen | 28:c724a8921f01 | 174 | PacketAck=0; |
gert_lauritsen | 12:debf76f0c0bf | 175 | GetSerial(); |
gert_lauritsen | 7:45511c3d2950 | 176 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 177 | } |
gert_lauritsen | 7:45511c3d2950 | 178 | |
gert_lauritsen | 7:45511c3d2950 | 179 | |
gert_lauritsen | 7:45511c3d2950 | 180 | int zigbee::PingOut() |
gert_lauritsen | 18:10fcddf238c9 | 181 | { |
gert_lauritsen | 18:10fcddf238c9 | 182 | //just return a OK (sends it ID out out the pan) |
gert_lauritsen | 28:c724a8921f01 | 183 | PacketAck=0; |
gert_lauritsen | 11:18ff088287ea | 184 | _zbee.printf("AT+ANNCE\r"); |
gert_lauritsen | 12:debf76f0c0bf | 185 | wait4OK(); |
gert_lauritsen | 7:45511c3d2950 | 186 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 187 | } |
gert_lauritsen | 7:45511c3d2950 | 188 | |
gert_lauritsen | 7:45511c3d2950 | 189 | |
gert_lauritsen | 7:45511c3d2950 | 190 | int zigbee::PanScan() |
gert_lauritsen | 18:10fcddf238c9 | 191 | { |
gert_lauritsen | 18:10fcddf238c9 | 192 | // |
gert_lauritsen | 31:c59bc92a047e | 193 | PacketAck=0; NPanVisible=0; |
gert_lauritsen | 11:18ff088287ea | 194 | _zbee.printf("AT+PANSCAN\r"); |
gert_lauritsen | 30:f9cdb6f62586 | 195 | wait4OK(); |
gert_lauritsen | 7:45511c3d2950 | 196 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 197 | } |
gert_lauritsen | 7:45511c3d2950 | 198 | |
gert_lauritsen | 7:45511c3d2950 | 199 | |
gert_lauritsen | 7:45511c3d2950 | 200 | int zigbee::Establish_Network() |
gert_lauritsen | 18:10fcddf238c9 | 201 | { |
gert_lauritsen | 18:10fcddf238c9 | 202 | // |
gert_lauritsen | 28:c724a8921f01 | 203 | PacketAck=0; |
gert_lauritsen | 11:18ff088287ea | 204 | _zbee.printf("AT+EN\r"); |
gert_lauritsen | 26:2a85af491d92 | 205 | wait4JPAN(); |
gert_lauritsen | 26:2a85af491d92 | 206 | wait4OK(); |
tristanjph | 2:cb627ea9b817 | 207 | return 1; |
tristanjph | 2:cb627ea9b817 | 208 | } |
tristanjph | 3:682615a0717e | 209 | |
gert_lauritsen | 7:45511c3d2950 | 210 | |
gert_lauritsen | 20:070dc2050052 | 211 | int zigbee::LeaveNetwork() |
gert_lauritsen | 20:070dc2050052 | 212 | { |
gert_lauritsen | 28:c724a8921f01 | 213 | PacketAck=0; |
gert_lauritsen | 20:070dc2050052 | 214 | _zbee.printf("AT+DASSL\r"); |
gert_lauritsen | 26:2a85af491d92 | 215 | wait4OK(); |
gert_lauritsen | 26:2a85af491d92 | 216 | wait4Offline(); |
gert_lauritsen | 20:070dc2050052 | 217 | return 1; |
gert_lauritsen | 20:070dc2050052 | 218 | } |
gert_lauritsen | 20:070dc2050052 | 219 | |
gert_lauritsen | 7:45511c3d2950 | 220 | int zigbee::JoinNetwork() |
gert_lauritsen | 7:45511c3d2950 | 221 | { |
gert_lauritsen | 28:c724a8921f01 | 222 | PacketAck=0; |
gert_lauritsen | 11:18ff088287ea | 223 | _zbee.printf("AT+JN\r"); |
gert_lauritsen | 28:c724a8921f01 | 224 | Ok=0; |
gert_lauritsen | 7:45511c3d2950 | 225 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 226 | } |
gert_lauritsen | 7:45511c3d2950 | 227 | |
gert_lauritsen | 7:45511c3d2950 | 228 | int zigbee::ScanNetwork() |
gert_lauritsen | 7:45511c3d2950 | 229 | { |
gert_lauritsen | 28:c724a8921f01 | 230 | PacketAck=0; |
gert_lauritsen | 11:18ff088287ea | 231 | _zbee.printf("AT+SN\r"); |
gert_lauritsen | 28:c724a8921f01 | 232 | Ok=0; |
gert_lauritsen | 28:c724a8921f01 | 233 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 234 | } |
gert_lauritsen | 7:45511c3d2950 | 235 | |
gert_lauritsen | 7:45511c3d2950 | 236 | int zigbee::NetworkInfo() |
gert_lauritsen | 7:45511c3d2950 | 237 | { |
gert_lauritsen | 7:45511c3d2950 | 238 | //Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522" |
gert_lauritsen | 28:c724a8921f01 | 239 | PacketAck=0; |
gert_lauritsen | 11:18ff088287ea | 240 | _zbee.printf("AT+N\r"); |
gert_lauritsen | 28:c724a8921f01 | 241 | Ok=wait4OK(); |
gert_lauritsen | 7:45511c3d2950 | 242 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 243 | } |
gert_lauritsen | 7:45511c3d2950 | 244 | |
gert_lauritsen | 9:c8e4339ccc29 | 245 | int zigbee::UniCast(char *adr,char *payload) //Ascii mode with null terminated string |
gert_lauritsen | 7:45511c3d2950 | 246 | { |
gert_lauritsen | 28:c724a8921f01 | 247 | PacketAck=0; |
gert_lauritsen | 11:18ff088287ea | 248 | _zbee.printf("AT+UCAST:%s=%s\r",adr,payload); |
gert_lauritsen | 26:2a85af491d92 | 249 | // printf("AT+UCAST:%s=%s\r",adr,payload); |
gert_lauritsen | 12:debf76f0c0bf | 250 | // _zbee.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata); |
gert_lauritsen | 28:c724a8921f01 | 251 | Ok=0; |
gert_lauritsen | 7:45511c3d2950 | 252 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 253 | } |
gert_lauritsen | 7:45511c3d2950 | 254 | |
gert_lauritsen | 9:c8e4339ccc29 | 255 | int zigbee::UniCastb(char *adr,char *payload, char payloadSize) //sends data in binary format |
gert_lauritsen | 7:45511c3d2950 | 256 | { |
gert_lauritsen | 28:c724a8921f01 | 257 | PacketAck=0; |
gert_lauritsen | 11:18ff088287ea | 258 | _zbee.printf("AT+UCASTB:%X,%s\r",adr,payloadSize); |
gert_lauritsen | 12:debf76f0c0bf | 259 | //_zbee.scanf ("%*s"); |
gert_lauritsen | 7:45511c3d2950 | 260 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 261 | } |
gert_lauritsen | 7:45511c3d2950 | 262 | |
gert_lauritsen | 12:debf76f0c0bf | 263 | //-----------------------Handle incoming data------------------------------ |
tristanjph | 3:682615a0717e | 264 | |
gert_lauritsen | 11:18ff088287ea | 265 | unsigned long zigbee::hextolong(const char *hex) |
gert_lauritsen | 7:45511c3d2950 | 266 | { |
gert_lauritsen | 7:45511c3d2950 | 267 | //return 32 bit |
gert_lauritsen | 7:45511c3d2950 | 268 | unsigned long result = 0; |
gert_lauritsen | 7:45511c3d2950 | 269 | while (*hex) { //så længe det ikke er null |
gert_lauritsen | 7:45511c3d2950 | 270 | if (*hex >= '0' && *hex <= '9') |
gert_lauritsen | 7:45511c3d2950 | 271 | result += (*hex - '0'); |
gert_lauritsen | 7:45511c3d2950 | 272 | else if (*hex >= 'A' && *hex <= 'F') |
gert_lauritsen | 7:45511c3d2950 | 273 | result += (*hex - 'A' +10); |
gert_lauritsen | 7:45511c3d2950 | 274 | else if (*hex >= 'a' && *hex <= 'f') |
gert_lauritsen | 7:45511c3d2950 | 275 | result += (*hex - 'a'+ 10); |
gert_lauritsen | 7:45511c3d2950 | 276 | |
gert_lauritsen | 12:debf76f0c0bf | 277 | if (*++hex) //if the neext isn't a null |
gert_lauritsen | 7:45511c3d2950 | 278 | result <<= 4; |
gert_lauritsen | 7:45511c3d2950 | 279 | } |
gert_lauritsen | 7:45511c3d2950 | 280 | return result; |
gert_lauritsen | 7:45511c3d2950 | 281 | } |
gert_lauritsen | 7:45511c3d2950 | 282 | |
gert_lauritsen | 7:45511c3d2950 | 283 | |
gert_lauritsen | 11:18ff088287ea | 284 | unsigned int zigbee::hextoint(const char *hex) |
gert_lauritsen | 7:45511c3d2950 | 285 | { |
gert_lauritsen | 7:45511c3d2950 | 286 | //return 16 bit |
gert_lauritsen | 7:45511c3d2950 | 287 | unsigned int result = 0; |
gert_lauritsen | 7:45511c3d2950 | 288 | while (*hex) { //så længe det ikke er null |
gert_lauritsen | 7:45511c3d2950 | 289 | if (*hex >= '0' && *hex <= '9') |
gert_lauritsen | 7:45511c3d2950 | 290 | result += (*hex - '0'); |
gert_lauritsen | 7:45511c3d2950 | 291 | else if (*hex >= 'A' && *hex <= 'F') |
gert_lauritsen | 7:45511c3d2950 | 292 | result += (*hex - 'A' +10); |
gert_lauritsen | 7:45511c3d2950 | 293 | else if (*hex >= 'a' && *hex <= 'f') |
gert_lauritsen | 7:45511c3d2950 | 294 | result += (*hex - 'a'+ 10); |
gert_lauritsen | 7:45511c3d2950 | 295 | |
gert_lauritsen | 12:debf76f0c0bf | 296 | if (*++hex) //if the neext isn't a null |
gert_lauritsen | 7:45511c3d2950 | 297 | result <<= 4; |
gert_lauritsen | 7:45511c3d2950 | 298 | } |
gert_lauritsen | 7:45511c3d2950 | 299 | return result; |
gert_lauritsen | 7:45511c3d2950 | 300 | } |
gert_lauritsen | 7:45511c3d2950 | 301 | //--------------------------------------------------------------- |
gert_lauritsen | 7:45511c3d2950 | 302 | |
gert_lauritsen | 11:18ff088287ea | 303 | |
gert_lauritsen | 18:10fcddf238c9 | 304 | void zigbee::SeePacket() |
gert_lauritsen | 18:10fcddf238c9 | 305 | { |
gert_lauritsen | 18:10fcddf238c9 | 306 | char *p; |
gert_lauritsen | 18:10fcddf238c9 | 307 | static uint8_t _pos; |
gert_lauritsen | 12:debf76f0c0bf | 308 | b = _zbee.getc(); |
gert_lauritsen | 26:2a85af491d92 | 309 | |
gert_lauritsen | 26:2a85af491d92 | 310 | // JHL : Validate that the incoming character is an ASCII character or CR/LF |
gert_lauritsen | 27:a38b67f5be8f | 311 | /*switch (b) { |
gert_lauritsen | 26:2a85af491d92 | 312 | case CR: |
gert_lauritsen | 26:2a85af491d92 | 313 | case LF: |
gert_lauritsen | 26:2a85af491d92 | 314 | case ','...'}': |
gert_lauritsen | 26:2a85af491d92 | 315 | break; |
gert_lauritsen | 26:2a85af491d92 | 316 | default: |
gert_lauritsen | 26:2a85af491d92 | 317 | // Non ASCII charater received! |
gert_lauritsen | 27:a38b67f5be8f | 318 | // printf("Non ASCII! %d\n\r", b); |
gert_lauritsen | 27:a38b67f5be8f | 319 | // _zbee.send_break(); |
gert_lauritsen | 26:2a85af491d92 | 320 | return; |
gert_lauritsen | 27:a38b67f5be8f | 321 | } */ |
gert_lauritsen | 26:2a85af491d92 | 322 | |
gert_lauritsen | 26:2a85af491d92 | 323 | if ((b!=CR) && (b!=LF)) { |
gert_lauritsen | 26:2a85af491d92 | 324 | _responseFrameString[_pos]=b; |
gert_lauritsen | 26:2a85af491d92 | 325 | _pos=(_pos+1) % MAX_FRAME_DATA_SIZE; |
gert_lauritsen | 26:2a85af491d92 | 326 | } |
gert_lauritsen | 12:debf76f0c0bf | 327 | if (b==CR) { |
gert_lauritsen | 18:10fcddf238c9 | 328 | // memcpy(&_responseFrameString,&_responseFrameData,_pos); |
gert_lauritsen | 18:10fcddf238c9 | 329 | GotFrame=1; |
gert_lauritsen | 18:10fcddf238c9 | 330 | _responseFrameString[_pos]=0; //Nul terminate |
gert_lauritsen | 18:10fcddf238c9 | 331 | _pos=0; |
gert_lauritsen | 29:6711180763b6 | 332 | // printf("%s \r\n",_responseFrameString); |
gert_lauritsen | 18:10fcddf238c9 | 333 | if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something |
gert_lauritsen | 18:10fcddf238c9 | 334 | //Do something |
gert_lauritsen | 18:10fcddf238c9 | 335 | } else if (strstr(_responseFrameString,"UCAST:")) { //checke for incoming UCAST data |
gert_lauritsen | 18:10fcddf238c9 | 336 | //if (sscanf (_responseFrameString,"UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata)>0) |
gert_lauritsen | 26:2a85af491d92 | 337 | p=strstr(_responseFrameString,"UCAST:"); |
gert_lauritsen | 21:9c5b72a64495 | 338 | p=p+6; |
gert_lauritsen | 29:6711180763b6 | 339 | strncpy(In.ID,p,16); |
gert_lauritsen | 29:6711180763b6 | 340 | In.ID[16]=0; |
gert_lauritsen | 29:6711180763b6 | 341 | //Zdat=1; |
gert_lauritsen | 18:10fcddf238c9 | 342 | p=strstr(_responseFrameString,"="); |
gert_lauritsen | 18:10fcddf238c9 | 343 | if (p) { |
gert_lauritsen | 18:10fcddf238c9 | 344 | p++; |
gert_lauritsen | 29:6711180763b6 | 345 | strcpy(In.Data,p); |
gert_lauritsen | 29:6711180763b6 | 346 | In.Ready=1; |
gert_lauritsen | 18:10fcddf238c9 | 347 | } |
gert_lauritsen | 18:10fcddf238c9 | 348 | } |
gert_lauritsen | 30:f9cdb6f62586 | 349 | if (strstr(_responseFrameString,"+PANSCAN:")) { //info om nærliggende pan |
gert_lauritsen | 31:c59bc92a047e | 350 | p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer |
gert_lauritsen | 31:c59bc92a047e | 351 | comma_parse(p,list,ZdataSize); |
gert_lauritsen | 31:c59bc92a047e | 352 | PanVisible[NPanVisible].channel=atoi(list[0]); |
gert_lauritsen | 31:c59bc92a047e | 353 | strcpy(PanVisible[NPanVisible].PID,list[1]); |
gert_lauritsen | 31:c59bc92a047e | 354 | NPanVisible++; |
gert_lauritsen | 30:f9cdb6f62586 | 355 | } |
gert_lauritsen | 31:c59bc92a047e | 356 | if (strstr(_responseFrameString,"LeftPAN")) PanOnline=0; //Local node has left the Pan |
gert_lauritsen | 31:c59bc92a047e | 357 | if (strstr(_responseFrameString,"LostPAN")) PanOnline=0; //Local node has left the Pan |
gert_lauritsen | 18:10fcddf238c9 | 358 | if (strstr(_responseFrameString,"ERROR:")) { |
gert_lauritsen | 18:10fcddf238c9 | 359 | p=strstr(_responseFrameString,":"); |
gert_lauritsen | 18:10fcddf238c9 | 360 | p++; //P indholder nu SEQ nummer |
gert_lauritsen | 17:5a3f81b10a61 | 361 | ErrorCode=hextoint(p); |
gert_lauritsen | 18:10fcddf238c9 | 362 | } |
gert_lauritsen | 18:10fcddf238c9 | 363 | |
gert_lauritsen | 18:10fcddf238c9 | 364 | if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID> |
gert_lauritsen | 18:10fcddf238c9 | 365 | //new node on the pan |
gert_lauritsen | 20:070dc2050052 | 366 | p=strstr(_responseFrameString,"NEWNODE:"); |
gert_lauritsen | 26:2a85af491d92 | 367 | p=p+13; |
gert_lauritsen | 26:2a85af491d92 | 368 | NetInfo=1; |
gert_lauritsen | 26:2a85af491d92 | 369 | strncpy(NetNodeID,p,16); |
gert_lauritsen | 18:10fcddf238c9 | 370 | } |
gert_lauritsen | 11:18ff088287ea | 371 | |
gert_lauritsen | 25:31120552e53c | 372 | if ((strstr(_responseFrameString,"OK")>0) && (strstr(_responseFrameString,"TOKDUMP")==0) ) { |
gert_lauritsen | 18:10fcddf238c9 | 373 | //if (ScriptState) ConnectScript(); |
gert_lauritsen | 28:c724a8921f01 | 374 | Ok=1; |
gert_lauritsen | 28:c724a8921f01 | 375 | //Cmd=0; LineNo=0; |
gert_lauritsen | 18:10fcddf238c9 | 376 | } |
gert_lauritsen | 20:070dc2050052 | 377 | if (strstr(_responseFrameString,"FFD:")) { |
gert_lauritsen | 20:070dc2050052 | 378 | //FFD:000D6F00026C5BEA,0000 |
gert_lauritsen | 20:070dc2050052 | 379 | p=strstr(_responseFrameString,"FFD:"); |
gert_lauritsen | 26:2a85af491d92 | 380 | p=p+4; |
gert_lauritsen | 26:2a85af491d92 | 381 | NetInfo=1; |
gert_lauritsen | 27:a38b67f5be8f | 382 | strncpy(NetNodeID,p,16); |
gert_lauritsen | 26:2a85af491d92 | 383 | } |
gert_lauritsen | 27:a38b67f5be8f | 384 | if (strstr(_responseFrameString,"N=")) { |
gert_lauritsen | 26:2a85af491d92 | 385 | // sscanf (_responseFrameString,"+N=%s,%d,%d,%4X,",Devicetype,&channel,&NodeID,&EPID); |
gert_lauritsen | 30:f9cdb6f62586 | 386 | size_t n; |
gert_lauritsen | 27:a38b67f5be8f | 387 | p=strstr(_responseFrameString,"="); |
gert_lauritsen | 27:a38b67f5be8f | 388 | p++; |
gert_lauritsen | 30:f9cdb6f62586 | 389 | n=comma_parse(p,list,ZdataSize); |
gert_lauritsen | 30:f9cdb6f62586 | 390 | strcpy(Devicetype,list[0]); |
gert_lauritsen | 31:c59bc92a047e | 391 | AktuelPan.channel=atoi(list[1]); |
gert_lauritsen | 31:c59bc92a047e | 392 | strcpy(AktuelPan.PID,list[3]); |
gert_lauritsen | 18:10fcddf238c9 | 393 | } |
gert_lauritsen | 18:10fcddf238c9 | 394 | if (strstr(_responseFrameString,"ACK")) { // |
gert_lauritsen | 26:2a85af491d92 | 395 | PacketAck=1; |
gert_lauritsen | 18:10fcddf238c9 | 396 | p=strstr(_responseFrameString,":"); |
gert_lauritsen | 18:10fcddf238c9 | 397 | p++; //P indholder nu SEQ nummer |
gert_lauritsen | 12:debf76f0c0bf | 398 | SeqNumber=hextoint(p); |
gert_lauritsen | 18:10fcddf238c9 | 399 | } |
gert_lauritsen | 18:10fcddf238c9 | 400 | if (strstr(_responseFrameString,"NAK")) { |
gert_lauritsen | 18:10fcddf238c9 | 401 | PacketAck=0; |
gert_lauritsen | 18:10fcddf238c9 | 402 | p=strstr(_responseFrameString,":"); |
gert_lauritsen | 18:10fcddf238c9 | 403 | p++; //P indholder nu SEQ nummer |
gert_lauritsen | 18:10fcddf238c9 | 404 | SeqNumber=hextoint(p); |
gert_lauritsen | 18:10fcddf238c9 | 405 | } |
gert_lauritsen | 18:10fcddf238c9 | 406 | if (strstr(_responseFrameString,"SEQ")) { // |
gert_lauritsen | 26:2a85af491d92 | 407 | // PacketAck=1; |
gert_lauritsen | 18:10fcddf238c9 | 408 | p=strstr(_responseFrameString,":"); |
gert_lauritsen | 18:10fcddf238c9 | 409 | p++; //P indholder nu SEQ nummer |
gert_lauritsen | 12:debf76f0c0bf | 410 | SeqNumber=hextoint(p); |
gert_lauritsen | 18:10fcddf238c9 | 411 | } |
gert_lauritsen | 31:c59bc92a047e | 412 | if (strstr(_responseFrameString,"JPAN:")) { |
gert_lauritsen | 31:c59bc92a047e | 413 | //JPAN:<channel>,<PID>,<EPID> |
gert_lauritsen | 31:c59bc92a047e | 414 | p=strstr(_responseFrameString,":"); p++; |
gert_lauritsen | 31:c59bc92a047e | 415 | comma_parse(p,list,ZdataSize); |
gert_lauritsen | 31:c59bc92a047e | 416 | strcpy(Devicetype,list[0]); |
gert_lauritsen | 31:c59bc92a047e | 417 | AktuelPan.channel=atoi(list[1]); |
gert_lauritsen | 31:c59bc92a047e | 418 | strcpy(AktuelPan.PID,list[2]); |
gert_lauritsen | 31:c59bc92a047e | 419 | PanOnline=1; |
gert_lauritsen | 31:c59bc92a047e | 420 | } |
gert_lauritsen | 31:c59bc92a047e | 421 | } |
gert_lauritsen | 11:18ff088287ea | 422 | } |
gert_lauritsen | 18:10fcddf238c9 | 423 | |
gert_lauritsen | 18:10fcddf238c9 | 424 | //-----------------------NON IRQ mode -------------------------------------------------------------------- |
gert_lauritsen | 18:10fcddf238c9 | 425 | void zigbee::readPacket() |
gert_lauritsen | 18:10fcddf238c9 | 426 | { |
gert_lauritsen | 18:10fcddf238c9 | 427 | //read and waits for the packet |
gert_lauritsen | 19:78f44233a12f | 428 | Timer t; |
gert_lauritsen | 19:78f44233a12f | 429 | t.reset(); |
gert_lauritsen | 26:2a85af491d92 | 430 | t.start(); |
gert_lauritsen | 19:78f44233a12f | 431 | b=0; |
gert_lauritsen | 25:31120552e53c | 432 | while((b!=CR) && (t.read_ms() < 1000)) |
gert_lauritsen | 18:10fcddf238c9 | 433 | if (_zbee.readable()) SeePacket(); |
gert_lauritsen | 26:2a85af491d92 | 434 | t.stop(); |
gert_lauritsen | 18:10fcddf238c9 | 435 | } |
gert_lauritsen | 18:10fcddf238c9 | 436 | |
gert_lauritsen | 31:c59bc92a047e | 437 | |
gert_lauritsen | 29:6711180763b6 | 438 | |
gert_lauritsen | 29:6711180763b6 | 439 | |
gert_lauritsen | 29:6711180763b6 | 440 | void zigbee::SletZdata(void){ |
gert_lauritsen | 29:6711180763b6 | 441 | int i; |
gert_lauritsen | 29:6711180763b6 | 442 | for (i=0; i<ZdataSize ; i++) In.Data[i]=0; |
gert_lauritsen | 31:c59bc92a047e | 443 | } |
gert_lauritsen | 31:c59bc92a047e | 444 | |
gert_lauritsen | 31:c59bc92a047e | 445 |