this is using the mbed os version 5-13-1
Diff: source/main-https.cpp
- Branch:
- PassingRegression
- Revision:
- 117:8fd05113efc1
- Parent:
- 116:2296cf274661
- Child:
- 118:8df0e9c2ee3f
--- 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,