ThingPlug Test

Dependents:   WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P

Fork of WizFi310Interface by WIZnet

Files at this revision

API Documentation at this revision

Comitter:
jehoon
Date:
Mon Oct 10 05:12:51 2016 +0000
Parent:
0:df571f8f8c03
Child:
2:04c8d61984a3
Commit message:
mqtt receive

Changed in this revision

WizFi310/WizFi310.cpp Show annotated file Show diff for this revision Revisions of this file
WizFi310/WizFi310.h Show annotated file Show diff for this revision Revisions of this file
WizFi310/WizFi310_at.cpp Show annotated file Show diff for this revision Revisions of this file
WizFi310/WizFi310_msg.cpp Show annotated file Show diff for this revision Revisions of this file
WizFi310/WizFi310_sock.cpp Show annotated file Show diff for this revision Revisions of this file
WizFi310Interface.cpp Show annotated file Show diff for this revision Revisions of this file
WizFi310Interface.h Show annotated file Show diff for this revision Revisions of this file
--- a/WizFi310/WizFi310.cpp	Wed Oct 05 09:40:30 2016 +0000
+++ b/WizFi310/WizFi310.cpp	Mon Oct 10 05:12:51 2016 +0000
@@ -70,10 +70,6 @@
     if ( cmdWNET(_state.dhcp) ) return -1;
     if ( cmdWSET(_state.wm, _state.ssid) ) return -1;
 
-    //NSAPI_SECURITY_NONE = 0,   /*!< open access point */
-//    NSAPI_SECURITY_WEP,        /*!< phrase conforms to WEP */
-//    NSAPI_SECURITY_WPA,        /*!< phrase conforms to WPA */
-//    NSAPI_SECURITY_WPA2,       /*!< phrase conforms to WPA2 */
 
     switch (_state.sec)
     {
@@ -94,37 +90,6 @@
         break;
     }
         
-
-/* daniel
-    switch (_state.sec)
-    {
-    case SEC_AUTO:
-        strcpy(sec,"");
-        break;
-    case SEC_OPEN:
-        strcpy(sec,"OPEN");
-        break;
-    case SEC_WEP:
-        strcpy(sec,"WEP");
-        break;
-    case SEC_WPA_TKIP:
-        strcpy(sec,"WPA");
-        break;
-    case SEC_WPA_AES:
-        strcpy(sec,"WPAAES");
-        break;
-    case SEC_WPA2_AES:
-        strcpy(sec,"WPA2AES");
-        break;
-    case SEC_WPA2_TKIP:
-        strcpy(sec,"WPA2TKIP");
-        break;
-    case SEC_WPA2_MIXED:
-        strcpy(sec,"WPA2");
-        break;
-    }
-*/
-    
     if ( cmdWSEC(_state.wm, _state.pass, sec) ) return -1;
     if ( cmdWJOIN() )   return -1;;
     _state.associated = true;
--- a/WizFi310/WizFi310.h	Wed Oct 05 09:40:30 2016 +0000
+++ b/WizFi310/WizFi310.h	Mon Oct 10 05:12:51 2016 +0000
@@ -103,6 +103,7 @@
         RES_FDNS,
         RES_SMGMT,
         RES_WSTATUS,
+        
     };
 
     enum Mode {
@@ -267,6 +268,9 @@
     void msgConnect     (const char *buf);
     void msgDisconnect  (const char *buf);
     void msgListen      (const char *buf);
+    //daniel
+    void msgMQTTConnect (const char *buf);
+    void msgMQTTDisconnect (const char *buf);
 
     void resMacAddress  (const char *buf);
     void resWJOIN       (const char *buf);
@@ -320,6 +324,10 @@
     * @return true only if the chip has an IP address
     */
     bool isConnected(void);
+    
+    
+    //daniel for mqtt
+    char rcvd_mqtt_topic[128];
 
 };
 
--- a/WizFi310/WizFi310_at.cpp	Wed Oct 05 09:40:30 2016 +0000
+++ b/WizFi310/WizFi310_at.cpp	Mon Oct 10 05:12:51 2016 +0000
@@ -69,6 +69,7 @@
             if (_state.ok && _state.res == RES_NULL){
                 break;
             }
