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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Wed Mar 20 21:02:47 2019 +0000
Revision:
79:a2187bbfa407
Parent:
78:07bb86e3ce14
Child:
80:e8f0e92e3ac9
now have working mechanism for comms between threads using memory pool and queue.; Next stage expand this for data.

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