A libery to connect to telegesis zigbee module. Bassed on implemtation of XBEE
Fork of xbee_lib by
Diff: telegesis.cpp
- Revision:
- 18:10fcddf238c9
- Parent:
- 17:5a3f81b10a61
- Child:
- 19:78f44233a12f
--- a/telegesis.cpp Fri Oct 18 09:05:43 2013 +0000 +++ b/telegesis.cpp Fri Oct 18 16:49:02 2013 +0000 @@ -1,60 +1,70 @@ #include "telegesis.h" #define CR 0x0D #define LF 0x0A - +//#define IRQCtrl -zigbee::zigbee(PinName tx, PinName rx): _zbee(tx, rx) { +zigbee::zigbee(PinName tx, PinName rx): _zbee(tx, rx) +{ _tx = tx; _rx = rx; - _pos=0; - _zbee.baud(19200); - _zbee.attach(this, &zigbee::SeePacket, Serial::RxIrq); + _zbee.baud(19200); +#ifdef IRQCtrl + _zbee.attach(this, &zigbee::SeePacket, Serial::RxIrq); +#endif } zigbee::~zigbee() { } -bool zigbee::wait4OK() { +void zigbee::Work() { + if (_zbee.readable()) readPacket(); +} + +bool zigbee::wait4OK() +{ Timer t; t.reset(); - t.start(); + t.start(); readPacket(); - while ((strstr(_responseFrameString,"OK")==0) & (t.read_ms() < 500)) readPacket(); + while ((strstr(_responseFrameString,"OK")==0) & (t.read_ms() < 500)) readPacket(); return (strstr(_responseFrameString,"OK")>0); } int zigbee::GetSerial() -{ - +{ /** comes with something like this - Telegesis ETRX357-LRS - R305C - 000D6F0000D5F06A - OK -*/ + Telegesis ETRX357-LRS + R305C + 000D6F0000D5F06A + OK + */ _zbee.printf("ATI\r"); - readPacket(); readPacket(); //commando - readPacket(); + readPacket(); + readPacket(); //commando + readPacket(); sscanf(_responseFrameString,"Telegesis %s",HWType); - readPacket(); readPacket(); - LocalID=hextoint(_responseFrameString); - wait_ms(5); + readPacket(); + readPacket(); + strcpy(LocalID,_responseFrameString); //vi lægger den bare over i stringformat. Så kan man altid senere convertere + // LocalID=hextoint(_responseFrameString); + // wait_ms(5); return 1; } -int zigbee::SetKey(char* key){ -/** -S09: the link key. Write the same 128-bit number (32 hexadecimal characters) into every device +int zigbee::SetKey(char* key) +{ + /** + S09: the link key. Write the same 128-bit number (32 hexadecimal characters) into every device -S0A: security configuration. You must set bit 4 in the coordinator, and set bit 8 in all other devices. -For more security set bit 2 also in the coordinator. The devices ignore the bits that are not relevant -for them, so it is easiest to just set bits 8, 4, and 2 in all devices which gives S0A=0114. -*/ -char psw[50]; + S0A: security configuration. You must set bit 4 in the coordinator, and set bit 8 in all other devices. + For more security set bit 2 also in the coordinator. The devices ignore the bits that are not relevant + for them, so it is easiest to just set bits 8, 4, and 2 in all devices which gives S0A=0114. + */ + char psw[50]; sprintf(psw,"ATS09=%s;password\r",key); _zbee.printf(psw); - wait4OK(); + wait4OK(); _zbee.printf("ATS0A=0114;password\r"); return 1; } @@ -67,7 +77,7 @@ if(numchar == 0) { numchar = sizeof(data_buf); } - + while(numchar!=count) { if(_zbee.readable()) { *data_buf = _zbee.getc(); @@ -93,7 +103,8 @@ int zigbee::PingOut() -{ //just return a OK (sends it ID out out the pan) +{ + //just return a OK (sends it ID out out the pan) _zbee.printf("AT+ANNCE\r"); wait4OK(); return 1; @@ -101,14 +112,16 @@ int zigbee::PanScan() -{// +{ + // _zbee.printf("AT+PANSCAN\r"); return 1; } int zigbee::Establish_Network() -{// +{ + // _zbee.printf("AT+EN\r"); return 1; } @@ -186,71 +199,90 @@ return result; } //--------------------------------------------------------------- -void zigbee::readPacket() { +#ifdef IRQCtrl +void zigbee::readPacket() +{ //read and waits for the packet Timer t; t.reset(); - t.start(); - while ((GotFrame==0) & (t.read_ms() < 500)){} - t.stop(); - GotFrame=0; -} + t.start(); + while ((GotFrame==0) & (t.read_ms() < 500)) {} + t.stop(); + GotFrame=0; +} +#endif -void zigbee::SeePacket() { - char *p; +void zigbee::SeePacket() +{ + char *p; + static uint8_t _pos; b = _zbee.getc(); - if ((b!=CR) & (b!=LF)) _responseFrameData[_pos]=b; + if ((b!=CR) & (b!=LF)) _responseFrameString[_pos]=b; _pos=(_pos+1) % MAX_FRAME_DATA_SIZE; if (b==CR) { - memcpy(&_responseFrameString,&_responseFrameData,_pos); - GotFrame=1; - _responseFrameString[_pos]=0; //Nul terminate - _pos=0; - if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something - //Do something - } else - if (strstr(_responseFrameString,"UCAST:")) {//checke for incoming UCAST data - //if (sscanf (_responseFrameString,"UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata)>0) - p=strstr(_responseFrameString,"="); - if (p) { - p++; - strcpy(Zdata,p); - Zdat=1; - } - } - if (strstr(_responseFrameString,"LeftPAN:")) PanOnline=0; //Local node has left the Pan - if (strstr(_responseFrameString,"LostPAN:")) PanOnline=0; //Local node has left the Pan - if (strstr(_responseFrameString,"ERROR:")) { - p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer + // memcpy(&_responseFrameString,&_responseFrameData,_pos); + GotFrame=1; + _responseFrameString[_pos]=0; //Nul terminate + _pos=0; + if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something + //Do something + } else if (strstr(_responseFrameString,"UCAST:")) { //checke for incoming UCAST data + //if (sscanf (_responseFrameString,"UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata)>0) + p=strstr(_responseFrameString,"="); + if (p) { + p++; + strcpy(Zdata,p); + Zdat=1; + } + } + if (strstr(_responseFrameString,"LeftPAN:")) PanOnline=0; //Local node has left the Pan + if (strstr(_responseFrameString,"LostPAN:")) PanOnline=0; //Local node has left the Pan + if (strstr(_responseFrameString,"ERROR:")) { + p=strstr(_responseFrameString,":"); + p++; //P indholder nu SEQ nummer ErrorCode=hextoint(p); - } + } + + if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID> + //new node on the pan + } - if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID> - //new node on the pan - } - - if ((strstr(_responseFrameString,"OK")>0) & (strstr(_responseFrameString,"TOKDUMP")==0) ) { - //if (ScriptState) ConnectScript(); - //Ok=1; Cmd=0; LineNo=0; - } - if (strstr(_responseFrameString,"+N=")) { - sscanf (_responseFrameString,"+N=%s,%d,%d,%4X,",Devicetype,&channel,&NodeID,&EPID); - } - if (strstr(_responseFrameString,"ACK")) { // + if ((strstr(_responseFrameString,"OK")>0) & (strstr(_responseFrameString,"TOKDUMP")==0) ) { + //if (ScriptState) ConnectScript(); + //Ok=1; Cmd=0; LineNo=0; + } + if (strstr(_responseFrameString,"+N=")) { + sscanf (_responseFrameString,"+N=%s,%d,%d,%4X,",Devicetype,&channel,&NodeID,&EPID); + } + if (strstr(_responseFrameString,"ACK")) { // PacketAck=1; - p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer + p=strstr(_responseFrameString,":"); + p++; //P indholder nu SEQ nummer SeqNumber=hextoint(p); - } - if (strstr(_responseFrameString,"NAK")) { - PacketAck=0; - p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer + } + if (strstr(_responseFrameString,"NAK")) { + PacketAck=0; + p=strstr(_responseFrameString,":"); + p++; //P indholder nu SEQ nummer + SeqNumber=hextoint(p); + } + if (strstr(_responseFrameString,"SEQ")) { // + PacketAck=1; + p=strstr(_responseFrameString,":"); + p++; //P indholder nu SEQ nummer SeqNumber=hextoint(p); - } - if (strstr(_responseFrameString,"SEQ")) { // - PacketAck=1; - p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer - SeqNumber=hextoint(p); - } - } + } + } } + +//-----------------------NON IRQ mode -------------------------------------------------------------------- +#ifndef IRQCtrl +void zigbee::readPacket() +{ + //read and waits for the packet + while(b!=CR) + if (_zbee.readable()) SeePacket(); +} + +#endif \ No newline at end of file