+            
             if (_state.failure || t.read_ms() > timeout)
             {
                 WIZ_WARN("failure of timeout[%d]ms\r\n",t.read_ms());
--- a/WizFi310/WizFi310_msg.cpp	Wed Oct 05 09:40:30 2016 +0000
+++ b/WizFi310/WizFi310_msg.cpp	Mon Oct 10 05:12:51 2016 +0000
@@ -27,11 +27,16 @@
 #define WIZ_DBG(x, ...)
 #endif
 
+//daniel
+char g_asyncbuf[256];
+
 // This function is operating in ISR. So you can't use debug message.
 void WizFi310::recvData ( char c )
 {
     static int cid, sub, len, count;
-
+    static int is_mqtt_data = 0;
+    char tbf[10];
+    
     switch(_state.mode)
     {
         case MODE_COMMAND:
@@ -76,6 +81,7 @@
             break;
 
         case MODE_DATA_RX:
+            
             switch(sub)
             {
                 case 0:
@@ -90,6 +96,13 @@
                     count = 0;
                     len = 0;
                 }
+                //daniel add for mqtt
+                else if ( c == 'Q' )
+                {
+                    cid = 0;
+                    is_mqtt_data = 1;
+                }
+                //
                 else
                 {
                     _state.mode = MODE_COMMAND;
@@ -97,8 +110,9 @@
                 break;
 
             case 1:
-                // ip
-                if ((c >= '0' && c <= '9') || c == '.')
+                // ip                
+//                if ((c >= '0' && c <= '9') || c == '.')
+                if (((c >= '0' && c <= '9') || c == '.') && is_mqtt_data == 0 )
                 {
                     _con[cid].ip[count] = c;
                     count++;
@@ -109,7 +123,14 @@
                     _con[cid].port = 0;
                     sub++;
                 }
-                else
+                //daniel for mqtt
+                else if( is_mqtt_data == 1)
+                {
+                    rcvd_mqtt_topic[count] = c;
+                    count++;
+                }
+//              else 
+                else if( is_mqtt_data == 0 )
                 {
                     _state.mode = MODE_COMMAND;
                 }
@@ -152,6 +173,7 @@
                 break;
 
             default:
+
                 if(_con[cid].buf != NULL)
                 {
                     _con[cid].buf->queue(c);
@@ -166,6 +188,8 @@
                 if(_con[cid].recv_length == 0)
                 {
                      //WIZ_DBG("recv cid: %d, count : %d, len : %d",cid, count, len);
+                     //sprintf(tbf, "recv cid: %d, count : %d, len : %d",cid, count, len);
+                     //strcat(g_asyncbuf, tbf);
                     _con[cid].received = true;
                     _state.mode = MODE_COMMAND;
                 }
@@ -176,7 +200,9 @@
 }
 
 
-#define MSG_TABLE_NUM 6
+//#define MSG_TABLE_NUM 6
+//daniel
+#define MSG_TABLE_NUM 8
 #define RES_TABLE_NUM 7
 int WizFi310::parseMessage () {
     int i;
@@ -192,6 +218,9 @@
         {"[CONNECT ",               &WizFi310::msgConnect},
         {"[DISCONNECT ",            &WizFi310::msgDisconnect},
         {"[LISTEN ",                &WizFi310::msgListen},
+        //daniel
+        {"[MQTT CONNECT]",               &WizFi310::msgMQTTConnect},
+        {"[MQTT DISCONNECT]",               &WizFi310::msgMQTTDisconnect},
     };
     static const struct RES_TABLE{
         const Response res;
@@ -286,6 +315,35 @@
     _con[cid].connected = false;
 }
 
+
+void WizFi310::msgMQTTConnect (const char *buf)
+{
+    int cid = 0;
+
+    //if (buf[9] < '0' || buf[9] > '8' || buf[10] != ']') return;
+
+    //cid = x2i(buf[9]);
+    initCon(cid, true);
+    _state.cid = cid;
+    _con[cid].accept = true;
+    _con[cid].parent = cid;
+    
+    _con[cid].connected = true;
+    _state.res = RES_NULL;
+    _state.ok = true;
+}
+
+void WizFi310::msgMQTTDisconnect (const char *buf)
+{
+    int cid = 0;
+
+    //if(buf[12] < '0' || buf[12] > '8' || buf[13] != ']')    return;
+
+    //cid = x2i(buf[12]);
+    _con[cid].connected = false;
+}
+
+
 void WizFi310::msgListen (const char *buf)
 {
     int cid;
@@ -428,4 +486,4 @@
         _state.gateway[gw_idx] = '\0';
         _state.res = RES_NULL;
     }
-}
+}
\ No newline at end of file
--- a/WizFi310/WizFi310_sock.cpp	Wed Oct 05 09:40:30 2016 +0000
+++ b/WizFi310/WizFi310_sock.cpp	Mon Oct 10 05:12:51 2016 +0000
@@ -169,14 +169,17 @@
     int i;
 
     if (!isConnected(cid))  return -1;
+    
+    if (_con[cid].buf == NULL ) return 0;
 
-    if (_con[cid].buf == NULL ) return 0;
     while (!_con[cid].received && _state.mode != MODE_COMMAND);
     _con[cid].received = false;
+    
     for(i=0; i<len; i++)
     {
         if(_con[cid].buf->dequeue(&buf[i]) == false)    break;
     }
+    
     setRts(true);       // release
     return i;
 }
@@ -215,7 +218,7 @@
 bool WizFi310::isConnected (int cid)
 {
     if ( cid < 0 || cid >=8 ) return false;
-
+    //printf("%d %d\r\n", cid, _con[cid].connected);
     return _con[cid].connected;
 }
 
--- a/WizFi310Interface.cpp	Wed Oct 05 09:40:30 2016 +0000
+++ b/WizFi310Interface.cpp	Mon Oct 10 05:12:51 2016 +0000
@@ -211,4 +211,3 @@
 void WizFi310Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
 {
 }
-
--- a/WizFi310Interface.h	Wed Oct 05 09:40:30 2016 +0000
+++ b/WizFi310Interface.h	Mon Oct 10 05:12:51 2016 +0000
@@ -25,11 +25,15 @@
 /** WizFi310Interface class
  *  Implementation of the NetworkStack for the WizFi310
  */
+ 
+ 
 class WizFi310Interface : public NetworkStack, public WiFiInterface
 {
 public:
     WizFi310Interface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud=115200 );
 
+    
+
     virtual int connect(
         const char *ssid,
         const char *pass,
@@ -49,7 +53,13 @@
      *  @return             MAC address of the interface
      */
     virtual const char *get_mac_address();
-
+    
+    
+    WizFi310* get_WizFi310_Pointer()
+    {
+         return &_wizfi310;
+    }
+    
 protected:
     /** Open a socket
      *  @param handle       Handle in which to store new socket
@@ -148,6 +158,9 @@
      *  @note Callback may be called in an interrupt context.
      */
     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
+    
+    
+    
 
 private:
     WizFi310 _wizfi310;