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

Dependencies:   mbed-http

Revision:
81:637a87eb8170
Parent:
80:e8f0e92e3ac9
Child:
82:10072c1794d3
--- a/source/ATCmdManager.cpp	Thu Mar 21 22:17:28 2019 +0000
+++ b/source/ATCmdManager.cpp	Sat Mar 23 16:28:34 2019 +0000
@@ -7,12 +7,12 @@
                            events::EventQueue &event_queue, WiFiManager *wifi, 
                            MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 
                            Queue<wifi_cmd_message_t, 16>      *aT2WiFiCmdQueue, 
-                           MemoryPool<wifi_cmd_message_t, 16> *wiFi2ATmPool, 
-                           Queue<wifi_cmd_message_t, 16>      *wiFi2ATCmdQueue, 
+                           MemoryPool<at_resp_message_t, 16>  *wiFi2ATmPool, 
+                           Queue<at_resp_message_t, 16>       *wiFi2ATCmdQueue, 
                            MemoryPool<wifi_data_msg_t, 4>     *aT2WiFiDatamPool, 
                            Queue<wifi_data_msg_t, 4>          *aT2WiFiDataQueue, 
-                           MemoryPool<wifi_data_msg_t, 4>     *wiFi2ATDatamPool, 
-                           Queue<wifi_data_msg_t, 4>          *wiFi2ATDataQueue, 
+                           MemoryPool<at_data_msg_t, 4>       *wiFi2ATDatamPool, 
+                           Queue<at_data_msg_t, 4>            *wiFi2ATDataQueue, 
                            bool debug)
     :
     _serial(tx, rx, DEFAULT_BAUD_RATE),
@@ -40,6 +40,7 @@
     _parser.debug_on(debug);
     _parser.set_delimiter("\r\n");
     _parser.send("+STARTUP");
+    _parser.oob("AT\n", callback(this, &ATCmdManager::_oob_ok_hdlr));
     _parser.oob("ATE0", callback(this, &ATCmdManager::_oob_echo_off));
     _parser.oob("ATE1", callback(this, &ATCmdManager::_oob_echo_on));
     _parser.oob("AT+UMRS", callback(this, &ATCmdManager::_oob_uart_setup));
@@ -52,11 +53,14 @@
     _parser.oob("AT+CGMR", callback(this, &ATCmdManager::_oob_get_fw_ver));
     _parser.oob("AT+UWSC=", callback(this, &ATCmdManager::_oob_scanWiFiNetworks));
     _parser.oob("AT+UWSCA=", callback(this, &ATCmdManager::_oob_connect2WiFiNetwork));
-    
+    _parser.oob("AT+UWSCD=", callback(this, &ATCmdManager::_oob_disconnectWiFiNetwork));
+    _parser.oob("AT+UDDRP", callback(this, &ATCmdManager::_oob_setupInternetConnection));
+ 
 
     //_parser.oob("ATE0", callback(this, &ATCmdManager::_oob_startup_hdlr));
     printf("\n --- ATCmdManager constructor completed ---\n");
-    
+    at_resp = AT_RESP_NONE;
+    dataMode = AT_CMD_DATA_MODE;
     //AT+UMRS=230400,2,8,1,1,1
     // AT+UBTLE
 }
@@ -65,11 +69,75 @@
 void ATCmdManager::runMain(){
     while(true){
         _process_oob(UBLOX_ODIN_W2_RECV_TIMEOUT, true);
-        wait_ms(MAIN_LOOP_WAIT_TIME_MS); // allow BTLE to be
+        wait_ms(MAIN_LOOP_WAIT_TIME_MS); // allow BTLE/WiFi some time 
+        processResponses();
     }
     
 }
 
