
this is using the mbed os version 5-13-1
source/main-https.cpp@73:6f5021cbe752, 2019-02-28 (annotated)
- Committer:
- ocomeni
- Date:
- Thu Feb 28 18:13:48 2019 +0000
- Revision:
- 73:6f5021cbe752
- Child:
- 74:f26e846adfe9
commit current application code
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 "mbed_trace.h" |
ocomeni | 73:6f5021cbe752 | 13 | #include "https_request.h" |
ocomeni | 73:6f5021cbe752 | 14 | #include "http_request.h" |
ocomeni | 73:6f5021cbe752 | 15 | #include "network-helper.h" |
ocomeni | 73:6f5021cbe752 | 16 | #include "ATCmdParser.h" |
ocomeni | 73:6f5021cbe752 | 17 | //#include "BLEDevice.h" |
ocomeni | 73:6f5021cbe752 | 18 | |
ocomeni | 73:6f5021cbe752 | 19 | #include "LEDService.h" |
ocomeni | 73:6f5021cbe752 | 20 | #include "ble/services/UARTService.h" |
ocomeni | 73:6f5021cbe752 | 21 | |
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 | #define DEFAULT_BAUD_RATE 115200 |
ocomeni | 73:6f5021cbe752 | 28 | |
ocomeni | 73:6f5021cbe752 | 29 | RawSerial *device; // tx, rx |
ocomeni | 73:6f5021cbe752 | 30 | UARTSerial *_serial; // tx, rx |
ocomeni | 73:6f5021cbe752 | 31 | ATCmdParser *_parser; |
ocomeni | 73:6f5021cbe752 | 32 | const static char DEVICE_NAME[] = "BLE-UART"; |
ocomeni | 73:6f5021cbe752 | 33 | static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; |
ocomeni | 73:6f5021cbe752 | 34 | #define BUFFER_LEN 256 |
ocomeni | 73:6f5021cbe752 | 35 | #define TX_BUFFER_LEN 4*256 |
ocomeni | 73:6f5021cbe752 | 36 | #define RX_BUFFER_LEN 4*256 |
ocomeni | 73:6f5021cbe752 | 37 | char buffer[BUFFER_LEN]; |
ocomeni | 73:6f5021cbe752 | 38 | uint8_t TxBuffer[TX_BUFFER_LEN]; |
ocomeni | 73:6f5021cbe752 | 39 | uint8_t RxBuffer[RX_BUFFER_LEN]; |
ocomeni | 73:6f5021cbe752 | 40 | static EventQueue eventQueue(/* event count */ 20 * EVENTS_EVENT_SIZE); |
ocomeni | 73:6f5021cbe752 | 41 | //static EventQueue eventQueue2(/* event count */ 10 * EVENTS_EVENT_SIZE); |
ocomeni | 73:6f5021cbe752 | 42 | |
ocomeni | 73:6f5021cbe752 | 43 | LEDService *ledServicePtr; |
ocomeni | 73:6f5021cbe752 | 44 | |
ocomeni | 73:6f5021cbe752 | 45 | Thread t; |
ocomeni | 73:6f5021cbe752 | 46 | |
ocomeni | 73:6f5021cbe752 | 47 | /* List of trusted root CA certificates |
ocomeni | 73:6f5021cbe752 | 48 | * currently two: GlobalSign, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org |
ocomeni | 73:6f5021cbe752 | 49 | * |
ocomeni | 73:6f5021cbe752 | 50 | * To add more root certificates, just concatenate them. |
ocomeni | 73:6f5021cbe752 | 51 | */ |
ocomeni | 73:6f5021cbe752 | 52 | #include "https_certificates.h" |
ocomeni | 73:6f5021cbe752 | 53 | |
ocomeni | 73:6f5021cbe752 | 54 | |
ocomeni | 73:6f5021cbe752 | 55 | |
ocomeni | 73:6f5021cbe752 | 56 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
ocomeni | 73:6f5021cbe752 | 57 | { |
ocomeni | 73:6f5021cbe752 | 58 | (void) params; |
ocomeni | 73:6f5021cbe752 | 59 | BLE::Instance().gap().startAdvertising(); |
ocomeni | 73:6f5021cbe752 | 60 | } |
ocomeni | 73:6f5021cbe752 | 61 | |
ocomeni | 73:6f5021cbe752 | 62 | void blinkCallback(void) |
ocomeni | 73:6f5021cbe752 | 63 | { |
ocomeni | 73:6f5021cbe752 | 64 | alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ |
ocomeni | 73:6f5021cbe752 | 65 | } |
ocomeni | 73:6f5021cbe752 | 66 | |
ocomeni | 73:6f5021cbe752 | 67 | void EchoBleUartReceived() |
ocomeni | 73:6f5021cbe752 | 68 | { |
ocomeni | 73:6f5021cbe752 | 69 | uart->writeString(buffer); |
ocomeni | 73:6f5021cbe752 | 70 | uart->writeString("\n"); //flushes uart output buffer and sends data |
ocomeni | 73:6f5021cbe752 | 71 | } |
ocomeni | 73:6f5021cbe752 | 72 | |
ocomeni | 73:6f5021cbe752 | 73 | /** |
ocomeni | 73:6f5021cbe752 | 74 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
ocomeni | 73:6f5021cbe752 | 75 | * |
ocomeni | 73:6f5021cbe752 | 76 | * @param[in] params |
ocomeni | 73:6f5021cbe752 | 77 | * Information about the characterisitc being updated. |
ocomeni | 73:6f5021cbe752 | 78 | */ |
ocomeni | 73:6f5021cbe752 | 79 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
ocomeni | 73:6f5021cbe752 | 80 | if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { |
ocomeni | 73:6f5021cbe752 | 81 | actuatedLED = *(params->data); |
ocomeni | 73:6f5021cbe752 | 82 | } |
ocomeni | 73:6f5021cbe752 | 83 | else if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) { |
ocomeni | 73:6f5021cbe752 | 84 | uint16_t bytesRead = params->len; |
ocomeni | 73:6f5021cbe752 | 85 | |
ocomeni | 73:6f5021cbe752 | 86 | printf("received %u bytes\n\r ", bytesRead); |
ocomeni | 73:6f5021cbe752 | 87 | |
ocomeni | 73:6f5021cbe752 | 88 | if(bytesRead >= 255){ |
ocomeni | 73:6f5021cbe752 | 89 | printf("Overflow command %u n\r ", bytesRead); |
ocomeni | 73:6f5021cbe752 | 90 | bytesRead = 255; |
ocomeni | 73:6f5021cbe752 | 91 | } |
ocomeni | 73:6f5021cbe752 | 92 | |
ocomeni | 73:6f5021cbe752 | 93 | unsigned index = 0; |
ocomeni | 73:6f5021cbe752 | 94 | for (; index < bytesRead; index++) { |
ocomeni | 73:6f5021cbe752 | 95 | buffer[index] = params->data[index]; |
ocomeni | 73:6f5021cbe752 | 96 | } |
ocomeni | 73:6f5021cbe752 | 97 | |
ocomeni | 73:6f5021cbe752 | 98 | buffer[index++] = 0; |
ocomeni | 73:6f5021cbe752 | 99 | |
ocomeni | 73:6f5021cbe752 | 100 | printf("Data : %s ",buffer); |
ocomeni | 73:6f5021cbe752 | 101 | printf("\r\n"); |
ocomeni | 73:6f5021cbe752 | 102 | eventQueue.call(EchoBleUartReceived); |
ocomeni | 73:6f5021cbe752 | 103 | |
ocomeni | 73:6f5021cbe752 | 104 | } |
ocomeni | 73:6f5021cbe752 | 105 | } |
ocomeni | 73:6f5021cbe752 | 106 | |
ocomeni | 73:6f5021cbe752 | 107 | |
ocomeni | 73:6f5021cbe752 | 108 | /** |
ocomeni | 73:6f5021cbe752 | 109 | * This function is called when the ble initialization process has failled |
ocomeni | 73:6f5021cbe752 | 110 | */ |
ocomeni | 73:6f5021cbe752 | 111 | void onBleInitError(BLE &ble, ble_error_t error) |
ocomeni | 73:6f5021cbe752 | 112 | { |
ocomeni | 73:6f5021cbe752 | 113 | printf("\n BLE Initialization failed!! \n"); |
ocomeni | 73:6f5021cbe752 | 114 | |
ocomeni | 73:6f5021cbe752 | 115 | /* Initialization error handling should go here */ |
ocomeni | 73:6f5021cbe752 | 116 | } |
ocomeni | 73:6f5021cbe752 | 117 | |
ocomeni | 73:6f5021cbe752 | 118 | void printMacAddress() |
ocomeni | 73:6f5021cbe752 | 119 | { |
ocomeni | 73:6f5021cbe752 | 120 | /* Print out device MAC address to the console*/ |
ocomeni | 73:6f5021cbe752 | 121 | Gap::AddressType_t addr_type; |
ocomeni | 73:6f5021cbe752 | 122 | Gap::Address_t address; |
ocomeni | 73:6f5021cbe752 | 123 | BLE::Instance().gap().getAddress(&addr_type, address); |
ocomeni | 73:6f5021cbe752 | 124 | printf("DEVICE MAC ADDRESS: "); |
ocomeni | 73:6f5021cbe752 | 125 | for (int i = 5; i >= 1; i--){ |
ocomeni | 73:6f5021cbe752 | 126 | printf("%02x:", address[i]); |
ocomeni | 73:6f5021cbe752 | 127 | } |
ocomeni | 73:6f5021cbe752 | 128 | printf("%02x\r\n", address[0]); |
ocomeni | 73:6f5021cbe752 | 129 | } |
ocomeni | 73:6f5021cbe752 | 130 | |
ocomeni | 73:6f5021cbe752 | 131 | /** |
ocomeni | 73:6f5021cbe752 | 132 | * Callback triggered when the ble initialization process has finished |
ocomeni | 73:6f5021cbe752 | 133 | */ |
ocomeni | 73:6f5021cbe752 | 134 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
ocomeni | 73:6f5021cbe752 | 135 | { |
ocomeni | 73:6f5021cbe752 | 136 | BLE& ble = params->ble; |
ocomeni | 73:6f5021cbe752 | 137 | ble_error_t error = params->error; |
ocomeni | 73:6f5021cbe752 | 138 | |
ocomeni | 73:6f5021cbe752 | 139 | if (error != BLE_ERROR_NONE) { |
ocomeni | 73:6f5021cbe752 | 140 | /* In case of error, forward the error handling to onBleInitError */ |
ocomeni | 73:6f5021cbe752 | 141 | onBleInitError(ble, error); |
ocomeni | 73:6f5021cbe752 | 142 | return; |
ocomeni | 73:6f5021cbe752 | 143 | } |
ocomeni | 73:6f5021cbe752 | 144 | |
ocomeni | 73:6f5021cbe752 | 145 | /* Ensure that it is the default instance of BLE */ |
ocomeni | 73:6f5021cbe752 | 146 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
ocomeni | 73:6f5021cbe752 | 147 | return; |
ocomeni | 73:6f5021cbe752 | 148 | } |
ocomeni | 73:6f5021cbe752 | 149 | |
ocomeni | 73:6f5021cbe752 | 150 | ble.gap().onDisconnection(disconnectionCallback); |
ocomeni | 73:6f5021cbe752 | 151 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
ocomeni | 73:6f5021cbe752 | 152 | |
ocomeni | 73:6f5021cbe752 | 153 | bool initialValueForLEDCharacteristic = false; |
ocomeni | 73:6f5021cbe752 | 154 | ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); |
ocomeni | 73:6f5021cbe752 | 155 | /* Setup primary service */ |
ocomeni | 73:6f5021cbe752 | 156 | uart = new UARTService(ble); |
ocomeni | 73:6f5021cbe752 | 157 | |
ocomeni | 73:6f5021cbe752 | 158 | /* setup advertising */ |
ocomeni | 73:6f5021cbe752 | 159 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
ocomeni | 73:6f5021cbe752 | 160 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
ocomeni | 73:6f5021cbe752 | 161 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
ocomeni | 73:6f5021cbe752 | 162 | /* set up the services that can be discovered */ |
ocomeni | 73:6f5021cbe752 | 163 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
ocomeni | 73:6f5021cbe752 | 164 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
ocomeni | 73:6f5021cbe752 | 165 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
ocomeni | 73:6f5021cbe752 | 166 | ble.gap().startAdvertising(); |
ocomeni | 73:6f5021cbe752 | 167 | |
ocomeni | 73:6f5021cbe752 | 168 | printMacAddress(); |
ocomeni | 73:6f5021cbe752 | 169 | } |
ocomeni | 73:6f5021cbe752 | 170 | |
ocomeni | 73:6f5021cbe752 | 171 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
ocomeni | 73:6f5021cbe752 | 172 | BLE &ble = BLE::Instance(); |
ocomeni | 73:6f5021cbe752 | 173 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
ocomeni | 73:6f5021cbe752 | 174 | } |
ocomeni | 73:6f5021cbe752 | 175 | |
ocomeni | 73:6f5021cbe752 | 176 | |
ocomeni | 73:6f5021cbe752 | 177 | void bleInitialization() |
ocomeni | 73:6f5021cbe752 | 178 | { |
ocomeni | 73:6f5021cbe752 | 179 | BLE &ble = BLE::Instance(); |
ocomeni | 73:6f5021cbe752 | 180 | device->printf("\n --- BLE Instance Instantiated --- \n"); |
ocomeni | 73:6f5021cbe752 | 181 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
ocomeni | 73:6f5021cbe752 | 182 | device->printf("\n --- BLE scheduleBleEventsProcessing setup --- \n"); |
ocomeni | 73:6f5021cbe752 | 183 | ble.init(bleInitComplete); |
ocomeni | 73:6f5021cbe752 | 184 | } |
ocomeni | 73:6f5021cbe752 | 185 | |
ocomeni | 73:6f5021cbe752 | 186 | |
ocomeni | 73:6f5021cbe752 | 187 | static int uartExpectedRcvCount = 0; |
ocomeni | 73:6f5021cbe752 | 188 | static int uartCharRcvCount = 0; |
ocomeni | 73:6f5021cbe752 | 189 | static bool UartBusy = false; |
ocomeni | 73:6f5021cbe752 | 190 | int WriteUartBytes(const uint8_t * txBuffer, size_t bufSize, int txLen) |
ocomeni | 73:6f5021cbe752 | 191 | { |
ocomeni | 73:6f5021cbe752 | 192 | if(txLen > bufSize) |
ocomeni | 73:6f5021cbe752 | 193 | { |
ocomeni | 73:6f5021cbe752 | 194 | txLen = bufSize; |
ocomeni | 73:6f5021cbe752 | 195 | } |
ocomeni | 73:6f5021cbe752 | 196 | //int goodTxLen; |
ocomeni | 73:6f5021cbe752 | 197 | //goodTxLen = _parser.write((const char *) txBuffer, txLen); |
ocomeni | 73:6f5021cbe752 | 198 | for(int i=0;i<txLen;i++) |
ocomeni | 73:6f5021cbe752 | 199 | { |
ocomeni | 73:6f5021cbe752 | 200 | device->putc(txBuffer[i]); |
ocomeni | 73:6f5021cbe752 | 201 | } |
ocomeni | 73:6f5021cbe752 | 202 | // return number of bytes written to UART |
ocomeni | 73:6f5021cbe752 | 203 | return (int) txLen; |
ocomeni | 73:6f5021cbe752 | 204 | } |
ocomeni | 73:6f5021cbe752 | 205 | |
ocomeni | 73:6f5021cbe752 | 206 | void UartRxcallback_ex() { |
ocomeni | 73:6f5021cbe752 | 207 | if(uartCharRcvCount >= uartExpectedRcvCount) |
ocomeni | 73:6f5021cbe752 | 208 | { |
ocomeni | 73:6f5021cbe752 | 209 | int x = device->getc(); |
ocomeni | 73:6f5021cbe752 | 210 | return; |
ocomeni | 73:6f5021cbe752 | 211 | } |
ocomeni | 73:6f5021cbe752 | 212 | if(uartCharRcvCount == 0) |
ocomeni | 73:6f5021cbe752 | 213 | { |
ocomeni | 73:6f5021cbe752 | 214 | device->printf("\nFirst Call to UART attach callback!!\n"); |
ocomeni | 73:6f5021cbe752 | 215 | } |
ocomeni | 73:6f5021cbe752 | 216 | // Note: you need to actually read from the serial to clear the RX interrupt |
ocomeni | 73:6f5021cbe752 | 217 | RxBuffer[uartCharRcvCount] = (uint8_t) device->getc(); |
ocomeni | 73:6f5021cbe752 | 218 | uartCharRcvCount++; |
ocomeni | 73:6f5021cbe752 | 219 | if(uartCharRcvCount >= uartExpectedRcvCount) |
ocomeni | 73:6f5021cbe752 | 220 | { |
ocomeni | 73:6f5021cbe752 | 221 | alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ |
ocomeni | 73:6f5021cbe752 | 222 | device->printf("\nNumber of Received Bytes = %d\n\n", uartCharRcvCount); |
ocomeni | 73:6f5021cbe752 | 223 | device->printf("--- Writing back received bytes --- \n"); |
ocomeni | 73:6f5021cbe752 | 224 | int n; |
ocomeni | 73:6f5021cbe752 | 225 | n = WriteUartBytes(RxBuffer, TX_BUFFER_LEN, uartCharRcvCount); |
ocomeni | 73:6f5021cbe752 | 226 | UartBusy = false; |
ocomeni | 73:6f5021cbe752 | 227 | } |
ocomeni | 73:6f5021cbe752 | 228 | } |
ocomeni | 73:6f5021cbe752 | 229 | |
ocomeni | 73:6f5021cbe752 | 230 | void BackGndUartRead(uint8_t * rxBuffer, size_t bufSize, int rxLen) |
ocomeni | 73:6f5021cbe752 | 231 | { |
ocomeni | 73:6f5021cbe752 | 232 | UartBusy = true; |
ocomeni | 73:6f5021cbe752 | 233 | device->printf("Setting up background UART read - rxLen = %d\n", rxLen); |
ocomeni | 73:6f5021cbe752 | 234 | uartCharRcvCount = 0; |
ocomeni | 73:6f5021cbe752 | 235 | if(rxLen > bufSize) |
ocomeni | 73:6f5021cbe752 | 236 | { |
ocomeni | 73:6f5021cbe752 | 237 | rxLen = bufSize; |
ocomeni | 73:6f5021cbe752 | 238 | } |
ocomeni | 73:6f5021cbe752 | 239 | uartExpectedRcvCount = rxLen; |
ocomeni | 73:6f5021cbe752 | 240 | device->attach(&UartRxcallback_ex); |
ocomeni | 73:6f5021cbe752 | 241 | device->printf("\nBackground UART read setup completed\n\n"); |
ocomeni | 73:6f5021cbe752 | 242 | //for(int i=0;i<rxLen;i++) |
ocomeni | 73:6f5021cbe752 | 243 | //{ |
ocomeni | 73:6f5021cbe752 | 244 | // rxBuffer[i] = (uint8_t) getc(); |
ocomeni | 73:6f5021cbe752 | 245 | //} |
ocomeni | 73:6f5021cbe752 | 246 | // return number of bytes written to UART |
ocomeni | 73:6f5021cbe752 | 247 | //return rxLen; |
ocomeni | 73:6f5021cbe752 | 248 | } |
ocomeni | 73:6f5021cbe752 | 249 | |
ocomeni | 73:6f5021cbe752 | 250 | int ReadUartBytes(uint8_t * rxBuffer, size_t bufSize, int rxLen, bool echo) |
ocomeni | 73:6f5021cbe752 | 251 | { |
ocomeni | 73:6f5021cbe752 | 252 | UartBusy = true; |
ocomeni | 73:6f5021cbe752 | 253 | if(rxLen > bufSize) |
ocomeni | 73:6f5021cbe752 | 254 | { |
ocomeni | 73:6f5021cbe752 | 255 | rxLen = bufSize; |
ocomeni | 73:6f5021cbe752 | 256 | } |
ocomeni | 73:6f5021cbe752 | 257 | for(int i=0;i<rxLen;i++) |
ocomeni | 73:6f5021cbe752 | 258 | { |
ocomeni | 73:6f5021cbe752 | 259 | rxBuffer[i] = (uint8_t) device->getc(); |
ocomeni | 73:6f5021cbe752 | 260 | if(echo)device->putc(rxBuffer[i]); |
ocomeni | 73:6f5021cbe752 | 261 | } |
ocomeni | 73:6f5021cbe752 | 262 | UartBusy = false; |
ocomeni | 73:6f5021cbe752 | 263 | //return number of bytes written to UART |
ocomeni | 73:6f5021cbe752 | 264 | return rxLen; |
ocomeni | 73:6f5021cbe752 | 265 | } |
ocomeni | 73:6f5021cbe752 | 266 | |
ocomeni | 73:6f5021cbe752 | 267 | |
ocomeni | 73:6f5021cbe752 | 268 | void checkUartReceive() |
ocomeni | 73:6f5021cbe752 | 269 | { |
ocomeni | 73:6f5021cbe752 | 270 | //device->printf("Hello World!\n\r"); |
ocomeni | 73:6f5021cbe752 | 271 | char cbuf[100]; |
ocomeni | 73:6f5021cbe752 | 272 | int rxCnt=0; |
ocomeni | 73:6f5021cbe752 | 273 | while(device->readable()) { |
ocomeni | 73:6f5021cbe752 | 274 | //device->printf("uartCharRcvCount = %d\n\r", uartCharRcvCount++); |
ocomeni | 73:6f5021cbe752 | 275 | cbuf[rxCnt++] = device->getc(); |
ocomeni | 73:6f5021cbe752 | 276 | //putc(getc() + 1); // echo input back to terminal |
ocomeni | 73:6f5021cbe752 | 277 | } |
ocomeni | 73:6f5021cbe752 | 278 | cbuf[rxCnt] = NULL; |
ocomeni | 73:6f5021cbe752 | 279 | if(rxCnt > 0) |
ocomeni | 73:6f5021cbe752 | 280 | { |
ocomeni | 73:6f5021cbe752 | 281 | device->printf("received %d chars\n", rxCnt); |
ocomeni | 73:6f5021cbe752 | 282 | device->printf("%s\n", cbuf); |
ocomeni | 73:6f5021cbe752 | 283 | } |
ocomeni | 73:6f5021cbe752 | 284 | |
ocomeni | 73:6f5021cbe752 | 285 | } |
ocomeni | 73:6f5021cbe752 | 286 | uint64_t lastTime = 0; |
ocomeni | 73:6f5021cbe752 | 287 | uint64_t now = 0; |
ocomeni | 73:6f5021cbe752 | 288 | uint32_t callCount = 0; |
ocomeni | 73:6f5021cbe752 | 289 | void HelloUart() |
ocomeni | 73:6f5021cbe752 | 290 | { |
ocomeni | 73:6f5021cbe752 | 291 | //if(UartBusy)return; |
ocomeni | 73:6f5021cbe752 | 292 | // 64-bit time doesn't wrap for half a billion years, at least |
ocomeni | 73:6f5021cbe752 | 293 | lastTime = now; |
ocomeni | 73:6f5021cbe752 | 294 | now = Kernel::get_ms_count(); |
ocomeni | 73:6f5021cbe752 | 295 | callCount++; |
ocomeni | 73:6f5021cbe752 | 296 | device->printf("\nHello : %d secs elapsed : CallCount = %d \n", uint32_t(now - lastTime), callCount); |
ocomeni | 73:6f5021cbe752 | 297 | } |
ocomeni | 73:6f5021cbe752 | 298 | |
ocomeni | 73:6f5021cbe752 | 299 | |
ocomeni | 73:6f5021cbe752 | 300 | |
ocomeni | 73:6f5021cbe752 | 301 | |
ocomeni | 73:6f5021cbe752 | 302 | //Serial device(USBTX, USBRX); // tx, rx |
ocomeni | 73:6f5021cbe752 | 303 | //RawSerial device(MBED_CONF_APP_UART1_TX, MBED_CONF_APP_UART1_RX); // tx, rx |
ocomeni | 73:6f5021cbe752 | 304 | |
ocomeni | 73:6f5021cbe752 | 305 | |
ocomeni | 73:6f5021cbe752 | 306 | int chunkNum; |
ocomeni | 73:6f5021cbe752 | 307 | void dump_response(HttpResponse* res) { |
ocomeni | 73:6f5021cbe752 | 308 | device->printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
ocomeni | 73:6f5021cbe752 | 309 | |
ocomeni | 73:6f5021cbe752 | 310 | device->printf("Headers:\n"); |
ocomeni | 73:6f5021cbe752 | 311 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
ocomeni | 73:6f5021cbe752 | 312 | device->printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
ocomeni | 73:6f5021cbe752 | 313 | } |
ocomeni | 73:6f5021cbe752 | 314 | device->printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
ocomeni | 73:6f5021cbe752 | 315 | } |
ocomeni | 73:6f5021cbe752 | 316 | |
ocomeni | 73:6f5021cbe752 | 317 | void completed(){ |
ocomeni | 73:6f5021cbe752 | 318 | } |
ocomeni | 73:6f5021cbe752 | 319 | void dump_chunked_response(const char *at, uint32_t length) { |
ocomeni | 73:6f5021cbe752 | 320 | device->printf("\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length); |
ocomeni | 73:6f5021cbe752 | 321 | //device->printf("\n Try Print Header as string:\n\n "); |
ocomeni | 73:6f5021cbe752 | 322 | //device->printf("recv %d [%.*s]\n", length, strstr((char *)at, "\r\n")-(char *)at, (char *)at); |
ocomeni | 73:6f5021cbe752 | 323 | //if(false) |
ocomeni | 73:6f5021cbe752 | 324 | if(chunkNum < 2) |
ocomeni | 73:6f5021cbe752 | 325 | for(int i=0; i < length; i++){ |
ocomeni | 73:6f5021cbe752 | 326 | |
ocomeni | 73:6f5021cbe752 | 327 | while(device->writeable()) |
ocomeni | 73:6f5021cbe752 | 328 | { |
ocomeni | 73:6f5021cbe752 | 329 | device->putc((uint8_t)at[i]); |
ocomeni | 73:6f5021cbe752 | 330 | } |
ocomeni | 73:6f5021cbe752 | 331 | //int resp = write( (const uint8_t *)at, (int) length, &completed, SERIAL_EVENT_TX_COMPLETE); |
ocomeni | 73:6f5021cbe752 | 332 | } |
ocomeni | 73:6f5021cbe752 | 333 | if(false) |
ocomeni | 73:6f5021cbe752 | 334 | for (size_t ix = 0; ix < length; ix++) { |
ocomeni | 73:6f5021cbe752 | 335 | device->printf("%02X: ", at[ix]); |
ocomeni | 73:6f5021cbe752 | 336 | if((ix % 32) == 0 and ix) |
ocomeni | 73:6f5021cbe752 | 337 | device->printf("\n"); |
ocomeni | 73:6f5021cbe752 | 338 | } |
ocomeni | 73:6f5021cbe752 | 339 | device->printf("\n\n"); |
ocomeni | 73:6f5021cbe752 | 340 | chunkNum++; |
ocomeni | 73:6f5021cbe752 | 341 | //device->printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
ocomeni | 73:6f5021cbe752 | 342 | } |
ocomeni | 73:6f5021cbe752 | 343 | |
ocomeni | 73:6f5021cbe752 | 344 | |
ocomeni | 73:6f5021cbe752 | 345 | // Wifi-demo |
ocomeni | 73:6f5021cbe752 | 346 | void wifi_demo(NetworkInterface* network){ |
ocomeni | 73:6f5021cbe752 | 347 | if (!network) { |
ocomeni | 73:6f5021cbe752 | 348 | device->printf("Cannot connect to the network, see serial output\n"); |
ocomeni | 73:6f5021cbe752 | 349 | return 1; |
ocomeni | 73:6f5021cbe752 | 350 | } |
ocomeni | 73:6f5021cbe752 | 351 | |
ocomeni | 73:6f5021cbe752 | 352 | mbed_trace_init(); |
ocomeni | 73:6f5021cbe752 | 353 | |
ocomeni | 73:6f5021cbe752 | 354 | // GET request to os.mbed.com |
ocomeni | 73:6f5021cbe752 | 355 | { |
ocomeni | 73:6f5021cbe752 | 356 | chunkNum = 0; |
ocomeni | 73:6f5021cbe752 | 357 | device->printf("\n----- HTTPS GET request -----\n"); |
ocomeni | 73:6f5021cbe752 | 358 | |
ocomeni | 73:6f5021cbe752 | 359 | HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt", &dump_chunked_response); |
ocomeni | 73:6f5021cbe752 | 360 | |
ocomeni | 73:6f5021cbe752 | 361 | HttpResponse* get_res = get_req->send(); |
ocomeni | 73:6f5021cbe752 | 362 | if (!get_res) { |
ocomeni | 73:6f5021cbe752 | 363 | device->printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
ocomeni | 73:6f5021cbe752 | 364 | return 1; |
ocomeni | 73:6f5021cbe752 | 365 | } |
ocomeni | 73:6f5021cbe752 | 366 | device->printf("\n----- HTTPS GET response -----\n"); |
ocomeni | 73:6f5021cbe752 | 367 | dump_response(get_res); |
ocomeni | 73:6f5021cbe752 | 368 | delete get_req; |
ocomeni | 73:6f5021cbe752 | 369 | } |
ocomeni | 73:6f5021cbe752 | 370 | |
ocomeni | 73:6f5021cbe752 | 371 | |
ocomeni | 73:6f5021cbe752 | 372 | |
ocomeni | 73:6f5021cbe752 | 373 | // Do a GET request to httpbin.org |
ocomeni | 73:6f5021cbe752 | 374 | { |
ocomeni | 73:6f5021cbe752 | 375 | chunkNum = 0; |
ocomeni | 73:6f5021cbe752 | 376 | device->printf("\n----- HTTP GET request to httpbin.org -----\n"); |
ocomeni | 73:6f5021cbe752 | 377 | |
ocomeni | 73:6f5021cbe752 | 378 | // By default the body is automatically parsed and stored in a buffer, this is memory heavy. |
ocomeni | 73:6f5021cbe752 | 379 | // To receive chunked response, pass in a callback as last parameter to the constructor. |
ocomeni | 73:6f5021cbe752 | 380 | HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418"); |
ocomeni | 73:6f5021cbe752 | 381 | |
ocomeni | 73:6f5021cbe752 | 382 | HttpResponse* get_res = get_req->send(); |
ocomeni | 73:6f5021cbe752 | 383 | if (!get_res) { |
ocomeni | 73:6f5021cbe752 | 384 | device->printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
ocomeni | 73:6f5021cbe752 | 385 | return 1; |
ocomeni | 73:6f5021cbe752 | 386 | } |
ocomeni | 73:6f5021cbe752 | 387 | |
ocomeni | 73:6f5021cbe752 | 388 | device->printf("\n----- HTTP GET response from httpbin.org -----\n"); |
ocomeni | 73:6f5021cbe752 | 389 | dump_response(get_res); |
ocomeni | 73:6f5021cbe752 | 390 | |
ocomeni | 73:6f5021cbe752 | 391 | delete get_req; |
ocomeni | 73:6f5021cbe752 | 392 | } |
ocomeni | 73:6f5021cbe752 | 393 | |
ocomeni | 73:6f5021cbe752 | 394 | |
ocomeni | 73:6f5021cbe752 | 395 | // Do a GET request to ovh.net |
ocomeni | 73:6f5021cbe752 | 396 | if(false) |
ocomeni | 73:6f5021cbe752 | 397 | { |
ocomeni | 73:6f5021cbe752 | 398 | chunkNum = 0; |
ocomeni | 73:6f5021cbe752 | 399 | device->printf("\n----- HTTP GET request to ovh.net -----\n"); |
ocomeni | 73:6f5021cbe752 | 400 | Timer t; |
ocomeni | 73:6f5021cbe752 | 401 | // By default the body is automatically parsed and stored in a buffer, this is memory heavy. |
ocomeni | 73:6f5021cbe752 | 402 | // To receive chunked response, pass in a callback as last parameter to the constructor. |
ocomeni | 73:6f5021cbe752 | 403 | t.start(); |
ocomeni | 73:6f5021cbe752 | 404 | HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://www.ovh.net/files/1Mio.dat", &dump_chunked_response); |
ocomeni | 73:6f5021cbe752 | 405 | |
ocomeni | 73:6f5021cbe752 | 406 | HttpResponse* get_res = get_req->send(); |
ocomeni | 73:6f5021cbe752 | 407 | if (!get_res) { |
ocomeni | 73:6f5021cbe752 | 408 | device->printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
ocomeni | 73:6f5021cbe752 | 409 | return 1; |
ocomeni | 73:6f5021cbe752 | 410 | } |
ocomeni | 73:6f5021cbe752 | 411 | |
ocomeni | 73:6f5021cbe752 | 412 | device->printf("\n----- HTTP GET response from ovh.net -----\n"); |
ocomeni | 73:6f5021cbe752 | 413 | dump_response(get_res); |
ocomeni | 73:6f5021cbe752 | 414 | t.stop(); |
ocomeni | 73:6f5021cbe752 | 415 | device->printf("The time taken was %f seconds\n", t.read()); |
ocomeni | 73:6f5021cbe752 | 416 | |
ocomeni | 73:6f5021cbe752 | 417 | |
ocomeni | 73:6f5021cbe752 | 418 | delete get_req; |
ocomeni | 73:6f5021cbe752 | 419 | } |
ocomeni | 73:6f5021cbe752 | 420 | |
ocomeni | 73:6f5021cbe752 | 421 | { |
ocomeni | 73:6f5021cbe752 | 422 | device->printf("\n----- HTTPS GET request (large file!) -----\n"); |
ocomeni | 73:6f5021cbe752 | 423 | |
ocomeni | 73:6f5021cbe752 | 424 | HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt"); |
ocomeni | 73:6f5021cbe752 | 425 | |
ocomeni | 73:6f5021cbe752 | 426 | HttpResponse* get_res = get_req->send(); |
ocomeni | 73:6f5021cbe752 | 427 | if (!get_res) { |
ocomeni | 73:6f5021cbe752 | 428 | device->printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
ocomeni | 73:6f5021cbe752 | 429 | return 1; |
ocomeni | 73:6f5021cbe752 | 430 | } |
ocomeni | 73:6f5021cbe752 | 431 | /* |
ocomeni | 73:6f5021cbe752 | 432 | while (0 < (response = socket.recv(p, remaining))) { |
ocomeni | 73:6f5021cbe752 | 433 | p += response; |
ocomeni | 73:6f5021cbe752 | 434 | rcount += response; |
ocomeni | 73:6f5021cbe752 | 435 | remaining -= response; |
ocomeni | 73:6f5021cbe752 | 436 | } |
ocomeni | 73:6f5021cbe752 | 437 | */ |
ocomeni | 73:6f5021cbe752 | 438 | device->printf("\n----- HTTPS GET response -----\n"); |
ocomeni | 73:6f5021cbe752 | 439 | dump_response(get_res); |
ocomeni | 73:6f5021cbe752 | 440 | |
ocomeni | 73:6f5021cbe752 | 441 | |
ocomeni | 73:6f5021cbe752 | 442 | |
ocomeni | 73:6f5021cbe752 | 443 | delete get_req; |
ocomeni | 73:6f5021cbe752 | 444 | } |
ocomeni | 73:6f5021cbe752 | 445 | |
ocomeni | 73:6f5021cbe752 | 446 | // POST request to httpbin.org |
ocomeni | 73:6f5021cbe752 | 447 | { |
ocomeni | 73:6f5021cbe752 | 448 | device->printf("\n----- HTTPS POST request -----\n"); |
ocomeni | 73:6f5021cbe752 | 449 | |
ocomeni | 73:6f5021cbe752 | 450 | HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post"); |
ocomeni | 73:6f5021cbe752 | 451 | post_req->set_header("Content-Type", "application/json"); |
ocomeni | 73:6f5021cbe752 | 452 | |
ocomeni | 73:6f5021cbe752 | 453 | const char body[] = "{\"hello\":\"world\"}"; |
ocomeni | 73:6f5021cbe752 | 454 | |
ocomeni | 73:6f5021cbe752 | 455 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
ocomeni | 73:6f5021cbe752 | 456 | if (!post_res) { |
ocomeni | 73:6f5021cbe752 | 457 | device->printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
ocomeni | 73:6f5021cbe752 | 458 | return 1; |
ocomeni | 73:6f5021cbe752 | 459 | } |
ocomeni | 73:6f5021cbe752 | 460 | |
ocomeni | 73:6f5021cbe752 | 461 | device->printf("\n----- HTTPS POST response -----\n"); |
ocomeni | 73:6f5021cbe752 | 462 | dump_response(post_res); |
ocomeni | 73:6f5021cbe752 | 463 | |
ocomeni | 73:6f5021cbe752 | 464 | delete post_req; |
ocomeni | 73:6f5021cbe752 | 465 | } |
ocomeni | 73:6f5021cbe752 | 466 | |
ocomeni | 73:6f5021cbe752 | 467 | |
ocomeni | 73:6f5021cbe752 | 468 | // POST request to ws.dnanudge.io:80 |
ocomeni | 73:6f5021cbe752 | 469 | { |
ocomeni | 73:6f5021cbe752 | 470 | device->printf("\n----- HTTP POST request (http://ws.dnanudge.io/nudgebox/v1) -----\n"); |
ocomeni | 73:6f5021cbe752 | 471 | |
ocomeni | 73:6f5021cbe752 | 472 | HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://ws.dnanudge.io/nudgebox/v1"); |
ocomeni | 73:6f5021cbe752 | 473 | post_req->set_header("Host", "ws.dnanudge.io"); |
ocomeni | 73:6f5021cbe752 | 474 | post_req->set_header("Accept", "*/*"); |
ocomeni | 73:6f5021cbe752 | 475 | post_req->set_header("Content-Type", "application/octet-stream"); |
ocomeni | 73:6f5021cbe752 | 476 | post_req->set_header("Content-Length", "20"); |
ocomeni | 73:6f5021cbe752 | 477 | // 00 08 6a 48 f8 2d 8e 82 01 68 |
ocomeni | 73:6f5021cbe752 | 478 | const uint8_t body[] = {0x00, 0x08, 0x6a, 0x48, 0xf8, 0x2d, 0x8e, 0x82, 0x01, 0x68, |
ocomeni | 73:6f5021cbe752 | 479 | // 65 6c 6c 6f 00 00 67 c3 19 f8 |
ocomeni | 73:6f5021cbe752 | 480 | 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x67, 0xc3, 0x19, 0xf8}; |
ocomeni | 73:6f5021cbe752 | 481 | |
ocomeni | 73:6f5021cbe752 | 482 | HttpResponse* post_res = post_req->send(body, 20); |
ocomeni | 73:6f5021cbe752 | 483 | if (!post_res) { |
ocomeni | 73:6f5021cbe752 | 484 | device->printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
ocomeni | 73:6f5021cbe752 | 485 | return 1; |
ocomeni | 73:6f5021cbe752 | 486 | } |
ocomeni | 73:6f5021cbe752 | 487 | |
ocomeni | 73:6f5021cbe752 | 488 | device->printf("\n----- HTTPS POST response -----\n"); |
ocomeni | 73:6f5021cbe752 | 489 | dump_response(post_res); |
ocomeni | 73:6f5021cbe752 | 490 | |
ocomeni | 73:6f5021cbe752 | 491 | delete post_req; |
ocomeni | 73:6f5021cbe752 | 492 | } |
ocomeni | 73:6f5021cbe752 | 493 | |
ocomeni | 73:6f5021cbe752 | 494 | |
ocomeni | 73:6f5021cbe752 | 495 | // POST request to httpbin.org |
ocomeni | 73:6f5021cbe752 | 496 | if(false) |
ocomeni | 73:6f5021cbe752 | 497 | { |
ocomeni | 73:6f5021cbe752 | 498 | device->printf("\n----- HTTPS POST request to AWS -----\n"); |
ocomeni | 73:6f5021cbe752 | 499 | |
ocomeni | 73:6f5021cbe752 | 500 | HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://q6bc0dxw7f.execute-api.eu-west-2.amazonaws.com/test/samples/v1"); |
ocomeni | 73:6f5021cbe752 | 501 | post_req->set_header("Content-Type", "application/json"); |
ocomeni | 73:6f5021cbe752 | 502 | |
ocomeni | 73:6f5021cbe752 | 503 | const char body[] = |
ocomeni | 73:6f5021cbe752 | 504 | "{" |
ocomeni | 73:6f5021cbe752 | 505 | "\"firstName\": \"Maria\", " |
ocomeni | 73:6f5021cbe752 | 506 | "\"lastName\": \"Huntera\", " |
ocomeni | 73:6f5021cbe752 | 507 | "\"dob\": \"1970-12-03\", " |
ocomeni | 73:6f5021cbe752 | 508 | "\"mobile\": \"07841887580\", " |
ocomeni | 73:6f5021cbe752 | 509 | "\"cartridgeId\": \"DN00000000RMPOE\", " |
ocomeni | 73:6f5021cbe752 | 510 | "\"labSampleId\": \"DYYAK\"" |
ocomeni | 73:6f5021cbe752 | 511 | "}"; |
ocomeni | 73:6f5021cbe752 | 512 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
ocomeni | 73:6f5021cbe752 | 513 | if (!post_res) { |
ocomeni | 73:6f5021cbe752 | 514 | device->printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
ocomeni | 73:6f5021cbe752 | 515 | return 1; |
ocomeni | 73:6f5021cbe752 | 516 | } |
ocomeni | 73:6f5021cbe752 | 517 | |
ocomeni | 73:6f5021cbe752 | 518 | device->printf("\n----- HTTPS POST response from AWS -----\n"); |
ocomeni | 73:6f5021cbe752 | 519 | dump_response(post_res); |
ocomeni | 73:6f5021cbe752 | 520 | |
ocomeni | 73:6f5021cbe752 | 521 | delete post_req; |
ocomeni | 73:6f5021cbe752 | 522 | } |
ocomeni | 73:6f5021cbe752 | 523 | } |
ocomeni | 73:6f5021cbe752 | 524 | |
ocomeni | 73:6f5021cbe752 | 525 | int main() { |
ocomeni | 73:6f5021cbe752 | 526 | //RawSerial *device(USBTX, USBRX); // tx, rx |
ocomeni | 73:6f5021cbe752 | 527 | device = new RawSerial(USBTX, USBRX, DEFAULT_BAUD_RATE); |
ocomeni | 73:6f5021cbe752 | 528 | |
ocomeni | 73:6f5021cbe752 | 529 | device->printf("\n --- Running UART-BLE-UartService --- \n"); |
ocomeni | 73:6f5021cbe752 | 530 | // Start the event queue |
ocomeni | 73:6f5021cbe752 | 531 | //t.start(callback(&eventQueue, &EventQueue::dispatch_forever)); |
ocomeni | 73:6f5021cbe752 | 532 | |
ocomeni | 73:6f5021cbe752 | 533 | eventQueue.call_every(500, blinkCallback); |
ocomeni | 73:6f5021cbe752 | 534 | eventQueue.call_every(60000, HelloUart); |
ocomeni | 73:6f5021cbe752 | 535 | //eventQueue.call_every(1000, checkUartReceive); |
ocomeni | 73:6f5021cbe752 | 536 | device->printf("\n --- EventQueues setup --- \n"); |
ocomeni | 73:6f5021cbe752 | 537 | //////////////////////////////////////////////////////////////////////////////// |
ocomeni | 73:6f5021cbe752 | 538 | // BLE Initialization ///////////////////////////////////////////////////////// |
ocomeni | 73:6f5021cbe752 | 539 | device->printf("\n --- about to instantiate BLE instance --- \n"); |
ocomeni | 73:6f5021cbe752 | 540 | device->getc(); |
ocomeni | 73:6f5021cbe752 | 541 | //cbMAIN_driverLock(); |
ocomeni | 73:6f5021cbe752 | 542 | BLE &ble = BLE::Instance(); |
ocomeni | 73:6f5021cbe752 | 543 | device->printf("\n --- BLE Instance Instantiated --- \n"); |
ocomeni | 73:6f5021cbe752 | 544 | //ble.onEventsToProcess(scheduleBleEventsProcessing); |
ocomeni | 73:6f5021cbe752 | 545 | //device->printf("\n --- BLE scheduleBleEventsProcessing setup --- \n"); |
ocomeni | 73:6f5021cbe752 | 546 | //ble.init(bleInitComplete); |
ocomeni | 73:6f5021cbe752 | 547 | bleInitialization(); |
ocomeni | 73:6f5021cbe752 | 548 | //cbMAIN_driverUnlock(); |
ocomeni | 73:6f5021cbe752 | 549 | device->printf("\n --- BLE Initialization completed --- \n"); |
ocomeni | 73:6f5021cbe752 | 550 | device->printf("\n Press any key to start Wifi demo: "); |
ocomeni | 73:6f5021cbe752 | 551 | device->getc(); |
ocomeni | 73:6f5021cbe752 | 552 | //////////////////////////////////////////////////////////////////////////////////// |
ocomeni | 73:6f5021cbe752 | 553 | |
ocomeni | 73:6f5021cbe752 | 554 | //device->baud(115200); |
ocomeni | 73:6f5021cbe752 | 555 | NetworkInterface* network = connect_to_default_network_interface(); |
ocomeni | 73:6f5021cbe752 | 556 | // run on separate thread; |
ocomeni | 73:6f5021cbe752 | 557 | t.start(wifi_demo(network)); |
ocomeni | 73:6f5021cbe752 | 558 | network->disconnect(); |
ocomeni | 73:6f5021cbe752 | 559 | t.join(); |
ocomeni | 73:6f5021cbe752 | 560 | |
ocomeni | 73:6f5021cbe752 | 561 | for(int i=0;i<255;i++) |
ocomeni | 73:6f5021cbe752 | 562 | { |
ocomeni | 73:6f5021cbe752 | 563 | device->putc(i); |
ocomeni | 73:6f5021cbe752 | 564 | } |
ocomeni | 73:6f5021cbe752 | 565 | int n; |
ocomeni | 73:6f5021cbe752 | 566 | //ReadUartBytes(RxBuffer, RX_BUFFER_LEN, 4); |
ocomeni | 73:6f5021cbe752 | 567 | device->printf("\n\n\nEnter # of expected bytes: "); |
ocomeni | 73:6f5021cbe752 | 568 | n = ReadUartBytes(RxBuffer, RX_BUFFER_LEN, 4, true); |
ocomeni | 73:6f5021cbe752 | 569 | int rxLen = (int) 100*(RxBuffer[0]-'0') + 10*(RxBuffer[1]-'0') + (RxBuffer[2]-'0'); |
ocomeni | 73:6f5021cbe752 | 570 | device->printf("\n\nExpected # of Received Bytes = %d\n", rxLen); |
ocomeni | 73:6f5021cbe752 | 571 | BackGndUartRead(RxBuffer, RX_BUFFER_LEN, rxLen); |
ocomeni | 73:6f5021cbe752 | 572 | //device->printf("--- Writing back received data --- \n\n"); |
ocomeni | 73:6f5021cbe752 | 573 | //n = WriteUartBytes(RxBuffer, TX_BUFFER_LEN, rxLen); |
ocomeni | 73:6f5021cbe752 | 574 | //write("\n\ntesting Serial Write\n", 40); //, checkUartReceive, SERIAL_EVENT_TX_COMPLETE); |
ocomeni | 73:6f5021cbe752 | 575 | |
ocomeni | 73:6f5021cbe752 | 576 | |
ocomeni | 73:6f5021cbe752 | 577 | device->printf("\nATCmdParser with ESP8266 example"); |
ocomeni | 73:6f5021cbe752 | 578 | device->printf("\n Waiting for 2 minutes "); |
ocomeni | 73:6f5021cbe752 | 579 | wait(120); |
ocomeni | 73:6f5021cbe752 | 580 | eventQueue.dispatch_forever(); |
ocomeni | 73:6f5021cbe752 | 581 | return 0; |
ocomeni | 73:6f5021cbe752 | 582 | device->printf("\n About to delete RawSerial device instance "); |
ocomeni | 73:6f5021cbe752 | 583 | delete device; |
ocomeni | 73:6f5021cbe752 | 584 | _serial = new UARTSerial(USBTX, USBRX, DEFAULT_BAUD_RATE); |
ocomeni | 73:6f5021cbe752 | 585 | printf("\n ATCmdParser printf being used now \n\n"); |
ocomeni | 73:6f5021cbe752 | 586 | _parser = new ATCmdParser(_serial); |
ocomeni | 73:6f5021cbe752 | 587 | _parser->debug_on( 1 ); |
ocomeni | 73:6f5021cbe752 | 588 | _parser->set_delimiter( "\r\n" ); |
ocomeni | 73:6f5021cbe752 | 589 | |
ocomeni | 73:6f5021cbe752 | 590 | //Now get the FW version number of ESP8266 by sending an AT command |
ocomeni | 73:6f5021cbe752 | 591 | printf("\nATCmdParser: Retrieving FW version"); |
ocomeni | 73:6f5021cbe752 | 592 | _parser->send("AT+GMR"); |
ocomeni | 73:6f5021cbe752 | 593 | int version; |
ocomeni | 73:6f5021cbe752 | 594 | if(_parser->recv("SDK version:%d", &version) && _parser->recv("OK")) { |
ocomeni | 73:6f5021cbe752 | 595 | printf("\nATCmdParser: FW version: %d", version); |
ocomeni | 73:6f5021cbe752 | 596 | printf("\nATCmdParser: Retrieving FW version success"); |
ocomeni | 73:6f5021cbe752 | 597 | } else { |
ocomeni | 73:6f5021cbe752 | 598 | printf("\nATCmdParser: Retrieving FW version failed"); |
ocomeni | 73:6f5021cbe752 | 599 | return -1; |
ocomeni | 73:6f5021cbe752 | 600 | } |
ocomeni | 73:6f5021cbe752 | 601 | |
ocomeni | 73:6f5021cbe752 | 602 | printf("\nDone\n"); |
ocomeni | 73:6f5021cbe752 | 603 | |
ocomeni | 73:6f5021cbe752 | 604 | |
ocomeni | 73:6f5021cbe752 | 605 | //eventQueue.dispatch_forever(); |
ocomeni | 73:6f5021cbe752 | 606 | //t.start(callback(&eventQueue, &EventQueue::dispatch_forever)); |
ocomeni | 73:6f5021cbe752 | 607 | //eventQueue2.dispatch_forever(); |
ocomeni | 73:6f5021cbe752 | 608 | |
ocomeni | 73:6f5021cbe752 | 609 | return 0; |
ocomeni | 73:6f5021cbe752 | 610 | |
ocomeni | 73:6f5021cbe752 | 611 | |
ocomeni | 73:6f5021cbe752 | 612 | |
ocomeni | 73:6f5021cbe752 | 613 | |
ocomeni | 73:6f5021cbe752 | 614 | //wait(osWaitForever); |
ocomeni | 73:6f5021cbe752 | 615 | } |
ocomeni | 73:6f5021cbe752 | 616 | |
ocomeni | 73:6f5021cbe752 | 617 | #endif |