A libery to connect to telegesis zigbee module. Bassed on implemtation of XBEE

Fork of xbee_lib by Tristan Hughes

Committer:
gert_lauritsen
Date:
Fri Oct 18 17:42:24 2013 +0000
Revision:
19:78f44233a12f
Parent:
18:10fcddf238c9
Child:
20:070dc2050052
lige et ekstra timeout til non irq mode

Who changed what in which revision?

UserRevisionLine numberNew 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 18:10fcddf238c9 11 #ifdef IRQCtrl
gert_lauritsen 18:10fcddf238c9 12 _zbee.attach(this, &zigbee::SeePacket, Serial::RxIrq);
gert_lauritsen 18:10fcddf238c9 13 #endif
tristanjph 0:2656fb225c5d 14 }
tristanjph 0:2656fb225c5d 15
gert_lauritsen 7:45511c3d2950 16 zigbee::~zigbee()
tristanjph 0:2656fb225c5d 17 {
tristanjph 0:2656fb225c5d 18 }
tristanjph 0:2656fb225c5d 19
gert_lauritsen 18:10fcddf238c9 20 void zigbee::Work() {
gert_lauritsen 18:10fcddf238c9 21 if (_zbee.readable()) readPacket();
gert_lauritsen 18:10fcddf238c9 22 }
gert_lauritsen 18:10fcddf238c9 23
gert_lauritsen 18:10fcddf238c9 24 bool zigbee::wait4OK()
gert_lauritsen 18:10fcddf238c9 25 {
gert_lauritsen 12:debf76f0c0bf 26 Timer t;
gert_lauritsen 12:debf76f0c0bf 27 t.reset();
gert_lauritsen 18:10fcddf238c9 28 t.start();
gert_lauritsen 11:18ff088287ea 29 readPacket();
gert_lauritsen 18:10fcddf238c9 30 while ((strstr(_responseFrameString,"OK")==0) & (t.read_ms() < 500)) readPacket();
gert_lauritsen 12:debf76f0c0bf 31 return (strstr(_responseFrameString,"OK")>0);
gert_lauritsen 11:18ff088287ea 32 }
tristanjph 0:2656fb225c5d 33
gert_lauritsen 11:18ff088287ea 34 int zigbee::GetSerial()
gert_lauritsen 18:10fcddf238c9 35 {
gert_lauritsen 11:18ff088287ea 36 /** comes with something like this
gert_lauritsen 18:10fcddf238c9 37 Telegesis ETRX357-LRS
gert_lauritsen 18:10fcddf238c9 38 R305C
gert_lauritsen 18:10fcddf238c9 39 000D6F0000D5F06A
gert_lauritsen 18:10fcddf238c9 40 OK
gert_lauritsen 18:10fcddf238c9 41 */
gert_lauritsen 11:18ff088287ea 42 _zbee.printf("ATI\r");
gert_lauritsen 18:10fcddf238c9 43 readPacket();
gert_lauritsen 18:10fcddf238c9 44 readPacket(); //commando
gert_lauritsen 18:10fcddf238c9 45 readPacket();
gert_lauritsen 11:18ff088287ea 46 sscanf(_responseFrameString,"Telegesis %s",HWType);
gert_lauritsen 18:10fcddf238c9 47 readPacket();
gert_lauritsen 18:10fcddf238c9 48 readPacket();
gert_lauritsen 18:10fcddf238c9 49 strcpy(LocalID,_responseFrameString); //vi lægger den bare over i stringformat. Så kan man altid senere convertere
gert_lauritsen 18:10fcddf238c9 50 // LocalID=hextoint(_responseFrameString);
gert_lauritsen 18:10fcddf238c9 51 // wait_ms(5);
tristanjph 0:2656fb225c5d 52 return 1;
tristanjph 0:2656fb225c5d 53 }
tristanjph 0:2656fb225c5d 54
gert_lauritsen 18:10fcddf238c9 55 int zigbee::SetKey(char* key)
gert_lauritsen 18:10fcddf238c9 56 {
gert_lauritsen 18:10fcddf238c9 57 /**
gert_lauritsen 18:10fcddf238c9 58 S09: the link key. Write the same 128-bit number (32 hexadecimal characters) into every device
gert_lauritsen 15:8d4990362a80 59
gert_lauritsen 18:10fcddf238c9 60 S0A: security configuration. You must set bit 4 in the coordinator, and set bit 8 in all other devices.
gert_lauritsen 18:10fcddf238c9 61 For more security set bit 2 also in the coordinator. The devices ignore the bits that are not relevant
gert_lauritsen 18:10fcddf238c9 62 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 63 */
gert_lauritsen 18:10fcddf238c9 64 char psw[50];
gert_lauritsen 15:8d4990362a80 65 sprintf(psw,"ATS09=%s;password\r",key);
gert_lauritsen 15:8d4990362a80 66 _zbee.printf(psw);
gert_lauritsen 18:10fcddf238c9 67 wait4OK();
gert_lauritsen 15:8d4990362a80 68 _zbee.printf("ATS0A=0114;password\r");
tristanjph 0:2656fb225c5d 69 return 1;
tristanjph 0:2656fb225c5d 70 }
tristanjph 0:2656fb225c5d 71
tristanjph 0:2656fb225c5d 72
tristanjph 0:2656fb225c5d 73
gert_lauritsen 7:45511c3d2950 74 void zigbee::RecieveData(char *data_buf, int numchar)
tristanjph 1:c3d9bdcb0b03 75 {
tristanjph 1:c3d9bdcb0b03 76 int count=0;
tristanjph 2:cb627ea9b817 77 if(numchar == 0) {
tristanjph 2:cb627ea9b817 78 numchar = sizeof(data_buf);
tristanjph 2:cb627ea9b817 79 }
gert_lauritsen 18:10fcddf238c9 80
tristanjph 1:c3d9bdcb0b03 81 while(numchar!=count) {
gert_lauritsen 11:18ff088287ea 82 if(_zbee.readable()) {
gert_lauritsen 11:18ff088287ea 83 *data_buf = _zbee.getc();
tristanjph 3:682615a0717e 84 data_buf+=1;
tristanjph 3:682615a0717e 85 count++;
tristanjph 1:c3d9bdcb0b03 86 }
tristanjph 1:c3d9bdcb0b03 87
tristanjph 1:c3d9bdcb0b03 88 }
tristanjph 2:cb627ea9b817 89 }
tristanjph 1:c3d9bdcb0b03 90
gert_lauritsen 12:debf76f0c0bf 91 int zigbee::Reset()
gert_lauritsen 12:debf76f0c0bf 92 {
gert_lauritsen 12:debf76f0c0bf 93 _zbee.printf("ATZ\r");
gert_lauritsen 12:debf76f0c0bf 94 wait4OK();
gert_lauritsen 12:debf76f0c0bf 95 return 1;
gert_lauritsen 12:debf76f0c0bf 96 }
gert_lauritsen 12:debf76f0c0bf 97
gert_lauritsen 7:45511c3d2950 98 int zigbee::ATI()
gert_lauritsen 7:45511c3d2950 99 {
gert_lauritsen 12:debf76f0c0bf 100 GetSerial();
gert_lauritsen 7:45511c3d2950 101 return 1;
gert_lauritsen 7:45511c3d2950 102 }
gert_lauritsen 7:45511c3d2950 103
gert_lauritsen 7:45511c3d2950 104
gert_lauritsen 7:45511c3d2950 105 int zigbee::PingOut()
gert_lauritsen 18:10fcddf238c9 106 {
gert_lauritsen 18:10fcddf238c9 107 //just return a OK (sends it ID out out the pan)
gert_lauritsen 11:18ff088287ea 108 _zbee.printf("AT+ANNCE\r");
gert_lauritsen 12:debf76f0c0bf 109 wait4OK();
gert_lauritsen 7:45511c3d2950 110 return 1;
gert_lauritsen 7:45511c3d2950 111 }
gert_lauritsen 7:45511c3d2950 112
gert_lauritsen 7:45511c3d2950 113
gert_lauritsen 7:45511c3d2950 114 int zigbee::PanScan()
gert_lauritsen 18:10fcddf238c9 115 {
gert_lauritsen 18:10fcddf238c9 116 //
gert_lauritsen 11:18ff088287ea 117 _zbee.printf("AT+PANSCAN\r");
gert_lauritsen 7:45511c3d2950 118 return 1;
gert_lauritsen 7:45511c3d2950 119 }
gert_lauritsen 7:45511c3d2950 120
gert_lauritsen 7:45511c3d2950 121
gert_lauritsen 7:45511c3d2950 122 int zigbee::Establish_Network()
gert_lauritsen 18:10fcddf238c9 123 {
gert_lauritsen 18:10fcddf238c9 124 //
gert_lauritsen 11:18ff088287ea 125 _zbee.printf("AT+EN\r");
tristanjph 2:cb627ea9b817 126 return 1;
tristanjph 2:cb627ea9b817 127 }
tristanjph 3:682615a0717e 128
gert_lauritsen 7:45511c3d2950 129
gert_lauritsen 7:45511c3d2950 130 int zigbee::JoinNetwork()
gert_lauritsen 7:45511c3d2950 131 {
gert_lauritsen 11:18ff088287ea 132 _zbee.printf("AT+JN\r");
gert_lauritsen 7:45511c3d2950 133 return 1;
gert_lauritsen 7:45511c3d2950 134 }
gert_lauritsen 7:45511c3d2950 135
gert_lauritsen 7:45511c3d2950 136 int zigbee::ScanNetwork()
gert_lauritsen 7:45511c3d2950 137 {
gert_lauritsen 11:18ff088287ea 138 _zbee.printf("AT+SN\r");
gert_lauritsen 7:45511c3d2950 139 return 1;
gert_lauritsen 7:45511c3d2950 140 }
gert_lauritsen 7:45511c3d2950 141
gert_lauritsen 7:45511c3d2950 142 int zigbee::NetworkInfo()
gert_lauritsen 7:45511c3d2950 143 {
gert_lauritsen 7:45511c3d2950 144 //Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522"
gert_lauritsen 11:18ff088287ea 145 _zbee.printf("AT+N\r");
gert_lauritsen 7:45511c3d2950 146 return 1;
gert_lauritsen 7:45511c3d2950 147 }
gert_lauritsen 7:45511c3d2950 148
gert_lauritsen 9:c8e4339ccc29 149 int zigbee::UniCast(char *adr,char *payload) //Ascii mode with null terminated string
gert_lauritsen 7:45511c3d2950 150 {
gert_lauritsen 11:18ff088287ea 151 _zbee.printf("AT+UCAST:%s=%s\r",adr,payload);
gert_lauritsen 12:debf76f0c0bf 152 // _zbee.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata);
gert_lauritsen 7:45511c3d2950 153 return 1;
gert_lauritsen 7:45511c3d2950 154 }
gert_lauritsen 7:45511c3d2950 155
gert_lauritsen 9:c8e4339ccc29 156 int zigbee::UniCastb(char *adr,char *payload, char payloadSize) //sends data in binary format
gert_lauritsen 7:45511c3d2950 157 {
gert_lauritsen 11:18ff088287ea 158 _zbee.printf("AT+UCASTB:%X,%s\r",adr,payloadSize);
gert_lauritsen 12:debf76f0c0bf 159 //_zbee.scanf ("%*s");
gert_lauritsen 7:45511c3d2950 160 return 1;
gert_lauritsen 7:45511c3d2950 161 }
gert_lauritsen 7:45511c3d2950 162
gert_lauritsen 12:debf76f0c0bf 163 //-----------------------Handle incoming data------------------------------
tristanjph 3:682615a0717e 164
gert_lauritsen 11:18ff088287ea 165 unsigned long zigbee::hextolong(const char *hex)
gert_lauritsen 7:45511c3d2950 166 {
gert_lauritsen 7:45511c3d2950 167 //return 32 bit
gert_lauritsen 7:45511c3d2950 168 unsigned long result = 0;
gert_lauritsen 7:45511c3d2950 169 while (*hex) { //så længe det ikke er null
gert_lauritsen 7:45511c3d2950 170 if (*hex >= '0' && *hex <= '9')
gert_lauritsen 7:45511c3d2950 171 result += (*hex - '0');
gert_lauritsen 7:45511c3d2950 172 else if (*hex >= 'A' && *hex <= 'F')
gert_lauritsen 7:45511c3d2950 173 result += (*hex - 'A' +10);
gert_lauritsen 7:45511c3d2950 174 else if (*hex >= 'a' && *hex <= 'f')
gert_lauritsen 7:45511c3d2950 175 result += (*hex - 'a'+ 10);
gert_lauritsen 7:45511c3d2950 176
gert_lauritsen 12:debf76f0c0bf 177 if (*++hex) //if the neext isn't a null
gert_lauritsen 7:45511c3d2950 178 result <<= 4;
gert_lauritsen 7:45511c3d2950 179 }
gert_lauritsen 7:45511c3d2950 180 return result;
gert_lauritsen 7:45511c3d2950 181 }
gert_lauritsen 7:45511c3d2950 182
gert_lauritsen 7:45511c3d2950 183
gert_lauritsen 11:18ff088287ea 184 unsigned int zigbee::hextoint(const char *hex)
gert_lauritsen 7:45511c3d2950 185 {
gert_lauritsen 7:45511c3d2950 186 //return 16 bit
gert_lauritsen 7:45511c3d2950 187 unsigned int result = 0;
gert_lauritsen 7:45511c3d2950 188 while (*hex) { //så længe det ikke er null
gert_lauritsen 7:45511c3d2950 189 if (*hex >= '0' && *hex <= '9')
gert_lauritsen 7:45511c3d2950 190 result += (*hex - '0');
gert_lauritsen 7:45511c3d2950 191 else if (*hex >= 'A' && *hex <= 'F')
gert_lauritsen 7:45511c3d2950 192 result += (*hex - 'A' +10);
gert_lauritsen 7:45511c3d2950 193 else if (*hex >= 'a' && *hex <= 'f')
gert_lauritsen 7:45511c3d2950 194 result += (*hex - 'a'+ 10);
gert_lauritsen 7:45511c3d2950 195
gert_lauritsen 12:debf76f0c0bf 196 if (*++hex) //if the neext isn't a null
gert_lauritsen 7:45511c3d2950 197 result <<= 4;
gert_lauritsen 7:45511c3d2950 198 }
gert_lauritsen 7:45511c3d2950 199 return result;
gert_lauritsen 7:45511c3d2950 200 }
gert_lauritsen 7:45511c3d2950 201 //---------------------------------------------------------------
gert_lauritsen 18:10fcddf238c9 202 #ifdef IRQCtrl
gert_lauritsen 18:10fcddf238c9 203 void zigbee::readPacket()
gert_lauritsen 18:10fcddf238c9 204 {
gert_lauritsen 11:18ff088287ea 205 //read and waits for the packet
gert_lauritsen 12:debf76f0c0bf 206 Timer t;
gert_lauritsen 12:debf76f0c0bf 207 t.reset();
gert_lauritsen 18:10fcddf238c9 208 t.start();
gert_lauritsen 18:10fcddf238c9 209 while ((GotFrame==0) & (t.read_ms() < 500)) {}
gert_lauritsen 18:10fcddf238c9 210 t.stop();
gert_lauritsen 18:10fcddf238c9 211 GotFrame=0;
gert_lauritsen 18:10fcddf238c9 212 }
gert_lauritsen 7:45511c3d2950 213
gert_lauritsen 18:10fcddf238c9 214 #endif
gert_lauritsen 11:18ff088287ea 215
gert_lauritsen 18:10fcddf238c9 216 void zigbee::SeePacket()
gert_lauritsen 18:10fcddf238c9 217 {
gert_lauritsen 18:10fcddf238c9 218 char *p;
gert_lauritsen 18:10fcddf238c9 219 static uint8_t _pos;
gert_lauritsen 12:debf76f0c0bf 220 b = _zbee.getc();
gert_lauritsen 18:10fcddf238c9 221 if ((b!=CR) & (b!=LF)) _responseFrameString[_pos]=b;
gert_lauritsen 12:debf76f0c0bf 222 _pos=(_pos+1) % MAX_FRAME_DATA_SIZE;
gert_lauritsen 12:debf76f0c0bf 223 if (b==CR) {
gert_lauritsen 18:10fcddf238c9 224 // memcpy(&_responseFrameString,&_responseFrameData,_pos);
gert_lauritsen 18:10fcddf238c9 225 GotFrame=1;
gert_lauritsen 18:10fcddf238c9 226 _responseFrameString[_pos]=0; //Nul terminate
gert_lauritsen 18:10fcddf238c9 227 _pos=0;
gert_lauritsen 18:10fcddf238c9 228 if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something
gert_lauritsen 18:10fcddf238c9 229 //Do something
gert_lauritsen 18:10fcddf238c9 230 } else if (strstr(_responseFrameString,"UCAST:")) { //checke for incoming UCAST data
gert_lauritsen 18:10fcddf238c9 231 //if (sscanf (_responseFrameString,"UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata)>0)
gert_lauritsen 18:10fcddf238c9 232 p=strstr(_responseFrameString,"=");
gert_lauritsen 18:10fcddf238c9 233 if (p) {
gert_lauritsen 18:10fcddf238c9 234 p++;
gert_lauritsen 18:10fcddf238c9 235 strcpy(Zdata,p);
gert_lauritsen 18:10fcddf238c9 236 Zdat=1;
gert_lauritsen 18:10fcddf238c9 237 }
gert_lauritsen 18:10fcddf238c9 238 }
gert_lauritsen 18:10fcddf238c9 239 if (strstr(_responseFrameString,"LeftPAN:")) PanOnline=0; //Local node has left the Pan
gert_lauritsen 18:10fcddf238c9 240 if (strstr(_responseFrameString,"LostPAN:")) PanOnline=0; //Local node has left the Pan
gert_lauritsen 18:10fcddf238c9 241 if (strstr(_responseFrameString,"ERROR:")) {
gert_lauritsen 18:10fcddf238c9 242 p=strstr(_responseFrameString,":");
gert_lauritsen 18:10fcddf238c9 243 p++; //P indholder nu SEQ nummer
gert_lauritsen 17:5a3f81b10a61 244 ErrorCode=hextoint(p);
gert_lauritsen 18:10fcddf238c9 245 }
gert_lauritsen 18:10fcddf238c9 246
gert_lauritsen 18:10fcddf238c9 247 if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID>
gert_lauritsen 18:10fcddf238c9 248 //new node on the pan
gert_lauritsen 18:10fcddf238c9 249 }
gert_lauritsen 11:18ff088287ea 250
gert_lauritsen 18:10fcddf238c9 251 if ((strstr(_responseFrameString,"OK")>0) & (strstr(_responseFrameString,"TOKDUMP")==0) ) {
gert_lauritsen 18:10fcddf238c9 252 //if (ScriptState) ConnectScript();
gert_lauritsen 18:10fcddf238c9 253 //Ok=1; Cmd=0; LineNo=0;
gert_lauritsen 18:10fcddf238c9 254 }
gert_lauritsen 18:10fcddf238c9 255 if (strstr(_responseFrameString,"+N=")) {
gert_lauritsen 18:10fcddf238c9 256 sscanf (_responseFrameString,"+N=%s,%d,%d,%4X,",Devicetype,&channel,&NodeID,&EPID);
gert_lauritsen 18:10fcddf238c9 257 }
gert_lauritsen 18:10fcddf238c9 258 if (strstr(_responseFrameString,"ACK")) { //
gert_lauritsen 12:debf76f0c0bf 259 PacketAck=1;
gert_lauritsen 18:10fcddf238c9 260 p=strstr(_responseFrameString,":");
gert_lauritsen 18:10fcddf238c9 261 p++; //P indholder nu SEQ nummer
gert_lauritsen 12:debf76f0c0bf 262 SeqNumber=hextoint(p);
gert_lauritsen 18:10fcddf238c9 263 }
gert_lauritsen 18:10fcddf238c9 264 if (strstr(_responseFrameString,"NAK")) {
gert_lauritsen 18:10fcddf238c9 265 PacketAck=0;
gert_lauritsen 18:10fcddf238c9 266 p=strstr(_responseFrameString,":");
gert_lauritsen 18:10fcddf238c9 267 p++; //P indholder nu SEQ nummer
gert_lauritsen 18:10fcddf238c9 268 SeqNumber=hextoint(p);
gert_lauritsen 18:10fcddf238c9 269 }
gert_lauritsen 18:10fcddf238c9 270 if (strstr(_responseFrameString,"SEQ")) { //
gert_lauritsen 18:10fcddf238c9 271 PacketAck=1;
gert_lauritsen 18:10fcddf238c9 272 p=strstr(_responseFrameString,":");
gert_lauritsen 18:10fcddf238c9 273 p++; //P indholder nu SEQ nummer
gert_lauritsen 12:debf76f0c0bf 274 SeqNumber=hextoint(p);
gert_lauritsen 18:10fcddf238c9 275 }
gert_lauritsen 18:10fcddf238c9 276 }
gert_lauritsen 11:18ff088287ea 277 }
gert_lauritsen 18:10fcddf238c9 278
gert_lauritsen 18:10fcddf238c9 279 //-----------------------NON IRQ mode --------------------------------------------------------------------
gert_lauritsen 18:10fcddf238c9 280 #ifndef IRQCtrl
gert_lauritsen 18:10fcddf238c9 281 void zigbee::readPacket()
gert_lauritsen 18:10fcddf238c9 282 {
gert_lauritsen 18:10fcddf238c9 283 //read and waits for the packet
gert_lauritsen 19:78f44233a12f 284 Timer t;
gert_lauritsen 19:78f44233a12f 285 t.reset();
gert_lauritsen 19:78f44233a12f 286 t.start();
gert_lauritsen 19:78f44233a12f 287 b=0;
gert_lauritsen 19:78f44233a12f 288 while((b!=CR) & (t.read_ms() < 1000))
gert_lauritsen 18:10fcddf238c9 289 if (_zbee.readable()) SeePacket();
gert_lauritsen 19:78f44233a12f 290 t.stop();
gert_lauritsen 18:10fcddf238c9 291 }
gert_lauritsen 18:10fcddf238c9 292
gert_lauritsen 18:10fcddf238c9 293 #endif