+void ATCmdManager::processResponses(){
+        dequeueATresponse();
+        switch(at_resp){
+            case AT_RESP_NONE:
+                // IDLE response state
+                break;
+            case AT_SCAN_RESP:
+                // AT_SCAN_RESP response state
+                _smutex.lock();
+                printf("\n [ATCMD MAN] WIFI SCAN RESPONSE RECEIVED!!\n");
+                _parser.send("OK\n");
+                _smutex.unlock();
+                at_resp = AT_RESP_NONE;
+                break;
+            case AT_DETAILED_SCAN_RESP:
+                // AT_DETAILED_SCAN_RESP response state
+                _smutex.lock();
+                printf("\n [ATCMD MAN] WIFI DETAILED SCAN RESPONSE RECEIVED!!\n");
+                _parser.send("OK\n");
+                _smutex.unlock();
+                at_resp = AT_RESP_NONE;
+                break;
+            case AT_CONNECT_RESP:  
+                // AT_CONNECT_RESP response state 
+                _smutex.lock();
+                printf("\n [ATCMD MAN] WIFI CONNECT RESPONSE RECEIVED!!\n");
+                _parser.send("OK\n");
+                _smutex.unlock();
+                at_resp = AT_RESP_NONE;
+                break;
+            case AT_DISCONNECT_RESP:  
+                // AT_DISCONNECT_RESP response state 
+                _smutex.lock();
+                printf("\n [ATCMD MAN] WIFI DISCONNECT RESPONSE RECEIVED!!\n");
+                _parser.send("OK\n");
+                _smutex.unlock();
+                at_resp = AT_RESP_NONE;
+                break;
+            case AT_HTTPS_RESP:  
+                // AT_HTTPS_RESP response state 
+                _smutex.lock();
+                printf("\n [ATCMD MAN] WIFI HTTPS RESPONSE RECEIVED!!\n");
+                _parser.send("OK\n");
+                _smutex.unlock();
+                at_resp = AT_RESP_NONE;
+                break;
+            case AT_HTTP_RESP:  
+                // AT_HTTP_RESP response state 
+                _smutex.lock();
+                printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n");
+                _parser.send("OK\n");
+                _smutex.unlock();
+                at_resp = AT_RESP_NONE;
+                break;
+            default:
+                // UNKNOWN response state
+                at_resp = AT_RESP_NONE;
+                break;
+        }
+}
+
+
+
 
 // OOB processing
 void ATCmdManager::_process_oob(uint32_t timeout, bool all){
@@ -85,6 +153,12 @@
 void ATCmdManager::_oob_startup_hdlr(){
 }
 
+void ATCmdManager::_oob_ok_hdlr(){
+    _smutex.lock();
+    _parser.send("OK\n");
+    _smutex.unlock();
+}
+
 
 void ATCmdManager::_oob_bleRole_hdlr(){
 }
@@ -111,6 +185,12 @@
     //if(_parser.recv("=%d,%d,%d,%d,%d,%d", &uOpts[0], &uOpts[1], &uOpts[2], &uOpts[3], &uOpts[4], &uOpts[5])) {
     if(_parser.scanf("=%d,%d,%d,%d,%d,%d", &uOpts[0], &uOpts[1], &uOpts[2], &uOpts[3], &uOpts[4], &uOpts[5]) >0) {
         printf("\nATCmdParser: Uart Options=%d,%d,%d,%d,%d,%d\n", uOpts[0], uOpts[1], uOpts[2], uOpts[3], uOpts[4], uOpts[5]);
+       //AT+UMRS=230400,2,8,1,1,1
+        printf("\n Changing Baud Rate to %d\n", uOpts[0]);
+
+        _serial.set_baud(uOpts[0]);
+        printf("\n Baud Rate now %d\n", uOpts[0]);
+
     } else {
         printf("\nATCmdParser: Retrieving Uart Options failed");
     }
@@ -154,12 +234,15 @@
         {
             case 0:
                 printf("\nATCmdParser: Command Mode request received");
+                dataMode = AT_CMD_DATA_MODE;
                 break;
             case 1:
                 printf("\nATCmdParser: Data Mode request received");
+                dataMode = AT_STD_DATA_MODE;
                 break;
             case 2:
                 printf("\nATCmdParser: Extended data Mode request received");
+                dataMode = AT_EXT_DATA_MODE;
                 break;
             default:
                 printf("\nATCmdParser: ERROR - UNKNOWN DATA MODE RECEIVED!!! \n");
@@ -236,13 +319,17 @@
             return "Unknown";
     }
 }
