this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Files at this revision

API Documentation at this revision

Comitter:
ocomeni
Date:
Sun Apr 14 14:38:51 2019 +0000
Parent:
92:ec9550034276
Child:
94:fb4414aff957
Commit message:
fixed bug with wifi configuration API (was adding an extra character causing wifi connection to fail because of incorrect credentials)

Changed in this revision

source/ATCmdManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/ATCmdManager.h Show annotated file Show diff for this revision Revisions of this file
source/WiFiManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/common_config.h Show annotated file Show diff for this revision Revisions of this file
source/common_types.h Show annotated file Show diff for this revision Revisions of this file
--- a/source/ATCmdManager.cpp	Sat Apr 13 14:17:29 2019 +0000
+++ b/source/ATCmdManager.cpp	Sun Apr 14 14:38:51 2019 +0000
@@ -173,6 +173,14 @@
                 _smutex.unlock();
                 at_resp = AT_RESP_NONE;
                 break;
+            case AT_COMMAND_FAILED:  
+                // AT_HTTP_RESP response state 
+                printf("\n [ATCMD MAN] WIFI COMMAND FAILED!!\n");
+                _smutex.lock();
+                _parser.send("\r\nNACK\r\n");
+                _smutex.unlock();
+                at_resp = AT_RESP_NONE;
+                break;
             default:
                 // UNKNOWN response state
                 _smutex.lock();
@@ -736,9 +744,11 @@
 { 
     int c;
     int sptr = 0;
+    int quoteCnt = 0;
     for(int i=0;i<maxBytes;i++){
         c = _parser.getc();
-        if(c==-1){
+        if(c== '"')quoteCnt++;
+        if(c==-1 || quoteCnt==2){
             buf[sptr] = NULL; // null terminate if string
             return i;
         }
@@ -870,8 +880,6 @@
     wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
     if(wifiCmd == NULL){
         printf("[ATCMD MAN] queued memory allocation failed\n");
-        print_memory_info();
-        print_heap_and_isr_stack_info();
         return false;
     }
 #else
@@ -905,6 +913,8 @@
 }
 
 bool  ATCmdManager::queueWiFiDataRequest(wifi_data_msg_t data_req){
+    //print_memory_info();
+    print_heap_and_isr_stack_info();
     wifi_data_msg_t *wifiData = _aT2WiFiDatamPool->alloc();
     wifiData->wifi_cmd        = data_req.wifi_cmd;
     wifiData->dataLen        = data_req.dataLen;
@@ -917,15 +927,29 @@
 
 bool  ATCmdManager::dequeueWiFidataResponse(){
     if(at_resp != AT_RESP_NONE) return false; // busy
+    //print_memory_info();
     osEvent evt = _wiFi2ATDataQueue->get(0);
     if(evt.status == osEventMessage){
         resp_data = (at_data_msg_t*)evt.value.p;
         setNextResponse(resp_data->at_resp);
         //_wiFi2ATDatamPool->free(resp_data);
+        print_heap_and_isr_stack_info();
     }
     return true;
 }
 
+
+void ATCmdManager::sendAtConfirmation(const char *buf)
+{
+    int len =  strlen(buf);
+    outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, CONFIRMATION_MSG_TYPE, NO_CHANNEL);
+}
+void ATCmdManager::sendAtEvent(const char *buf)
+{
+    int len =  strlen(buf);
+    outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, EVENT_MSG_TYPE, NO_CHANNEL);
+}
+
 void ATCmdManager::outputEDMdata(const uint8_t *buf, int pLen, 
                                  edm_msg_id_t identifier, edm_msg_type_t type,
                                  channel_id_t channel_id)
@@ -940,8 +964,9 @@
     // send EDM Identifier + Type
     _parser.putc(identifier>>8);
     _parser.putc(identifier%256 | type);
-    // send EDM Identifier + Type
-    _parser.putc(channel_id);
+    // send channel id if valid
+    if(channel_id != NO_CHANNEL)
+        _parser.putc(channel_id);
     // send the data
     _parser.write((const char *)buf, pLen);
     // send EDM Message stop byte
@@ -967,5 +992,6 @@
     _smutex.unlock();
     _wiFi2ATDatamPool->free(resp_data);
     
-    
+    //print_memory_info();
+    print_heap_and_isr_stack_info();   
 }
--- a/source/ATCmdManager.h	Sat Apr 13 14:17:29 2019 +0000
+++ b/source/ATCmdManager.h	Sun Apr 14 14:38:51 2019 +0000
@@ -113,6 +113,9 @@
                               edm_msg_id_t identifier, edm_msg_type_t type,
                               channel_id_t channel_id);
     
