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

Fork of xbee_lib by Tristan Hughes

Committer:
gert_lauritsen
Date:
Tue Oct 15 14:10:08 2013 +0000
Revision:
12:debf76f0c0bf
Parent:
11:18ff088287ea
Child:
14:dcf2390f89e2
Now with ISR on the serial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gert_lauritsen 7:45511c3d2950 1 #include "telegesis.h"
tristanjph 0:2656fb225c5d 2
tristanjph 0:2656fb225c5d 3
gert_lauritsen 11:18ff088287ea 4 zigbee::zigbee(PinName tx, PinName rx): _zbee(tx, rx) {
tristanjph 0:2656fb225c5d 5 _tx = tx;
tristanjph 0:2656fb225c5d 6 _rx = rx;
gert_lauritsen 12:debf76f0c0bf 7 _pos=0;
gert_lauritsen 11:18ff088287ea 8 _zbee.baud(19200);
gert_lauritsen 12:debf76f0c0bf 9 _zbee.attach(this, &zigbee::SeePacket, Serial::RxIrq);
tristanjph 0:2656fb225c5d 10 }
tristanjph 0:2656fb225c5d 11
gert_lauritsen 7:45511c3d2950 12 zigbee::~zigbee()
tristanjph 0:2656fb225c5d 13 {
tristanjph 0:2656fb225c5d 14 }
tristanjph 0:2656fb225c5d 15
gert_lauritsen 12:debf76f0c0bf 16 bool zigbee::wait4OK() {
gert_lauritsen 12:debf76f0c0bf 17 Timer t;
gert_lauritsen 12:debf76f0c0bf 18 t.reset();
gert_lauritsen 12:debf76f0c0bf 19 t.start();
gert_lauritsen 11:18ff088287ea 20 readPacket();
gert_lauritsen 12:debf76f0c0bf 21 while ((strstr(_responseFrameString,"OK")==0) & (t.read_ms() < 500)) readPacket();
gert_lauritsen 12:debf76f0c0bf 22 return (strstr(_responseFrameString,"OK")>0);
gert_lauritsen 11:18ff088287ea 23 }
tristanjph 0:2656fb225c5d 24
gert_lauritsen 11:18ff088287ea 25 int zigbee::GetSerial()
gert_lauritsen 8:4682155753ec 26 {
gert_lauritsen 12:debf76f0c0bf 27
gert_lauritsen 11:18ff088287ea 28 /** comes with something like this
gert_lauritsen 8:4682155753ec 29 Telegesis ETRX357-LRS
gert_lauritsen 8:4682155753ec 30 R305C
gert_lauritsen 8:4682155753ec 31 000D6F0000D5F06A
gert_lauritsen 8:4682155753ec 32 OK
gert_lauritsen 8:4682155753ec 33 */
gert_lauritsen 11:18ff088287ea 34 _zbee.printf("ATI\r");
gert_lauritsen 11:18ff088287ea 35 readPacket(); readPacket(); //commando
gert_lauritsen 11:18ff088287ea 36 readPacket();
gert_lauritsen 11:18ff088287ea 37 sscanf(_responseFrameString,"Telegesis %s",HWType);
gert_lauritsen 11:18ff088287ea 38 readPacket(); readPacket();
gert_lauritsen 12:debf76f0c0bf 39 LocalID=hextoint(_responseFrameString);
gert_lauritsen 12:debf76f0c0bf 40 wait_ms(5);
tristanjph 0:2656fb225c5d 41 return 1;
tristanjph 0:2656fb225c5d 42 }
tristanjph 0:2656fb225c5d 43
gert_lauritsen 7:45511c3d2950 44 int zigbee::SetKey(char* key)
tristanjph 0:2656fb225c5d 45 {
tristanjph 0:2656fb225c5d 46 return 1;
tristanjph 0:2656fb225c5d 47 }
tristanjph 0:2656fb225c5d 48
tristanjph 0:2656fb225c5d 49
tristanjph 0:2656fb225c5d 50
gert_lauritsen 7:45511c3d2950 51 void zigbee::RecieveData(char *data_buf, int numchar)
tristanjph 1:c3d9bdcb0b03 52 {
tristanjph 1:c3d9bdcb0b03 53 int count=0;
tristanjph 2:cb627ea9b817 54 if(numchar == 0) {
tristanjph 2:cb627ea9b817 55 numchar = sizeof(data_buf);
tristanjph 2:cb627ea9b817 56 }
gert_lauritsen 11:18ff088287ea 57
tristanjph 1:c3d9bdcb0b03 58 while(numchar!=count) {
gert_lauritsen 11:18ff088287ea 59 if(_zbee.readable()) {
gert_lauritsen 11:18ff088287ea 60 *data_buf = _zbee.getc();
tristanjph 3:682615a0717e 61 data_buf+=1;
tristanjph 3:682615a0717e 62 count++;
tristanjph 1:c3d9bdcb0b03 63 }
tristanjph 1:c3d9bdcb0b03 64
tristanjph 1:c3d9bdcb0b03 65 }
tristanjph 2:cb627ea9b817 66 }
tristanjph 1:c3d9bdcb0b03 67
gert_lauritsen 12:debf76f0c0bf 68 int zigbee::Reset()
gert_lauritsen 12:debf76f0c0bf 69 {
gert_lauritsen 12:debf76f0c0bf 70 _zbee.printf("ATZ\r");
gert_lauritsen 12:debf76f0c0bf 71 wait4OK();
gert_lauritsen 12:debf76f0c0bf 72 return 1;
gert_lauritsen 12:debf76f0c0bf 73 }
gert_lauritsen 12:debf76f0c0bf 74
gert_lauritsen 7:45511c3d2950 75 int zigbee::ATI()
gert_lauritsen 7:45511c3d2950 76 {
gert_lauritsen 12:debf76f0c0bf 77 GetSerial();
gert_lauritsen 7:45511c3d2950 78 return 1;
gert_lauritsen 7:45511c3d2950 79 }
gert_lauritsen 7:45511c3d2950 80
gert_lauritsen 7:45511c3d2950 81
gert_lauritsen 7:45511c3d2950 82 int zigbee::PingOut()
gert_lauritsen 12:debf76f0c0bf 83 { //just return a OK (sends it ID out out the pan)
gert_lauritsen 11:18ff088287ea 84 _zbee.printf("AT+ANNCE\r");
gert_lauritsen 12:debf76f0c0bf 85 wait4OK();
gert_lauritsen 7:45511c3d2950 86 return 1;
gert_lauritsen 7:45511c3d2950 87 }
gert_lauritsen 7:45511c3d2950 88
gert_lauritsen 7:45511c3d2950 89
gert_lauritsen 7:45511c3d2950 90 int zigbee::PanScan()
gert_lauritsen 12:debf76f0c0bf 91 {//
gert_lauritsen 11:18ff088287ea 92 _zbee.printf("AT+PANSCAN\r");
gert_lauritsen 7:45511c3d2950 93 return 1;
gert_lauritsen 7:45511c3d2950 94 }
gert_lauritsen 7:45511c3d2950 95
gert_lauritsen 7:45511c3d2950 96
gert_lauritsen 7:45511c3d2950 97 int zigbee::Establish_Network()
gert_lauritsen 12:debf76f0c0bf 98 {//
gert_lauritsen 11:18ff088287ea 99 _zbee.printf("AT+EN\r");
tristanjph 2:cb627ea9b817 100 return 1;
tristanjph 2:cb627ea9b817 101 }
tristanjph 3:682615a0717e 102
gert_lauritsen 7:45511c3d2950 103
gert_lauritsen 7:45511c3d2950 104 int zigbee::JoinNetwork()
gert_lauritsen 7:45511c3d2950 105 {
gert_lauritsen 11:18ff088287ea 106 _zbee.printf("AT+JN\r");
gert_lauritsen 7:45511c3d2950 107 return 1;
gert_lauritsen 7:45511c3d2950 108 }
gert_lauritsen 7:45511c3d2950 109
gert_lauritsen 7:45511c3d2950 110 int zigbee::ScanNetwork()
gert_lauritsen 7:45511c3d2950 111 {
gert_lauritsen 11:18ff088287ea 112 _zbee.printf("AT+SN\r");
gert_lauritsen 7:45511c3d2950 113 return 1;
gert_lauritsen 7:45511c3d2950 114 }
gert_lauritsen 7:45511c3d2950 115
gert_lauritsen 7:45511c3d2950 116 int zigbee::NetworkInfo()
gert_lauritsen 7:45511c3d2950 117 {
gert_lauritsen 7:45511c3d2950 118 //Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522"
gert_lauritsen 11:18ff088287ea 119 _zbee.printf("AT+N\r");
gert_lauritsen 7:45511c3d2950 120 return 1;
gert_lauritsen 7:45511c3d2950 121 }
gert_lauritsen 7:45511c3d2950 122
gert_lauritsen 9:c8e4339ccc29 123 int zigbee::UniCast(char *adr,char *payload) //Ascii mode with null terminated string
gert_lauritsen 7:45511c3d2950 124 {
gert_lauritsen 11:18ff088287ea 125 _zbee.printf("AT+UCAST:%s=%s\r",adr,payload);
gert_lauritsen 12:debf76f0c0bf 126 // _zbee.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata);
gert_lauritsen 7:45511c3d2950 127 return 1;
gert_lauritsen 7:45511c3d2950 128 }
gert_lauritsen 7:45511c3d2950 129
gert_lauritsen 9:c8e4339ccc29 130 int zigbee::UniCastb(char *adr,char *payload, char payloadSize) //sends data in binary format
gert_lauritsen 7:45511c3d2950 131 {
gert_lauritsen 11:18ff088287ea 132 _zbee.printf("AT+UCASTB:%X,%s\r",adr,payloadSize);
gert_lauritsen 12:debf76f0c0bf 133 //_zbee.scanf ("%*s");
gert_lauritsen 7:45511c3d2950 134 return 1;
gert_lauritsen 7:45511c3d2950 135 }
gert_lauritsen 7:45511c3d2950 136
gert_lauritsen 12:debf76f0c0bf 137 //-----------------------Handle incoming data------------------------------
tristanjph 3:682615a0717e 138
gert_lauritsen 11:18ff088287ea 139 unsigned long zigbee::hextolong(const char *hex)
gert_lauritsen 7:45511c3d2950 140 {
gert_lauritsen 7:45511c3d2950 141 //return 32 bit
gert_lauritsen 7:45511c3d2950 142 unsigned long result = 0;
gert_lauritsen 7:45511c3d2950 143 while (*hex) { //så længe det ikke er null
gert_lauritsen 7:45511c3d2950 144 if (*hex >= '0' && *hex <= '9')
gert_lauritsen 7:45511c3d2950 145 result += (*hex - '0');
gert_lauritsen 7:45511c3d2950 146 else if (*hex >= 'A' && *hex <= 'F')
gert_lauritsen 7:45511c3d2950 147 result += (*hex - 'A' +10);
gert_lauritsen 7:45511c3d2950 148 else if (*hex >= 'a' && *hex <= 'f')
gert_lauritsen 7:45511c3d2950 149 result += (*hex - 'a'+ 10);
gert_lauritsen 7:45511c3d2950 150
gert_lauritsen 12:debf76f0c0bf 151 if (*++hex) //if the neext isn't a null
gert_lauritsen 7:45511c3d2950 152 result <<= 4;
gert_lauritsen 7:45511c3d2950 153 }
gert_lauritsen 7:45511c3d2950 154 return result;
gert_lauritsen 7:45511c3d2950 155 }
gert_lauritsen 7:45511c3d2950 156
gert_lauritsen 7:45511c3d2950 157
gert_lauritsen 11:18ff088287ea 158 unsigned int zigbee::hextoint(const char *hex)
gert_lauritsen 7:45511c3d2950 159 {
gert_lauritsen 7:45511c3d2950 160 //return 16 bit
gert_lauritsen 7:45511c3d2950 161 unsigned int result = 0;
gert_lauritsen 7:45511c3d2950 162 while (*hex) { //så længe det ikke er null
gert_lauritsen 7:45511c3d2950 163 if (*hex >= '0' && *hex <= '9')
gert_lauritsen 7:45511c3d2950 164 result += (*hex - '0');
gert_lauritsen 7:45511c3d2950 165 else if (*hex >= 'A' && *hex <= 'F')
gert_lauritsen 7:45511c3d2950 166 result += (*hex - 'A' +10);
gert_lauritsen 7:45511c3d2950 167 else if (*hex >= 'a' && *hex <= 'f')
gert_lauritsen 7:45511c3d2950 168 result += (*hex - 'a'+ 10);
gert_lauritsen 7:45511c3d2950 169
gert_lauritsen 12:debf76f0c0bf 170 if (*++hex) //if the neext isn't a null
gert_lauritsen 7:45511c3d2950 171 result <<= 4;
gert_lauritsen 7:45511c3d2950 172 }
gert_lauritsen 7:45511c3d2950 173 return result;
gert_lauritsen 7:45511c3d2950 174 }
gert_lauritsen 7:45511c3d2950 175 //---------------------------------------------------------------
gert_lauritsen 11:18ff088287ea 176 void zigbee::readPacket() {
gert_lauritsen 11:18ff088287ea 177 //read and waits for the packet
gert_lauritsen 12:debf76f0c0bf 178 Timer t;
gert_lauritsen 12:debf76f0c0bf 179 t.reset();
gert_lauritsen 12:debf76f0c0bf 180 t.start();
gert_lauritsen 12:debf76f0c0bf 181 while ((GotFrame==0) & (t.read_ms() < 500)){}
gert_lauritsen 12:debf76f0c0bf 182 t.stop();
gert_lauritsen 12:debf76f0c0bf 183 GotFrame=0;
gert_lauritsen 11:18ff088287ea 184 }
gert_lauritsen 7:45511c3d2950 185
gert_lauritsen 11:18ff088287ea 186
gert_lauritsen 12:debf76f0c0bf 187 void zigbee::SeePacket() {
gert_lauritsen 11:18ff088287ea 188 char *p;
gert_lauritsen 12:debf76f0c0bf 189 b = _zbee.getc();
gert_lauritsen 12:debf76f0c0bf 190 if ((b!=CR) & (b!=LF)) _responseFrameData[_pos]=b;
gert_lauritsen 12:debf76f0c0bf 191 _pos=(_pos+1) % MAX_FRAME_DATA_SIZE;
gert_lauritsen 12:debf76f0c0bf 192 if (b==CR) {
gert_lauritsen 12:debf76f0c0bf 193 memcpy(&_responseFrameString,&_responseFrameData,_pos);
gert_lauritsen 12:debf76f0c0bf 194 GotFrame=1;
gert_lauritsen 12:debf76f0c0bf 195 _responseFrameString[_pos]=0; //Nul terminate
gert_lauritsen 12:debf76f0c0bf 196 _pos=0;
gert_lauritsen 12:debf76f0c0bf 197 if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something
gert_lauritsen 12:debf76f0c0bf 198 //Do something
gert_lauritsen 11:18ff088287ea 199 } else
gert_lauritsen 12:debf76f0c0bf 200 if (strstr(_responseFrameString,"UCAST:")) {//checke for incoming UCAST data
gert_lauritsen 12:debf76f0c0bf 201 //if (sscanf (_responseFrameString,"UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata)>0)
gert_lauritsen 12:debf76f0c0bf 202 p=strstr(_responseFrameString,"=");
gert_lauritsen 12:debf76f0c0bf 203 if (p) {
gert_lauritsen 12:debf76f0c0bf 204 p++;
gert_lauritsen 12:debf76f0c0bf 205 strcpy(Zdata,p);
gert_lauritsen 12:debf76f0c0bf 206 Zdat=1;
gert_lauritsen 12:debf76f0c0bf 207 }
gert_lauritsen 11:18ff088287ea 208 }
gert_lauritsen 12:debf76f0c0bf 209 if (strstr(_responseFrameString,"LeftPAN:")) PanOnline=0; //Local node has left the Pan
gert_lauritsen 11:18ff088287ea 210
gert_lauritsen 12:debf76f0c0bf 211 if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID>
gert_lauritsen 12:debf76f0c0bf 212 //new node on the pan
gert_lauritsen 12:debf76f0c0bf 213 }
gert_lauritsen 12:debf76f0c0bf 214
gert_lauritsen 12:debf76f0c0bf 215 if ((strstr(_responseFrameString,"OK")>0) & (strstr(_responseFrameString,"TOKDUMP")==0) ) {
gert_lauritsen 12:debf76f0c0bf 216 //if (ScriptState) ConnectScript();
gert_lauritsen 12:debf76f0c0bf 217 //Ok=1; Cmd=0; LineNo=0;
gert_lauritsen 12:debf76f0c0bf 218 }
gert_lauritsen 12:debf76f0c0bf 219 if (strstr(_responseFrameString,"+N=")) {
gert_lauritsen 12:debf76f0c0bf 220 sscanf (_responseFrameString,"+N=%s,%d,%d,%4X,",Devicetype,&channel,&NodeID,&EPID);
gert_lauritsen 12:debf76f0c0bf 221 }
gert_lauritsen 12:debf76f0c0bf 222 if (strstr(_responseFrameString,"ACK")) { //
gert_lauritsen 12:debf76f0c0bf 223 PacketAck=1;
gert_lauritsen 12:debf76f0c0bf 224 p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
gert_lauritsen 12:debf76f0c0bf 225 SeqNumber=hextoint(p);
gert_lauritsen 12:debf76f0c0bf 226 }
gert_lauritsen 12:debf76f0c0bf 227 if (strstr(_responseFrameString,"NAK")) {
gert_lauritsen 12:debf76f0c0bf 228 PacketAck=0;
gert_lauritsen 12:debf76f0c0bf 229 p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
gert_lauritsen 12:debf76f0c0bf 230 SeqNumber=hextoint(p);
gert_lauritsen 12:debf76f0c0bf 231 }
gert_lauritsen 12:debf76f0c0bf 232 if (strstr(_responseFrameString,"SEQ")) { //
gert_lauritsen 12:debf76f0c0bf 233 PacketAck=1;
gert_lauritsen 12:debf76f0c0bf 234 p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
gert_lauritsen 12:debf76f0c0bf 235 SeqNumber=hextoint(p);
gert_lauritsen 12:debf76f0c0bf 236 }
gert_lauritsen 12:debf76f0c0bf 237 }
gert_lauritsen 11:18ff088287ea 238 }