
this is using the mbed os version 5-13-1
source/main-https.cpp@117:8fd05113efc1, 2019-05-19 (annotated)
- Committer:
- ocomeni
- Date:
- Sun May 19 16:22:59 2019 +0000
- Branch:
- PassingRegression
- Revision:
- 117:8fd05113efc1
- Parent:
- 116:2296cf274661
- Child:
- 118:8df0e9c2ee3f
Added Non-Volatile storage support. Default configuration now saved in NVStore.; Verified on python platform.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ocomeni | 73:6f5021cbe752 | 1 | #define MBED_CONF_MBED_TRACE_ENABLE 1 |
ocomeni | 73:6f5021cbe752 | 2 | |
ocomeni | 73:6f5021cbe752 | 3 | #include "select-demo.h" |
ocomeni | 103:7b566b522427 | 4 | #include "debug.h" |
ocomeni | 73:6f5021cbe752 | 5 | |
ocomeni | 73:6f5021cbe752 | 6 | #if DEMO == DEMO_HTTPS |
ocomeni | 73:6f5021cbe752 | 7 | |
ocomeni | 73:6f5021cbe752 | 8 | #include <events/mbed_events.h> |
ocomeni | 73:6f5021cbe752 | 9 | #include <mbed.h> |
ocomeni | 117:8fd05113efc1 | 10 | #include "nvstore.h" |
ocomeni | 73:6f5021cbe752 | 11 | #include "ble/BLE.h" |
ocomeni | 88:7ffa053be662 | 12 | #include "fault_handlers.h" |
ocomeni | 73:6f5021cbe752 | 13 | #include "ATCmdParser.h" |
ocomeni | 103:7b566b522427 | 14 | |
ocomeni | 73:6f5021cbe752 | 15 | |
ocomeni | 73:6f5021cbe752 | 16 | #include "LEDService.h" |
ocomeni | 73:6f5021cbe752 | 17 | #include "ble/services/UARTService.h" |
ocomeni | 74:f26e846adfe9 | 18 | #include "common_config.h" |
ocomeni | 79:a2187bbfa407 | 19 | #include "common_types.h" |
ocomeni | 74:f26e846adfe9 | 20 | #include "ATCmdManager.h" |
ocomeni | 75:08eff6258e1b | 21 | #include "BleManager.h" |
ocomeni | 78:07bb86e3ce14 | 22 | #include "WiFiManager.h" |
ocomeni | 92:ec9550034276 | 23 | #include "mbed_memory_status.h" |
ocomeni | 73:6f5021cbe752 | 24 | UARTService *uart; |
ocomeni | 73:6f5021cbe752 | 25 | |
ocomeni | 108:3c8fb2c6e7bf | 26 | DigitalOut led1(LED1); |
ocomeni | 108:3c8fb2c6e7bf | 27 | DigitalOut led2(LED2); |
ocomeni | 108:3c8fb2c6e7bf | 28 | DigitalOut led3(LED3); |
ocomeni | 73:6f5021cbe752 | 29 | |
ocomeni | 103:7b566b522427 | 30 | #define FILE_CODE "main" |
ocomeni | 117:8fd05113efc1 | 31 | |
ocomeni | 117:8fd05113efc1 | 32 | // NVStore is a sigleton, get its instance |
ocomeni | 117:8fd05113efc1 | 33 | NVStore &nvstore = NVStore::get_instance(); |
ocomeni | 117:8fd05113efc1 | 34 | |
ocomeni | 114:b11bb96c09f3 | 35 | main_states_t mainLoop; |
ocomeni | 74:f26e846adfe9 | 36 | static RawSerial *device; // tx, rx |
ocomeni | 78:07bb86e3ce14 | 37 | |
ocomeni | 116:2296cf274661 | 38 | static app_config_t app_config; |
ocomeni | 78:07bb86e3ce14 | 39 | // wifi configuration |
ocomeni | 116:2296cf274661 | 40 | //static wifi_config_t wifi_config; |
ocomeni | 78:07bb86e3ce14 | 41 | // wifi interface pointer |
ocomeni | 78:07bb86e3ce14 | 42 | static WiFiInterface *network; |
ocomeni | 78:07bb86e3ce14 | 43 | // wifi manager pointer |
ocomeni | 78:07bb86e3ce14 | 44 | static WiFiManager *wiFiManager; |
ocomeni | 78:07bb86e3ce14 | 45 | |
ocomeni | 114:b11bb96c09f3 | 46 | // BLE instance |
ocomeni | 114:b11bb96c09f3 | 47 | //BLE& _ble; |
ocomeni | 114:b11bb96c09f3 | 48 | BLE& _ble = BLE::Instance(); |
ocomeni | 114:b11bb96c09f3 | 49 | |
ocomeni | 78:07bb86e3ce14 | 50 | // BLE configuration |
ocomeni | 116:2296cf274661 | 51 | //static ble_config_t ble_config; |
ocomeni | 116:2296cf274661 | 52 | |
ocomeni | 116:2296cf274661 | 53 | // internet/cloud configuration |
ocomeni | 116:2296cf274661 | 54 | //internet_config_t internet_config; |
ocomeni | 116:2296cf274661 | 55 | |
ocomeni | 79:a2187bbfa407 | 56 | const uint8_t pairingPassword[6] = "1101"; |
ocomeni | 78:07bb86e3ce14 | 57 | // BLE peripheral pointer |
ocomeni | 77:0b505d1e15f4 | 58 | static SMDevicePeripheral *peripheral; |
ocomeni | 78:07bb86e3ce14 | 59 | |
ocomeni | 78:07bb86e3ce14 | 60 | const static char DEVICE_NAME_MAIN[] = "UBLOX-BLE"; |
ocomeni | 73:6f5021cbe752 | 61 | char buffer[BUFFER_LEN]; |
ocomeni | 109:c274780ff609 | 62 | static EventQueue eventQueue_atcmd(/* event count */ 32 * EVENTS_EVENT_SIZE); |
ocomeni | 109:c274780ff609 | 63 | static EventQueue eventQueue_wifi(/* event count */ 32 * EVENTS_EVENT_SIZE); |
ocomeni | 113:888e262ff0a9 | 64 | static EventQueue eventQueue_ble(/* event count */ 10 * EVENTS_EVENT_SIZE); |
ocomeni | 73:6f5021cbe752 | 65 | |
ocomeni | 80:e8f0e92e3ac9 | 66 | /* Queue and memory pool for AT to Wifi commands */ |
ocomeni | 92:ec9550034276 | 67 | static MemoryPool<wifi_cmd_message_t, 16> aT2WiFimPool; |
ocomeni | 92:ec9550034276 | 68 | static Queue<wifi_cmd_message_t, 16> aT2WiFiCmdQueue; |
ocomeni | 79:a2187bbfa407 | 69 | |
ocomeni | 80:e8f0e92e3ac9 | 70 | /* Queue and memory pool for WiFi to AT commands */ |
ocomeni | 92:ec9550034276 | 71 | static MemoryPool<at_resp_message_t, 16> wiFi2ATmPool; |
ocomeni | 92:ec9550034276 | 72 | static Queue<at_resp_message_t, 16> wiFi2ATCmdQueue; |
ocomeni | 80:e8f0e92e3ac9 | 73 | |
ocomeni | 80:e8f0e92e3ac9 | 74 | /* Queue and memory pool for AT to WiFi data */ |
ocomeni | 92:ec9550034276 | 75 | static MemoryPool<wifi_data_msg_t, PQDSZ> aT2WiFiDatamPool; |
ocomeni | 92:ec9550034276 | 76 | static Queue<wifi_data_msg_t, PQDSZ> aT2WiFiDataQueue; |
ocomeni | 80:e8f0e92e3ac9 | 77 | |
ocomeni | 80:e8f0e92e3ac9 | 78 | |
ocomeni | 80:e8f0e92e3ac9 | 79 | /* Queue and memory pool for WiFi to AT data */ |
ocomeni | 92:ec9550034276 | 80 | static MemoryPool<at_data_msg_t, PQDSZ> wiFi2ATDatamPool; |
ocomeni | 92:ec9550034276 | 81 | static Queue<at_data_msg_t, PQDSZ> wiFi2ATDataQueue; |
ocomeni | 80:e8f0e92e3ac9 | 82 | |
ocomeni | 80:e8f0e92e3ac9 | 83 | |
ocomeni | 80:e8f0e92e3ac9 | 84 | |
ocomeni | 74:f26e846adfe9 | 85 | |
ocomeni | 74:f26e846adfe9 | 86 | /* creates three tread objects with different priorities */ |
ocomeni | 74:f26e846adfe9 | 87 | //Thread real_time_thread(osPriorityRealtime, 1024, &rt_stk[0]); |
ocomeni | 74:f26e846adfe9 | 88 | //Thread high_prio_thread(osPriorityHigh, 1024, &hp_stk[0]); |
ocomeni | 74:f26e846adfe9 | 89 | //Thread low_prio_thread(osPriorityNormal, 1024, &lp_stk[0]); |
ocomeni | 84:7c7add00f4bf | 90 | |
ocomeni | 84:7c7add00f4bf | 91 | #ifdef USE_MAIN_THREAD_STACK |
ocomeni | 84:7c7add00f4bf | 92 | // using main thread stack |
ocomeni | 84:7c7add00f4bf | 93 | unsigned char btle_stk[1024]; |
ocomeni | 84:7c7add00f4bf | 94 | unsigned char wifi_stk[8*1024]; |
ocomeni | 84:7c7add00f4bf | 95 | unsigned char atcmd_stk[4*1024]; |
ocomeni | 74:f26e846adfe9 | 96 | Thread btle_thread(BTLE_THREAD_PRIORITY, 1024, &btle_stk[0]); |
ocomeni | 84:7c7add00f4bf | 97 | Thread wifi_thread(WIFI_THREAD_PRIORITY, 8*1024, &wifi_stk[0]); |
ocomeni | 81:637a87eb8170 | 98 | Thread atcmd_thread(ATCMD_THREAD_PRIORITY, 4*1024, &atcmd_stk[0]); |
ocomeni | 84:7c7add00f4bf | 99 | #else |
ocomeni | 84:7c7add00f4bf | 100 | // using global heap |
ocomeni | 113:888e262ff0a9 | 101 | Thread btle_thread(BTLE_THREAD_PRIORITY, 4*1024); |
ocomeni | 109:c274780ff609 | 102 | Thread wifi_thread(WIFI_THREAD_PRIORITY, 4*1024); |
ocomeni | 87:99b37d26ff2a | 103 | Thread atcmd_thread(ATCMD_THREAD_PRIORITY, 4*1024); |
ocomeni | 84:7c7add00f4bf | 104 | #endif |
ocomeni | 74:f26e846adfe9 | 105 | |
ocomeni | 74:f26e846adfe9 | 106 | /* create a semaphore to synchronize the threads */ |
ocomeni | 74:f26e846adfe9 | 107 | Semaphore sync_sema; |
ocomeni | 74:f26e846adfe9 | 108 | |
ocomeni | 109:c274780ff609 | 109 | Thread atcmd_evt_thread(osPriorityNormal, 1024); |
ocomeni | 109:c274780ff609 | 110 | Thread wifi_evt_thread; |
ocomeni | 74:f26e846adfe9 | 111 | #include "network-helper.h" |
ocomeni | 73:6f5021cbe752 | 112 | |
ocomeni | 73:6f5021cbe752 | 113 | /* List of trusted root CA certificates |
ocomeni | 73:6f5021cbe752 | 114 | * currently two: GlobalSign, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org |
ocomeni | 73:6f5021cbe752 | 115 | * |
ocomeni | 73:6f5021cbe752 | 116 | * To add more root certificates, just concatenate them. |
ocomeni | 73:6f5021cbe752 | 117 | */ |
ocomeni | 73:6f5021cbe752 | 118 | #include "https_certificates.h" |
ocomeni | 73:6f5021cbe752 | 119 | |
ocomeni | 74:f26e846adfe9 | 120 | // wifi demo |
ocomeni | 74:f26e846adfe9 | 121 | #include "wifi_demo.h" |
ocomeni | 73:6f5021cbe752 | 122 | |
ocomeni | 77:0b505d1e15f4 | 123 | Mutex _smutex; // Protect memory access |
ocomeni | 87:99b37d26ff2a | 124 | |
ocomeni | 73:6f5021cbe752 | 125 | uint64_t lastTime = 0; |
ocomeni | 73:6f5021cbe752 | 126 | uint64_t now = 0; |
ocomeni | 73:6f5021cbe752 | 127 | uint32_t callCount = 0; |
ocomeni | 73:6f5021cbe752 | 128 | |
ocomeni | 73:6f5021cbe752 | 129 | |
ocomeni | 73:6f5021cbe752 | 130 | // Wifi-demo |
ocomeni | 73:6f5021cbe752 | 131 | void wifi_demo(NetworkInterface* network){ |
ocomeni | 74:f26e846adfe9 | 132 | int n = wifi_demo_func(network); |
ocomeni | 74:f26e846adfe9 | 133 | if(n > 0)// error |
ocomeni | 73:6f5021cbe752 | 134 | { |
ocomeni | 103:7b566b522427 | 135 | dbg_printf(LOG, "\n --- Error running wifi demo --- \n"); |
ocomeni | 73:6f5021cbe752 | 136 | } |
ocomeni | 74:f26e846adfe9 | 137 | } |
ocomeni | 73:6f5021cbe752 | 138 | |
ocomeni | 74:f26e846adfe9 | 139 | // Wifi-demo2 |
ocomeni | 74:f26e846adfe9 | 140 | void wifi_demo2(){ |
ocomeni | 74:f26e846adfe9 | 141 | //int n = wifi_demo_func(network); |
ocomeni | 74:f26e846adfe9 | 142 | int n =5; |
ocomeni | 74:f26e846adfe9 | 143 | if(n > 0)// error |
ocomeni | 73:6f5021cbe752 | 144 | { |
ocomeni | 103:7b566b522427 | 145 | dbg_printf(LOG, "\n --- Error running wifi demo --- \n"); |
ocomeni | 73:6f5021cbe752 | 146 | } |
ocomeni | 74:f26e846adfe9 | 147 | } |
ocomeni | 73:6f5021cbe752 | 148 | |
ocomeni | 73:6f5021cbe752 | 149 | |
ocomeni | 113:888e262ff0a9 | 150 | void printWaitAbortKeyPress(int numSecs, const char * printStr=NULL) |
ocomeni | 79:a2187bbfa407 | 151 | { |
ocomeni | 115:8054dbadfaa0 | 152 | #ifdef DEBUG_ENABLED |
ocomeni | 115:8054dbadfaa0 | 153 | return |
ocomeni | 115:8054dbadfaa0 | 154 | #endif |
ocomeni | 113:888e262ff0a9 | 155 | dbg_printf(LOG, "%s", printStr); |
ocomeni | 103:7b566b522427 | 156 | dbg_printf(LOG, "Waiting for %d seconds... [press key to abort]\n", numSecs); |
ocomeni | 79:a2187bbfa407 | 157 | char fmtstr[20]; |
ocomeni | 79:a2187bbfa407 | 158 | for(int i=0;i<numSecs;i++){ |
ocomeni | 103:7b566b522427 | 159 | dbg_printf(LOG, "%d", i); |
ocomeni | 103:7b566b522427 | 160 | dbg_printf(LOG, "\n"); |
ocomeni | 79:a2187bbfa407 | 161 | sprintf(fmtstr, "BLE: loop # %d\n", i); |
ocomeni | 79:a2187bbfa407 | 162 | peripheral->sendBLEUartData(fmtstr); |
ocomeni | 79:a2187bbfa407 | 163 | wait(0.5); |
ocomeni | 113:888e262ff0a9 | 164 | //eventQueue_atcmd.dispatch(500); // Dispatch time - 500msec |
ocomeni | 79:a2187bbfa407 | 165 | if(device->readable()){ |
ocomeni | 103:7b566b522427 | 166 | dbg_printf(LOG, "keypress detected aborting....\n"); |
ocomeni | 79:a2187bbfa407 | 167 | device->getc(); |
ocomeni | 79:a2187bbfa407 | 168 | break; |
ocomeni | 79:a2187bbfa407 | 169 | } |
ocomeni | 79:a2187bbfa407 | 170 | } |
ocomeni | 79:a2187bbfa407 | 171 | } |
ocomeni | 79:a2187bbfa407 | 172 | |
ocomeni | 79:a2187bbfa407 | 173 | |
ocomeni | 78:07bb86e3ce14 | 174 | |
ocomeni | 78:07bb86e3ce14 | 175 | void setupDefaultBleConfig() |
ocomeni | 78:07bb86e3ce14 | 176 | { |
ocomeni | 116:2296cf274661 | 177 | strcpy(app_config.ble_config.deviceName, DEVICE_NAME_MAIN);// set BLE device name |
ocomeni | 116:2296cf274661 | 178 | app_config.ble_config.advInterval = 1000; // set advertising interval to 1 second default |
ocomeni | 116:2296cf274661 | 179 | app_config.ble_config.advTimeout = 0; // set advertising timeout to disabled by default |
ocomeni | 79:a2187bbfa407 | 180 | // This works in C and C++ |
ocomeni | 116:2296cf274661 | 181 | memcpy(app_config.ble_config.pairingKey, pairingPassword, 6); // |
ocomeni | 79:a2187bbfa407 | 182 | |
ocomeni | 79:a2187bbfa407 | 183 | //ble_config.pairingKey = pairingPassword; |
ocomeni | 78:07bb86e3ce14 | 184 | } |
ocomeni | 78:07bb86e3ce14 | 185 | |
ocomeni | 78:07bb86e3ce14 | 186 | void setupDefaultWiFiConfig() |
ocomeni | 78:07bb86e3ce14 | 187 | { |
ocomeni | 116:2296cf274661 | 188 | strcpy(app_config.wifi_config.ssid, MBED_CONF_APP_WIFI_SSID); |
ocomeni | 116:2296cf274661 | 189 | strcpy(app_config.wifi_config.pass, MBED_CONF_APP_WIFI_PASSWORD); |
ocomeni | 116:2296cf274661 | 190 | app_config.wifi_config.security = NSAPI_SECURITY_WPA_WPA2; |
ocomeni | 116:2296cf274661 | 191 | } |
ocomeni | 116:2296cf274661 | 192 | |
ocomeni | 117:8fd05113efc1 | 193 | // Entry point for the example |
ocomeni | 117:8fd05113efc1 | 194 | void print_app_config() |
ocomeni | 117:8fd05113efc1 | 195 | { |
ocomeni | 117:8fd05113efc1 | 196 | //dbg_printf(LOG, "Return code is %d ", rc); |
ocomeni | 117:8fd05113efc1 | 197 | } |
ocomeni | 117:8fd05113efc1 | 198 | |
ocomeni | 117:8fd05113efc1 | 199 | // Entry point for the example |
ocomeni | 117:8fd05113efc1 | 200 | void print_return_code(int rc, int expected_rc) |
ocomeni | 117:8fd05113efc1 | 201 | { |
ocomeni | 117:8fd05113efc1 | 202 | dbg_printf(LOG, "Return code is %d ", rc); |
ocomeni | 117:8fd05113efc1 | 203 | if (rc == expected_rc) |
ocomeni | 117:8fd05113efc1 | 204 | dbg_printf(LOG, "(as expected).\n"); |
ocomeni | 117:8fd05113efc1 | 205 | else |
ocomeni | 117:8fd05113efc1 | 206 | dbg_printf(LOG, "(expected %d!).\n", expected_rc); |
ocomeni | 117:8fd05113efc1 | 207 | } |
ocomeni | 117:8fd05113efc1 | 208 | |
ocomeni | 117:8fd05113efc1 | 209 | |
ocomeni | 116:2296cf274661 | 210 | void setupDefaultCloudConfig() |
ocomeni | 116:2296cf274661 | 211 | { |
ocomeni | 116:2296cf274661 | 212 | strcpy(app_config.internet_config.url, "tcp://https://dev2.dnanudge.io"); |
ocomeni | 116:2296cf274661 | 213 | app_config.internet_config.peer_id = 0; |
ocomeni | 116:2296cf274661 | 214 | app_config.internet_config.remote_port = 443; // default HTTPS port |
ocomeni | 116:2296cf274661 | 215 | app_config.internet_config.connectionScheme = ALWAYS_CONNECTED; |
ocomeni | 78:07bb86e3ce14 | 216 | } |
ocomeni | 78:07bb86e3ce14 | 217 | |
ocomeni | 117:8fd05113efc1 | 218 | void setupDefaultUartConfig() |
ocomeni | 117:8fd05113efc1 | 219 | { |
ocomeni | 117:8fd05113efc1 | 220 | app_config.uart_config.baudrate = 2*DEFAULT_BAUD_RATE; |
ocomeni | 117:8fd05113efc1 | 221 | app_config.uart_config.flow_ctrl = 2; |
ocomeni | 117:8fd05113efc1 | 222 | app_config.uart_config.data_bits = 8; |
ocomeni | 117:8fd05113efc1 | 223 | app_config.uart_config.stop_bits = 1; |
ocomeni | 117:8fd05113efc1 | 224 | app_config.uart_config.parity = 1; |
ocomeni | 117:8fd05113efc1 | 225 | app_config.uart_config.change_after_confirm = 1; |
ocomeni | 117:8fd05113efc1 | 226 | } |
ocomeni | 117:8fd05113efc1 | 227 | |
ocomeni | 117:8fd05113efc1 | 228 | void resetConfiguration() |
ocomeni | 117:8fd05113efc1 | 229 | { |
ocomeni | 117:8fd05113efc1 | 230 | int rc; |
ocomeni | 117:8fd05113efc1 | 231 | // Clear NVStore data. Should only be done once at factory configuration |
ocomeni | 117:8fd05113efc1 | 232 | rc = nvstore.reset(); |
ocomeni | 117:8fd05113efc1 | 233 | dbg_printf(LOG, "Reset NVStore. "); |
ocomeni | 117:8fd05113efc1 | 234 | print_return_code(rc, NVSTORE_SUCCESS); |
ocomeni | 117:8fd05113efc1 | 235 | |
ocomeni | 117:8fd05113efc1 | 236 | } |
ocomeni | 117:8fd05113efc1 | 237 | |
ocomeni | 117:8fd05113efc1 | 238 | void saveConfiguration(uint32_t configKey) |
ocomeni | 117:8fd05113efc1 | 239 | { |
ocomeni | 117:8fd05113efc1 | 240 | int rc; |
ocomeni | 117:8fd05113efc1 | 241 | rc = nvstore.set(configKey, sizeof(app_config), &app_config); |
ocomeni | 117:8fd05113efc1 | 242 | print_return_code(rc, NVSTORE_SUCCESS); |
ocomeni | 117:8fd05113efc1 | 243 | } |
ocomeni | 117:8fd05113efc1 | 244 | |
ocomeni | 117:8fd05113efc1 | 245 | void loadConfiguration(uint32_t configKey) |
ocomeni | 117:8fd05113efc1 | 246 | { |
ocomeni | 117:8fd05113efc1 | 247 | int rc; |
ocomeni | 117:8fd05113efc1 | 248 | // Get the value of this key (should be 3000) |
ocomeni | 117:8fd05113efc1 | 249 | uint16_t actual_len_bytes; |
ocomeni | 117:8fd05113efc1 | 250 | rc = nvstore.get(configKey, sizeof(app_config), &app_config, actual_len_bytes); |
ocomeni | 117:8fd05113efc1 | 251 | print_app_config(); |
ocomeni | 117:8fd05113efc1 | 252 | print_return_code(rc, NVSTORE_SUCCESS); |
ocomeni | 117:8fd05113efc1 | 253 | } |
ocomeni | 117:8fd05113efc1 | 254 | |
ocomeni | 74:f26e846adfe9 | 255 | static int reset_counter = 0; |
ocomeni | 73:6f5021cbe752 | 256 | |
ocomeni | 75:08eff6258e1b | 257 | |
ocomeni | 75:08eff6258e1b | 258 | |
ocomeni | 105:e5ce023eee93 | 259 | //#define ENABLE_MEMORY_CHECKS |
ocomeni | 75:08eff6258e1b | 260 | |
ocomeni | 77:0b505d1e15f4 | 261 | void print_memory_info() { |
ocomeni | 103:7b566b522427 | 262 | #ifdef ENABLE_MEMORY_CHECKS |
ocomeni | 77:0b505d1e15f4 | 263 | // allocate enough room for every thread's stack statistics |
ocomeni | 77:0b505d1e15f4 | 264 | int cnt = osThreadGetCount(); |
ocomeni | 77:0b505d1e15f4 | 265 | mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t)); |
ocomeni | 77:0b505d1e15f4 | 266 | |
ocomeni | 77:0b505d1e15f4 | 267 | cnt = mbed_stats_stack_get_each(stats, cnt); |
ocomeni | 77:0b505d1e15f4 | 268 | for (int i = 0; i < cnt; i++) { |
ocomeni | 103:7b566b522427 | 269 | dbg_printf(LOG, "Thread: 0x%lX, Stack size: %lu / %lu\r\n", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size); |
ocomeni | 77:0b505d1e15f4 | 270 | } |
ocomeni | 77:0b505d1e15f4 | 271 | free(stats); |
ocomeni | 77:0b505d1e15f4 | 272 | |
ocomeni | 77:0b505d1e15f4 | 273 | // Grab the heap statistics |
ocomeni | 77:0b505d1e15f4 | 274 | mbed_stats_heap_t heap_stats; |
ocomeni | 77:0b505d1e15f4 | 275 | mbed_stats_heap_get(&heap_stats); |
ocomeni | 103:7b566b522427 | 276 | dbg_printf(LOG, "Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size); |
ocomeni | 103:7b566b522427 | 277 | #endif |
ocomeni | 77:0b505d1e15f4 | 278 | } |
ocomeni | 77:0b505d1e15f4 | 279 | |
ocomeni | 108:3c8fb2c6e7bf | 280 | void blinkLEDs() |
ocomeni | 108:3c8fb2c6e7bf | 281 | { |
ocomeni | 108:3c8fb2c6e7bf | 282 | static int cnt =0; |
ocomeni | 108:3c8fb2c6e7bf | 283 | cnt++; |
ocomeni | 108:3c8fb2c6e7bf | 284 | if(cnt == 3){ |
ocomeni | 108:3c8fb2c6e7bf | 285 | cnt = 0; |
ocomeni | 108:3c8fb2c6e7bf | 286 | } |
ocomeni | 108:3c8fb2c6e7bf | 287 | if(cnt==0) |
ocomeni | 108:3c8fb2c6e7bf | 288 | led1 = !led1; |
ocomeni | 108:3c8fb2c6e7bf | 289 | //wait(1.0); |
ocomeni | 108:3c8fb2c6e7bf | 290 | if(cnt==1) |
ocomeni | 108:3c8fb2c6e7bf | 291 | led2 = !led2; |
ocomeni | 108:3c8fb2c6e7bf | 292 | //wait(1.0); |
ocomeni | 108:3c8fb2c6e7bf | 293 | if(cnt==2) |
ocomeni | 108:3c8fb2c6e7bf | 294 | led3 = !led3; |
ocomeni | 108:3c8fb2c6e7bf | 295 | } |
ocomeni | 108:3c8fb2c6e7bf | 296 | |
ocomeni | 114:b11bb96c09f3 | 297 | void start_BLE() |
ocomeni | 114:b11bb96c09f3 | 298 | { |
ocomeni | 114:b11bb96c09f3 | 299 | //_ble = BLE::Instance(); |
ocomeni | 77:0b505d1e15f4 | 300 | #if MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 77:0b505d1e15f4 | 301 | /* if filesystem creation fails or there is no filesystem the security manager |
ocomeni | 77:0b505d1e15f4 | 302 | * will fallback to storing the security database in memory */ |
ocomeni | 77:0b505d1e15f4 | 303 | if (!create_filesystem()) { |
ocomeni | 103:7b566b522427 | 304 | dbg_printf(LOG, "Filesystem creation failed, will use memory storage\r\n"); |
ocomeni | 75:08eff6258e1b | 305 | } |
ocomeni | 76:6afda865fbf8 | 306 | #endif |
ocomeni | 103:7b566b522427 | 307 | dbg_printf(LOG, "\r\n PERIPHERAL \r\n\r\n"); |
ocomeni | 116:2296cf274661 | 308 | peripheral = new SMDevicePeripheral(_ble, eventQueue_ble, peer_address, |
ocomeni | 116:2296cf274661 | 309 | &app_config.ble_config); |
ocomeni | 77:0b505d1e15f4 | 310 | |
ocomeni | 77:0b505d1e15f4 | 311 | peripheral->run(); |
ocomeni | 113:888e262ff0a9 | 312 | btle_thread.start(callback(&eventQueue_ble, &EventQueue::dispatch_forever)); |
ocomeni | 114:b11bb96c09f3 | 313 | } |
ocomeni | 114:b11bb96c09f3 | 314 | |
ocomeni | 114:b11bb96c09f3 | 315 | void stop_BLE() |
ocomeni | 114:b11bb96c09f3 | 316 | { |
ocomeni | 114:b11bb96c09f3 | 317 | delete peripheral; |
ocomeni | 114:b11bb96c09f3 | 318 | } |
ocomeni | 114:b11bb96c09f3 | 319 | |
ocomeni | 114:b11bb96c09f3 | 320 | void start_WiFi() |
ocomeni | 114:b11bb96c09f3 | 321 | { |
ocomeni | 116:2296cf274661 | 322 | wiFiManager = new WiFiManager(&app_config.wifi_config, network, |
ocomeni | 116:2296cf274661 | 323 | &app_config.internet_config, |
ocomeni | 109:c274780ff609 | 324 | eventQueue_wifi, |
ocomeni | 80:e8f0e92e3ac9 | 325 | &aT2WiFimPool, &aT2WiFiCmdQueue, |
ocomeni | 80:e8f0e92e3ac9 | 326 | &wiFi2ATmPool, &wiFi2ATCmdQueue, |
ocomeni | 80:e8f0e92e3ac9 | 327 | &aT2WiFiDatamPool, &aT2WiFiDataQueue, |
ocomeni | 80:e8f0e92e3ac9 | 328 | &wiFi2ATDatamPool, &wiFi2ATDataQueue |
ocomeni | 80:e8f0e92e3ac9 | 329 | ); |
ocomeni | 103:7b566b522427 | 330 | dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); |
ocomeni | 103:7b566b522427 | 331 | dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network scan from thread ++++++ \r\n"); |
ocomeni | 103:7b566b522427 | 332 | dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); |
ocomeni | 81:637a87eb8170 | 333 | wifi_thread.start(callback(wiFiManager, &WiFiManager::runMain)); |
ocomeni | 103:7b566b522427 | 334 | dbg_printf(LOG, "\r\n after starting wifi thread \r\n"); |
ocomeni | 113:888e262ff0a9 | 335 | printWaitAbortKeyPress(120, "Wait after WiFi Manager dispatch\r\n"); |
ocomeni | 112:a0999ea4ece0 | 336 | // dispatch wifi event queue on event thread |
ocomeni | 109:c274780ff609 | 337 | wifi_evt_thread.start(callback(&eventQueue_wifi, &EventQueue::dispatch_forever)); |
ocomeni | 114:b11bb96c09f3 | 338 | } |
ocomeni | 114:b11bb96c09f3 | 339 | |
ocomeni | 114:b11bb96c09f3 | 340 | void stop_WiFi() |
ocomeni | 114:b11bb96c09f3 | 341 | { |
ocomeni | 114:b11bb96c09f3 | 342 | delete network; |
ocomeni | 114:b11bb96c09f3 | 343 | } |
ocomeni | 114:b11bb96c09f3 | 344 | |
ocomeni | 114:b11bb96c09f3 | 345 | |
ocomeni | 114:b11bb96c09f3 | 346 | void trigger_start_BLE() |
ocomeni | 114:b11bb96c09f3 | 347 | { |
ocomeni | 114:b11bb96c09f3 | 348 | mainLoop = START_BLE; |
ocomeni | 114:b11bb96c09f3 | 349 | } |
ocomeni | 114:b11bb96c09f3 | 350 | |
ocomeni | 114:b11bb96c09f3 | 351 | |
ocomeni | 114:b11bb96c09f3 | 352 | void trigger_stop_BLE() |
ocomeni | 114:b11bb96c09f3 | 353 | { |
ocomeni | 114:b11bb96c09f3 | 354 | mainLoop = STOP_BLE; |
ocomeni | 114:b11bb96c09f3 | 355 | } |
ocomeni | 114:b11bb96c09f3 | 356 | |
ocomeni | 114:b11bb96c09f3 | 357 | |
ocomeni | 114:b11bb96c09f3 | 358 | void trigger_start_WiFi() |
ocomeni | 114:b11bb96c09f3 | 359 | { |
ocomeni | 114:b11bb96c09f3 | 360 | mainLoop = START_WIFI; |
ocomeni | 114:b11bb96c09f3 | 361 | } |
ocomeni | 114:b11bb96c09f3 | 362 | |
ocomeni | 114:b11bb96c09f3 | 363 | |
ocomeni | 114:b11bb96c09f3 | 364 | void trigger_stop_WiFi() |
ocomeni | 114:b11bb96c09f3 | 365 | { |
ocomeni | 114:b11bb96c09f3 | 366 | mainLoop = STOP_WIFI; |
ocomeni | 114:b11bb96c09f3 | 367 | } |
ocomeni | 114:b11bb96c09f3 | 368 | |
ocomeni | 117:8fd05113efc1 | 369 | //#define USE_DEFAULT_CONFIGURATION |
ocomeni | 117:8fd05113efc1 | 370 | //#define TEST_NVSTORE |
ocomeni | 115:8054dbadfaa0 | 371 | #define STARTUP_DEBUG_ENABLE |
ocomeni | 115:8054dbadfaa0 | 372 | //#define AUTO_START_BLE_MANAGER |
ocomeni | 115:8054dbadfaa0 | 373 | //#define AUTO_START_WIFI_MANAGER |
ocomeni | 114:b11bb96c09f3 | 374 | #define PAUSE_SECONDS 0 |
ocomeni | 114:b11bb96c09f3 | 375 | #define PAUSE_SECONDS_BLE 0 |
ocomeni | 114:b11bb96c09f3 | 376 | int main() { |
ocomeni | 117:8fd05113efc1 | 377 | #ifndef USE_DEFAULT_CONFIGURATION |
ocomeni | 117:8fd05113efc1 | 378 | loadConfiguration(APP_CONFIG_0); |
ocomeni | 117:8fd05113efc1 | 379 | #else |
ocomeni | 117:8fd05113efc1 | 380 | setupDefaultUartConfig(); |
ocomeni | 117:8fd05113efc1 | 381 | setupDefaultWiFiConfig(); |
ocomeni | 117:8fd05113efc1 | 382 | setupDefaultBleConfig(); |
ocomeni | 117:8fd05113efc1 | 383 | setupDefaultCloudConfig(); |
ocomeni | 117:8fd05113efc1 | 384 | #endif |
ocomeni | 117:8fd05113efc1 | 385 | device = new RawSerial(USBTX, USBRX, app_config.uart_config.baudrate); |
ocomeni | 114:b11bb96c09f3 | 386 | uint8_t debug_level = (LOG | ERR | TXT | DBG); |
ocomeni | 114:b11bb96c09f3 | 387 | initialise_debug(debug_level); |
ocomeni | 114:b11bb96c09f3 | 388 | #ifdef MBED_MAJOR_VERSION |
ocomeni | 114:b11bb96c09f3 | 389 | dbg_printf(LOG, "Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
ocomeni | 114:b11bb96c09f3 | 390 | #endif |
ocomeni | 117:8fd05113efc1 | 391 | #ifdef TEST_NVSTORE |
ocomeni | 117:8fd05113efc1 | 392 | app_config_t *current_config = new app_config_t; |
ocomeni | 117:8fd05113efc1 | 393 | memcpy(current_config, &app_config, sizeof(app_config_t)); |
ocomeni | 117:8fd05113efc1 | 394 | saveConfiguration(APP_CONFIG_0); |
ocomeni | 117:8fd05113efc1 | 395 | memset(&app_config, 0, sizeof(app_config_t)); |
ocomeni | 117:8fd05113efc1 | 396 | loadConfiguration(APP_CONFIG_0); |
ocomeni | 117:8fd05113efc1 | 397 | int cmp; |
ocomeni | 117:8fd05113efc1 | 398 | cmp = memcmp(current_config, &app_config, sizeof(app_config_t)); |
ocomeni | 117:8fd05113efc1 | 399 | if(cmp == 0) |
ocomeni | 117:8fd05113efc1 | 400 | { |
ocomeni | 117:8fd05113efc1 | 401 | dbg_printf(LOG, " NVSTRORE TEST Passed!!\r\n"); |
ocomeni | 117:8fd05113efc1 | 402 | } |
ocomeni | 117:8fd05113efc1 | 403 | else |
ocomeni | 117:8fd05113efc1 | 404 | { |
ocomeni | 117:8fd05113efc1 | 405 | dbg_printf(LOG, " NVSTRORE TEST Failed!!\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
ocomeni | 117:8fd05113efc1 | 406 | } |
ocomeni | 117:8fd05113efc1 | 407 | delete current_config; |
ocomeni | 117:8fd05113efc1 | 408 | #endif |
ocomeni | 114:b11bb96c09f3 | 409 | |
ocomeni | 114:b11bb96c09f3 | 410 | reset_counter++; |
ocomeni | 114:b11bb96c09f3 | 411 | dbg_printf(LOG, "\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter); |
ocomeni | 115:8054dbadfaa0 | 412 | #ifdef AUTO_START_BLE_MANAGER |
ocomeni | 114:b11bb96c09f3 | 413 | //btle_thread.start(callback(peripheral, &SMDevicePeripheral::run)); |
ocomeni | 114:b11bb96c09f3 | 414 | start_BLE(); |
ocomeni | 114:b11bb96c09f3 | 415 | printWaitAbortKeyPress(120, "Wait after BLE dispatch\r\n"); |
ocomeni | 115:8054dbadfaa0 | 416 | #endif |
ocomeni | 115:8054dbadfaa0 | 417 | //int start = Kernel::get_ms_count(); |
ocomeni | 115:8054dbadfaa0 | 418 | #ifdef AUTO_START_WIFI_MANAGER |
ocomeni | 114:b11bb96c09f3 | 419 | start_WiFi(); |
ocomeni | 114:b11bb96c09f3 | 420 | printWaitAbortKeyPress(120, "Wait after WiFi instantiation\r\n"); |
ocomeni | 115:8054dbadfaa0 | 421 | #endif |
ocomeni | 112:a0999ea4ece0 | 422 | // dispatch atcmd event queue on event thread |
ocomeni | 109:c274780ff609 | 423 | atcmd_evt_thread.start(callback(&eventQueue_atcmd, &EventQueue::dispatch_forever)); |
ocomeni | 103:7b566b522427 | 424 | dbg_printf(LOG, "\r\n++++++ Starting ATCmdmanager ++++++ \r\n"); |
ocomeni | 117:8fd05113efc1 | 425 | ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, &app_config.uart_config, |
ocomeni | 109:c274780ff609 | 426 | eventQueue_atcmd, wiFiManager, |
ocomeni | 80:e8f0e92e3ac9 | 427 | &aT2WiFimPool, &aT2WiFiCmdQueue, |
ocomeni | 80:e8f0e92e3ac9 | 428 | &wiFi2ATmPool, &wiFi2ATCmdQueue, |
ocomeni | 80:e8f0e92e3ac9 | 429 | &aT2WiFiDatamPool, &aT2WiFiDataQueue, |
ocomeni | 80:e8f0e92e3ac9 | 430 | &wiFi2ATDatamPool, &wiFi2ATDataQueue, |
ocomeni | 82:10072c1794d3 | 431 | false); |
ocomeni | 81:637a87eb8170 | 432 | atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain)); |
ocomeni | 103:7b566b522427 | 433 | dbg_printf(LOG, "\r\n after starting atcmd thread \r\n"); |
ocomeni | 88:7ffa053be662 | 434 | print_memory_info(); |
ocomeni | 115:8054dbadfaa0 | 435 | #ifdef STARTUP_DEBUG_ENABLE |
ocomeni | 115:8054dbadfaa0 | 436 | initialise_debug(NONE); |
ocomeni | 115:8054dbadfaa0 | 437 | #endif |
ocomeni | 108:3c8fb2c6e7bf | 438 | while(1) |
ocomeni | 108:3c8fb2c6e7bf | 439 | { |
ocomeni | 114:b11bb96c09f3 | 440 | switch(mainLoop) |
ocomeni | 114:b11bb96c09f3 | 441 | { |
ocomeni | 114:b11bb96c09f3 | 442 | case MAIN_IDLE: |
ocomeni | 114:b11bb96c09f3 | 443 | break; |
ocomeni | 114:b11bb96c09f3 | 444 | case START_BLE: |
ocomeni | 114:b11bb96c09f3 | 445 | start_BLE(); |
ocomeni | 114:b11bb96c09f3 | 446 | mainLoop = MAIN_IDLE; |
ocomeni | 114:b11bb96c09f3 | 447 | break; |
ocomeni | 114:b11bb96c09f3 | 448 | case START_WIFI: |
ocomeni | 114:b11bb96c09f3 | 449 | start_WiFi(); |
ocomeni | 114:b11bb96c09f3 | 450 | mainLoop = MAIN_IDLE; |
ocomeni | 114:b11bb96c09f3 | 451 | break; |
ocomeni | 114:b11bb96c09f3 | 452 | case STOP_BLE: |
ocomeni | 114:b11bb96c09f3 | 453 | stop_BLE(); |
ocomeni | 114:b11bb96c09f3 | 454 | mainLoop = MAIN_IDLE; |
ocomeni | 114:b11bb96c09f3 | 455 | break; |
ocomeni | 114:b11bb96c09f3 | 456 | case STOP_WIFI: |
ocomeni | 114:b11bb96c09f3 | 457 | stop_WiFi(); |
ocomeni | 114:b11bb96c09f3 | 458 | mainLoop = MAIN_IDLE; |
ocomeni | 114:b11bb96c09f3 | 459 | break; |
ocomeni | 114:b11bb96c09f3 | 460 | default: |
ocomeni | 114:b11bb96c09f3 | 461 | mainLoop = MAIN_IDLE; |
ocomeni | 114:b11bb96c09f3 | 462 | break; |
ocomeni | 114:b11bb96c09f3 | 463 | } |
ocomeni | 108:3c8fb2c6e7bf | 464 | wait(0.1); |
ocomeni | 108:3c8fb2c6e7bf | 465 | } |
ocomeni | 73:6f5021cbe752 | 466 | } |
ocomeni | 73:6f5021cbe752 | 467 | |
ocomeni | 73:6f5021cbe752 | 468 | #endif |