CLI example for NNN50
Dependencies: NNN50_WIFI_API
Fork of NNN50_WiFi_HelloWorld by
Diff: main.cpp
- Revision:
- 9:871fc0231c7f
- Parent:
- 6:1dac7bcca23d
--- a/main.cpp Thu Jun 29 04:21:01 2017 +0000
+++ b/main.cpp Thu Sep 14 01:48:08 2017 +0000
@@ -1,79 +1,147 @@
-/******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. ***************
-*
-* File Name : main.cpp
-* Authors : Tsungta Wu - CPBG (tsungta.wu@deltaww.com)
-* Version : V.1.0.0
-* Date : 2016/Nov/24
-*
-* This example only show the most basic WiFi operation include AP scan and connect
-* The usage of TCP/UDP socket please refer to the mbed Handbook from the link below
-* https://developer.mbed.org/handbook/Socket
-*
-*******************************************************************************/
+#include <mbed.h>
+
+#include "Gap.h"
+#include "command-interpreter.h"
+#include "ble/BLE.h"
+#include "ble/services/BatteryService.h"
+#include "ble/services/DeviceInformationService.h"
+#include "ble/services/UARTService.h"
+
+#define uart_buffer_size 64 //Do not increase uart_buffer_size to prevent out of memory,
+#define uart_baudrate 38400 //supported baudrate range between 1200 to 230400 (38400) for NQ620 (NNN50)
+unsigned char uart_buf[uart_buffer_size];
+unsigned int i = 0;
+bool isGetCommand = false;
+
+Serial console(USBTX, USBRX);
-#include "mbed.h"
-#include "EthernetInterface.h"
-#include "WIFIDevice.h"
+DeviceInformationService *deviceInfo;
+BatteryService *batteryService;
+static UARTService *uartServicePtr;
+const static char DEVICE_NAME[] = "DELTA_CLI_UART";
+static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE,
+ GattService::UUID_BATTERY_SERVICE,
+ GattService::UUID_DEVICE_INFORMATION_SERVICE};
-const char* ECHO_SERVER_ADDRESS = "192.168.2.13";
-const int ECHO_SERVER_PORT = 1030;
-
-void scanCallback(tstrM2mWifiscanResult result)
-{
- printf("SSID: %s \n", result.au8SSID);
- printf("RSSI: %i \n", result.s8rssi);
+void CLI_execute() {
+ if (uart_buf[i-2] != '\r' || uart_buf[i-1] != '\n') return; //detecting \r\n
+
+ isGetCommand = true;
+
+}
+void uart_interrupt() {
+ uart_buf[i++] = console.getc();
+ CLI_execute();
}
-int main() {
-
- EthernetInterface eth;
- WIFIDevice wifi;
-
- eth.init();
+void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params)
+{
+ console.printf("Connected\r\n");
+}
- wifi.apScan(scanCallback);
-
- wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "TP-LINK_2.4G_TTWU", "0972753720");
-
- eth.connect();
-
- if(wifi.is_AP_connected())
- printf("Connect Success! \n");
- else
- printf("Connect Fail! \n");
-
- printf("MAC: %s\n", eth.getMACAddress());
- printf("IP: %s\n", eth.getIPAddress());
- printf("Gateway: %s\n", eth.getGateway());
- printf("NetworkMask: %s\n", eth.getNetworkMask());
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+ console.printf("Disconnected\r\n");
+ BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
+}
- UDPSocket sock;
- sock.init();
-
- Endpoint echo_server;
- echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
-
- char out_buffer[] = "Hello World";
- printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS);
- sock.sendTo(echo_server, out_buffer, sizeof(out_buffer));
-
- char in_buffer[256]; //IMPORTANT, array size MUST >= the actual received data size or set to maximum of 1400 if there is uncertainty
- int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer));
-
- if(n <0)
- in_buffer[0] = '\0';//IMPORTANT, in case n = -1 when set_bloacking is timeout, prevent the illegal array in_buffer[-1] access
- else
- in_buffer[n] = '\0';
-
- printf("Received message from server: '%s'\n", in_buffer);
-
- sock.close();
-
- eth.disconnect();
-
- wifi.sleep();
-
- while(1) {
+void onTimeoutCallback(Gap::TimeoutSource_t source)
+{
+ switch (source) {
+ case Gap::TIMEOUT_SRC_ADVERTISING:
+ console.printf("Advertising timeout\r\n");
+ break;
+ case Gap::TIMEOUT_SRC_SECURITY_REQUEST:
+ console.printf("Security request timeout\r\n");
+ break;
+ case Gap::TIMEOUT_SRC_SCAN:
+ console.printf("Scanning timeout\r\n");
+ break;
+ case Gap::TIMEOUT_SRC_CONN:
+ console.printf("Connection timeout\r\n");
+ break;
+ }
+}
+
+void serverDataWrittenCallback(const GattWriteCallbackParams *response) {
+ console.printf("serverDataWrittenCallback\r\n");
+ if (response->handle == uartServicePtr->getTXCharacteristicHandle()) {
+ for(int j=0;j<response->len;j++) {
+ console.printf("data: %02X\r\n", response->data[j]);
+ }
}
}
+void dataSentCallback(const unsigned callback) {
+ //uart.printf("dataSentCallback\r\n");
+}
+
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+ BLE &ble = params->ble;
+ ble_error_t error = params->error;
+
+ if (error != BLE_ERROR_NONE) {
+ return;
+ }
+
+ ble.gap().onDisconnection(disconnectionCallback);
+ ble.gap().onConnection(onConnectionCallback);
+ ble.gap().onTimeout(onTimeoutCallback);
+ ble.gattServer().onDataWritten(serverDataWrittenCallback);
+ ble.gattServer().onDataSent(dataSentCallback);
+
+ /* Setup primary service. */
+ uartServicePtr = new UARTService(ble);
+
+ /* Setup auxiliary service. */
+ batteryService = new BatteryService(ble, 100);
+ deviceInfo = new DeviceInformationService(ble, "DELTA", "NQ620", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
+
+ /* Setup advertising. */
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+ ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+ ble.gap().setAdvertisingInterval(50); /* 50ms */
+ ble.gap().startAdvertising();
+ console.printf("Start advertising\r\n");
+}
+
+int main(void)
+{
+
+ console.attach(&uart_interrupt);
+ console.baud(uart_baudrate);
+ console.printf("Application Start\r\n");
+
+ BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
+ ble.init(bleInitComplete);
+
+ /* SpinWait for initialization to complete. This is necessary because the
+ * BLE object is used in the main loop below. */
+ while (ble.hasInitialized() == false) { /* spin loop */ }
+
+ while(1) {
+ if(isGetCommand) {
+ isGetCommand = false;
+
+ if(uart_buf[0] == 'b') {
+ if (ble.getGapState().connected) {
+
+ //Write data via RX characteristic
+ ble.gattServer().write(uartServicePtr->getRXCharacteristicHandle(), static_cast<const uint8_t *>(uart_buf)+2, i-4);
+ }
+ }
+ else{
+ for (int j=0; j<i; j++)
+ cyntecProcessCommandInput(uart_buf[j]);
+ //console.putc(uart_buf[j]);//used for debug only
+ }
+
+ i=0;
+ }
+
+ ble.waitForEvent(); // low power wait for event
+ }
+}
