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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sun Mar 31 22:09:45 2019 +0000
Revision:
88:7ffa053be662
Parent:
87:99b37d26ff2a
Child:
92:ec9550034276
tls connection now working.

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