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:
Sat Jun 01 15:29:03 2019 +0000
Branch:
PassingRegression
Parent:
120:779b74689747
Child:
122:62166886db5f
Commit message:
- BLE AT commands and responses implemented; - configuration storage implemented; - configuration delete implemented; - factory reset implemented; BLE connection with Box tested ok so far.

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/BleManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/BleManager.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
source/main-https.cpp Show annotated file Show diff for this revision Revisions of this file
source/main.h Show annotated file Show diff for this revision Revisions of this file
--- a/source/ATCmdManager.cpp	Mon May 27 17:00:43 2019 +0000
+++ b/source/ATCmdManager.cpp	Sat Jun 01 15:29:03 2019 +0000
@@ -21,7 +21,7 @@
                            Queue<at_ble_msg_t, PQDSZ_BLE>         *aT2BleDataQueue, 
                            MemoryPool<ble_at_msg_t, PQDSZ_BLE>    *ble2ATDatamPool, 
                            Queue<ble_at_msg_t, PQDSZ_BLE>         *ble2ATDataQueue, 
-                           bool debug)
+                           startup_config_t *startup_config, bool debug)
     :
     _serial(tx, rx, DEFAULT_BAUD_RATE),
     uart_config(uart_config),
@@ -42,6 +42,7 @@
     _aT2BleDataQueue(aT2BleDataQueue),
     _ble2ATDatamPool(ble2ATDatamPool),
     _ble2ATDataQueue(ble2ATDataQueue),
+    startup_config(startup_config),
      _parser(&_serial)
     
 
@@ -75,7 +76,7 @@
     _parser.oob("AT+UBTPM", callback(this, &ATCmdManager::_oob_ok_hdlr));
     
     _parser.oob("AT+UDSC=", callback(this, &ATCmdManager::_oob_ok_hdlr));
-    _parser.oob("AT&W", callback(this, &ATCmdManager::_oob_ok_hdlr));
+    _parser.oob("AT&W", callback(this, &ATCmdManager::_oob_saveSettings_hdlr));
     //_parser.oob("AT+UBTPM", callback(this, &ATCmdManager::_oob_ok_hdlr));
     //_parser.oob("AT+UBTPM", callback(this, &ATCmdManager::_oob_ok_hdlr));
     //_parser.oob("AT+UWSCD=", callback(this, &ATCmdManager::_oob_disconnectWiFiNetwork));
@@ -87,6 +88,9 @@
     _parser.oob("AT+UNSTAT=", callback(this, &ATCmdManager::_oob_getNetworkStatus));
     _parser.oob("AT+UWSSTAT", callback(this, &ATCmdManager::_oob_WiFiNetworkStatus));
     
+    _parser.oob("AT+UFACTORY", callback(this, &ATCmdManager::_oob_factoryReset));
+    _parser.oob("AT+UDELCFG=", callback(this, &ATCmdManager::_oob_deleteConfiguration));
+
     //_parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_sendHttpMessage));
     //sendAtConfirmation("Testing:: +UBTLE:2\r\nOK\r\n");
     dbg_printf(LOG, "\n --- ATCmdManager constructor completed ---\n");
@@ -260,8 +264,8 @@
             {
                 // AT Event state 
                 dbg_printf(LOG, "\n [ATCMD MAN] AT_BLE_EVENT RESPONSE RECEIVED!!\r\n");
-                respStr = (char *) resp_data->buffer;
-                sendBleAtEvent(respStr, resp_data->dataLen);
+                respStr = (char *) ble_resp_data->buffer;
+                sendBleAtEvent(respStr, ble_resp_data->dataLen);
                 at_resp = AT_RESP_NONE;
                 break;
             }
@@ -270,6 +274,7 @@
                 // AT Event state 
                 dbg_printf(LOG, "\n [ATCMD MAN] AT_BLE_RESPONSE RECEIVED!!\r\n");
                 respStr = (char *) ble_resp_data->buffer;
+                ble_resp_data->buffer[ble_resp_data->dataLen] = NULL;
                 sendBleDataEvent(respStr, ble_resp_data->dataLen);
                 at_resp = AT_RESP_NONE;
                 break;
