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:
Mon Apr 22 18:34:31 2019 +0000
Parent:
99:05398b3184f8
Child:
101:1cfd468e5009
Commit message:
added support for the following response:; - AT_EVENT; - CONNECT_EVENT

Changed in this revision

source/ATCmdManager.cpp 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/WiFiManager.h 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	Mon Apr 22 11:11:41 2019 +0000
+++ b/source/ATCmdManager.cpp	Mon Apr 22 18:34:31 2019 +0000
@@ -229,6 +229,15 @@
                 at_resp = AT_RESP_NONE;
                 break;
             }
+            case AT_EVENT:  
+            {
+                // AT Event state 
+                printf("\n [ATCMD MAN] AT EVENT RECEIVED!!\r\n");
+                respStr = (char *) resp_data->buffer;
+                sendAtEvent(respStr);
+                at_resp = AT_RESP_NONE;
+                break;
+            }
             case AT_COMMAND_FAILED:  
             {
                 // AT_HTTP_RESP response state 
@@ -241,14 +250,14 @@
                 at_resp = AT_RESP_NONE;
                 break;
             }
-            //default:
-                // UNKNOWN response state
+            default:
+                 //UNKNOWN response state
                 //_smutex.lock();
-                //printf("\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\r\n");
-                //sendAtConfirmationFreeMpool(ERROR_RESP);   //_parser.send(OK_RESP);
+                printf("\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\r\n");
+                sendAtConfirmationFreeMpool(ERROR_RESP);   //_parser.send(OK_RESP);
                 //_smutex.unlock();
-                //at_resp = AT_RESP_NONE;
-                //break;
+                at_resp = AT_RESP_NONE;
+                break;
         }
 }
 
@@ -1058,6 +1067,7 @@
     if(evt.status == osEventMessage){
         at_resp_message_t *resp = (at_resp_message_t*)evt.value.p;
         setNextResponse(resp->at_resp);
+        printf("[ATCMD MAN] dequeued AT CMD : at_resp = %d\n", resp->at_resp);
 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
         _wiFi2ATmPool->free(resp);
         resp = NULL;
@@ -1093,7 +1103,7 @@
     if(evt.status == osEventMessage){
         resp_data = (at_data_msg_t*)evt.value.p;
         setNextResponse(resp_data->at_resp);
-        printf("[ATCMD MAN] dequeued data size = %d : at_resr = %d\n", resp_data->dataLen, resp_data->at_resp);
+        printf("[ATCMD MAN] dequeued data size = %d : at_resp = %d\n", resp_data->dataLen, resp_data->at_resp);
         _event_queue.call_in(10, &print_heap_and_isr_stack_info);
         //print_heap_and_isr_stack_info();
     }
@@ -1145,6 +1155,8 @@
             break;
     }
     _smutex.unlock();
+    _wiFi2ATDatamPool->free(resp_data);
+    resp_data = NULL;
 }
 
 
@@ -1162,6 +1174,9 @@
             outputEDMdata((const uint8_t *) buf, len, CONNECT_MSG_ID, EVENT_MSG_TYPE, WIFI_CHANNEL);
             break;
     }
+    _wiFi2ATDatamPool->free(resp_data);
+    resp_data = NULL;
+    
 }
 
 
--- a/source/WiFiManager.cpp	Mon Apr 22 11:11:41 2019 +0000
+++ b/source/WiFiManager.cpp	Mon Apr 22 18:34:31 2019 +0000
@@ -33,6 +33,7 @@
  wifiCmd = WIFI_CMD_NONE;
  internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme
  is_connected = false;
+ http_response = NULL;
  chunkNum = 0;
  socket = NULL;
  https_connection_active = false;
@@ -123,15 +124,8 @@
                 queueATresponse(AT_CONFIG_RESP);
                 break;
             case WIFI_CMD_INTERNET_CONFIG:
-                result = set_internet_config();
-                if(result == true)
-                {
-                    queueATresponse(AT_INTERNET_CONFIG_RESP);
-                }
-                else
-                {
-                    sendATresponseString(AT_COMMAND_FAILED);
-                }
+                set_internet_config();
+                queueATresponse(AT_INTERNET_CONFIG_RESP);
                 wifiCmd = WIFI_CMD_NONE;
                 break;
             case WIFI_CMD_NETWORK_STATUS:
