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

Fork of xbee_lib by Tristan Hughes

Revision:
20:070dc2050052
Parent:
19:78f44233a12f
Child:
21:9c5b72a64495
--- a/telegesis.cpp	Fri Oct 18 17:42:24 2013 +0000
+++ b/telegesis.cpp	Sat Oct 19 18:47:59 2013 +0000
@@ -21,6 +21,41 @@
   if (_zbee.readable()) readPacket();
 }
 
+bool zigbee::wait4str(char *p) //joinpan
+{
+    Timer t;
+    t.reset();
+    t.start();
+    while ((strstr(_responseFrameString,p)==0) & (t.read_ms() < 5000)) readPacket();
+      t.stop();
+      return (strstr(_responseFrameString,p)>0);
+}
+
+
+bool zigbee::wait4JPAN() //joinpan
+{
+    Timer t;
+    t.reset();
+    t.start();
+    readPacket();
+    while ((strstr(_responseFrameString,"JPAN")==0) & (t.read_ms() < 5000)) readPacket();
+      t.stop();
+      return (strstr(_responseFrameString,"JPAN")>0);
+}
+
+
+bool zigbee::wait4Offline()
+{
+    Timer t;
+    t.reset();
+    t.start();
+    readPacket();
+    while ((PanOnline>0) & (t.read_ms() < 3000)) readPacket();
+      t.stop();
+      return PanOnline;
+}
+
+
 bool zigbee::wait4OK()
 {
     Timer t;
@@ -28,7 +63,8 @@
     t.start();
     readPacket();
     while ((strstr(_responseFrameString,"OK")==0) & (t.read_ms() < 500)) readPacket();
-    return (strstr(_responseFrameString,"OK")>0);
+      t.stop();
+      return (strstr(_responseFrameString,"OK")>0);
 }
 
 int zigbee::GetSerial()
@@ -40,13 +76,15 @@
     OK
     */
     _zbee.printf("ATI\r");
-    readPacket();
+    /*readPacket();
     readPacket(); //commando
-    readPacket();
+    readPacket();*/
+      wait4str("Telegesis");
     sscanf(_responseFrameString,"Telegesis %s",HWType);
     readPacket();
     readPacket();
     strcpy(LocalID,_responseFrameString); //vi lægger den bare over i stringformat. Så kan man altid senere convertere
+    wait4OK();  
     //   LocalID=hextoint(_responseFrameString);
     //   wait_ms(5);
     return 1;
@@ -123,10 +161,20 @@
 {
     //
     _zbee.printf("AT+EN\r");
+      wait4JPAN();
+      wait4OK();
     return 1;
 }
 
 
+int zigbee::LeaveNetwork()
+{
+    _zbee.printf("AT+DASSL\r");
+      wait4OK();
+      wait4Offline();
+    return 1;
+}
+
 int zigbee::JoinNetwork()
 {
     _zbee.printf("AT+JN\r");
@@ -143,6 +191,7 @@
 {
 //Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522"
     _zbee.printf("AT+N\r");
+    wait4OK();
     return 1;
 }
 
@@ -218,8 +267,10 @@
     char *p;
     static uint8_t _pos;
     b = _zbee.getc();
-    if ((b!=CR) & (b!=LF)) _responseFrameString[_pos]=b;
-    _pos=(_pos+1) % MAX_FRAME_DATA_SIZE;
+    if ((b!=CR) & (b!=LF)) { 
+            _responseFrameString[_pos]=b;
+      _pos=(_pos+1) % MAX_FRAME_DATA_SIZE;
+        }   
     if (b==CR) {
         //  memcpy(&_responseFrameString,&_responseFrameData,_pos);
         GotFrame=1;
@@ -246,14 +297,27 @@
 
         if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID>
             //new node on the pan
+            p=strstr(_responseFrameString,"NEWNODE:");
+            p=p+8; NetInfo=1;
+            strncpy(NetNodeID,p,16);             
         }
 
         if ((strstr(_responseFrameString,"OK")>0) & (strstr(_responseFrameString,"TOKDUMP")==0) ) {
             //if (ScriptState) ConnectScript();
             //Ok=1; Cmd=0; LineNo=0;
         }
+        if (strstr(_responseFrameString,"FFD:")) {
+            //FFD:000D6F00026C5BEA,0000
+            p=strstr(_responseFrameString,"FFD:");
+            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                    
         }
         if (strstr(_responseFrameString,"ACK")) { //
             PacketAck=1;
@@ -290,4 +354,4 @@
     t.stop();     
 }
 
-#endif
\ No newline at end of file
+#endif