@@ -869,11 +874,19 @@
 void ATCmdManager::_oob_get_ble_role(){
     trigger_start_BLE();
     dbg_printf(LOG, "\n Received get BLE role command!!\n");
-    sendAtConfirmation("+UBTLE:2\r\nOK\r\n");   //_parser.send(OK_RESP);
+    if(startup_config->ble_enable)
+    {
+        sendAtConfirmation("+UBTLE:2\r\nOK\r\n");   //_parser.send(OK_RESP);
+    }
+    else // disabled
+    {
+        sendAtConfirmation("+UBTLE:0\r\nOK\r\n");   //_parser.send(OK_RESP);
+    }
 }
 
 void ATCmdManager::_oob_ena_ble_peri(){
     dbg_printf(LOG, "\n Received enable BLE Peripheral command!!\n");
+    startup_config->ble_enable = true;
     sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
 }
 
@@ -883,6 +896,33 @@
     system_reset();
 }
 
+
+void ATCmdManager::_oob_factoryReset(){
+    dbg_printf(LOG, "\n Received factory reset command!!\n");
+    resetConfiguration();
+    sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
+}
+
+
+void ATCmdManager::_oob_deleteConfiguration(){
+    dbg_printf(LOG, "\n Received delete configuration command!!\n");
+    int configKey;
+    bool res = false;
+    if(_parser.scanf("%d", &configKey) >0)
+    {
+        res = deleteConfiguration((nvstore_key_t) configKey);
+    }
+    if(res)
+    sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
+}
+
+
+
+void ATCmdManager::_oob_saveSettings_hdlr()
+{
+    saveConfiguration(APP_CONFIG_0);
+    sendAtConfirmation(OK_RESP);    
+}
 const char * ATCmdManager::sec2str(nsapi_security_t sec)
 {
     switch (sec) {
@@ -1258,7 +1298,7 @@
     if(evt.status == osEventMessage){
         ble_resp_data = (ble_at_msg_t*)evt.value.p;
         setNextResponse(ble_resp_data->at_resp);
-        dbg_printf(LOG, "[ATCMD MAN] dequeued data size = %d : at_resp = %d\n", resp_data->dataLen, resp_data->at_resp);
+        dbg_printf(LOG, "[ATCMD MAN] dequeued data size = %d : at_resp = %d\n", ble_resp_data->dataLen, ble_resp_data->at_resp);
     }
     return true;
 }
--- a/source/ATCmdManager.h	Mon May 27 17:00:43 2019 +0000
+++ b/source/ATCmdManager.h	Sat Jun 01 15:29:03 2019 +0000
@@ -36,7 +36,7 @@
                  Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, 
                  MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, 
                  Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, 
-                 bool debug = false);
+                 startup_config_t *startup_config, bool debug = false);
 public:
     void runMain();
 
@@ -91,6 +91,7 @@
     ble_at_msg_t *ble_resp_data;
     edm_header_t   edm_hdr;
     uint8_t        *rx_buf_ptr;
+    startup_config_t *startup_config;
     int            debug_flag;
     at_cmd_resp_t  wifiStateControl;
 #ifdef BOX_UBLOX_DEMO_TESTING
@@ -118,10 +119,13 @@
     void _oob_ena_ble_peri();
     void _oob_reboot();
     void _oob_get_fw_ver();
+    void _oob_saveSettings_hdlr();
     void _oob_scanWiFiNetworks();
     void _oob_WiFiStationConfigAction();
     void _oob_disconnectWiFiNetwork();
     void _oob_setupInternetConnection();
+    void _oob_factoryReset();
+    void _oob_deleteConfiguration();
     void _oob_setWiFiSSID();
     void _oob_setWiFiPWD();
     void _oob_setWiFiSecurity();
--- a/source/BleManager.cpp	Mon May 27 17:00:43 2019 +0000
+++ b/source/BleManager.cpp	Sat Jun 01 15:29:03 2019 +0000
@@ -288,10 +288,9 @@
 #ifndef DEMO_BLE_SECURITY
     dbg_printf(LOG, "Restarting advertising...\r\n");
     _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
-#else
-    _event_queue.break_dispatch();
 #endif
     isConnected = false;