@@ -363,9 +357,30 @@
     delete socket;
 }
 
-
+void WiFiManager::gethostbyname()
+{
+    nsapi_value_or_error_t value_or_error;
+    value_or_error = network->gethostbyname_async(internet_config.url, 
+                                         callback(this, &WiFiManager::gethostbyname_callback), 
+                                         NSAPI_UNSPEC);
+    if(value_or_error >= NSAPI_ERROR_OK) // success
+    {
+        printf("[WIFI-MAN] hostname translation successful value_or_error = %d\r\n", value_or_error);
+        //strcpy(responseString, UDDRP_WRITE_OK);
+        //printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN);
+        //sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
+    }
+    else // -ve number means error
+    {
+        responseString = (char *) malloc(20);
+        printf("[WIFI-MAN] hostname translation failed\r\n");
+        strcpy(responseString, UDDRP_ERROR);
+        sendATresponseString(AT_COMMAND_FAILED);
+    }
 
-bool WiFiManager::set_internet_config()
+}
+
+void WiFiManager::set_internet_config()
 {
     internet_config_t *internet_cfg  = (internet_config_t *) data_msg->buffer;
     internet_config.peer_id          = internet_cfg->peer_id;
@@ -383,23 +398,7 @@
         delete socket;
         socket = NULL;
     }
-    nsapi_value_or_error_t value_or_error;
-    value_or_error = network->gethostbyname_async(internet_config.url, 
-                                         callback(this, &WiFiManager::gethostbyname_callback), 
-                                         NSAPI_UNSPEC);
-    if(value_or_error >= NSAPI_ERROR_OK) // success
-    {
-        printf("[WIFI-MAN] hostname translation successful value_or_error = %d\r\n", value_or_error);
-        //strcpy(responseString, UDDRP_WRITE_OK);
-        return true;
-    }
-    else // -ve number means error
-    {
-        responseString = (char *) malloc(20);
-        printf("[WIFI-MAN] hostname translation failed\r\n");
-        strcpy(responseString, UDDRP_ERROR);
-        return false;
-    }
+    _event_queue.call_in(10, this, &WiFiManager::gethostbyname);
 }
 
 
@@ -614,16 +613,19 @@
     responseBytes[i++] = IPv4_CONNECTION; // connect type IPv4
     responseBytes[i++] = TCP_PROTOCOL; // Protocol = TCP
     memcpy(&responseBytes[i], address->get_ip_bytes(), 4); // remote IPv4 address
+    strcpy(internet_config.remote_IPv4Address, address->get_ip_address());
     i +=4;
-    int port = address->get_port();
+    uint16_t port = address->get_port();
+    internet_config.remote_port = port;
     memcpy(&responseBytes[i], &port, 2); // remote IPv4 port #
     i +=2;
     if(is_connected)
     {
         // local IPv4 address
         int ipAddr[4];
-        sscanf(network->get_ip_address(), "%d.%d.%d.%d", &ipAddr[0], &ipAddr[1], 
-                                                         &ipAddr[2], &ipAddr[3]);
+        strcpy(internet_config.local_IPv4Address, network->get_ip_address());
+        sscanf(internet_config.local_IPv4Address, "%d.%d.%d.%d", &ipAddr[0], &ipAddr[1], 
+                                                                 &ipAddr[2], &ipAddr[3]);
         responseBytes[i++] = (uint8_t) ipAddr[0];
         responseBytes[i++] = (uint8_t) ipAddr[1];
         responseBytes[i++] = (uint8_t) ipAddr[2];
@@ -638,9 +640,25 @@
         // if unconnected set ip and port to zeroes
         memset(&responseBytes[i], 0x00, 6);
     }
-    sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
+    _event_queue.call_in(2, this, &WiFiManager::sendATresponseBytes, 
+                                    CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
 }   
 
