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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sat May 25 16:25:42 2019 +0000
Branch:
PassingRegression
Revision:
118:8df0e9c2ee3f
Parent:
117:8fd05113efc1
Child:
119:8d939a902333
- fixed memory leak bug with ATCMD manager; - added BLE memory pool and queues and connected up to main, BLE manager and ATCMD; - code compiling; - python test passing

Who changed what in which revision?

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