+    void        sendAtConfirmation(const char *buf);
+    void        sendAtEvent(const char *buf);
+    
     /**
     * Allows timeout to be changed between commands
     *
--- a/source/WiFiManager.cpp	Sat Apr 13 14:17:29 2019 +0000
+++ b/source/WiFiManager.cpp	Sun Apr 14 14:38:51 2019 +0000
@@ -89,8 +89,18 @@
                 break;
             case WIFI_CMD_CONNECT:
                 error = connect();
+                int secCount = 0;
+                while(secCount++ < WIFI_CONNECT_TIMEOUT_SECS || is_connected==false){
+                    wait(1); // wait 1 sec
+                }
                 wifiCmd = WIFI_CMD_NONE;
-                queueATresponse(AT_CONNECT_RESP);
+                if(is_connected==false){
+                    printf("[WIFI MAN] +++ WIFI CONNECTION TIMEOUT +++ \r\n");
+                    queueATresponse(AT_COMMAND_FAILED);
+                }
+                else {
+                    queueATresponse(AT_CONNECT_RESP);
+                }
                 break;
             case WIFI_CMD_DISCONNECT:
                 error = disconnect();
@@ -372,7 +382,10 @@
         printf("\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n");
         return error;
     }
-    printf("[WIFI-MAN] Connecting to network %s\n", wifi_config.ssid);
+    printf("[WIFI-MAN] Connecting to network ssid = %s passwd = %s  security = %s \r\n", 
+                                                    wifi_config.ssid, 
+                                                    wifi_config.pass, 
+                                                    sec2str(wifi_config.security));
     error = network->connect(wifi_config.ssid,
                      wifi_config.pass,
                      wifi_config.security);
@@ -506,7 +519,9 @@
 
 bool WiFiManager::createTLSconnection(const char * hostName)
 {
-    //mbed_trace_init();
+#ifdef ENABLE_MBED_TRACE
+    mbed_trace_init();
+#endif
     socket = new TLSSocket();
 
     nsapi_error_t r;
@@ -528,7 +543,10 @@
     r = socket->connect(hostName, 443);
     if(r != NSAPI_ERROR_OK)
     { 
-        printf("TLS connect failed for hostname %s!!\n", hostName);
+        char errstr[100];
+        mbedtls_strerror(r, errstr, 100);
+        printf("TLS connect failed for hostname %s -- ERROR = %s !!\n", hostName, errstr);
+        socket->close();
         return false;
     }
     printf("TLS connection successful for https site :  %s\n", hostName);
@@ -560,7 +578,7 @@
     printf("contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen, bodyLen);
     
     if(bodyLen > 10){
-        printf("\n Message Body:\n");
+        printf("\n [WIFI MAN] Message Body:\n");
         printBufferInHex(http_req_cfg->body, bodyLen);
     }
     if(strstr(internet_config.url, "http:")!=NULL) // http request
@@ -601,6 +619,9 @@
         if(https_connection_active == false){
             bool tlsResult;
             tlsResult = createTLSconnection(host);
+#ifdef ENABLE_MBED_TRACE
+            mbed_trace_free(); // free trace memory
+#endif
             if(tlsResult == false){
                  delete socket;
                  free_DataMsg();
--- a/source/common_config.h	Sat Apr 13 14:17:29 2019 +0000
+++ b/source/common_config.h	Sun Apr 14 14:38:51 2019 +0000
@@ -12,6 +12,7 @@
 #define UBLOX_ODIN_W2_RECV_TIMEOUT    100
 #endif
 #define USE_MALLOC_FOR_COMMAND_MEMORY_POOL
+#define WIFI_CONNECT_TIMEOUT_SECS 10
 #define PQDSZ   2  // size of Pool/Queue data structures
 const uint8_t hello_msg[] = {0xaa,0x00,0x96,0x00,0x36,0x00,0x50,0x4f
                             ,0x53,0x54,0x20,0x2f,0x6e,0x75,0x64,0x67
--- a/source/common_types.h	Sat Apr 13 14:17:29 2019 +0000
+++ b/source/common_types.h	Sun Apr 14 14:38:51 2019 +0000
@@ -70,7 +70,8 @@
   AT_HTTPS_RESP,
   AT_HTTPS_RESP_DOWNLOAD,
   AT_HTTP_RESP,
-  AT_HTTP_RESP_DOWNLOAD
+  AT_HTTP_RESP_DOWNLOAD,
+  AT_COMMAND_FAILED
 }at_cmd_resp_t;
 
 typedef enum edm_msg_id
@@ -95,7 +96,8 @@
 typedef enum channel_id
 {
   WIFI_CHANNEL          = 0x00,
-  BLE_CHANNEL           = 0x01
+  BLE_CHANNEL           = 0x01,
+  NO_CHANNEL            = 0xFF
 }channel_id_t;
 
 
@@ -128,6 +130,7 @@
 
 typedef struct {
     wifi_cmd_t    wifi_cmd;   /* wifi command */
+    uint8_t       padding[4-sizeof(wifi_cmd_t)];   /* padding */
 } wifi_cmd_message_t;
 
 typedef struct {
@@ -136,6 +139,7 @@
 
 typedef struct {
     wifi_cmd_t    wifi_cmd;              /* wifi data command */
+    uint8_t       padding[4-sizeof(wifi_cmd_t)];   /* padding */
     int           dataLen;              /* size of data in buffer */
     uint8_t       buffer[TX_BUFFER_LEN]; /* buffer length     */
 } wifi_data_msg_t;