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