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