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 May 19 16:22:59 2019 +0000
Branch:
PassingRegression
Parent:
116:2296cf274661
Child:
118:8df0e9c2ee3f
Commit message:
Added Non-Volatile storage support. Default configuration now saved in NVStore.; Verified on python platform.

Changed in this revision

mbed_app.json Show annotated file Show diff for this revision Revisions of this file
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/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
--- a/mbed_app.json	Sun May 19 13:09:27 2019 +0000
+++ b/mbed_app.json	Sun May 19 16:22:59 2019 +0000
@@ -9,11 +9,11 @@
         },
        "wifi-ssid": {
             "help": "WiFi SSID",
-            "value": "\"VM9109107_5GEXT\""
+            "value": "\"NudgeEngTeam\""
         },
         "wifi-password": {
             "help": "WiFi Password",
-            "value": "\"Bonga01shop02\""
+            "value": "\"T1nk3RDNA\""
         },
         "overrides": {
             "lse_available": false 
--- a/source/ATCmdManager.cpp	Sun May 19 13:09:27 2019 +0000
+++ b/source/ATCmdManager.cpp	Sun May 19 16:22:59 2019 +0000
@@ -8,7 +8,7 @@
 //#include "mbed_memory_status.h"
 #define FILE_CODE       "atcmd"
 
-ATCmdManager::ATCmdManager(PinName tx, PinName rx, SMDevicePeripheral *blePeripheral, 
+ATCmdManager::ATCmdManager(PinName tx, PinName rx, uart_config_t *uart_config, 
                            events::EventQueue &event_queue, WiFiManager *wifi, 
                            MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 
                            Queue<wifi_cmd_message_t, 16>      *aT2WiFiCmdQueue, 
@@ -21,7 +21,7 @@
                            bool debug)
     :
     _serial(tx, rx, DEFAULT_BAUD_RATE),
-    blePeripheral(blePeripheral),
+    uart_config(uart_config),
     _event_queue(event_queue),
     wiFiManager(wiFiManager),
     _aT2WiFimPool(aT2WiFimPool),
@@ -41,7 +41,7 @@
 
 { 
 // constructor
-    _serial.set_baud(2*DEFAULT_BAUD_RATE);
+    _serial.set_baud(uart_config->baudrate);
     //Flow flowCtrl = Flow::Disabled;
     //_serial.set_flow_control(mbed::SerialBase::Disabled);
     _parser.debug_on(debug);
--- a/source/ATCmdManager.h	Sun May 19 13:09:27 2019 +0000
+++ b/source/ATCmdManager.h	Sun May 19 16:22:59 2019 +0000
@@ -20,7 +20,7 @@
 //extern void blinkLEDs();
 class ATCmdManager {
 public:
-    ATCmdManager(PinName tx, PinName rx, SMDevicePeripheral *blePeripheral, 
+    ATCmdManager(PinName tx, PinName rx, uart_config_t *uart_config, 
                  events::EventQueue &event_queue, WiFiManager *wifi, 
                  MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 
                  Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, 
@@ -38,6 +38,7 @@
 
 private:
 // UART settings
+    uart_config_t *uart_config;
     UARTSerial _serial;
     Mutex _smutex; // Protect serial port access
     Mutex _rmutex; // Reset protection
--- a/source/common_types.h	Sun May 19 13:09:27 2019 +0000
+++ b/source/common_types.h	Sun May 19 16:22:59 2019 +0000
@@ -166,6 +166,25 @@
 }conn_scheme_t;
 
 
+typedef enum nvstore_key
+{
+  APP_CONFIG_0 = 1,
+  APP_CONFIG_1 = 2,
+  APP_CONFIG_2 = 4
+}nvstore_key_t;
+
+
+typedef struct {
+    int     baudrate;  /* baud rate */
+    uint8_t flow_ctrl; /* 2 = no flow control */
+    uint8_t data_bits; /* 8 data bits */
+    uint8_t stop_bits; /* 1 start bit */
+    uint8_t parity;    /* 1 parity bit */
+    uint8_t change_after_confirm; /* 1 */
+} uart_config_t;
+
+
+
 typedef struct {
     wifi_cmd_t    wifi_cmd;   /* wifi command */
     uint8_t       padding[4-sizeof(wifi_cmd_t)];   /* padding */
@@ -227,9 +246,10 @@
 /** application configuration structure
 */
 typedef struct  {
-    wifi_config_t     wifi_config;      /* wifi configuration */
-    ble_config_t      ble_config;       /* ble configuration */
-    internet_config_t internet_config;  /* cloud configuration */
+    uart_config_t     uart_config;      /* UART configuration      */
+    wifi_config_t     wifi_config;      /* wifi configuration      */
+    ble_config_t      ble_config;       /* ble configuration       */
+    internet_config_t internet_config;  /* cloud configuration     */
     login_config_t    login_config;     /* cloud login credentials */
 } app_config_t;
 
--- a/source/main-https.cpp	Sun May 19 13:09:27 2019 +0000
+++ b/source/main-https.cpp	Sun May 19 16:22:59 2019 +0000
@@ -7,6 +7,7 @@
 
 #include <events/mbed_events.h>
 #include <mbed.h>
+#include "nvstore.h"
 #include "ble/BLE.h"
 #include "fault_handlers.h"
 #include "ATCmdParser.h"
@@ -27,6 +28,10 @@
 DigitalOut led3(LED3);
 
 #define FILE_CODE       "main"
+
+// NVStore is a sigleton, get its instance
+NVStore &nvstore = NVStore::get_instance();
+
 main_states_t mainLoop;
 static RawSerial *device; // tx, rx
 
@@ -185,6 +190,23 @@
     app_config.wifi_config.security = NSAPI_SECURITY_WPA_WPA2;
 }
 
+// Entry point for the example
+void print_app_config()
+{
+    //dbg_printf(LOG, "Return code is %d ", rc);
+}
+
+// Entry point for the example
+void print_return_code(int rc, int expected_rc)
+{
+    dbg_printf(LOG, "Return code is %d ", rc);
+    if (rc == expected_rc)
+        dbg_printf(LOG, "(as expected).\n");
+    else
+        dbg_printf(LOG, "(expected %d!).\n", expected_rc);
+}
+
+
 void setupDefaultCloudConfig()
 {
     strcpy(app_config.internet_config.url, "tcp://https://dev2.dnanudge.io");
@@ -193,6 +215,43 @@
     app_config.internet_config.connectionScheme = ALWAYS_CONNECTED;
 }
 
+void setupDefaultUartConfig()
+{
+    app_config.uart_config.baudrate             = 2*DEFAULT_BAUD_RATE;
+    app_config.uart_config.flow_ctrl            = 2;
+    app_config.uart_config.data_bits            = 8;
+    app_config.uart_config.stop_bits            = 1;
+    app_config.uart_config.parity               = 1;
+    app_config.uart_config.change_after_confirm = 1;
+}
+
+void resetConfiguration()
+{
+    int rc;
+    // Clear NVStore data. Should only be done once at factory configuration
+    rc = nvstore.reset();
+    dbg_printf(LOG, "Reset NVStore. ");
+    print_return_code(rc, NVSTORE_SUCCESS);
+
+}
+
+void saveConfiguration(uint32_t configKey)
+{
+    int rc;
+    rc = nvstore.set(configKey, sizeof(app_config), &app_config);
+    print_return_code(rc, NVSTORE_SUCCESS);
+}
+
+void loadConfiguration(uint32_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);
+    print_app_config();
+    print_return_code(rc, NVSTORE_SUCCESS);
+}
+
 static int reset_counter = 0;
 
 
@@ -307,25 +366,49 @@
     mainLoop = STOP_WIFI;
 }
 
-
-
+//#define USE_DEFAULT_CONFIGURATION
+//#define TEST_NVSTORE
 #define STARTUP_DEBUG_ENABLE
 //#define AUTO_START_BLE_MANAGER
 //#define AUTO_START_WIFI_MANAGER
 #define PAUSE_SECONDS   0
 #define PAUSE_SECONDS_BLE 0
 int main() {
-    device = new RawSerial(USBTX, USBRX, 2*DEFAULT_BAUD_RATE);
+#ifndef USE_DEFAULT_CONFIGURATION
+    loadConfiguration(APP_CONFIG_0);
+#else
+    setupDefaultUartConfig();
+    setupDefaultWiFiConfig();
+    setupDefaultBleConfig();
+    setupDefaultCloudConfig();
+#endif
+    device = new RawSerial(USBTX, USBRX, app_config.uart_config.baudrate);
     uint8_t debug_level = (LOG | ERR | TXT | DBG);
     initialise_debug(debug_level);
 #ifdef MBED_MAJOR_VERSION
     dbg_printf(LOG, "Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
 #endif
+#ifdef TEST_NVSTORE
+    app_config_t *current_config = new app_config_t;
+    memcpy(current_config, &app_config, sizeof(app_config_t));
+    saveConfiguration(APP_CONFIG_0);
+    memset(&app_config, 0, sizeof(app_config_t));
+    loadConfiguration(APP_CONFIG_0);
+    int cmp;
+    cmp = memcmp(current_config, &app_config, sizeof(app_config_t));
+    if(cmp == 0)
+    {
+        dbg_printf(LOG, " NVSTRORE TEST Passed!!\r\n");
+    }
+    else
+    {
+        dbg_printf(LOG, " NVSTRORE TEST Failed!!\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
+    }
+    delete current_config;
+#endif
     
     reset_counter++;
     dbg_printf(LOG, "\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter);
-    setupDefaultWiFiConfig();
-    setupDefaultBleConfig();
 #ifdef AUTO_START_BLE_MANAGER
     //btle_thread.start(callback(peripheral, &SMDevicePeripheral::run));
     start_BLE();
@@ -339,7 +422,7 @@
     // dispatch atcmd event queue on event thread
     atcmd_evt_thread.start(callback(&eventQueue_atcmd, &EventQueue::dispatch_forever));
     dbg_printf(LOG, "\r\n++++++ Starting ATCmdmanager ++++++ \r\n");
-    ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, peripheral, 
+    ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, &app_config.uart_config, 
                                                 eventQueue_atcmd, wiFiManager, 
                                                 &aT2WiFimPool, &aT2WiFiCmdQueue,
                                                 &wiFi2ATmPool, &wiFi2ATCmdQueue,