+    _event_queue.call(this, &SMDevice::setNextCommand, BLE_CMD_DISCONNECT);
 }
 
 /** End demonstration unexpectedly. Called if timeout is reached during advertising,
@@ -356,11 +355,18 @@
         buffer[index++] = 0;
         at_data_resp = new ble_at_msg_t;
         at_data_resp->dataLen = params->len;
+        // first set buffer to zeroes
+        //memset(at_data_resp->buffer, 0x00, sizeof(at_data_resp->buffer));
         memcpy(at_data_resp->buffer, params->data, params->len);
-        dbg_printf(LOG, "Data : %s ",buffer);
+        at_data_resp->buffer[params->len] = NULL;
+        at_data_resp->at_resp = AT_BLE_RESPONSE;
+        dbg_printf(LOG, "Data : %s : len = %d",(char *)at_data_resp->buffer, at_data_resp->dataLen);
         dbg_printf(LOG, "\r\n");
+        dbg_printf(LOG, "Data : %s : len = %d",buffer, params->len);
+        dbg_printf(LOG, "\r\n");
+        /* directly queue received data */
+        _event_queue.call(this, &SMDevice::sendATresponseBytes, AT_BLE_RESPONSE);
         /* start echo in 50 ms */
-        _event_queue.call(this, &SMDevice::setNextCommand, BLE_CMD_SEND_RX_DATA_2AT);
         _event_queue.call_in(50, this, &SMDevice::EchoBleUartReceived);
         //_event_queue.call(EchoBleUartReceived);
 
@@ -539,6 +545,66 @@
 }
 
 
+void SMDevicePeripheral::sendConnectionResponses()
+{
+    // send ACL Connected +UUBTACLC
+    at_data_resp = new ble_at_msg_t;
+    char * respStr = (char *)at_data_resp->buffer;
+    sprintf(respStr, "\r\n+UUBTACLC:%d,%d,%02X%02X%02X%02X%02X%02X\r\n", 
+            DEFAULT_BTLE_CHANNEL, GATT_TYPE,_peer_address[5] ,_peer_address[4],
+            _peer_address[3],_peer_address[2],_peer_address[1],_peer_address[0]);
+    at_data_resp->dataLen = strlen(respStr); // get bytes total
+    sendATresponseBytes(AT_BLE_EVENT);
+    
+    // send +UUDPC
+    at_data_resp = new ble_at_msg_t;
+    respStr = (char *)at_data_resp->buffer;
+    sprintf(respStr, "\r\n+UUDPC:%d,%d,%d,%02X%02X%02X%02X%02X%02X,%d\r\n", 
+            BTLE_PEER_HANDLE, BLE_CONNECTION, BLE_UUID_PROFILE,_peer_address[5] ,
+            _peer_address[4], _peer_address[3],_peer_address[2],_peer_address[1],
+            _peer_address[0], BLE_FRAME_SIZE);
+    at_data_resp->dataLen = strlen(respStr); // get bytes total
+    sendATresponseBytes(AT_BLE_EVENT);
+    
+    // send connect event
+    at_data_resp = new ble_at_msg_t;
+    at_data_resp->dataLen = 10; // 10 bytes total
+    int idx = 0;
+    // connect type BLE = 0x01
+    at_data_resp->buffer[idx++] = 0x01;
+    // Serial Port Service BLE profile = 0x0E (14)
+    at_data_resp->buffer[idx++] = 0x0E;
+    // copy peer device address
+    memcpy(&at_data_resp->buffer[idx], _peer_address, sizeof(_peer_address));
+    idx+=sizeof(_peer_address);
+    // frame size 0x0166
+    at_data_resp->buffer[idx++] = 0x01;
+    at_data_resp->buffer[idx++] = 0x66;
+    sendATresponseBytes(BLE_CONNECT_EVENT);
+}
+
+
+void SMDevicePeripheral::sendDisconnectionResponses()
+{
+    // send ACL disconnected +UUBTACLD
+    at_data_resp = new ble_at_msg_t;
+    char * respStr = (char *)at_data_resp->buffer;
+    sprintf(respStr, "\r\n+UUBTACLD:%d\r\n", DEFAULT_BTLE_CHANNEL);
+    at_data_resp->dataLen = strlen(respStr); // get bytes total
+    sendATresponseBytes(AT_BLE_EVENT);
+    
+    // send +UUDPD
+    at_data_resp = new ble_at_msg_t;
+    respStr = (char *)at_data_resp->buffer;
+    sprintf(respStr, "\r\n+UUDPD:%d\r\n", BTLE_PEER_HANDLE);
+    at_data_resp->dataLen = strlen(respStr); // get bytes total
+    sendATresponseBytes(AT_BLE_EVENT);
+    
+    // send connect event
+    at_data_resp = new ble_at_msg_t;
+    sendATresponseBytes(BLE_DISCONNECT_EVENT);
+}
+
 void SMDevicePeripheral::processQueues()
 {
     dequeueATdataResponse();
@@ -551,26 +617,15 @@
         bleCmd = BLE_CMD_NONE;
         break;
     case BLE_CMD_CONNECT:
-        at_data_resp = new ble_at_msg_t;
-        at_data_resp->dataLen = 10; // 10 bytes total
-        int idx = 0;
-        // connect type BLE = 0x01
-        at_data_resp->buffer[idx++] = 0x01;
-        // Serial Port Service BLE profile = 0x0E (14)
-        at_data_resp->buffer[idx++] = 0x0E;
-        // copy peer device address
-        memcpy(&at_data_resp->buffer[idx], _peer_address, sizeof(_peer_address));
-        idx+=sizeof(_peer_address);
-        // frame size 0x0166
-        at_data_resp->buffer[idx++] = 0x01;
-        at_data_resp->buffer[idx++] = 0x66;
+        sendConnectionResponses();
         bleCmd = BLE_CMD_NONE;
-        sendATresponseBytes(BLE_CONNECT_EVENT);
         break;
     case BLE_CMD_DISCONNECT:
-        sendATresponseBytes(BLE_CONNECT_EVENT);
+    {
+        sendDisconnectionResponses();
         bleCmd = BLE_CMD_NONE;
         break;
+    }
     case BLE_CMD_SEND_RX_DATA_2AT:
         sendATresponseBytes(AT_BLE_RESPONSE);
         bleCmd = BLE_CMD_NONE;
--- a/source/BleManager.h	Mon May 27 17:00:43 2019 +0000
+++ b/source/BleManager.h	Sat Jun 01 15:29:03 2019 +0000
@@ -189,7 +189,9 @@
     virtual void sendATresponseBytes(at_cmd_resp_t at_cmd);
     virtual bool setNextCommand(ble_cmd_t cmd);
     virtual void processQueues();
-    
+private:
+    void sendConnectionResponses();
+    void sendDisconnectionResponses();    
 };
 
 /** A central device will scan, connect to a peer and request pairing. */
