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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sat May 11 11:55:29 2019 +0000
Branch:
PassingRegression
Revision:
112:a0999ea4ece0
Parent:
109:c274780ff609
Child:
113:888e262ff0a9
first pass code cleanup

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 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 73:6f5021cbe752 30
ocomeni 74:f26e846adfe9 31 static RawSerial *device; // tx, rx
ocomeni 78:07bb86e3ce14 32
ocomeni 78:07bb86e3ce14 33 // wifi configuration
ocomeni 78:07bb86e3ce14 34 static wifi_config_t wifi_config;
ocomeni 78:07bb86e3ce14 35 // wifi interface pointer
ocomeni 78:07bb86e3ce14 36 static WiFiInterface *network;
ocomeni 78:07bb86e3ce14 37 // wifi manager pointer
ocomeni 78:07bb86e3ce14 38 static WiFiManager *wiFiManager;
ocomeni 78:07bb86e3ce14 39
ocomeni 78:07bb86e3ce14 40 // BLE configuration
ocomeni 78:07bb86e3ce14 41 static ble_config_t ble_config;
ocomeni 79:a2187bbfa407 42 const uint8_t pairingPassword[6] = "1101";
ocomeni 78:07bb86e3ce14 43 // BLE peripheral pointer
ocomeni 77:0b505d1e15f4 44 static SMDevicePeripheral *peripheral;
ocomeni 78:07bb86e3ce14 45
ocomeni 78:07bb86e3ce14 46 const static char DEVICE_NAME_MAIN[] = "UBLOX-BLE";
ocomeni 73:6f5021cbe752 47 char buffer[BUFFER_LEN];
ocomeni 109:c274780ff609 48 static EventQueue eventQueue_atcmd(/* event count */ 32 * EVENTS_EVENT_SIZE);
ocomeni 109:c274780ff609 49 static EventQueue eventQueue_wifi(/* event count */ 32 * EVENTS_EVENT_SIZE);
ocomeni 73:6f5021cbe752 50
ocomeni 80:e8f0e92e3ac9 51 /* Queue and memory pool for AT to Wifi commands */
ocomeni 92:ec9550034276 52 static MemoryPool<wifi_cmd_message_t, 16> aT2WiFimPool;
ocomeni 92:ec9550034276 53 static Queue<wifi_cmd_message_t, 16> aT2WiFiCmdQueue;
ocomeni 79:a2187bbfa407 54
ocomeni 80:e8f0e92e3ac9 55 /* Queue and memory pool for WiFi to AT commands */
ocomeni 92:ec9550034276 56 static MemoryPool<at_resp_message_t, 16> wiFi2ATmPool;
ocomeni 92:ec9550034276 57 static Queue<at_resp_message_t, 16> wiFi2ATCmdQueue;
ocomeni 80:e8f0e92e3ac9 58
ocomeni 80:e8f0e92e3ac9 59 /* Queue and memory pool for AT to WiFi data */
ocomeni 92:ec9550034276 60 static MemoryPool<wifi_data_msg_t, PQDSZ> aT2WiFiDatamPool;
ocomeni 92:ec9550034276 61 static Queue<wifi_data_msg_t, PQDSZ> aT2WiFiDataQueue;
ocomeni 80:e8f0e92e3ac9 62
ocomeni 80:e8f0e92e3ac9 63
ocomeni 80:e8f0e92e3ac9 64 /* Queue and memory pool for WiFi to AT data */
ocomeni 92:ec9550034276 65 static MemoryPool<at_data_msg_t, PQDSZ> wiFi2ATDatamPool;
ocomeni 92:ec9550034276 66 static Queue<at_data_msg_t, PQDSZ> wiFi2ATDataQueue;
ocomeni 80:e8f0e92e3ac9 67
ocomeni 80:e8f0e92e3ac9 68
ocomeni 80:e8f0e92e3ac9 69
ocomeni 74:f26e846adfe9 70
ocomeni 74:f26e846adfe9 71 /* creates three tread objects with different priorities */
ocomeni 74:f26e846adfe9 72 //Thread real_time_thread(osPriorityRealtime, 1024, &rt_stk[0]);
ocomeni 74:f26e846adfe9 73 //Thread high_prio_thread(osPriorityHigh, 1024, &hp_stk[0]);
ocomeni 74:f26e846adfe9 74 //Thread low_prio_thread(osPriorityNormal, 1024, &lp_stk[0]);
ocomeni 84:7c7add00f4bf 75
ocomeni 84:7c7add00f4bf 76 #ifdef USE_MAIN_THREAD_STACK
ocomeni 84:7c7add00f4bf 77 // using main thread stack
ocomeni 84:7c7add00f4bf 78 unsigned char btle_stk[1024];
ocomeni 84:7c7add00f4bf 79 unsigned char wifi_stk[8*1024];
ocomeni 84:7c7add00f4bf 80 unsigned char atcmd_stk[4*1024];
ocomeni 74:f26e846adfe9 81 Thread btle_thread(BTLE_THREAD_PRIORITY, 1024, &btle_stk[0]);
ocomeni 84:7c7add00f4bf 82 Thread wifi_thread(WIFI_THREAD_PRIORITY, 8*1024, &wifi_stk[0]);
ocomeni 81:637a87eb8170 83 Thread atcmd_thread(ATCMD_THREAD_PRIORITY, 4*1024, &atcmd_stk[0]);
ocomeni 84:7c7add00f4bf 84 #else
ocomeni 84:7c7add00f4bf 85 // using global heap
ocomeni 87:99b37d26ff2a 86 Thread btle_thread(BTLE_THREAD_PRIORITY, 1024);
ocomeni 109:c274780ff609 87 Thread wifi_thread(WIFI_THREAD_PRIORITY, 4*1024);
ocomeni 87:99b37d26ff2a 88 Thread atcmd_thread(ATCMD_THREAD_PRIORITY, 4*1024);
ocomeni 84:7c7add00f4bf 89 #endif
ocomeni 74:f26e846adfe9 90
ocomeni 74:f26e846adfe9 91 /* create a semaphore to synchronize the threads */
ocomeni 74:f26e846adfe9 92 Semaphore sync_sema;
ocomeni 74:f26e846adfe9 93
ocomeni 109:c274780ff609 94 Thread atcmd_evt_thread(osPriorityNormal, 1024);
ocomeni 109:c274780ff609 95 Thread wifi_evt_thread;
ocomeni 74:f26e846adfe9 96 #include "network-helper.h"
ocomeni 73:6f5021cbe752 97
ocomeni 73:6f5021cbe752 98 /* List of trusted root CA certificates
ocomeni 73:6f5021cbe752 99 * currently two: GlobalSign, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org
ocomeni 73:6f5021cbe752 100 *
ocomeni 73:6f5021cbe752 101 * To add more root certificates, just concatenate them.
ocomeni 73:6f5021cbe752 102 */
ocomeni 73:6f5021cbe752 103 #include "https_certificates.h"
ocomeni 73:6f5021cbe752 104
ocomeni 74:f26e846adfe9 105 // wifi demo
ocomeni 74:f26e846adfe9 106 #include "wifi_demo.h"
ocomeni 73:6f5021cbe752 107
ocomeni 77:0b505d1e15f4 108 Mutex _smutex; // Protect memory access
ocomeni 87:99b37d26ff2a 109
ocomeni 73:6f5021cbe752 110 uint64_t lastTime = 0;
ocomeni 73:6f5021cbe752 111 uint64_t now = 0;
ocomeni 73:6f5021cbe752 112 uint32_t callCount = 0;
ocomeni 73:6f5021cbe752 113
ocomeni 73:6f5021cbe752 114
ocomeni 73:6f5021cbe752 115 // Wifi-demo
ocomeni 73:6f5021cbe752 116 void wifi_demo(NetworkInterface* network){
ocomeni 74:f26e846adfe9 117 int n = wifi_demo_func(network);
ocomeni 74:f26e846adfe9 118 if(n > 0)// error
ocomeni 73:6f5021cbe752 119 {
ocomeni 103:7b566b522427 120 dbg_printf(LOG, "\n --- Error running wifi demo --- \n");
ocomeni 73:6f5021cbe752 121 }
ocomeni 74:f26e846adfe9 122 }
ocomeni 73:6f5021cbe752 123
ocomeni 74:f26e846adfe9 124 // Wifi-demo2
ocomeni 74:f26e846adfe9 125 void wifi_demo2(){
ocomeni 74:f26e846adfe9 126 //int n = wifi_demo_func(network);
ocomeni 74:f26e846adfe9 127 int n =5;
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 73:6f5021cbe752 134
ocomeni 79:a2187bbfa407 135 void printWaitAbortKeyPress(int numSecs)
ocomeni 79:a2187bbfa407 136 {
ocomeni 103:7b566b522427 137 dbg_printf(LOG, "Waiting for %d seconds... [press key to abort]\n", numSecs);
ocomeni 79:a2187bbfa407 138 char fmtstr[20];
ocomeni 79:a2187bbfa407 139 for(int i=0;i<numSecs;i++){
ocomeni 103:7b566b522427 140 dbg_printf(LOG, "%d", i);
ocomeni 103:7b566b522427 141 dbg_printf(LOG, "\n");
ocomeni 79:a2187bbfa407 142 sprintf(fmtstr, "BLE: loop # %d\n", i);
ocomeni 79:a2187bbfa407 143 peripheral->sendBLEUartData(fmtstr);
ocomeni 79:a2187bbfa407 144 wait(0.5);
ocomeni 109:c274780ff609 145 eventQueue_atcmd.dispatch(500); // Dispatch time - 500msec
ocomeni 79:a2187bbfa407 146 if(device->readable()){
ocomeni 103:7b566b522427 147 dbg_printf(LOG, "keypress detected aborting....\n");
ocomeni 79:a2187bbfa407 148 device->getc();
ocomeni 79:a2187bbfa407 149 break;
ocomeni 79:a2187bbfa407 150 }
ocomeni 79:a2187bbfa407 151 }
ocomeni 79:a2187bbfa407 152 }
ocomeni 79:a2187bbfa407 153
ocomeni 79:a2187bbfa407 154
ocomeni 78:07bb86e3ce14 155
ocomeni 78:07bb86e3ce14 156 void setupDefaultBleConfig()
ocomeni 78:07bb86e3ce14 157 {
ocomeni 78:07bb86e3ce14 158 strcpy(ble_config.deviceName, DEVICE_NAME_MAIN);// set BLE device name
ocomeni 78:07bb86e3ce14 159 ble_config.advInterval = 1000; // set advertising interval to 1 second default
ocomeni 78:07bb86e3ce14 160 ble_config.advTimeout = 0; // set advertising timeout to disabled by default
ocomeni 79:a2187bbfa407 161 // This works in C and C++
ocomeni 79:a2187bbfa407 162 memcpy(ble_config.pairingKey, pairingPassword, 6); //
ocomeni 79:a2187bbfa407 163
ocomeni 79:a2187bbfa407 164 //ble_config.pairingKey = pairingPassword;
ocomeni 78:07bb86e3ce14 165 }
ocomeni 78:07bb86e3ce14 166
ocomeni 78:07bb86e3ce14 167 void setupDefaultWiFiConfig()
ocomeni 78:07bb86e3ce14 168 {
ocomeni 78:07bb86e3ce14 169 strcpy(wifi_config.ssid, MBED_CONF_APP_WIFI_SSID);
ocomeni 78:07bb86e3ce14 170 strcpy(wifi_config.pass, MBED_CONF_APP_WIFI_PASSWORD);
ocomeni 78:07bb86e3ce14 171 wifi_config.security = NSAPI_SECURITY_WPA_WPA2;
ocomeni 78:07bb86e3ce14 172 }
ocomeni 78:07bb86e3ce14 173
ocomeni 74:f26e846adfe9 174 static int reset_counter = 0;
ocomeni 73:6f5021cbe752 175
ocomeni 75:08eff6258e1b 176
ocomeni 75:08eff6258e1b 177
ocomeni 105:e5ce023eee93 178 //#define ENABLE_MEMORY_CHECKS
ocomeni 75:08eff6258e1b 179
ocomeni 77:0b505d1e15f4 180 void print_memory_info() {
ocomeni 103:7b566b522427 181 #ifdef ENABLE_MEMORY_CHECKS
ocomeni 77:0b505d1e15f4 182 // allocate enough room for every thread's stack statistics
ocomeni 77:0b505d1e15f4 183 int cnt = osThreadGetCount();
ocomeni 77:0b505d1e15f4 184 mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t));
ocomeni 77:0b505d1e15f4 185
ocomeni 77:0b505d1e15f4 186 cnt = mbed_stats_stack_get_each(stats, cnt);
ocomeni 77:0b505d1e15f4 187 for (int i = 0; i < cnt; i++) {
ocomeni 103:7b566b522427 188 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 189 }
ocomeni 77:0b505d1e15f4 190 free(stats);
ocomeni 77:0b505d1e15f4 191
ocomeni 77:0b505d1e15f4 192 // Grab the heap statistics
ocomeni 77:0b505d1e15f4 193 mbed_stats_heap_t heap_stats;
ocomeni 77:0b505d1e15f4 194 mbed_stats_heap_get(&heap_stats);
ocomeni 103:7b566b522427 195 dbg_printf(LOG, "Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
ocomeni 103:7b566b522427 196 #endif
ocomeni 77:0b505d1e15f4 197 }
ocomeni 77:0b505d1e15f4 198
ocomeni 108:3c8fb2c6e7bf 199 void blinkLEDs()
ocomeni 108:3c8fb2c6e7bf 200 {
ocomeni 108:3c8fb2c6e7bf 201 static int cnt =0;
ocomeni 108:3c8fb2c6e7bf 202 cnt++;
ocomeni 108:3c8fb2c6e7bf 203 if(cnt == 3){
ocomeni 108:3c8fb2c6e7bf 204 cnt = 0;
ocomeni 108:3c8fb2c6e7bf 205 }
ocomeni 108:3c8fb2c6e7bf 206 if(cnt==0)
ocomeni 108:3c8fb2c6e7bf 207 led1 = !led1;
ocomeni 108:3c8fb2c6e7bf 208 //wait(1.0);
ocomeni 108:3c8fb2c6e7bf 209 if(cnt==1)
ocomeni 108:3c8fb2c6e7bf 210 led2 = !led2;
ocomeni 108:3c8fb2c6e7bf 211 //wait(1.0);
ocomeni 108:3c8fb2c6e7bf 212 if(cnt==2)
ocomeni 108:3c8fb2c6e7bf 213 led3 = !led3;
ocomeni 108:3c8fb2c6e7bf 214 }
ocomeni 108:3c8fb2c6e7bf 215
ocomeni 86:04fc2fcda7ec 216 #define PAUSE_SECONDS 0
ocomeni 103:7b566b522427 217 #define PAUSE_SECONDS_BLE 0
ocomeni 73:6f5021cbe752 218 int main() {
ocomeni 106:e1f04c3d0647 219 device = new RawSerial(USBTX, USBRX, 2*DEFAULT_BAUD_RATE);
ocomeni 103:7b566b522427 220 initialise_debug();
ocomeni 99:05398b3184f8 221 #ifdef MBED_MAJOR_VERSION
ocomeni 103:7b566b522427 222 dbg_printf(LOG, "Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
ocomeni 99:05398b3184f8 223 #endif
ocomeni 99:05398b3184f8 224
ocomeni 74:f26e846adfe9 225 reset_counter++;
ocomeni 103:7b566b522427 226 dbg_printf(LOG, "\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter);
ocomeni 79:a2187bbfa407 227 setupDefaultWiFiConfig();
ocomeni 78:07bb86e3ce14 228 setupDefaultBleConfig();
ocomeni 78:07bb86e3ce14 229 BLE& _ble = BLE::Instance();
ocomeni 77:0b505d1e15f4 230 events::EventQueue queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
ocomeni 77:0b505d1e15f4 231 #if MBED_CONF_APP_FILESYSTEM_SUPPORT
ocomeni 77:0b505d1e15f4 232 /* if filesystem creation fails or there is no filesystem the security manager
ocomeni 77:0b505d1e15f4 233 * will fallback to storing the security database in memory */
ocomeni 77:0b505d1e15f4 234 if (!create_filesystem()) {
ocomeni 103:7b566b522427 235 dbg_printf(LOG, "Filesystem creation failed, will use memory storage\r\n");
ocomeni 75:08eff6258e1b 236 }
ocomeni 76:6afda865fbf8 237 #endif
ocomeni 103:7b566b522427 238 dbg_printf(LOG, "\r\n PERIPHERAL \r\n\r\n");
ocomeni 78:07bb86e3ce14 239 peripheral = new SMDevicePeripheral(_ble, queue, peer_address, ble_config);
ocomeni 77:0b505d1e15f4 240
ocomeni 77:0b505d1e15f4 241 peripheral->run();
ocomeni 77:0b505d1e15f4 242 btle_thread.start(callback(&queue, &EventQueue::dispatch_forever));
ocomeni 77:0b505d1e15f4 243
ocomeni 77:0b505d1e15f4 244 int start = Kernel::get_ms_count();
ocomeni 79:a2187bbfa407 245 network = WiFiInterface::get_default_instance();
ocomeni 79:a2187bbfa407 246 if (!network) {
ocomeni 103:7b566b522427 247 dbg_printf(LOG, "ERROR: No WiFiInterface found.\n");
ocomeni 79:a2187bbfa407 248 }
ocomeni 80:e8f0e92e3ac9 249 wiFiManager = new WiFiManager(wifi_config, network,
ocomeni 109:c274780ff609 250 eventQueue_wifi,
ocomeni 80:e8f0e92e3ac9 251 &aT2WiFimPool, &aT2WiFiCmdQueue,
ocomeni 80:e8f0e92e3ac9 252 &wiFi2ATmPool, &wiFi2ATCmdQueue,
ocomeni 80:e8f0e92e3ac9 253 &aT2WiFiDatamPool, &aT2WiFiDataQueue,
ocomeni 80:e8f0e92e3ac9 254 &wiFi2ATDatamPool, &wiFi2ATDataQueue
ocomeni 80:e8f0e92e3ac9 255 );
ocomeni 103:7b566b522427 256 dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
ocomeni 103:7b566b522427 257 dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network scan from thread ++++++ \r\n");
ocomeni 103:7b566b522427 258 dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
ocomeni 81:637a87eb8170 259 wifi_thread.start(callback(wiFiManager, &WiFiManager::runMain));
ocomeni 103:7b566b522427 260 dbg_printf(LOG, "\r\n after starting wifi thread \r\n");
ocomeni 112:a0999ea4ece0 261 // dispatch wifi event queue on event thread
ocomeni 109:c274780ff609 262 wifi_evt_thread.start(callback(&eventQueue_wifi, &EventQueue::dispatch_forever));
ocomeni 112:a0999ea4ece0 263 // dispatch atcmd event queue on event thread
ocomeni 109:c274780ff609 264 atcmd_evt_thread.start(callback(&eventQueue_atcmd, &EventQueue::dispatch_forever));
ocomeni 103:7b566b522427 265 dbg_printf(LOG, "\r\n++++++ Starting ATCmdmanager ++++++ \r\n");
ocomeni 79:a2187bbfa407 266 ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, peripheral,
ocomeni 109:c274780ff609 267 eventQueue_atcmd, wiFiManager,
ocomeni 80:e8f0e92e3ac9 268 &aT2WiFimPool, &aT2WiFiCmdQueue,
ocomeni 80:e8f0e92e3ac9 269 &wiFi2ATmPool, &wiFi2ATCmdQueue,
ocomeni 80:e8f0e92e3ac9 270 &aT2WiFiDatamPool, &aT2WiFiDataQueue,
ocomeni 80:e8f0e92e3ac9 271 &wiFi2ATDatamPool, &wiFi2ATDataQueue,
ocomeni 82:10072c1794d3 272 false);
ocomeni 81:637a87eb8170 273 atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain));
ocomeni 103:7b566b522427 274 dbg_printf(LOG, "\r\n after starting atcmd thread \r\n");
ocomeni 88:7ffa053be662 275 print_memory_info();
ocomeni 108:3c8fb2c6e7bf 276 while(1)
ocomeni 108:3c8fb2c6e7bf 277 {
ocomeni 108:3c8fb2c6e7bf 278 wait(0.1);
ocomeni 108:3c8fb2c6e7bf 279 }
ocomeni 73:6f5021cbe752 280 }
ocomeni 73:6f5021cbe752 281
ocomeni 73:6f5021cbe752 282 #endif