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

Fork of xbee_lib by Tristan Hughes

Revision:
26:2a85af491d92
Parent:
25:31120552e53c
Child:
27:a38b67f5be8f
--- a/telegesis.cpp	Sat Nov 16 13:14:26 2013 +0000
+++ b/telegesis.cpp	Sun Dec 22 14:46:40 2013 +0000
@@ -17,16 +17,22 @@
 {
 }
 
-void zigbee::Work() {
+bool zigbee::Work()
+{
     //read and waits for the packet
+    bool datain=false;
     Timer t;
     t.reset();
-    t.start();    
+    t.start();
     b=0;
-    while((b!=CR) && (t.read_ms() < 1000))
-        if (_zbee.readable()) SeePacket();
-    t.stop(); 
-  //if (_zbee.readable()) readPacket();
+    while((b!=CR) && (t.read_ms() < 200))
+        if (_zbee.readable()) {
+            datain=true;
+            SeePacket();
+        }
+    t.stop();
+    return datain;
+    //if (_zbee.readable()) readPacket();
 }
 
 bool zigbee::wait4str(char *p) //joinpan
@@ -35,8 +41,8 @@
     t.reset();
     t.start();
     while ((strstr(_responseFrameString,p)==0) && (t.read_ms() < 5000)) readPacket();
-      t.stop();
-      return (strstr(_responseFrameString,p)>0);
+    t.stop();
+    return (strstr(_responseFrameString,p)>0);
 }
 
 
@@ -47,8 +53,8 @@
     t.start();
     readPacket();
     while ((strstr(_responseFrameString,"JPAN")==0) && (t.read_ms() < 5000)) readPacket();
-      t.stop();
-      return (strstr(_responseFrameString,"JPAN")>0);
+    t.stop();
+    return (strstr(_responseFrameString,"JPAN")>0);
 }
 
 
@@ -59,8 +65,8 @@
     t.start();
     readPacket();
     while ((PanOnline>0) && (t.read_ms() < 3000)) readPacket();
-      t.stop();
-      return PanOnline;
+    t.stop();
+    return PanOnline;
 }
 
 
@@ -71,8 +77,8 @@
     t.start();
     readPacket();
     while ((strstr(_responseFrameString,"OK")==0) && (t.read_ms() < 500)) readPacket();
-      t.stop();
-      return (strstr(_responseFrameString,"OK")>0);
+    t.stop();
+    return (strstr(_responseFrameString,"OK")>0);
 }
 
 int zigbee::GetSerial()
@@ -89,7 +95,7 @@
     readPacket();
     readPacket();
     strcpy(LocalID,_responseFrameString); //vi lægger den bare over i stringformat. Så kan man altid senere convertere
-    wait4OK();  
+    wait4OK();
     //   LocalID=hextoint(_responseFrameString);
     //   wait_ms(5);
     return 1;
@@ -166,8 +172,8 @@
 {
     //
     _zbee.printf("AT+EN\r");
-      wait4JPAN();
-      wait4OK();
+    wait4JPAN();
+    wait4OK();
     return 1;
 }
 
@@ -175,8 +181,8 @@
 int zigbee::LeaveNetwork()
 {
     _zbee.printf("AT+DASSL\r");
-      wait4OK();
-      wait4Offline();
+    wait4OK();
+    wait4Offline();
     return 1;
 }
 
@@ -203,7 +209,7 @@
 int zigbee::UniCast(char *adr,char *payload)  //Ascii mode with null terminated string
 {
     _zbee.printf("AT+UCAST:%s=%s\r",adr,payload);
-   // printf("AT+UCAST:%s=%s\r",adr,payload);
+    // printf("AT+UCAST:%s=%s\r",adr,payload);
 //    _zbee.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata);
     return 1;
 }
@@ -273,21 +279,35 @@
     char *p;
     static uint8_t _pos;
     b = _zbee.getc();
-    if ((b!=CR) && (b!=LF)) { 
-            _responseFrameString[_pos]=b;
-      _pos=(_pos+1) % MAX_FRAME_DATA_SIZE;
-        }   
+    
+    // JHL : Validate that the incoming character is an ASCII character or CR/LF
+    switch (b) {
+        case CR:
+        case LF:
+        case ','...'}':
+            break;        
+        default:
+            // Non ASCII charater received!
+            printf("Non ASCII! %d\n\r", b);
+            _zbee.send_break();
+            return;
+    }            
+            
+    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;
-     //   printf(_responseFrameString);
+        //printf("%s \r\n",_responseFrameString);
         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,"UCAST:");            
+            p=strstr(_responseFrameString,"UCAST:");
             p=p+6;
             strncpy(NetNodeID,p,16);
             NetNodeID[16]=0;
@@ -310,8 +330,9 @@
         if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID>
             //new node on the pan
             p=strstr(_responseFrameString,"NEWNODE:");
-            p=p+13; NetInfo=1;
-            strncpy(NetNodeID,p,16);             
+            p=p+13;
+            NetInfo=1;
+            strncpy(NetNodeID,p,16);
         }
 
         if ((strstr(_responseFrameString,"OK")>0) && (strstr(_responseFrameString,"TOKDUMP")==0) ) {
@@ -321,18 +342,19 @@
         if (strstr(_responseFrameString,"FFD:")) {
             //FFD:000D6F00026C5BEA,0000
             p=strstr(_responseFrameString,"FFD:");
-            p=p+4; NetInfo=1;
-            strncpy(NetNodeID,p,16);                                        
-        }                   
+            p=p+4;
+            NetInfo=1;
+            strncpy(NetNodeID,p,16);
+        }
         if (strstr(_responseFrameString,"+N=")) {
-    //        sscanf (_responseFrameString,"+N=%s,%d,%d,%4X,",Devicetype,&channel,&NodeID,&EPID);
+            //        sscanf (_responseFrameString,"+N=%s,%d,%d,%4X,",Devicetype,&channel,&NodeID,&EPID);
             p=strstr(_responseFrameString,"+N=");
             p=p+3;
             strncpy(Devicetype,p,3);
-            Devicetype[3]=0; //null termination                    
+            Devicetype[3]=0; //null termination
         }
         if (strstr(_responseFrameString,"ACK")) { //
-            //PacketAck=1;
+            PacketAck=1;
             p=strstr(_responseFrameString,":");
             p++; //P indholder nu SEQ nummer
             SeqNumber=hextoint(p);
@@ -344,7 +366,7 @@
             SeqNumber=hextoint(p);
         }
         if (strstr(_responseFrameString,"SEQ")) { //
-            PacketAck=1;
+            // PacketAck=1;
             p=strstr(_responseFrameString,":");
             p++; //P indholder nu SEQ nummer
             SeqNumber=hextoint(p);
@@ -359,11 +381,11 @@
     //read and waits for the packet
     Timer t;
     t.reset();
-    t.start();    
+    t.start();
     b=0;
     while((b!=CR) && (t.read_ms() < 1000))
         if (_zbee.readable()) SeePacket();
-    t.stop();     
+    t.stop();
 }
 
 #endif