--- a/source/common_config.h	Mon May 27 17:00:43 2019 +0000
+++ b/source/common_config.h	Sat Jun 01 15:29:03 2019 +0000
@@ -30,11 +30,15 @@
 #define WIFI_CONFIG_ID            0
 #define WIFI_INTERFACE_ID         0
 #define DEFAULT_DNS_ADDRESS       "0.0.0.0"
+#define BLE_CONNECTION            1
 #define TCP_PROTOCOL              0
 #define UDP_PROTOCOL              1
 #define IPv4_CONNECTION           0x02
 #define IP_PEER_HANDLE            0x02
 #define BTLE_PEER_HANDLE          0x01
+#define GATT_TYPE                 0x00
+#define BLE_UUID_PROFILE          3
+#define BLE_FRAME_SIZE            64
 #define DEFAULT_LOCAL_PORT        0
 #define PQDSZ                     2  // size of Wifi Pool/Queue data structures
 #define PQDSZ_BLE                 8  // size of BLE Pool/Queue data structures
@@ -44,7 +48,7 @@
 #define HTTP_HEADER_CONTENT_LEN   "Content-Length:"
 #define QUEUE_WAIT_TIMEOUT_MS     1000
 #define CLOUD_RETRY_TIME_MS       10000
-#define MAX_BLE_PACKET_SIZE       20
+#define MAX_BLE_POOL_DATA_SIZE    40
 #define BLE_PROCESS_QUEUES_INTERVAL_MS  200 // check BLE queues every 200 ms
 //#define ENABLE_MEMORY_CHECKS
 #define SEND_DEBUG_MESSAGES
--- a/source/common_types.h	Mon May 27 17:00:43 2019 +0000
+++ b/source/common_types.h	Sat Jun 01 15:29:03 2019 +0000
@@ -250,10 +250,16 @@
     uint16_t      remote_port;                        /* remote port     */
 } internet_config_t;
 