-bool  ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){
-    wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
-    wifiCmd->wifi_cmd            = cmd;
-    _aT2WiFiCmdQueue->put(wifiCmd);
-    return true;
+
+bool ATCmdManager::setNextResponse(at_cmd_resp_t resp)
+{
+     if(at_resp == AT_RESP_NONE){
+        at_resp = resp;
+        return true; // success
+    }
+    return false; // wiFiManager busy
 }
 
+
 void ATCmdManager::_oob_scanWiFiNetworks(){
     _smutex.lock();
     printf("\n Received scanWiFiNetworks command!!\n");
@@ -251,56 +338,95 @@
     wifi_cmd_t cmd = WIFI_CMD_SCAN;
     // queue next command
     queueWiFiCommand(cmd);
-    bool success;
-    success = wiFiManager->setNextCommand(cmd);
-    if(success){
-        printf("\n scan command successfully sent to wiFiManager!!\n");
-    }
-    else {
-        printf("\n ERROR: Failed to send scan command wiFiManager!!\n");
-    }
-    return;
-    /* call WiFi Scan in 20 ms */
-    //_event_queue.call_in(
-    //    20, wiFiManager,
-    //    &WiFiManager::scanNetworks);
-    //return;
-    WiFiAccessPoint       *ap;
-    nsapi_size_or_error_t count;
-    //count = wiFiManager->scanNetworks();
-    if (count <= 0) {
-        _smutex.lock();
-        printf("scan() failed with return value: %d\n", count);
-        _smutex.unlock();
-        return;
-    }
-    /* Limit number of network arbitrary to 15 */
-    count = count < 15 ? count : 15;
-    ap = new WiFiAccessPoint[count];
-    count = wiFiManager->getAvailableAPs(ap, count);
-    if (count <= 0) {
-        printf("scan() failed with return value: %d\n", count);
-        return;
-    }
-
-    for (int i = 0; i < count; i++) {
-        printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
-               sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
-               ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
-    }
-    printf("%d networks available.\n", count);
-
-    delete[] ap;
-    
+    return;    
 }
 
 void ATCmdManager::_oob_connect2WiFiNetwork()
 {
-    wiFiManager->connect();
+    wifi_cmd_t cmd = WIFI_CMD_CONNECT;
+    // queue next command
+    queueWiFiCommand(cmd);
+    return;
 }
 
 
 void ATCmdManager::_oob_disconnectWiFiNetwork()
 {
-    wiFiManager->disconnect();
+    wifi_cmd_t cmd = WIFI_CMD_DISCONNECT;
+    // queue next command
+    queueWiFiCommand(cmd);
+    return;
+}
+
+void ATCmdManager::_oob_setupInternetConnection()
+{
+    char url[200];
+    int n;
+    internet_config_t internet_config;
+    _smutex.lock();
+    n = _parser.scanf("=%1d,%200[^,],%1d", &internet_config.peer_id, 
+                                      url,
+                                     &internet_config.connectionScheme);
+    if(n>0) 
+    {
+        internet_config.url = url;
+        printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id, 
+                                                          internet_config.url.c_str(), 
+                                                          internet_config.connectionScheme);
+        // package and send on wifi data queue
+        wifi_data_msg_t data_msg;
+        data_msg.wifi_cmd = WIFI_CMD_INTERNET_CONFIG;
+        data_msg.dataLen = sizeof(internet_config) + internet_config.url.length();
+        memcpy(data_msg.buffer,&internet_config, data_msg.dataLen);
+        queueWiFiDataRequest(data_msg);
+    } else {
+        printf("\n[ATCMD MAN]: internet configuration failed %d fields parsed \n", n);
+        printf("\npeer_id = %d, url = %s, connScheme = %d\n", internet_config.peer_id, 
+                                                          url, 
+                                                          internet_config.connectionScheme);
+    }
+    _parser.send("OK\n");
+    _smutex.unlock();
 }
+
+bool  ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){
+    wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
+    wifiCmd->wifi_cmd            = cmd;
+    _aT2WiFiCmdQueue->put(wifiCmd);
+    return true;
+}
+
+bool  ATCmdManager::dequeueATresponse(){
+    if(at_resp != AT_RESP_NONE) return false; // busy
+    osEvent evt = _wiFi2ATCmdQueue->get(0);
+    if(evt.status == osEventMessage){
+        at_resp_message_t *resp = (at_resp_message_t*)evt.value.p;
+        setNextResponse(resp->at_resp);
+        _wiFi2ATmPool->free(resp);
+    }
+    return true;
+}
+
+bool  ATCmdManager::queueWiFiDataRequest(wifi_data_msg_t data_req){
+    wifi_data_msg_t *wifiData = _aT2WiFiDatamPool->alloc();
+    wifiData->wifi_cmd        = data_req.wifi_cmd;
+    wifiData->dataLen        = data_req.dataLen;
+    memcpy(wifiData->buffer, data_req.buffer, data_req.dataLen);
+    _aT2WiFiDataQueue->put(wifiData);
+    printf("[ATCMD MAN] queued data size = %d : wifi_cmd = %d\n", data_req.dataLen, data_req.wifi_cmd);
+    return true;
+}
+
+bool  ATCmdManager::dequeueATdataResponse(){
+    if(at_resp != AT_RESP_NONE) return false; // busy
+    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);
+    }
+    return true;
+}
+
+
+