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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Mon May 06 10:24:53 2019 +0000
Branch:
PassingRegression
Revision:
106:e1f04c3d0647
Parent:
105:e5ce023eee93
Child:
107:f1a83fd41b17
latest version - still issues with TLS connection

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 "mbed.h"
ocomeni 73:6f5021cbe752 9 #include <events/mbed_events.h>
ocomeni 73:6f5021cbe752 10 #include <mbed.h>
ocomeni 73:6f5021cbe752 11 #include "ble/BLE.h"
ocomeni 88:7ffa053be662 12 #include "fault_handlers.h"
ocomeni 73:6f5021cbe752 13 //#include "BLE.h"
ocomeni 73:6f5021cbe752 14 #include "ATCmdParser.h"
ocomeni 103:7b566b522427 15
ocomeni 73:6f5021cbe752 16 //#include "BLEDevice.h"
ocomeni 73:6f5021cbe752 17
ocomeni 73:6f5021cbe752 18 #include "LEDService.h"
ocomeni 73:6f5021cbe752 19 #include "ble/services/UARTService.h"
ocomeni 74:f26e846adfe9 20 #include "common_config.h"
ocomeni 79:a2187bbfa407 21 #include "common_types.h"
ocomeni 74:f26e846adfe9 22 #include "ATCmdManager.h"
ocomeni 75:08eff6258e1b 23 #include "BleManager.h"
ocomeni 78:07bb86e3ce14 24 #include "WiFiManager.h"
ocomeni 92:ec9550034276 25 #include "mbed_memory_status.h"
ocomeni 73:6f5021cbe752 26 UARTService *uart;
ocomeni 73:6f5021cbe752 27
ocomeni 73:6f5021cbe752 28 DigitalOut alivenessLED(LED1, 0);
ocomeni 73:6f5021cbe752 29 DigitalOut actuatedLED(LED2, 0);
ocomeni 73:6f5021cbe752 30
ocomeni 103:7b566b522427 31 #define FILE_CODE "main"
ocomeni 73:6f5021cbe752 32
ocomeni 74:f26e846adfe9 33 static RawSerial *device; // tx, rx
ocomeni 78:07bb86e3ce14 34
ocomeni 78:07bb86e3ce14 35 // wifi configuration
ocomeni 78:07bb86e3ce14 36 static wifi_config_t wifi_config;
ocomeni 78:07bb86e3ce14 37 // wifi interface pointer
ocomeni 78:07bb86e3ce14 38 static WiFiInterface *network;
ocomeni 78:07bb86e3ce14 39 // wifi manager pointer
ocomeni 78:07bb86e3ce14 40 static WiFiManager *wiFiManager;
ocomeni 78:07bb86e3ce14 41
ocomeni 78:07bb86e3ce14 42 // BLE configuration
ocomeni 78:07bb86e3ce14 43 static ble_config_t ble_config;
ocomeni 79:a2187bbfa407 44 const uint8_t pairingPassword[6] = "1101";
ocomeni 78:07bb86e3ce14 45 // BLE interface pointer
ocomeni 78:07bb86e3ce14 46 //BLE &_ble;
ocomeni 78:07bb86e3ce14 47 // BLE peripheral pointer
ocomeni 77:0b505d1e15f4 48 static SMDevicePeripheral *peripheral;
ocomeni 78:07bb86e3ce14 49
ocomeni 78:07bb86e3ce14 50 const static char DEVICE_NAME_MAIN[] = "UBLOX-BLE";
ocomeni 73:6f5021cbe752 51 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
ocomeni 73:6f5021cbe752 52 char buffer[BUFFER_LEN];
ocomeni 87:99b37d26ff2a 53 #ifdef ENABLE_UART_BACKGRND_DEMO
ocomeni 73:6f5021cbe752 54 uint8_t TxBuffer[TX_BUFFER_LEN];
ocomeni 73:6f5021cbe752 55 uint8_t RxBuffer[RX_BUFFER_LEN];
ocomeni 87:99b37d26ff2a 56 #endif
ocomeni 73:6f5021cbe752 57 static EventQueue eventQueue(/* event count */ 20 * EVENTS_EVENT_SIZE);
ocomeni 73:6f5021cbe752 58 //static EventQueue eventQueue2(/* event count */ 10 * EVENTS_EVENT_SIZE);
ocomeni 73:6f5021cbe752 59
ocomeni 73:6f5021cbe752 60 LEDService *ledServicePtr;
ocomeni 73:6f5021cbe752 61
ocomeni 80:e8f0e92e3ac9 62 /* Queue and memory pool for AT to Wifi commands */
ocomeni 92:ec9550034276 63 static MemoryPool<wifi_cmd_message_t, 16> aT2WiFimPool;
ocomeni 92:ec9550034276 64 static Queue<wifi_cmd_message_t, 16> aT2WiFiCmdQueue;
ocomeni 79:a2187bbfa407 65
ocomeni 80:e8f0e92e3ac9 66 /* Queue and memory pool for WiFi to AT commands */
ocomeni 92:ec9550034276 67 static MemoryPool<at_resp_message_t, 16> wiFi2ATmPool;
ocomeni 92:ec9550034276 68 static Queue<at_resp_message_t, 16> wiFi2ATCmdQueue;
ocomeni 80:e8f0e92e3ac9 69
ocomeni 80:e8f0e92e3ac9 70 /* Queue and memory pool for AT to WiFi data */
ocomeni 92:ec9550034276 71 static MemoryPool<wifi_data_msg_t, PQDSZ> aT2WiFiDatamPool;
ocomeni 92:ec9550034276 72 static Queue<wifi_data_msg_t, PQDSZ> aT2WiFiDataQueue;
ocomeni 80:e8f0e92e3ac9 73
ocomeni 80:e8f0e92e3ac9 74
ocomeni 80:e8f0e92e3ac9 75 /* Queue and memory pool for WiFi to AT data */
ocomeni 92:ec9550034276 76 static MemoryPool<at_data_msg_t, PQDSZ> wiFi2ATDatamPool;
ocomeni 92:ec9550034276 77 static Queue<at_data_msg_t, PQDSZ> wiFi2ATDataQueue;
ocomeni 80:e8f0e92e3ac9 78
ocomeni 80:e8f0e92e3ac9 79
ocomeni 80:e8f0e92e3ac9 80
ocomeni 74:f26e846adfe9 81
ocomeni 74:f26e846adfe9 82 /* allocate statically stacks for the three threads */
ocomeni 74:f26e846adfe9 83 //unsigned char rt_stk[1024];
ocomeni 74:f26e846adfe9 84 //unsigned char hp_stk[1024];
ocomeni 74:f26e846adfe9 85 //unsigned char lp_stk[1024];
ocomeni 74:f26e846adfe9 86
ocomeni 74:f26e846adfe9 87 /* creates three tread objects with different priorities */
ocomeni 74:f26e846adfe9 88 //Thread real_time_thread(osPriorityRealtime, 1024, &rt_stk[0]);
ocomeni 74:f26e846adfe9 89 //Thread high_prio_thread(osPriorityHigh, 1024, &hp_stk[0]);
ocomeni 74:f26e846adfe9 90 //Thread low_prio_thread(osPriorityNormal, 1024, &lp_stk[0]);
ocomeni 84:7c7add00f4bf 91
ocomeni 84:7c7add00f4bf 92 #ifdef USE_MAIN_THREAD_STACK
ocomeni 84:7c7add00f4bf 93 // using main thread stack
ocomeni 84:7c7add00f4bf 94 unsigned char btle_stk[1024];
ocomeni 84:7c7add00f4bf 95 unsigned char wifi_stk[8*1024];
ocomeni 84:7c7add00f4bf 96 unsigned char atcmd_stk[4*1024];
ocomeni 74:f26e846adfe9 97 Thread btle_thread(BTLE_THREAD_PRIORITY, 1024, &btle_stk[0]);
ocomeni 84:7c7add00f4bf 98 Thread wifi_thread(WIFI_THREAD_PRIORITY, 8*1024, &wifi_stk[0]);
ocomeni 81:637a87eb8170 99 Thread atcmd_thread(ATCMD_THREAD_PRIORITY, 4*1024, &atcmd_stk[0]);
ocomeni 84:7c7add00f4bf 100 #else
ocomeni 84:7c7add00f4bf 101 // using global heap
ocomeni 87:99b37d26ff2a 102 Thread btle_thread(BTLE_THREAD_PRIORITY, 1024);
ocomeni 88:7ffa053be662 103 Thread wifi_thread(WIFI_THREAD_PRIORITY, 6*1024);
ocomeni 87:99b37d26ff2a 104 Thread atcmd_thread(ATCMD_THREAD_PRIORITY, 4*1024);
ocomeni 84:7c7add00f4bf 105 #endif
ocomeni 74:f26e846adfe9 106
ocomeni 74:f26e846adfe9 107 /* create a semaphore to synchronize the threads */
ocomeni 74:f26e846adfe9 108 Semaphore sync_sema;
ocomeni 74:f26e846adfe9 109
ocomeni 81:637a87eb8170 110 Thread 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 74:f26e846adfe9 124 // check free memory
ocomeni 74:f26e846adfe9 125
ocomeni 74:f26e846adfe9 126 void performFreeMemoryCheck()
ocomeni 74:f26e846adfe9 127 {
ocomeni 77:0b505d1e15f4 128 _smutex.lock();
ocomeni 74:f26e846adfe9 129 // perform free memory check
ocomeni 74:f26e846adfe9 130 int blockSize = 16;
ocomeni 74:f26e846adfe9 131 int i = 1;
ocomeni 103:7b566b522427 132 dbg_printf(LOG, "Checking memory with blocksize %d char ...\n", blockSize);
ocomeni 74:f26e846adfe9 133 while (true) {
ocomeni 74:f26e846adfe9 134 char *p = (char *) malloc(i * blockSize);
ocomeni 74:f26e846adfe9 135 if (p == NULL)
ocomeni 74:f26e846adfe9 136 break;
ocomeni 74:f26e846adfe9 137 free(p);
ocomeni 74:f26e846adfe9 138 ++i;
ocomeni 74:f26e846adfe9 139 }
ocomeni 103:7b566b522427 140 dbg_printf(LOG, "Ok for %d char\n", (i - 1) * blockSize);
ocomeni 77:0b505d1e15f4 141 _smutex.unlock();
ocomeni 74:f26e846adfe9 142
ocomeni 74:f26e846adfe9 143 }
ocomeni 87:99b37d26ff2a 144 #ifdef ENABLE_UART_BACKGRND_DEMO
ocomeni 73:6f5021cbe752 145 static int uartExpectedRcvCount = 0;
ocomeni 73:6f5021cbe752 146 static int uartCharRcvCount = 0;
ocomeni 73:6f5021cbe752 147 static bool UartBusy = false;
ocomeni 73:6f5021cbe752 148 int WriteUartBytes(const uint8_t * txBuffer, size_t bufSize, int txLen)
ocomeni 73:6f5021cbe752 149 {
ocomeni 73:6f5021cbe752 150 if(txLen > bufSize)
ocomeni 73:6f5021cbe752 151 {
ocomeni 73:6f5021cbe752 152 txLen = bufSize;
ocomeni 73:6f5021cbe752 153 }
ocomeni 73:6f5021cbe752 154 //int goodTxLen;
ocomeni 73:6f5021cbe752 155 //goodTxLen = _parser.write((const char *) txBuffer, txLen);
ocomeni 73:6f5021cbe752 156 for(int i=0;i<txLen;i++)
ocomeni 73:6f5021cbe752 157 {
ocomeni 73:6f5021cbe752 158 device->putc(txBuffer[i]);
ocomeni 73:6f5021cbe752 159 }
ocomeni 73:6f5021cbe752 160 // return number of bytes written to UART
ocomeni 73:6f5021cbe752 161 return (int) txLen;
ocomeni 73:6f5021cbe752 162 }
ocomeni 73:6f5021cbe752 163
ocomeni 75:08eff6258e1b 164 void printUartRxResult()
ocomeni 75:08eff6258e1b 165 {
ocomeni 75:08eff6258e1b 166
ocomeni 75:08eff6258e1b 167 if(uartCharRcvCount == 0)
ocomeni 75:08eff6258e1b 168 {
ocomeni 103:7b566b522427 169 dbg_printf(LOG, "\nFirst Call to UART attach callback!!\n");
ocomeni 75:08eff6258e1b 170 }
ocomeni 75:08eff6258e1b 171 else if(uartCharRcvCount >= uartExpectedRcvCount)
ocomeni 75:08eff6258e1b 172 {
ocomeni 103:7b566b522427 173 dbg_printf(LOG, "\nNumber of Received Bytes = %d\n\n", uartCharRcvCount);
ocomeni 103:7b566b522427 174 dbg_printf(LOG, "--- Writing back received bytes --- \n");
ocomeni 75:08eff6258e1b 175 int n;
ocomeni 75:08eff6258e1b 176 n = WriteUartBytes(RxBuffer, TX_BUFFER_LEN, uartCharRcvCount);
ocomeni 75:08eff6258e1b 177 UartBusy = false;
ocomeni 75:08eff6258e1b 178 }
ocomeni 75:08eff6258e1b 179 }
ocomeni 75:08eff6258e1b 180
ocomeni 75:08eff6258e1b 181
ocomeni 73:6f5021cbe752 182 void UartRxcallback_ex() {
ocomeni 73:6f5021cbe752 183 if(uartCharRcvCount >= uartExpectedRcvCount)
ocomeni 73:6f5021cbe752 184 {
ocomeni 73:6f5021cbe752 185 int x = device->getc();
ocomeni 73:6f5021cbe752 186 return;
ocomeni 73:6f5021cbe752 187 }
ocomeni 73:6f5021cbe752 188 if(uartCharRcvCount == 0)
ocomeni 73:6f5021cbe752 189 {
ocomeni 75:08eff6258e1b 190 eventQueue.call(printUartRxResult);
ocomeni 73:6f5021cbe752 191 }
ocomeni 73:6f5021cbe752 192 // Note: you need to actually read from the serial to clear the RX interrupt
ocomeni 73:6f5021cbe752 193 RxBuffer[uartCharRcvCount] = (uint8_t) device->getc();
ocomeni 73:6f5021cbe752 194 uartCharRcvCount++;
ocomeni 73:6f5021cbe752 195 if(uartCharRcvCount >= uartExpectedRcvCount)
ocomeni 73:6f5021cbe752 196 {
ocomeni 73:6f5021cbe752 197 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
ocomeni 75:08eff6258e1b 198 eventQueue.call(printUartRxResult);
ocomeni 73:6f5021cbe752 199 }
ocomeni 73:6f5021cbe752 200 }
ocomeni 73:6f5021cbe752 201 void BackGndUartRead(uint8_t * rxBuffer, size_t bufSize, int rxLen)
ocomeni 73:6f5021cbe752 202 {
ocomeni 73:6f5021cbe752 203 UartBusy = true;
ocomeni 103:7b566b522427 204 dbg_printf(LOG, "Setting up background UART read - rxLen = %d\n", rxLen);
ocomeni 73:6f5021cbe752 205 uartCharRcvCount = 0;
ocomeni 73:6f5021cbe752 206 if(rxLen > bufSize)
ocomeni 73:6f5021cbe752 207 {
ocomeni 73:6f5021cbe752 208 rxLen = bufSize;
ocomeni 73:6f5021cbe752 209 }
ocomeni 73:6f5021cbe752 210 uartExpectedRcvCount = rxLen;
ocomeni 103:7b566b522427 211 dbg_printf(LOG, "\nattaching to device UART\n\n");
ocomeni 73:6f5021cbe752 212 device->attach(&UartRxcallback_ex);
ocomeni 103:7b566b522427 213 dbg_printf(LOG, "\nBackground UART read setup completed\n\n");
ocomeni 73:6f5021cbe752 214 }
ocomeni 73:6f5021cbe752 215
ocomeni 73:6f5021cbe752 216 int ReadUartBytes(uint8_t * rxBuffer, size_t bufSize, int rxLen, bool echo)
ocomeni 73:6f5021cbe752 217 {
ocomeni 73:6f5021cbe752 218 UartBusy = true;
ocomeni 73:6f5021cbe752 219 if(rxLen > bufSize)
ocomeni 73:6f5021cbe752 220 {
ocomeni 73:6f5021cbe752 221 rxLen = bufSize;
ocomeni 73:6f5021cbe752 222 }
ocomeni 73:6f5021cbe752 223 for(int i=0;i<rxLen;i++)
ocomeni 73:6f5021cbe752 224 {
ocomeni 73:6f5021cbe752 225 rxBuffer[i] = (uint8_t) device->getc();
ocomeni 73:6f5021cbe752 226 if(echo)device->putc(rxBuffer[i]);
ocomeni 73:6f5021cbe752 227 }
ocomeni 73:6f5021cbe752 228 UartBusy = false;
ocomeni 73:6f5021cbe752 229 //return number of bytes written to UART
ocomeni 73:6f5021cbe752 230 return rxLen;
ocomeni 73:6f5021cbe752 231 }
ocomeni 73:6f5021cbe752 232
ocomeni 73:6f5021cbe752 233
ocomeni 73:6f5021cbe752 234 void checkUartReceive()
ocomeni 73:6f5021cbe752 235 {
ocomeni 103:7b566b522427 236 //dbg_printf(LOG, "Hello World!\n\r");
ocomeni 73:6f5021cbe752 237 char cbuf[100];
ocomeni 73:6f5021cbe752 238 int rxCnt=0;
ocomeni 73:6f5021cbe752 239 while(device->readable()) {
ocomeni 103:7b566b522427 240 //dbg_printf(LOG, "uartCharRcvCount = %d\n\r", uartCharRcvCount++);
ocomeni 73:6f5021cbe752 241 cbuf[rxCnt++] = device->getc();
ocomeni 73:6f5021cbe752 242 //putc(getc() + 1); // echo input back to terminal
ocomeni 73:6f5021cbe752 243 }
ocomeni 73:6f5021cbe752 244 cbuf[rxCnt] = NULL;
ocomeni 73:6f5021cbe752 245 if(rxCnt > 0)
ocomeni 73:6f5021cbe752 246 {
ocomeni 103:7b566b522427 247 dbg_printf(LOG, "received %d chars\n", rxCnt);
ocomeni 103:7b566b522427 248 dbg_printf(LOG, "%s\n", cbuf);
ocomeni 73:6f5021cbe752 249 }
ocomeni 73:6f5021cbe752 250
ocomeni 73:6f5021cbe752 251 }
ocomeni 87:99b37d26ff2a 252 #endif
ocomeni 87:99b37d26ff2a 253
ocomeni 73:6f5021cbe752 254 uint64_t lastTime = 0;
ocomeni 73:6f5021cbe752 255 uint64_t now = 0;
ocomeni 73:6f5021cbe752 256 uint32_t callCount = 0;
ocomeni 73:6f5021cbe752 257 void HelloUart()
ocomeni 73:6f5021cbe752 258 {
ocomeni 73:6f5021cbe752 259 //if(UartBusy)return;
ocomeni 73:6f5021cbe752 260 // 64-bit time doesn't wrap for half a billion years, at least
ocomeni 73:6f5021cbe752 261 lastTime = now;
ocomeni 73:6f5021cbe752 262 now = Kernel::get_ms_count();
ocomeni 73:6f5021cbe752 263 callCount++;
ocomeni 103:7b566b522427 264 dbg_printf(LOG, "\nHello : %d secs elapsed : CallCount = %d \n", uint32_t(now - lastTime), callCount);
ocomeni 73:6f5021cbe752 265 }
ocomeni 73:6f5021cbe752 266
ocomeni 73:6f5021cbe752 267
ocomeni 73:6f5021cbe752 268
ocomeni 73:6f5021cbe752 269
ocomeni 73:6f5021cbe752 270 //Serial device(USBTX, USBRX); // tx, rx
ocomeni 73:6f5021cbe752 271 //RawSerial device(MBED_CONF_APP_UART1_TX, MBED_CONF_APP_UART1_RX); // tx, rx
ocomeni 73:6f5021cbe752 272
ocomeni 73:6f5021cbe752 273
ocomeni 73:6f5021cbe752 274
ocomeni 73:6f5021cbe752 275
ocomeni 73:6f5021cbe752 276 // Wifi-demo
ocomeni 73:6f5021cbe752 277 void wifi_demo(NetworkInterface* network){
ocomeni 74:f26e846adfe9 278 int n = wifi_demo_func(network);
ocomeni 74:f26e846adfe9 279 if(n > 0)// error
ocomeni 73:6f5021cbe752 280 {
ocomeni 103:7b566b522427 281 dbg_printf(LOG, "\n --- Error running wifi demo --- \n");
ocomeni 73:6f5021cbe752 282 }
ocomeni 74:f26e846adfe9 283 }
ocomeni 73:6f5021cbe752 284
ocomeni 74:f26e846adfe9 285 // Wifi-demo2
ocomeni 74:f26e846adfe9 286 void wifi_demo2(){
ocomeni 74:f26e846adfe9 287 //int n = wifi_demo_func(network);
ocomeni 74:f26e846adfe9 288 int n =5;
ocomeni 74:f26e846adfe9 289 if(n > 0)// error
ocomeni 73:6f5021cbe752 290 {
ocomeni 103:7b566b522427 291 dbg_printf(LOG, "\n --- Error running wifi demo --- \n");
ocomeni 73:6f5021cbe752 292 }
ocomeni 74:f26e846adfe9 293 }
ocomeni 73:6f5021cbe752 294
ocomeni 74:f26e846adfe9 295 void printWait(int numSecs)
ocomeni 74:f26e846adfe9 296 {
ocomeni 103:7b566b522427 297 dbg_printf(LOG, "Waiting for %d seconds...\n", numSecs);
ocomeni 74:f26e846adfe9 298 for(int i=0;i<numSecs;i++){
ocomeni 103:7b566b522427 299 dbg_printf(LOG, "%d", i);
ocomeni 103:7b566b522427 300 dbg_printf(LOG, "\n");
ocomeni 74:f26e846adfe9 301 wait(0.5);
ocomeni 74:f26e846adfe9 302 eventQueue.dispatch(500); // Dispatch time - 500msec
ocomeni 73:6f5021cbe752 303 }
ocomeni 74:f26e846adfe9 304 }
ocomeni 73:6f5021cbe752 305
ocomeni 79:a2187bbfa407 306 void printWaitAbortKeyPress(int numSecs)
ocomeni 79:a2187bbfa407 307 {
ocomeni 103:7b566b522427 308 dbg_printf(LOG, "Waiting for %d seconds... [press key to abort]\n", numSecs);
ocomeni 79:a2187bbfa407 309 char fmtstr[20];
ocomeni 79:a2187bbfa407 310 for(int i=0;i<numSecs;i++){
ocomeni 103:7b566b522427 311 dbg_printf(LOG, "%d", i);
ocomeni 103:7b566b522427 312 dbg_printf(LOG, "\n");
ocomeni 79:a2187bbfa407 313 sprintf(fmtstr, "BLE: loop # %d\n", i);
ocomeni 79:a2187bbfa407 314 peripheral->sendBLEUartData(fmtstr);
ocomeni 79:a2187bbfa407 315 wait(0.5);
ocomeni 79:a2187bbfa407 316 eventQueue.dispatch(500); // Dispatch time - 500msec
ocomeni 79:a2187bbfa407 317 if(device->readable()){
ocomeni 103:7b566b522427 318 dbg_printf(LOG, "keypress detected aborting....\n");
ocomeni 79:a2187bbfa407 319 device->getc();
ocomeni 79:a2187bbfa407 320 break;
ocomeni 79:a2187bbfa407 321 }
ocomeni 79:a2187bbfa407 322 }
ocomeni 79:a2187bbfa407 323 }
ocomeni 79:a2187bbfa407 324
ocomeni 79:a2187bbfa407 325
ocomeni 78:07bb86e3ce14 326
ocomeni 78:07bb86e3ce14 327 void setupDefaultBleConfig()
ocomeni 78:07bb86e3ce14 328 {
ocomeni 78:07bb86e3ce14 329 strcpy(ble_config.deviceName, DEVICE_NAME_MAIN);// set BLE device name
ocomeni 78:07bb86e3ce14 330 ble_config.advInterval = 1000; // set advertising interval to 1 second default
ocomeni 78:07bb86e3ce14 331 ble_config.advTimeout = 0; // set advertising timeout to disabled by default
ocomeni 79:a2187bbfa407 332 // This works in C and C++
ocomeni 79:a2187bbfa407 333 memcpy(ble_config.pairingKey, pairingPassword, 6); //
ocomeni 79:a2187bbfa407 334
ocomeni 79:a2187bbfa407 335 //ble_config.pairingKey = pairingPassword;
ocomeni 78:07bb86e3ce14 336 }
ocomeni 78:07bb86e3ce14 337
ocomeni 78:07bb86e3ce14 338 void setupDefaultWiFiConfig()
ocomeni 78:07bb86e3ce14 339 {
ocomeni 78:07bb86e3ce14 340 strcpy(wifi_config.ssid, MBED_CONF_APP_WIFI_SSID);
ocomeni 78:07bb86e3ce14 341 strcpy(wifi_config.pass, MBED_CONF_APP_WIFI_PASSWORD);
ocomeni 78:07bb86e3ce14 342 wifi_config.security = NSAPI_SECURITY_WPA_WPA2;
ocomeni 78:07bb86e3ce14 343 }
ocomeni 78:07bb86e3ce14 344
ocomeni 74:f26e846adfe9 345 static int reset_counter = 0;
ocomeni 73:6f5021cbe752 346
ocomeni 75:08eff6258e1b 347
ocomeni 75:08eff6258e1b 348
ocomeni 105:e5ce023eee93 349 //#define ENABLE_MEMORY_CHECKS
ocomeni 75:08eff6258e1b 350
ocomeni 77:0b505d1e15f4 351 void print_memory_info() {
ocomeni 103:7b566b522427 352 #ifdef ENABLE_MEMORY_CHECKS
ocomeni 77:0b505d1e15f4 353 // allocate enough room for every thread's stack statistics
ocomeni 77:0b505d1e15f4 354 int cnt = osThreadGetCount();
ocomeni 77:0b505d1e15f4 355 mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t));
ocomeni 77:0b505d1e15f4 356
ocomeni 77:0b505d1e15f4 357 cnt = mbed_stats_stack_get_each(stats, cnt);
ocomeni 77:0b505d1e15f4 358 for (int i = 0; i < cnt; i++) {
ocomeni 103:7b566b522427 359 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 360 }
ocomeni 77:0b505d1e15f4 361 free(stats);
ocomeni 77:0b505d1e15f4 362
ocomeni 77:0b505d1e15f4 363 // Grab the heap statistics
ocomeni 77:0b505d1e15f4 364 mbed_stats_heap_t heap_stats;
ocomeni 77:0b505d1e15f4 365 mbed_stats_heap_get(&heap_stats);
ocomeni 103:7b566b522427 366 dbg_printf(LOG, "Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
ocomeni 103:7b566b522427 367 #endif
ocomeni 77:0b505d1e15f4 368 }
ocomeni 77:0b505d1e15f4 369
ocomeni 78:07bb86e3ce14 370 //#define DISABLE_WIFI
ocomeni 79:a2187bbfa407 371 #define DISABLE_WIFI_DEMO
ocomeni 81:637a87eb8170 372 #define SKIP_WIFI_SCAN_DEMO
ocomeni 81:637a87eb8170 373 #define BLE_STOP_START_ADV_SCAN_DEMO
ocomeni 81:637a87eb8170 374 #define SKIP_WIFI_CONNECT_DEMO
ocomeni 86:04fc2fcda7ec 375 #define PAUSE_SECONDS 0
ocomeni 103:7b566b522427 376 #define PAUSE_SECONDS_BLE 0
ocomeni 73:6f5021cbe752 377 int main() {
ocomeni 106:e1f04c3d0647 378 device = new RawSerial(USBTX, USBRX, 2*DEFAULT_BAUD_RATE);
ocomeni 103:7b566b522427 379 initialise_debug();
ocomeni 99:05398b3184f8 380 #ifdef MBED_MAJOR_VERSION
ocomeni 103:7b566b522427 381 dbg_printf(LOG, "Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
ocomeni 99:05398b3184f8 382 #endif
ocomeni 99:05398b3184f8 383
ocomeni 74:f26e846adfe9 384 reset_counter++;
ocomeni 77:0b505d1e15f4 385 print_memory_info();
ocomeni 103:7b566b522427 386 dbg_printf(LOG, "\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter);
ocomeni 84:7c7add00f4bf 387 //device = new RawSerial(PA_9, PA_10, DEFAULT_BAUD_RATE);
ocomeni 83:9c271a50a70b 388
ocomeni 103:7b566b522427 389 dbg_printf(LOG, "*** HELLO MESSAGE *** \n %s", (char *)&hello_msg[6]);
ocomeni 79:a2187bbfa407 390 setupDefaultWiFiConfig();
ocomeni 78:07bb86e3ce14 391 setupDefaultBleConfig();
ocomeni 78:07bb86e3ce14 392 BLE& _ble = BLE::Instance();
ocomeni 77:0b505d1e15f4 393 events::EventQueue queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
ocomeni 77:0b505d1e15f4 394 #if MBED_CONF_APP_FILESYSTEM_SUPPORT
ocomeni 77:0b505d1e15f4 395 /* if filesystem creation fails or there is no filesystem the security manager
ocomeni 77:0b505d1e15f4 396 * will fallback to storing the security database in memory */
ocomeni 77:0b505d1e15f4 397 if (!create_filesystem()) {
ocomeni 103:7b566b522427 398 dbg_printf(LOG, "Filesystem creation failed, will use memory storage\r\n");
ocomeni 75:08eff6258e1b 399 }
ocomeni 76:6afda865fbf8 400 #endif
ocomeni 77:0b505d1e15f4 401 print_memory_info();
ocomeni 103:7b566b522427 402 dbg_printf(LOG, "\r\n PERIPHERAL \r\n\r\n");
ocomeni 78:07bb86e3ce14 403 peripheral = new SMDevicePeripheral(_ble, queue, peer_address, ble_config);
ocomeni 77:0b505d1e15f4 404 print_memory_info();
ocomeni 77:0b505d1e15f4 405
ocomeni 77:0b505d1e15f4 406 peripheral->run();
ocomeni 77:0b505d1e15f4 407 btle_thread.start(callback(&queue, &EventQueue::dispatch_forever));
ocomeni 77:0b505d1e15f4 408 print_memory_info();
ocomeni 87:99b37d26ff2a 409 printWaitAbortKeyPress(PAUSE_SECONDS_BLE); // give BLE time to settle
ocomeni 84:7c7add00f4bf 410 //peripheral->stopAdvertising();
ocomeni 77:0b505d1e15f4 411
ocomeni 78:07bb86e3ce14 412 #ifndef DISABLE_WIFI // comment out wifi part
ocomeni 77:0b505d1e15f4 413 int start = Kernel::get_ms_count();
ocomeni 78:07bb86e3ce14 414 #ifdef DISABLE_WIFI_DEMO
ocomeni 79:a2187bbfa407 415 network = WiFiInterface::get_default_instance();
ocomeni 79:a2187bbfa407 416 if (!network) {
ocomeni 103:7b566b522427 417 dbg_printf(LOG, "ERROR: No WiFiInterface found.\n");
ocomeni 79:a2187bbfa407 418 }
ocomeni 80:e8f0e92e3ac9 419 wiFiManager = new WiFiManager(wifi_config, network,
ocomeni 98:65c2333a38b6 420 eventQueue,
ocomeni 80:e8f0e92e3ac9 421 &aT2WiFimPool, &aT2WiFiCmdQueue,
ocomeni 80:e8f0e92e3ac9 422 &wiFi2ATmPool, &wiFi2ATCmdQueue,
ocomeni 80:e8f0e92e3ac9 423 &aT2WiFiDatamPool, &aT2WiFiDataQueue,
ocomeni 80:e8f0e92e3ac9 424 &wiFi2ATDatamPool, &wiFi2ATDataQueue
ocomeni 80:e8f0e92e3ac9 425 );
ocomeni 78:07bb86e3ce14 426 #else
ocomeni 77:0b505d1e15f4 427 NetworkInterface* network = connect_to_default_network_interface();
ocomeni 77:0b505d1e15f4 428 int stop = Kernel::get_ms_count();
ocomeni 103:7b566b522427 429 dbg_printf(LOG, "\n The Wifi Network scan took %d ms or %4.1f seconds\n", (stop - start), (float)((stop - start)/1000.0));
ocomeni 77:0b505d1e15f4 430 // run on separate thread;
ocomeni 81:637a87eb8170 431 evt_thread.start(callback(wifi_demo, network));
ocomeni 81:637a87eb8170 432 evt_thread.join();
ocomeni 77:0b505d1e15f4 433 network->disconnect();
ocomeni 77:0b505d1e15f4 434 delete network;
ocomeni 103:7b566b522427 435 dbg_printf(LOG, "\n Wifi-Demo completed - restarting BLE \n\n");
ocomeni 81:637a87eb8170 436 #endif /// endif DISABLE_WIFI_DEMO
ocomeni 81:637a87eb8170 437
ocomeni 77:0b505d1e15f4 438 #else
ocomeni 103:7b566b522427 439 dbg_printf(LOG, "\n Wifi Demo disabled so just waiting it out... \n\n");
ocomeni 79:a2187bbfa407 440 printWait(2); // lets wait for a minute before turning BLE back on
ocomeni 103:7b566b522427 441 dbg_printf(LOG, "\n ++++++ restarting BLE ++++++ \n\n");
ocomeni 81:637a87eb8170 442 #endif /// endif DISABLE_WIFI
ocomeni 84:7c7add00f4bf 443 printWaitAbortKeyPress(PAUSE_SECONDS);
ocomeni 84:7c7add00f4bf 444 //peripheral->startAdvertising();
ocomeni 77:0b505d1e15f4 445 #ifdef ENABLE_UART_BACKGRND_DEMO
ocomeni 73:6f5021cbe752 446 for(int i=0;i<255;i++)
ocomeni 73:6f5021cbe752 447 {
ocomeni 73:6f5021cbe752 448 device->putc(i);
ocomeni 73:6f5021cbe752 449 }
ocomeni 74:f26e846adfe9 450 reportGapState();
ocomeni 103:7b566b522427 451 dbg_printf(LOG, "\n\n\nEnter # of expected bytes: ");
ocomeni 77:0b505d1e15f4 452 ReadUartBytes(RxBuffer, RX_BUFFER_LEN, 4, true);
ocomeni 74:f26e846adfe9 453 uint8_t rxLen = (uint8_t) (100*(RxBuffer[0]-'0') + 10*(RxBuffer[1]-'0') + (RxBuffer[2]-'0')) %256;
ocomeni 103:7b566b522427 454 dbg_printf(LOG, "\n\nExpected # of Received Bytes = %d\n", rxLen);
ocomeni 74:f26e846adfe9 455 BackGndUartRead(RxBuffer, RX_BUFFER_LEN, (int) rxLen);
ocomeni 73:6f5021cbe752 456
ocomeni 103:7b566b522427 457 dbg_printf(LOG, "\n Waiting for 5 seconds ");
ocomeni 75:08eff6258e1b 458 printWait(5);
ocomeni 103:7b566b522427 459 dbg_printf(LOG, "\n Waiting finished!!!\n Now waiting for expected bytes to be received \n\n");
ocomeni 75:08eff6258e1b 460 while(UartBusy){
ocomeni 75:08eff6258e1b 461 wait(0.1);
ocomeni 75:08eff6258e1b 462 }
ocomeni 77:0b505d1e15f4 463 #endif
ocomeni 79:a2187bbfa407 464
ocomeni 103:7b566b522427 465 dbg_printf(LOG, "\r\n++++++ Press key for Wifi demo test ++++++ \r\n");
ocomeni 84:7c7add00f4bf 466 printWaitAbortKeyPress(PAUSE_SECONDS);
ocomeni 81:637a87eb8170 467 #ifndef SKIP_WIFI_SCAN_DEMO
ocomeni 103:7b566b522427 468 dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network Scan ++++++ \r\n");
ocomeni 79:a2187bbfa407 469 int count;
ocomeni 79:a2187bbfa407 470 count = wiFiManager->scanNetworks();
ocomeni 79:a2187bbfa407 471 if (count <= 0) {
ocomeni 103:7b566b522427 472 dbg_printf(LOG, "scan() failed with return value: %d\n", count);
ocomeni 79:a2187bbfa407 473 }
ocomeni 79:a2187bbfa407 474 else {
ocomeni 103:7b566b522427 475 dbg_printf(LOG, "\r\n++++++ Test WiFi Scan found %d networks ++++++ \r\n ++++ SUCCESS ++++\r\n", count);
ocomeni 79:a2187bbfa407 476 }
ocomeni 79:a2187bbfa407 477 #endif
ocomeni 81:637a87eb8170 478
ocomeni 81:637a87eb8170 479 #ifndef SKIP_WIFI_CONNECT_DEMO
ocomeni 103:7b566b522427 480 dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network connect ++++++ \r\n");
ocomeni 79:a2187bbfa407 481 nsapi_error_t werror;
ocomeni 79:a2187bbfa407 482 werror = wiFiManager->connect();
ocomeni 79:a2187bbfa407 483 if (werror < 0) {
ocomeni 103:7b566b522427 484 dbg_printf(LOG, "connect() failed with return value: %d\n", werror);
ocomeni 79:a2187bbfa407 485 }
ocomeni 79:a2187bbfa407 486 else {
ocomeni 103:7b566b522427 487 dbg_printf(LOG, "\r\n++++++ Test WiFi connect SUCCESSFUL ++++++ \r\n");
ocomeni 79:a2187bbfa407 488 }
ocomeni 79:a2187bbfa407 489 if(!werror) // connect successful - test dicsonnection
ocomeni 79:a2187bbfa407 490 {
ocomeni 103:7b566b522427 491 dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network disconnect ++++++ \r\n");
ocomeni 79:a2187bbfa407 492 werror = wiFiManager->disconnect();
ocomeni 79:a2187bbfa407 493 if (werror) {
ocomeni 103:7b566b522427 494 dbg_printf(LOG, "disconnect() failed with return value: %d\n", werror);
ocomeni 79:a2187bbfa407 495 }
ocomeni 79:a2187bbfa407 496 else {
ocomeni 103:7b566b522427 497 dbg_printf(LOG, "\r\n++++++ Test WiFi disconnect SUCCESSFUL ++++++ \r\n");
ocomeni 79:a2187bbfa407 498 }
ocomeni 79:a2187bbfa407 499 }
ocomeni 81:637a87eb8170 500 #endif
ocomeni 79:a2187bbfa407 501 //wiFiManager->runMain();
ocomeni 103:7b566b522427 502 dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
ocomeni 103:7b566b522427 503 dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
ocomeni 103:7b566b522427 504 dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
ocomeni 103:7b566b522427 505 dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network scan from thread ++++++ \r\n");
ocomeni 103:7b566b522427 506 dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
ocomeni 103:7b566b522427 507 dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
ocomeni 103:7b566b522427 508 dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
ocomeni 81:637a87eb8170 509 wifi_thread.start(callback(wiFiManager, &WiFiManager::runMain));
ocomeni 103:7b566b522427 510 dbg_printf(LOG, "\r\n after starting wifi thread \r\n");
ocomeni 88:7ffa053be662 511 print_memory_info();
ocomeni 81:637a87eb8170 512 // dispatch event queue on event thread
ocomeni 103:7b566b522427 513 dbg_printf(LOG, "\r\n [MAIN THREAD] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id());
ocomeni 103:7b566b522427 514 dbg_printf(LOG, "\r\n [EVENT THREAD] Thread Id = %X\r\n", (uint32_t)evt_thread.get_id());
ocomeni 81:637a87eb8170 515 evt_thread.start(callback(&eventQueue, &EventQueue::dispatch_forever));
ocomeni 103:7b566b522427 516 dbg_printf(LOG, "\r\n++++++ Starting ATCmdmanager ++++++ \r\n");
ocomeni 79:a2187bbfa407 517 ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, peripheral,
ocomeni 79:a2187bbfa407 518 eventQueue, wiFiManager,
ocomeni 80:e8f0e92e3ac9 519 &aT2WiFimPool, &aT2WiFiCmdQueue,
ocomeni 80:e8f0e92e3ac9 520 &wiFi2ATmPool, &wiFi2ATCmdQueue,
ocomeni 80:e8f0e92e3ac9 521 &aT2WiFiDatamPool, &aT2WiFiDataQueue,
ocomeni 80:e8f0e92e3ac9 522 &wiFi2ATDatamPool, &wiFi2ATDataQueue,
ocomeni 82:10072c1794d3 523 false);
ocomeni 81:637a87eb8170 524 //aTCmdManager->runMain();
ocomeni 81:637a87eb8170 525 atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain));
ocomeni 103:7b566b522427 526 dbg_printf(LOG, "\r\n after starting atcmd thread \r\n");
ocomeni 88:7ffa053be662 527 print_memory_info();
ocomeni 88:7ffa053be662 528 //SCB->SHCSR |= 0x00070000;//Enable fault handler.
ocomeni 87:99b37d26ff2a 529 //wiFiManager->runMain();
ocomeni 79:a2187bbfa407 530 while(1) wait(0.1);
ocomeni 75:08eff6258e1b 531 //performFreeMemoryCheck();
ocomeni 73:6f5021cbe752 532
ocomeni 73:6f5021cbe752 533 //eventQueue.dispatch_forever();
ocomeni 73:6f5021cbe752 534 //t.start(callback(&eventQueue, &EventQueue::dispatch_forever));
ocomeni 73:6f5021cbe752 535 //eventQueue2.dispatch_forever();
ocomeni 73:6f5021cbe752 536
ocomeni 73:6f5021cbe752 537 return 0;
ocomeni 73:6f5021cbe752 538 }
ocomeni 73:6f5021cbe752 539
ocomeni 73:6f5021cbe752 540 #endif