Dependencies:   mbed-http

Committer:
ocomeni
Date:
Wed May 08 19:38:35 2019 +0000
Branch:
PassingRegression
Revision:
109:c274780ff609
Parent:
108:3c8fb2c6e7bf
Child:
112:a0999ea4ece0
1. updated HTTP packet state to monitor for multiple packets; 2. fixed bug causing crash (was canceling an active task on event queue); 2. updates to watchdog. added ticker.;

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