CLI example for NNN50

Dependencies:   NNN50_WIFI_API

Fork of NNN50_WiFi_HelloWorld by Delta

Committer:
silviaChen
Date:
Thu Sep 14 01:48:08 2017 +0000
Revision:
9:871fc0231c7f
Parent:
6:1dac7bcca23d
Support both BLE UART service and WiFi CLI command

Who changed what in which revision?

UserRevisionLine numberNew contents of line
silviaChen 9:871fc0231c7f 1 #include <mbed.h>
silviaChen 9:871fc0231c7f 2
silviaChen 9:871fc0231c7f 3 #include "Gap.h"
silviaChen 9:871fc0231c7f 4 #include "command-interpreter.h"
silviaChen 9:871fc0231c7f 5 #include "ble/BLE.h"
silviaChen 9:871fc0231c7f 6 #include "ble/services/BatteryService.h"
silviaChen 9:871fc0231c7f 7 #include "ble/services/DeviceInformationService.h"
silviaChen 9:871fc0231c7f 8 #include "ble/services/UARTService.h"
silviaChen 9:871fc0231c7f 9
silviaChen 9:871fc0231c7f 10 #define uart_buffer_size 64 //Do not increase uart_buffer_size to prevent out of memory,
silviaChen 9:871fc0231c7f 11 #define uart_baudrate 38400 //supported baudrate range between 1200 to 230400 (38400) for NQ620 (NNN50)
silviaChen 9:871fc0231c7f 12 unsigned char uart_buf[uart_buffer_size];
silviaChen 9:871fc0231c7f 13 unsigned int i = 0;
silviaChen 9:871fc0231c7f 14 bool isGetCommand = false;
silviaChen 9:871fc0231c7f 15
silviaChen 9:871fc0231c7f 16 Serial console(USBTX, USBRX);
tsungta 6:1dac7bcca23d 17
silviaChen 9:871fc0231c7f 18 DeviceInformationService *deviceInfo;
silviaChen 9:871fc0231c7f 19 BatteryService *batteryService;
silviaChen 9:871fc0231c7f 20 static UARTService *uartServicePtr;
silviaChen 9:871fc0231c7f 21 const static char DEVICE_NAME[] = "DELTA_CLI_UART";
silviaChen 9:871fc0231c7f 22 static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE,
silviaChen 9:871fc0231c7f 23 GattService::UUID_BATTERY_SERVICE,
silviaChen 9:871fc0231c7f 24 GattService::UUID_DEVICE_INFORMATION_SERVICE};
tsungta 6:1dac7bcca23d 25
silviaChen 9:871fc0231c7f 26 void CLI_execute() {
silviaChen 9:871fc0231c7f 27 if (uart_buf[i-2] != '\r' || uart_buf[i-1] != '\n') return; //detecting \r\n
silviaChen 9:871fc0231c7f 28
silviaChen 9:871fc0231c7f 29 isGetCommand = true;
silviaChen 9:871fc0231c7f 30
silviaChen 9:871fc0231c7f 31 }
silviaChen 9:871fc0231c7f 32 void uart_interrupt() {
silviaChen 9:871fc0231c7f 33 uart_buf[i++] = console.getc();
silviaChen 9:871fc0231c7f 34 CLI_execute();
tsungta 6:1dac7bcca23d 35 }
tsungta 6:1dac7bcca23d 36
silviaChen 9:871fc0231c7f 37 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params)
silviaChen 9:871fc0231c7f 38 {
silviaChen 9:871fc0231c7f 39 console.printf("Connected\r\n");
silviaChen 9:871fc0231c7f 40 }
tsungta 6:1dac7bcca23d 41
silviaChen 9:871fc0231c7f 42 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
silviaChen 9:871fc0231c7f 43 {
silviaChen 9:871fc0231c7f 44 console.printf("Disconnected\r\n");
silviaChen 9:871fc0231c7f 45 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
silviaChen 9:871fc0231c7f 46 }
tsungta 6:1dac7bcca23d 47
silviaChen 9:871fc0231c7f 48 void onTimeoutCallback(Gap::TimeoutSource_t source)
silviaChen 9:871fc0231c7f 49 {
silviaChen 9:871fc0231c7f 50 switch (source) {
silviaChen 9:871fc0231c7f 51 case Gap::TIMEOUT_SRC_ADVERTISING:
silviaChen 9:871fc0231c7f 52 console.printf("Advertising timeout\r\n");
silviaChen 9:871fc0231c7f 53 break;
silviaChen 9:871fc0231c7f 54 case Gap::TIMEOUT_SRC_SECURITY_REQUEST:
silviaChen 9:871fc0231c7f 55 console.printf("Security request timeout\r\n");
silviaChen 9:871fc0231c7f 56 break;
silviaChen 9:871fc0231c7f 57 case Gap::TIMEOUT_SRC_SCAN:
silviaChen 9:871fc0231c7f 58 console.printf("Scanning timeout\r\n");
silviaChen 9:871fc0231c7f 59 break;
silviaChen 9:871fc0231c7f 60 case Gap::TIMEOUT_SRC_CONN:
silviaChen 9:871fc0231c7f 61 console.printf("Connection timeout\r\n");
silviaChen 9:871fc0231c7f 62 break;
silviaChen 9:871fc0231c7f 63 }
silviaChen 9:871fc0231c7f 64 }
silviaChen 9:871fc0231c7f 65
silviaChen 9:871fc0231c7f 66 void serverDataWrittenCallback(const GattWriteCallbackParams *response) {
silviaChen 9:871fc0231c7f 67 console.printf("serverDataWrittenCallback\r\n");
silviaChen 9:871fc0231c7f 68 if (response->handle == uartServicePtr->getTXCharacteristicHandle()) {
silviaChen 9:871fc0231c7f 69 for(int j=0;j<response->len;j++) {
silviaChen 9:871fc0231c7f 70 console.printf("data: %02X\r\n", response->data[j]);
silviaChen 9:871fc0231c7f 71 }
tsungta 6:1dac7bcca23d 72 }
tsungta 6:1dac7bcca23d 73 }
tsungta 6:1dac7bcca23d 74
silviaChen 9:871fc0231c7f 75 void dataSentCallback(const unsigned callback) {
silviaChen 9:871fc0231c7f 76 //uart.printf("dataSentCallback\r\n");
silviaChen 9:871fc0231c7f 77 }
silviaChen 9:871fc0231c7f 78
silviaChen 9:871fc0231c7f 79 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
silviaChen 9:871fc0231c7f 80 {
silviaChen 9:871fc0231c7f 81 BLE &ble = params->ble;
silviaChen 9:871fc0231c7f 82 ble_error_t error = params->error;
silviaChen 9:871fc0231c7f 83
silviaChen 9:871fc0231c7f 84 if (error != BLE_ERROR_NONE) {
silviaChen 9:871fc0231c7f 85 return;
silviaChen 9:871fc0231c7f 86 }
silviaChen 9:871fc0231c7f 87
silviaChen 9:871fc0231c7f 88 ble.gap().onDisconnection(disconnectionCallback);
silviaChen 9:871fc0231c7f 89 ble.gap().onConnection(onConnectionCallback);
silviaChen 9:871fc0231c7f 90 ble.gap().onTimeout(onTimeoutCallback);
silviaChen 9:871fc0231c7f 91 ble.gattServer().onDataWritten(serverDataWrittenCallback);
silviaChen 9:871fc0231c7f 92 ble.gattServer().onDataSent(dataSentCallback);
silviaChen 9:871fc0231c7f 93
silviaChen 9:871fc0231c7f 94 /* Setup primary service. */
silviaChen 9:871fc0231c7f 95 uartServicePtr = new UARTService(ble);
silviaChen 9:871fc0231c7f 96
silviaChen 9:871fc0231c7f 97 /* Setup auxiliary service. */
silviaChen 9:871fc0231c7f 98 batteryService = new BatteryService(ble, 100);
silviaChen 9:871fc0231c7f 99 deviceInfo = new DeviceInformationService(ble, "DELTA", "NQ620", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
silviaChen 9:871fc0231c7f 100
silviaChen 9:871fc0231c7f 101 /* Setup advertising. */
silviaChen 9:871fc0231c7f 102 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
silviaChen 9:871fc0231c7f 103 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
silviaChen 9:871fc0231c7f 104 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
silviaChen 9:871fc0231c7f 105 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
silviaChen 9:871fc0231c7f 106 ble.gap().setAdvertisingInterval(50); /* 50ms */
silviaChen 9:871fc0231c7f 107 ble.gap().startAdvertising();
silviaChen 9:871fc0231c7f 108 console.printf("Start advertising\r\n");
silviaChen 9:871fc0231c7f 109 }
silviaChen 9:871fc0231c7f 110
silviaChen 9:871fc0231c7f 111 int main(void)
silviaChen 9:871fc0231c7f 112 {
silviaChen 9:871fc0231c7f 113
silviaChen 9:871fc0231c7f 114 console.attach(&uart_interrupt);
silviaChen 9:871fc0231c7f 115 console.baud(uart_baudrate);
silviaChen 9:871fc0231c7f 116 console.printf("Application Start\r\n");
silviaChen 9:871fc0231c7f 117
silviaChen 9:871fc0231c7f 118 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
silviaChen 9:871fc0231c7f 119 ble.init(bleInitComplete);
silviaChen 9:871fc0231c7f 120
silviaChen 9:871fc0231c7f 121 /* SpinWait for initialization to complete. This is necessary because the
silviaChen 9:871fc0231c7f 122 * BLE object is used in the main loop below. */
silviaChen 9:871fc0231c7f 123 while (ble.hasInitialized() == false) { /* spin loop */ }
silviaChen 9:871fc0231c7f 124
silviaChen 9:871fc0231c7f 125 while(1) {
silviaChen 9:871fc0231c7f 126 if(isGetCommand) {
silviaChen 9:871fc0231c7f 127 isGetCommand = false;
silviaChen 9:871fc0231c7f 128
silviaChen 9:871fc0231c7f 129 if(uart_buf[0] == 'b') {
silviaChen 9:871fc0231c7f 130 if (ble.getGapState().connected) {
silviaChen 9:871fc0231c7f 131
silviaChen 9:871fc0231c7f 132 //Write data via RX characteristic
silviaChen 9:871fc0231c7f 133 ble.gattServer().write(uartServicePtr->getRXCharacteristicHandle(), static_cast<const uint8_t *>(uart_buf)+2, i-4);
silviaChen 9:871fc0231c7f 134 }
silviaChen 9:871fc0231c7f 135 }
silviaChen 9:871fc0231c7f 136 else{
silviaChen 9:871fc0231c7f 137 for (int j=0; j<i; j++)
silviaChen 9:871fc0231c7f 138 cyntecProcessCommandInput(uart_buf[j]);
silviaChen 9:871fc0231c7f 139 //console.putc(uart_buf[j]);//used for debug only
silviaChen 9:871fc0231c7f 140 }
silviaChen 9:871fc0231c7f 141
silviaChen 9:871fc0231c7f 142 i=0;
silviaChen 9:871fc0231c7f 143 }
silviaChen 9:871fc0231c7f 144
silviaChen 9:871fc0231c7f 145 ble.waitForEvent(); // low power wait for event
silviaChen 9:871fc0231c7f 146 }
silviaChen 9:871fc0231c7f 147 }