+typedef struct {
+    bool          ble_enable;  /* enable BLE on startup */
+    bool          wifi_enable; /* enable WiFi on startup */
+} startup_config_t;
+
 
 /** application configuration structure
 */
 typedef struct  {
+    startup_config_t  startup_config;
     uart_config_t     uart_config;      /* UART configuration      */
     wifi_config_t     wifi_config;      /* wifi configuration      */
     ble_config_t      ble_config;       /* ble configuration       */
@@ -279,14 +285,14 @@
 typedef struct {
     ble_cmd_t     ble_cmd;                     /* BLE command             */
     int           dataLen;                     /* size of data in buffer */
-    uint8_t       buffer[MAX_BLE_PACKET_SIZE]; /* buffer length     */
+    uint8_t       buffer[MAX_BLE_POOL_DATA_SIZE]; /* buffer length     */
 } at_ble_msg_t;
 
 
 typedef struct {
     at_cmd_resp_t at_resp;              /* AT response */
     int           dataLen;              /* size of data in buffer */
-    uint8_t       buffer[MAX_BLE_PACKET_SIZE]; /* buffer length     */
+    uint8_t       buffer[MAX_BLE_POOL_DATA_SIZE]; /* buffer length     */
 } ble_at_msg_t;
 
 
--- a/source/main-https.cpp	Mon May 27 17:00:43 2019 +0000
+++ b/source/main-https.cpp	Sat Jun 01 15:29:03 2019 +0000
@@ -220,7 +220,10 @@
     else
         dbg_printf(LOG, "(expected %d!).\n", expected_rc);
 }
-
+void setupDefaultStartupConfig()
+{
+    app_config.startup_config.ble_enable = true;
+}
 
 void setupDefaultCloudConfig()
 {
@@ -247,24 +250,34 @@
     rc = nvstore.reset();
     dbg_printf(LOG, "Reset NVStore. ");
     print_return_code(rc, NVSTORE_SUCCESS);
-
 }
 
-void saveConfiguration(uint32_t configKey)
+bool deleteConfiguration(nvstore_key_t configKey)
 {
     int rc;
-    rc = nvstore.set(configKey, sizeof(app_config), &app_config);
+    // Clear NVStore data. Should only be done once at factory configuration
+    rc = nvstore.remove(configKey);
+    dbg_printf(LOG, "Deleted config key %d from NVStore. ", (int)configKey);
+    print_return_code(rc, NVSTORE_SUCCESS);
+    return (rc == NVSTORE_SUCCESS);
+}
+
+void saveConfiguration(nvstore_key_t configKey)
+{
+    int rc;
+    rc = nvstore.set((uint16_t)configKey, sizeof(app_config), &app_config);
     print_return_code(rc, NVSTORE_SUCCESS);
 }
 
-void loadConfiguration(uint32_t configKey)
+bool loadConfiguration(nvstore_key_t configKey)
 {
     int rc;
     // Get the value of this key (should be 3000)
     uint16_t actual_len_bytes;
-    rc = nvstore.get(configKey, sizeof(app_config), &app_config, actual_len_bytes);
+    rc = nvstore.get((uint16_t)configKey, sizeof(app_config), &app_config, actual_len_bytes);
     print_app_config();
     print_return_code(rc, NVSTORE_SUCCESS);
+    return (rc == NVSTORE_SUCCESS && (sizeof(app_config) == actual_len_bytes));
 }
 
 static int reset_counter = 0;
@@ -398,7 +411,18 @@
 #define PAUSE_SECONDS_BLE 0
 int main() {
 #ifndef USE_DEFAULT_CONFIGURATION
-    loadConfiguration(APP_CONFIG_0);
+    if(loadConfiguration(APP_CONFIG_0) == false)
+    {
+        setupDefaultStartupConfig();
+        setupDefaultUartConfig();
+        setupDefaultWiFiConfig();
+        setupDefaultBleConfig();
+        setupDefaultCloudConfig();
+    }
+    if(app_config.startup_config.ble_enable)
+    {
+        trigger_start_BLE();
+    }
 #else
     setupDefaultUartConfig();
     setupDefaultWiFiConfig();
@@ -457,7 +481,7 @@
                                                 &wiFi2ATDatamPool, &wiFi2ATDataQueue,
                                                 &aT2BleDatamPool, &aT2BleDataQueue,
                                                 &ble2ATDatamPool, &ble2ATDataQueue,
-                                                false);
+                                                &app_config.startup_config, false);
     atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain));
     dbg_printf(LOG, "\r\n after starting atcmd thread \r\n");
     print_memory_info();
--- a/source/main.h	Mon May 27 17:00:43 2019 +0000
+++ b/source/main.h	Sat Jun 01 15:29:03 2019 +0000
@@ -6,5 +6,8 @@
 void trigger_stop_BLE();
 void trigger_start_WiFi();
 void trigger_stop_WiFi();
+void saveConfiguration(nvstore_key_t configKey);
+void resetConfiguration();
+bool deleteConfiguration(nvstore_key_t configKey);
 
 #endif // __MAIN_H__
\ No newline at end of file