A libery to connect to telegesis zigbee module. Bassed on implemtation of XBEE
Fork of xbee_lib by
telegesis.cpp@30:f9cdb6f62586, 2014-11-28 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Fri Nov 28 09:28:26 2014 +0000
- Revision:
- 30:f9cdb6f62586
- Parent:
- 29:6711180763b6
- Child:
- 31:c59bc92a047e
Added Join spec pan
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 | 28:c724a8921f01 | 193 | PacketAck=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 | 18:10fcddf238c9 | 302 | #ifdef IRQCtrl |
gert_lauritsen | 18:10fcddf238c9 | 303 | void zigbee::readPacket() |
gert_lauritsen | 18:10fcddf238c9 | 304 | { |
gert_lauritsen | 11:18ff088287ea | 305 | //read and waits for the packet |
gert_lauritsen | 12:debf76f0c0bf | 306 | Timer t; |
gert_lauritsen | 12:debf76f0c0bf | 307 | t.reset(); |
gert_lauritsen | 18:10fcddf238c9 | 308 | t.start(); |
gert_lauritsen | 25:31120552e53c | 309 | while ((GotFrame==0) && (t.read_ms() < 500)) {} |
gert_lauritsen | 18:10fcddf238c9 | 310 | t.stop(); |
gert_lauritsen | 18:10fcddf238c9 | 311 | GotFrame=0; |
gert_lauritsen | 18:10fcddf238c9 | 312 | } |
gert_lauritsen | 7:45511c3d2950 | 313 | |
gert_lauritsen | 18:10fcddf238c9 | 314 | #endif |
gert_lauritsen | 11:18ff088287ea | 315 | |
gert_lauritsen | 18:10fcddf238c9 | 316 | void zigbee::SeePacket() |
gert_lauritsen | 18:10fcddf238c9 | 317 | { |
gert_lauritsen | 18:10fcddf238c9 | 318 | char *p; |
gert_lauritsen | 18:10fcddf238c9 | 319 | static uint8_t _pos; |
gert_lauritsen | 12:debf76f0c0bf | 320 | b = _zbee.getc(); |
gert_lauritsen | 26:2a85af491d92 | 321 | |
gert_lauritsen | 26:2a85af491d92 | 322 | // JHL : Validate that the incoming character is an ASCII character or CR/LF |
gert_lauritsen | 27:a38b67f5be8f | 323 | /*switch (b) { |
gert_lauritsen | 26:2a85af491d92 | 324 | case CR: |
gert_lauritsen | 26:2a85af491d92 | 325 | case LF: |
gert_lauritsen | 26:2a85af491d92 | 326 | case ','...'}': |
gert_lauritsen | 26:2a85af491d92 | 327 | break; |
gert_lauritsen | 26:2a85af491d92 | 328 | default: |
gert_lauritsen | 26:2a85af491d92 | 329 | // Non ASCII charater received! |
gert_lauritsen | 27:a38b67f5be8f | 330 | // printf("Non ASCII! %d\n\r", b); |
gert_lauritsen | 27:a38b67f5be8f | 331 | // _zbee.send_break(); |
gert_lauritsen | 26:2a85af491d92 | 332 | return; |
gert_lauritsen | 27:a38b67f5be8f | 333 | } */ |
gert_lauritsen | 26:2a85af491d92 | 334 | |
gert_lauritsen | 26:2a85af491d92 | 335 | if ((b!=CR) && (b!=LF)) { |
gert_lauritsen | 26:2a85af491d92 | 336 | _responseFrameString[_pos]=b; |
gert_lauritsen | 26:2a85af491d92 | 337 | _pos=(_pos+1) % MAX_FRAME_DATA_SIZE; |
gert_lauritsen | 26:2a85af491d92 | 338 | } |
gert_lauritsen | 12:debf76f0c0bf | 339 | if (b==CR) { |
gert_lauritsen | 18:10fcddf238c9 | 340 | // memcpy(&_responseFrameString,&_responseFrameData,_pos); |
gert_lauritsen | 18:10fcddf238c9 | 341 | GotFrame=1; |
gert_lauritsen | 18:10fcddf238c9 | 342 | _responseFrameString[_pos]=0; //Nul terminate |
gert_lauritsen | 18:10fcddf238c9 | 343 | _pos=0; |
gert_lauritsen | 29:6711180763b6 | 344 | // printf("%s \r\n",_responseFrameString); |
gert_lauritsen | 18:10fcddf238c9 | 345 | if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something |
gert_lauritsen | 18:10fcddf238c9 | 346 | //Do something |
gert_lauritsen | 18:10fcddf238c9 | 347 | } else if (strstr(_responseFrameString,"UCAST:")) { //checke for incoming UCAST data |
gert_lauritsen | 18:10fcddf238c9 | 348 | //if (sscanf (_responseFrameString,"UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata)>0) |
gert_lauritsen | 26:2a85af491d92 | 349 | p=strstr(_responseFrameString,"UCAST:"); |
gert_lauritsen | 21:9c5b72a64495 | 350 | p=p+6; |
gert_lauritsen | 29:6711180763b6 | 351 | strncpy(In.ID,p,16); |
gert_lauritsen | 29:6711180763b6 | 352 | In.ID[16]=0; |
gert_lauritsen | 29:6711180763b6 | 353 | //Zdat=1; |
gert_lauritsen | 18:10fcddf238c9 | 354 | p=strstr(_responseFrameString,"="); |
gert_lauritsen | 18:10fcddf238c9 | 355 | if (p) { |
gert_lauritsen | 18:10fcddf238c9 | 356 | p++; |
gert_lauritsen | 29:6711180763b6 | 357 | strcpy(In.Data,p); |
gert_lauritsen | 29:6711180763b6 | 358 | In.Ready=1; |
gert_lauritsen | 18:10fcddf238c9 | 359 | } |
gert_lauritsen | 18:10fcddf238c9 | 360 | } |
gert_lauritsen | 30:f9cdb6f62586 | 361 | if (strstr(_responseFrameString,"+PANSCAN:")) { //info om nærliggende pan |
gert_lauritsen | 30:f9cdb6f62586 | 362 | |
gert_lauritsen | 30:f9cdb6f62586 | 363 | } |
gert_lauritsen | 18:10fcddf238c9 | 364 | if (strstr(_responseFrameString,"LeftPAN:")) PanOnline=0; //Local node has left the Pan |
gert_lauritsen | 18:10fcddf238c9 | 365 | if (strstr(_responseFrameString,"LostPAN:")) PanOnline=0; //Local node has left the Pan |
gert_lauritsen | 18:10fcddf238c9 | 366 | if (strstr(_responseFrameString,"ERROR:")) { |
gert_lauritsen | 18:10fcddf238c9 | 367 | p=strstr(_responseFrameString,":"); |
gert_lauritsen | 18:10fcddf238c9 | 368 | p++; //P indholder nu SEQ nummer |
gert_lauritsen | 17:5a3f81b10a61 | 369 | ErrorCode=hextoint(p); |
gert_lauritsen | 18:10fcddf238c9 | 370 | } |
gert_lauritsen | 18:10fcddf238c9 | 371 | |
gert_lauritsen | 18:10fcddf238c9 | 372 | if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID> |
gert_lauritsen | 18:10fcddf238c9 | 373 | //new node on the pan |
gert_lauritsen | 20:070dc2050052 | 374 | p=strstr(_responseFrameString,"NEWNODE:"); |
gert_lauritsen | 26:2a85af491d92 | 375 | p=p+13; |
gert_lauritsen | 26:2a85af491d92 | 376 | NetInfo=1; |
gert_lauritsen | 26:2a85af491d92 | 377 | strncpy(NetNodeID,p,16); |
gert_lauritsen | 18:10fcddf238c9 | 378 | } |
gert_lauritsen | 11:18ff088287ea | 379 | |
gert_lauritsen | 25:31120552e53c | 380 | if ((strstr(_responseFrameString,"OK")>0) && (strstr(_responseFrameString,"TOKDUMP")==0) ) { |
gert_lauritsen | 18:10fcddf238c9 | 381 | //if (ScriptState) ConnectScript(); |
gert_lauritsen | 28:c724a8921f01 | 382 | Ok=1; |
gert_lauritsen | 28:c724a8921f01 | 383 | //Cmd=0; LineNo=0; |
gert_lauritsen | 18:10fcddf238c9 | 384 | } |
gert_lauritsen | 20:070dc2050052 | 385 | if (strstr(_responseFrameString,"FFD:")) { |
gert_lauritsen | 20:070dc2050052 | 386 | //FFD:000D6F00026C5BEA,0000 |
gert_lauritsen | 20:070dc2050052 | 387 | p=strstr(_responseFrameString,"FFD:"); |
gert_lauritsen | 26:2a85af491d92 | 388 | p=p+4; |
gert_lauritsen | 26:2a85af491d92 | 389 | NetInfo=1; |
gert_lauritsen | 27:a38b67f5be8f | 390 | strncpy(NetNodeID,p,16); |
gert_lauritsen | 26:2a85af491d92 | 391 | } |
gert_lauritsen | 27:a38b67f5be8f | 392 | if (strstr(_responseFrameString,"N=")) { |
gert_lauritsen | 26:2a85af491d92 | 393 | // sscanf (_responseFrameString,"+N=%s,%d,%d,%4X,",Devicetype,&channel,&NodeID,&EPID); |
gert_lauritsen | 30:f9cdb6f62586 | 394 | size_t n; |
gert_lauritsen | 27:a38b67f5be8f | 395 | p=strstr(_responseFrameString,"="); |
gert_lauritsen | 27:a38b67f5be8f | 396 | p++; |
gert_lauritsen | 30:f9cdb6f62586 | 397 | n=comma_parse(p,list,ZdataSize); |
gert_lauritsen | 30:f9cdb6f62586 | 398 | strcpy(Devicetype,list[0]); |
gert_lauritsen | 30:f9cdb6f62586 | 399 | // Devicetype[3]=0; //nulterminate |
gert_lauritsen | 30:f9cdb6f62586 | 400 | PanChan=atoi(list[1]); |
gert_lauritsen | 30:f9cdb6f62586 | 401 | strcpy(PanNumber,list[3]); |
gert_lauritsen | 18:10fcddf238c9 | 402 | } |
gert_lauritsen | 18:10fcddf238c9 | 403 | if (strstr(_responseFrameString,"ACK")) { // |
gert_lauritsen | 26:2a85af491d92 | 404 | PacketAck=1; |
gert_lauritsen | 18:10fcddf238c9 | 405 | p=strstr(_responseFrameString,":"); |
gert_lauritsen | 18:10fcddf238c9 | 406 | p++; //P indholder nu SEQ nummer |
gert_lauritsen | 12:debf76f0c0bf | 407 | SeqNumber=hextoint(p); |
gert_lauritsen | 18:10fcddf238c9 | 408 | } |
gert_lauritsen | 18:10fcddf238c9 | 409 | if (strstr(_responseFrameString,"NAK")) { |
gert_lauritsen | 18:10fcddf238c9 | 410 | PacketAck=0; |
gert_lauritsen | 18:10fcddf238c9 | 411 | p=strstr(_responseFrameString,":"); |
gert_lauritsen | 18:10fcddf238c9 | 412 | p++; //P indholder nu SEQ nummer |
gert_lauritsen | 18:10fcddf238c9 | 413 | SeqNumber=hextoint(p); |
gert_lauritsen | 18:10fcddf238c9 | 414 | } |
gert_lauritsen | 18:10fcddf238c9 | 415 | if (strstr(_responseFrameString,"SEQ")) { // |
gert_lauritsen | 26:2a85af491d92 | 416 | // PacketAck=1; |
gert_lauritsen | 18:10fcddf238c9 | 417 | p=strstr(_responseFrameString,":"); |
gert_lauritsen | 18:10fcddf238c9 | 418 | p++; //P indholder nu SEQ nummer |
gert_lauritsen | 12:debf76f0c0bf | 419 | SeqNumber=hextoint(p); |
gert_lauritsen | 18:10fcddf238c9 | 420 | } |
gert_lauritsen | 18:10fcddf238c9 | 421 | } |
gert_lauritsen | 11:18ff088287ea | 422 | } |
gert_lauritsen | 18:10fcddf238c9 | 423 | |
gert_lauritsen | 18:10fcddf238c9 | 424 | //-----------------------NON IRQ mode -------------------------------------------------------------------- |
gert_lauritsen | 18:10fcddf238c9 | 425 | #ifndef IRQCtrl |
gert_lauritsen | 18:10fcddf238c9 | 426 | void zigbee::readPacket() |
gert_lauritsen | 18:10fcddf238c9 | 427 | { |
gert_lauritsen | 18:10fcddf238c9 | 428 | //read and waits for the packet |
gert_lauritsen | 19:78f44233a12f | 429 | Timer t; |
gert_lauritsen | 19:78f44233a12f | 430 | t.reset(); |
gert_lauritsen | 26:2a85af491d92 | 431 | t.start(); |
gert_lauritsen | 19:78f44233a12f | 432 | b=0; |
gert_lauritsen | 25:31120552e53c | 433 | while((b!=CR) && (t.read_ms() < 1000)) |
gert_lauritsen | 18:10fcddf238c9 | 434 | if (_zbee.readable()) SeePacket(); |
gert_lauritsen | 26:2a85af491d92 | 435 | t.stop(); |
gert_lauritsen | 18:10fcddf238c9 | 436 | } |
gert_lauritsen | 18:10fcddf238c9 | 437 | |
gert_lauritsen | 20:070dc2050052 | 438 | #endif |
gert_lauritsen | 29:6711180763b6 | 439 | |
gert_lauritsen | 29:6711180763b6 | 440 | |
gert_lauritsen | 29:6711180763b6 | 441 | void zigbee::SletZdata(void){ |
gert_lauritsen | 29:6711180763b6 | 442 | int i; |
gert_lauritsen | 29:6711180763b6 | 443 | for (i=0; i<ZdataSize ; i++) In.Data[i]=0; |
gert_lauritsen | 29:6711180763b6 | 444 | } |