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