+void WiFiManager::sendSocketConnectionEvent()
+{
+    // 
+    responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN); 
+    sprintf(responseString, "\r\n%s%d,%d,%d,%s,%d,%s,%d\r\n", PEER_CONNECTED_URC, 
+                                                              IP_PEER_HANDLE,
+                                                              IPv4_CONNECTION,
+                                                              TCP_PROTOCOL,
+                                                              internet_config.local_IPv4Address,
+                                                              DEFAULT_LOCAL_PORT,
+                                                              internet_config.remote_IPv4Address,
+                                                              internet_config.remote_port);
+    sendATresponseString(AT_EVENT);
+}
+
 
 nsapi_error_t WiFiManager::disconnect()
 {
@@ -698,6 +716,7 @@
     }while(queueResult ==  false || pos < bufLen);
     printf("[WIFI-MAN] response data queued - deleting data memory\r\n");
     delete at_data_resp;
+    at_data_resp = NULL;
 }
 
 bool WiFiManager::copyResponseHdr2Queue()
@@ -706,7 +725,12 @@
     // create message pointer for response header generation
     char * msgPtr = (char *)at_data_resp->buffer;
     // do status line
-    printf("before getting HTTP status line\n");
+    if(http_response == NULL)
+    {
+        printf("[WIFI-MAN] copy failed: response pointer NULL!!\r\n");
+        return false;
+    }
+    printf("before getting HTTP status line http_response = 0x%08X\n", http_response);
     numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", http_response->get_status_code(), 
                                                      http_response->get_status_message().c_str());
     msgPtr += numChars;
@@ -780,6 +804,7 @@
         queueResult = queueWiFiDataResponse(*at_data_resp);
     }while(queueResult == false);
     delete at_data_resp;
+    at_data_resp = NULL;
 }
 
 
@@ -838,6 +863,20 @@
     printf("TLS connection successful for https site :  %s\n", hostName);
     return true;
 }
+
+
+void WiFiManager::updateRemotePeerDetails()
+{
+    printf("Updating internet_config... \r\n");
+    nsapi_error_t error;
+    SocketAddress * address = new SocketAddress;
+    error = socket->getpeername(address);   
+    strcpy(internet_config.remote_IPv4Address, address->get_ip_address());
+    uint16_t port = address->get_port();
+    internet_config.remote_port = port;
+    delete address;
+}
+
 #define TESTING_HTTPS
 
 //#define DONT_USE_TLS_SOCKET
@@ -921,6 +960,11 @@
                  printf("data msg deleted \r\n");
                  return false;
             }
+            // update remote peer details after socket connection
+            updateRemotePeerDetails();
+            // send socket connection event before proceeding to send https request
+            // give about 2 ms
+            _event_queue.call_in(2, this, &WiFiManager::sendSocketConnectionEvent);
             //printf("[create https] TLS connection successful for https site :  %s\n", host);
             //printf("after call to createTLSconnection \n");
             //print_memory_info();
@@ -1015,6 +1059,7 @@
     delete https_request; // free the request & response memory
     printf("deleted https_request\r\n");
     https_request = NULL;
+    http_response = NULL;
     return true;
 }
 
--- a/source/WiFiManager.h	Mon Apr 22 11:11:41 2019 +0000
+++ b/source/WiFiManager.h	Mon Apr 22 18:34:31 2019 +0000
@@ -80,7 +80,7 @@
     void                  set_WIFI_PASSWORD(char * wifi_pass);
     void                  set_WIFI_SECURITY(nsapi_security_t wifi_security);
     void                  set_WIFI_CONFIG();
-    bool                  set_internet_config();
+    void                  set_internet_config();
     nsapi_error_t         connect();
     nsapi_error_t         disconnect();
     bool                  createHttpsRequest();
@@ -113,6 +113,10 @@
     void                  getWiFiStatus();
     void                  status_callback_event(nsapi_event_t status, intptr_t param);    
     void                  gethostbyname_callback(nsapi_error_t result, SocketAddress *address);    
+    void                  gethostbyname();
+    void                  sendSocketConnectionEvent();
+    void                  updateRemotePeerDetails();
+
 
 
     /**
--- a/source/common_config.h	Mon Apr 22 11:11:41 2019 +0000
+++ b/source/common_config.h	Mon Apr 22 18:34:31 2019 +0000
@@ -18,6 +18,7 @@
 #define DEFAULT_BTLE_CHANNEL      1
 #define WIFI_LINK_ENABLED         "+UUWLE:"
 #define NETWORK_UP_URC            "+UUNU"
+#define PEER_CONNECTED_URC        "+UUDPC:"
 #define NETWORK_STATUS            "+UNSTAT="
 #define WIFI_NETWORK_STATUS       "+UWSSTAT="
 #define UDDRP_ERROR               "\r\nERROR\r\n"
@@ -30,6 +31,9 @@
 #define TCP_PROTOCOL              0
 #define UDP_PROTOCOL              1
 #define IPv4_CONNECTION           0x02
+#define IP_PEER_HANDLE            0x02
+#define BTLE_PEER_HANDLE          0x01
+#define DEFAULT_LOCAL_PORT        0
 #define PQDSZ   2  // size of Pool/Queue data structures
 
 typedef enum
--- a/source/common_types.h	Mon Apr 22 11:11:41 2019 +0000
+++ b/source/common_types.h	Mon Apr 22 18:34:31 2019 +0000
@@ -16,6 +16,7 @@
 #define MIN_EDM_PAYLOAD_LEN (2)
 #define MAX_HTTP_HEADER_LINES
 #define MAX_URL_LEN          100
+#define MAX_IPv4_LEN         16
 
 /** ble configuration structure
 */
@@ -70,24 +71,25 @@
 
 typedef enum
 {
-  AT_RESP_NONE,
-  AT_SCAN_RESP,
-  AT_DETAILED_SCAN_RESP,
-  AT_CONNECT_RESP,
-  AT_DISCONNECT_RESP,
-  WIFI_RECONNECT_INFO,
-  WIFI_DISCONNECT_INFO,
-  AT_CONFIG_RESP,
-  AT_INTERNET_CONFIG_RESP,
-  AT_NETWORK_STATUS_RESP,
-  AT_WIFI_STATUS_RESP,
-  AT_HTTPS_RESP,
-  AT_HTTPS_RESP_DOWNLOAD,
-  AT_HTTP_RESP,
-  AT_HTTP_RESP_DOWNLOAD,
-  CONNECT_EVENT,
-  DATA_EVENT,
-  AT_COMMAND_FAILED
+  AT_RESP_NONE = 0,
+  AT_SCAN_RESP = 1,
+  AT_DETAILED_SCAN_RESP = 2,
+  AT_CONNECT_RESP = 3,
+  AT_DISCONNECT_RESP = 4,
+  WIFI_RECONNECT_INFO = 5,
+  WIFI_DISCONNECT_INFO = 6,
+  AT_CONFIG_RESP = 7,
+  AT_INTERNET_CONFIG_RESP = 8,
+  AT_NETWORK_STATUS_RESP = 9,
+  AT_WIFI_STATUS_RESP = 10,
+  AT_HTTPS_RESP = 11,
+  AT_HTTPS_RESP_DOWNLOAD = 12,
+  AT_HTTP_RESP = 13,
+  AT_HTTP_RESP_DOWNLOAD = 14,
+  AT_EVENT = 15,
+  CONNECT_EVENT = 16,
+  DATA_EVENT = 17,
+  AT_COMMAND_FAILED = 18
 }at_cmd_resp_t;
 
 typedef enum edm_msg_id
@@ -196,6 +198,9 @@
     uint8_t       peer_id          ;  /* peer id */
     conn_scheme_t connectionScheme;   /* connection scheme     */
     char          url[MAX_URL_LEN];   /* url string     */
+    char          local_IPv4Address[MAX_IPv4_LEN];    /* local Ip address     */
+    char          remote_IPv4Address[MAX_IPv4_LEN];   /* remote IP address     */
+    uint16_t      remote_port;                        /* remote port     */
 } internet_config_t;
 
 #endif  // __COMMON_TYPES_H__
\ No newline at end of file