fork test
Dependencies: BLE_API WIFI_API_32kRAM mbed nRF51822
Fork of NNN40_CLI by
CLI_Source/ble_cli.cpp@5:ee474e3133eb, 2015-12-09 (annotated)
- Committer:
- gillwei7
- Date:
- Wed Dec 09 11:31:59 2015 +0000
- Revision:
- 5:ee474e3133eb
- Parent:
- 4:b52035367aee
- Child:
- 6:1243f9e584f4
Gill Wei 20151209, add Wifi device commands for WIFI_32KRAM driver, add BLE central mode command.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gillwei7 | 0:5c195ab2f696 | 1 | /** |
gillwei7 | 0:5c195ab2f696 | 2 | * File: ble-cli.c |
gillwei7 | 0:5c195ab2f696 | 3 | * Description: BLE CLI commands used by all applications BLE of profile. |
gillwei7 | 0:5c195ab2f696 | 4 | * |
gillwei7 | 0:5c195ab2f696 | 5 | * Copyright 2014 by CYNTEC Corporation. All rights reserved. |
gillwei7 | 0:5c195ab2f696 | 6 | */ |
gillwei7 | 3:38ec8ad317f4 | 7 | |
gillwei7 | 0:5c195ab2f696 | 8 | #include <stdio.h> |
gillwei7 | 0:5c195ab2f696 | 9 | #include <stdlib.h> |
gillwei7 | 0:5c195ab2f696 | 10 | #include <stdint.h> |
gillwei7 | 0:5c195ab2f696 | 11 | #include <string.h> |
gillwei7 | 0:5c195ab2f696 | 12 | #include "command-interpreter.h" |
gillwei7 | 0:5c195ab2f696 | 13 | #include "simple_uart.h" |
gillwei7 | 0:5c195ab2f696 | 14 | #include "mbed.h" |
gillwei7 | 0:5c195ab2f696 | 15 | #include "BLE.h" |
gillwei7 | 0:5c195ab2f696 | 16 | #include "Gap.h" |
gillwei7 | 0:5c195ab2f696 | 17 | #include "nrf_gpio.h" |
gillwei7 | 0:5c195ab2f696 | 18 | #include "nrf_soc.h" |
gillwei7 | 0:5c195ab2f696 | 19 | #include "ble_cli.h" |
gillwei7 | 0:5c195ab2f696 | 20 | #include "ble_hci.h" |
gillwei7 | 0:5c195ab2f696 | 21 | #include "app_error.h" |
gillwei7 | 0:5c195ab2f696 | 22 | #include "PinNames.h" |
gillwei7 | 0:5c195ab2f696 | 23 | //#include "custom_helper.h" |
gillwei7 | 0:5c195ab2f696 | 24 | extern "C" { |
gillwei7 | 3:38ec8ad317f4 | 25 | #include "ble_advdata.h" |
gillwei7 | 0:5c195ab2f696 | 26 | } |
gillwei7 | 4:b52035367aee | 27 | #include "ble/GattCharacteristic.h" |
gillwei7 | 4:b52035367aee | 28 | #include "ble/UUID.h" |
gillwei7 | 4:b52035367aee | 29 | #include "ble/DiscoveredCharacteristic.h" |
gillwei7 | 4:b52035367aee | 30 | #include "ble/DiscoveredService.h" |
gillwei7 | 0:5c195ab2f696 | 31 | |
gillwei7 | 4:b52035367aee | 32 | #define APP_ADV_INTERVAL 40 /**< The advertising interval (in units of ms). */ |
gillwei7 | 0:5c195ab2f696 | 33 | #define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout (in units of seconds). */ |
gillwei7 | 4:b52035367aee | 34 | #define APP_ADV_INTERVAL_MAX 10240 // correspond to 10.24s |
gillwei7 | 4:b52035367aee | 35 | #define APP_ADV_INTERVAL_MIN 20 // correspond to 20ms |
gillwei7 | 0:5c195ab2f696 | 36 | // Offset for Advertising Data. |
gillwei7 | 0:5c195ab2f696 | 37 | #define APP_ADV_DATA_OFFSET 2 |
gillwei7 | 0:5c195ab2f696 | 38 | |
gillwei7 | 0:5c195ab2f696 | 39 | #define MAX_DEVNAME_LEN 32 |
gillwei7 | 0:5c195ab2f696 | 40 | #define CLI_SERVICE_MAX_NUM 10 |
gillwei7 | 0:5c195ab2f696 | 41 | #define CLI_CHAR_MAX_NUM 10 |
gillwei7 | 0:5c195ab2f696 | 42 | #define MAX_VALUE_LENGTH 20 |
gillwei7 | 0:5c195ab2f696 | 43 | |
gillwei7 | 5:ee474e3133eb | 44 | #define CLI_FWVERION "DELTA_CLI_V1.7" |
gillwei7 | 0:5c195ab2f696 | 45 | #define MODULE_NAME "DFCM-NNN40-DT1R" |
gillwei7 | 0:5c195ab2f696 | 46 | #define ADVERTISING_LED_PIN_NO LED1 |
gillwei7 | 5:ee474e3133eb | 47 | //#define BLE_DEBUG |
gillwei7 | 0:5c195ab2f696 | 48 | //#define LBS_UUID_BASE {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00} |
gillwei7 | 0:5c195ab2f696 | 49 | #define NEW_VS_SERVICE 0x0001 |
gillwei7 | 3:38ec8ad317f4 | 50 | #define DEFAULT_SCAN_INTERVAL 500 |
gillwei7 | 3:38ec8ad317f4 | 51 | #define DEFAULT_SCAN_WINDOW 400 |
gillwei7 | 4:b52035367aee | 52 | #define DEFAULT_SCAN_TIMEOUT 0x0005 |
gillwei7 | 3:38ec8ad317f4 | 53 | #define BLE_MAX_ADDRESS_NUMBER 10 |
gillwei7 | 4:b52035367aee | 54 | #define TARGET_DEVNAME_LEN 30 |
gillwei7 | 0:5c195ab2f696 | 55 | |
gillwei7 | 0:5c195ab2f696 | 56 | typedef struct bufferGattChar { |
gillwei7 | 4:b52035367aee | 57 | uint32_t props; |
gillwei7 | 4:b52035367aee | 58 | UUID char_uuid; |
gillwei7 | 4:b52035367aee | 59 | uint8_t value[MAX_VALUE_LENGTH]; |
gillwei7 | 4:b52035367aee | 60 | //uint16_t char_value_handle; |
gillwei7 | 3:38ec8ad317f4 | 61 | uint8_t valueLength; |
gillwei7 | 0:5c195ab2f696 | 62 | } bufferGattChar_t; |
gillwei7 | 0:5c195ab2f696 | 63 | |
gillwei7 | 0:5c195ab2f696 | 64 | typedef struct bufferGattService { |
gillwei7 | 4:b52035367aee | 65 | UUID ser_uuid; |
gillwei7 | 3:38ec8ad317f4 | 66 | bufferGattChar_t bufferGattChar[CLI_CHAR_MAX_NUM]; |
gillwei7 | 4:b52035367aee | 67 | //uint16_t service_handle; |
gillwei7 | 0:5c195ab2f696 | 68 | } bufferGattService_t; |
gillwei7 | 0:5c195ab2f696 | 69 | |
gillwei7 | 3:38ec8ad317f4 | 70 | // gill 20150916 for scan |
gillwei7 | 3:38ec8ad317f4 | 71 | typedef struct { |
gillwei7 | 3:38ec8ad317f4 | 72 | const uint8_t * p_data; /**< Pointer to data. */ |
gillwei7 | 3:38ec8ad317f4 | 73 | uint8_t data_len; /**< Length of data. */ |
gillwei7 | 3:38ec8ad317f4 | 74 | } data_t; |
gillwei7 | 3:38ec8ad317f4 | 75 | |
gillwei7 | 0:5c195ab2f696 | 76 | /* save entry services */ |
gillwei7 | 0:5c195ab2f696 | 77 | bufferGattService_t bufferService[CLI_SERVICE_MAX_NUM]; |
gillwei7 | 4:b52035367aee | 78 | static ble_gap_addr_t m_peer_addr; |
gillwei7 | 4:b52035367aee | 79 | extern Serial console; |
gillwei7 | 4:b52035367aee | 80 | BLE deltaBLE; //gill 0904 |
gillwei7 | 4:b52035367aee | 81 | extern const char* cyntecCommandErrorNames[]; |
gillwei7 | 5:ee474e3133eb | 82 | static bool connState = false; // gill 0904, define currently connecting state |
gillwei7 | 4:b52035367aee | 83 | static uint8_t service_count=0; |
gillwei7 | 4:b52035367aee | 84 | static uint8_t char_count=0; |
gillwei7 | 0:5c195ab2f696 | 85 | static uint16_t test_conn_handle; //Connection handle, assign after connected |
gillwei7 | 5:ee474e3133eb | 86 | //extern bool advState; // currently no use |
gillwei7 | 3:38ec8ad317f4 | 87 | static Gap::Address_t saveAddr[BLE_MAX_ADDRESS_NUMBER]; // check in advertisementCallback |
gillwei7 | 3:38ec8ad317f4 | 88 | static uint8_t bleDevInd; |
gillwei7 | 4:b52035367aee | 89 | //static Gap::GapState_t nowState; |
gillwei7 | 4:b52035367aee | 90 | static char targetDevName[TARGET_DEVNAME_LEN]; |
gillwei7 | 5:ee474e3133eb | 91 | static char DEVICE_NAME[] = "nRF5x"; // default device name, same as defined in Gap.h |
gillwei7 | 4:b52035367aee | 92 | static bool conn_action = false; // Gill add 20151015 |
gillwei7 | 4:b52035367aee | 93 | static GattCharacteristic *charAry[CLI_SERVICE_MAX_NUM][CLI_CHAR_MAX_NUM]; |
gillwei7 | 0:5c195ab2f696 | 94 | |
gillwei7 | 0:5c195ab2f696 | 95 | DigitalOut led1(p7); |
gillwei7 | 0:5c195ab2f696 | 96 | DigitalOut led2(p13); |
gillwei7 | 5:ee474e3133eb | 97 | DigitalOut WriteInterrupt(p0); |
gillwei7 | 0:5c195ab2f696 | 98 | |
gillwei7 | 0:5c195ab2f696 | 99 | /****************************************************** |
gillwei7 | 0:5c195ab2f696 | 100 | * Function Definitions |
gillwei7 | 0:5c195ab2f696 | 101 | ******************************************************/ |
gillwei7 | 0:5c195ab2f696 | 102 | |
gillwei7 | 5:ee474e3133eb | 103 | //DiscoveredCharacteristic ledCharacteristic; |
gillwei7 | 0:5c195ab2f696 | 104 | |
gillwei7 | 4:b52035367aee | 105 | void serviceDiscoveryCallback(const DiscoveredService *service) |
gillwei7 | 4:b52035367aee | 106 | { |
gillwei7 | 4:b52035367aee | 107 | console.printf("serviceDiscoveryCallback\r\n"); |
gillwei7 | 4:b52035367aee | 108 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
gillwei7 | 4:b52035367aee | 109 | printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
gillwei7 | 4:b52035367aee | 110 | } else { |
gillwei7 | 4:b52035367aee | 111 | printf("S UUID-"); |
gillwei7 | 4:b52035367aee | 112 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
gillwei7 | 4:b52035367aee | 113 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
gillwei7 | 4:b52035367aee | 114 | printf("%02x", longUUIDBytes[i]); |
gillwei7 | 4:b52035367aee | 115 | } |
gillwei7 | 4:b52035367aee | 116 | printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
gillwei7 | 4:b52035367aee | 117 | } |
gillwei7 | 4:b52035367aee | 118 | } |
gillwei7 | 4:b52035367aee | 119 | |
gillwei7 | 4:b52035367aee | 120 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) |
gillwei7 | 4:b52035367aee | 121 | { |
gillwei7 | 4:b52035367aee | 122 | console.printf("characteristicDiscoveryCallback\r\n"); |
gillwei7 | 4:b52035367aee | 123 | //printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); |
gillwei7 | 4:b52035367aee | 124 | //if (characteristicP->getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */ |
gillwei7 | 5:ee474e3133eb | 125 | //if(1) { |
gillwei7 | 5:ee474e3133eb | 126 | // ledCharacteristic = *characteristicP; |
gillwei7 | 5:ee474e3133eb | 127 | // } |
gillwei7 | 4:b52035367aee | 128 | } |
gillwei7 | 4:b52035367aee | 129 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) |
gillwei7 | 4:b52035367aee | 130 | { |
gillwei7 | 4:b52035367aee | 131 | printf("terminated SD for handle %u\r\n", connectionHandle); |
gillwei7 | 4:b52035367aee | 132 | } |
gillwei7 | 4:b52035367aee | 133 | |
gillwei7 | 0:5c195ab2f696 | 134 | void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params) |
gillwei7 | 0:5c195ab2f696 | 135 | { |
gillwei7 | 0:5c195ab2f696 | 136 | test_conn_handle = params->handle; |
gillwei7 | 0:5c195ab2f696 | 137 | connState = true; |
gillwei7 | 4:b52035367aee | 138 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 139 | console.printf("Connect: connState write to %d\r\n",connState); |
gillwei7 | 4:b52035367aee | 140 | #endif |
gillwei7 | 4:b52035367aee | 141 | |
gillwei7 | 4:b52035367aee | 142 | //led2 = !led2; |
gillwei7 | 4:b52035367aee | 143 | |
gillwei7 | 4:b52035367aee | 144 | // gill test |
gillwei7 | 4:b52035367aee | 145 | //if (params->role == Gap::CENTRAL) { |
gillwei7 | 4:b52035367aee | 146 | // //deltaBLE.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
gillwei7 | 4:b52035367aee | 147 | // deltaBLE.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001); |
gillwei7 | 4:b52035367aee | 148 | // } |
gillwei7 | 0:5c195ab2f696 | 149 | } |
gillwei7 | 0:5c195ab2f696 | 150 | |
gillwei7 | 5:ee474e3133eb | 151 | void onTimeoutCallback(Gap::TimeoutSource_t source) |
gillwei7 | 5:ee474e3133eb | 152 | { |
gillwei7 | 5:ee474e3133eb | 153 | led1 = !led1; |
gillwei7 | 5:ee474e3133eb | 154 | } |
gillwei7 | 5:ee474e3133eb | 155 | |
gillwei7 | 4:b52035367aee | 156 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
gillwei7 | 0:5c195ab2f696 | 157 | { |
gillwei7 | 0:5c195ab2f696 | 158 | //console.printf("connState:%d\r\n",connState); |
gillwei7 | 3:38ec8ad317f4 | 159 | //Gap::GapState_t->connected = 0; |
gillwei7 | 0:5c195ab2f696 | 160 | connState = false; |
gillwei7 | 0:5c195ab2f696 | 161 | } |
gillwei7 | 0:5c195ab2f696 | 162 | |
gillwei7 | 5:ee474e3133eb | 163 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
gillwei7 | 5:ee474e3133eb | 164 | //if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { |
gillwei7 | 5:ee474e3133eb | 165 | // actuatedLED = *(params->data); |
gillwei7 | 5:ee474e3133eb | 166 | // } |
gillwei7 | 5:ee474e3133eb | 167 | #ifdef BLE_DEBUG |
gillwei7 | 5:ee474e3133eb | 168 | console.printf("onDataWritten\r\n"); |
gillwei7 | 5:ee474e3133eb | 169 | #endif |
gillwei7 | 5:ee474e3133eb | 170 | WriteInterrupt = !WriteInterrupt; |
gillwei7 | 5:ee474e3133eb | 171 | } |
gillwei7 | 0:5c195ab2f696 | 172 | |
gillwei7 | 0:5c195ab2f696 | 173 | static void cyntecPrintOk(void) |
gillwei7 | 0:5c195ab2f696 | 174 | { |
gillwei7 | 3:38ec8ad317f4 | 175 | console.printf("\r\nOK\r\n\r\n"); |
gillwei7 | 0:5c195ab2f696 | 176 | } |
gillwei7 | 0:5c195ab2f696 | 177 | |
gillwei7 | 0:5c195ab2f696 | 178 | static void cyntecPrintError(uint8_t errIdx) |
gillwei7 | 0:5c195ab2f696 | 179 | { |
gillwei7 | 3:38ec8ad317f4 | 180 | console.printf("\r\nERROR;"); |
gillwei7 | 3:38ec8ad317f4 | 181 | console.printf(cyntecCommandErrorNames[errIdx]); |
gillwei7 | 3:38ec8ad317f4 | 182 | console.printf("\r\n\r\n"); |
gillwei7 | 3:38ec8ad317f4 | 183 | } |
gillwei7 | 3:38ec8ad317f4 | 184 | |
gillwei7 | 0:5c195ab2f696 | 185 | static void cynAdvertiseStartCommand(void) |
gillwei7 | 0:5c195ab2f696 | 186 | { |
gillwei7 | 3:38ec8ad317f4 | 187 | uint8_t argLen = 0; |
gillwei7 | 3:38ec8ad317f4 | 188 | uint8_t *arg; |
gillwei7 | 3:38ec8ad317f4 | 189 | uint16_t advInterval; |
gillwei7 | 3:38ec8ad317f4 | 190 | uint16_t advTimeout; |
gillwei7 | 3:38ec8ad317f4 | 191 | |
gillwei7 | 3:38ec8ad317f4 | 192 | switch (cyntecGetCommandTokenCnt()) { |
gillwei7 | 3:38ec8ad317f4 | 193 | case 2: |
gillwei7 | 3:38ec8ad317f4 | 194 | advInterval = APP_ADV_INTERVAL; |
gillwei7 | 3:38ec8ad317f4 | 195 | advTimeout = APP_ADV_TIMEOUT_IN_SECONDS; |
gillwei7 | 3:38ec8ad317f4 | 196 | break; |
gillwei7 | 3:38ec8ad317f4 | 197 | case 3: |
gillwei7 | 3:38ec8ad317f4 | 198 | /* arg1 is adv interval parameter */ |
gillwei7 | 4:b52035367aee | 199 | arg = cyntecGetCommandArgument(0,&argLen); |
gillwei7 | 3:38ec8ad317f4 | 200 | advInterval = cyntecAtoiUint16( arg, argLen ); |
gillwei7 | 3:38ec8ad317f4 | 201 | advTimeout = APP_ADV_TIMEOUT_IN_SECONDS; |
gillwei7 | 3:38ec8ad317f4 | 202 | break; |
gillwei7 | 3:38ec8ad317f4 | 203 | case 4: |
gillwei7 | 4:b52035367aee | 204 | arg = cyntecGetCommandArgument(0,&argLen); |
gillwei7 | 4:b52035367aee | 205 | advInterval = cyntecAtoiUint16( arg, argLen ); |
gillwei7 | 3:38ec8ad317f4 | 206 | arg = cyntecGetCommandArgument(1,&argLen); |
gillwei7 | 3:38ec8ad317f4 | 207 | advTimeout = cyntecAtoiUint16( arg, argLen ); |
gillwei7 | 3:38ec8ad317f4 | 208 | break; |
gillwei7 | 3:38ec8ad317f4 | 209 | default: |
gillwei7 | 3:38ec8ad317f4 | 210 | advInterval = APP_ADV_INTERVAL; |
gillwei7 | 3:38ec8ad317f4 | 211 | advTimeout = APP_ADV_TIMEOUT_IN_SECONDS; |
gillwei7 | 3:38ec8ad317f4 | 212 | break; |
gillwei7 | 3:38ec8ad317f4 | 213 | } |
gillwei7 | 3:38ec8ad317f4 | 214 | |
gillwei7 | 3:38ec8ad317f4 | 215 | //if ( advInterval< APP_ADV_INTERVAL_MIN | advInterval> APP_ADV_INTERVAL_MAX | advTimeout < 1| advTimeout > BLE_GAP_ADV_TIMEOUT_LIMITED_MAX ) { |
gillwei7 | 3:38ec8ad317f4 | 216 | // cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); |
gillwei7 | 3:38ec8ad317f4 | 217 | // return; |
gillwei7 | 3:38ec8ad317f4 | 218 | // } |
gillwei7 | 4:b52035367aee | 219 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 220 | console.printf("advInterval:%d,advTimeout:%d",advInterval,advTimeout); |
gillwei7 | 4:b52035367aee | 221 | #endif |
gillwei7 | 4:b52035367aee | 222 | //uint16_t uuid16_list[] = {0x0001}; |
gillwei7 | 4:b52035367aee | 223 | deltaBLE.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
gillwei7 | 4:b52035367aee | 224 | //deltaBLE.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
gillwei7 | 4:b52035367aee | 225 | deltaBLE.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
gillwei7 | 4:b52035367aee | 226 | deltaBLE.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
gillwei7 | 4:b52035367aee | 227 | deltaBLE.gap().setAdvertisingInterval(advInterval); /* minisecond. */ |
gillwei7 | 4:b52035367aee | 228 | deltaBLE.gap().setAdvertisingTimeout(advTimeout); /* second. */ |
gillwei7 | 4:b52035367aee | 229 | deltaBLE.gap().startAdvertising(); |
gillwei7 | 4:b52035367aee | 230 | cyntecPrintOk(); |
gillwei7 | 4:b52035367aee | 231 | nrf_gpio_pin_set(ADVERTISING_LED_PIN_NO); |
gillwei7 | 0:5c195ab2f696 | 232 | } |
gillwei7 | 0:5c195ab2f696 | 233 | |
gillwei7 | 0:5c195ab2f696 | 234 | /**@brief Function for stop advertising. |
gillwei7 | 0:5c195ab2f696 | 235 | */ |
gillwei7 | 0:5c195ab2f696 | 236 | static void cynAdvertiseStopCommand(void) |
gillwei7 | 0:5c195ab2f696 | 237 | { |
gillwei7 | 4:b52035367aee | 238 | deltaBLE.gap().stopAdvertising(); |
gillwei7 | 4:b52035367aee | 239 | cyntecPrintOk(); |
gillwei7 | 4:b52035367aee | 240 | nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO); |
gillwei7 | 0:5c195ab2f696 | 241 | } |
gillwei7 | 0:5c195ab2f696 | 242 | |
gillwei7 | 0:5c195ab2f696 | 243 | //gill modify 20150904 for accept blank in name |
gillwei7 | 0:5c195ab2f696 | 244 | static void cynBLENameCommand(void) |
gillwei7 | 0:5c195ab2f696 | 245 | { |
gillwei7 | 3:38ec8ad317f4 | 246 | if (cyntecGetCommandTokenCnt() >= 3) { |
gillwei7 | 3:38ec8ad317f4 | 247 | uint8_t tempNameLen = 0; |
gillwei7 | 3:38ec8ad317f4 | 248 | // check first and last char is " |
gillwei7 | 3:38ec8ad317f4 | 249 | uint8_t *argName1 = cyntecGetCommandArgument(0, &tempNameLen); |
gillwei7 | 3:38ec8ad317f4 | 250 | uint8_t *argNameLast = cyntecGetCommandArgument(cyntecGetCommandTokenCnt() - 3, &tempNameLen); |
gillwei7 | 3:38ec8ad317f4 | 251 | char bracket = '*'; |
gillwei7 | 3:38ec8ad317f4 | 252 | //console.printf("%02X\r\n",tempNameLen); |
gillwei7 | 3:38ec8ad317f4 | 253 | if (!memcmp(&argName1[0],&bracket,sizeof(char)) && !memcmp(&argNameLast[tempNameLen - 1],&bracket,sizeof(char))) |
gillwei7 | 3:38ec8ad317f4 | 254 | //if (!memcmp(&argName1[0],&bracket,sizeof(char))) |
gillwei7 | 3:38ec8ad317f4 | 255 | { |
gillwei7 | 3:38ec8ad317f4 | 256 | uint8_t nameLen = 0; |
gillwei7 | 3:38ec8ad317f4 | 257 | uint8_t *argName = cyntecGetCommandArgument(0, &nameLen); |
gillwei7 | 3:38ec8ad317f4 | 258 | uint8_t * argName2 = cyntecGetCommandTotalBuffer(); |
gillwei7 | 3:38ec8ad317f4 | 259 | uint32_t err_code; |
gillwei7 | 3:38ec8ad317f4 | 260 | ble_gap_conn_sec_mode_t sec_mode; |
gillwei7 | 3:38ec8ad317f4 | 261 | //console.printf("%02X\r\n",cyntecGetTotalIndex()); |
gillwei7 | 5:ee474e3133eb | 262 | //console.printf("\r\nSet BLE Name: "); |
gillwei7 | 3:38ec8ad317f4 | 263 | uint8_t i = 0; |
gillwei7 | 5:ee474e3133eb | 264 | |
gillwei7 | 3:38ec8ad317f4 | 265 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); |
gillwei7 | 3:38ec8ad317f4 | 266 | err_code = sd_ble_gap_device_name_set(&sec_mode, |
gillwei7 | 3:38ec8ad317f4 | 267 | (const uint8_t *) &argName2[11], |
gillwei7 | 3:38ec8ad317f4 | 268 | (uint16_t) (cyntecGetTotalIndex()-12)); |
gillwei7 | 3:38ec8ad317f4 | 269 | if (err_code == NRF_SUCCESS) { |
gillwei7 | 5:ee474e3133eb | 270 | console.printf("\r\nOK;"); |
gillwei7 | 5:ee474e3133eb | 271 | for(i = 0; i < cyntecGetTotalIndex()-12; i++) { |
gillwei7 | 5:ee474e3133eb | 272 | console.printf("%c",argName2[11+i]); |
gillwei7 | 5:ee474e3133eb | 273 | } |
gillwei7 | 5:ee474e3133eb | 274 | console.printf("\r\n"); |
gillwei7 | 3:38ec8ad317f4 | 275 | } else { |
gillwei7 | 3:38ec8ad317f4 | 276 | console.printf("ERROR;%04X\r\n",err_code); |
gillwei7 | 3:38ec8ad317f4 | 277 | } |
gillwei7 | 3:38ec8ad317f4 | 278 | APP_ERROR_CHECK(err_code); |
gillwei7 | 5:ee474e3133eb | 279 | //#ifdef BLE_DEBUG |
gillwei7 | 3:38ec8ad317f4 | 280 | //console.printf("\r\nSet BLE Name: "); |
gillwei7 | 0:5c195ab2f696 | 281 | // uint8_t i = 0; |
gillwei7 | 0:5c195ab2f696 | 282 | // for(i = 0; i < nameLen; i++) |
gillwei7 | 0:5c195ab2f696 | 283 | // { |
gillwei7 | 0:5c195ab2f696 | 284 | // console.printf("%c",argName[i]); |
gillwei7 | 0:5c195ab2f696 | 285 | // } |
gillwei7 | 0:5c195ab2f696 | 286 | //#endif |
gillwei7 | 3:38ec8ad317f4 | 287 | } else { |
gillwei7 | 3:38ec8ad317f4 | 288 | cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_SYNTAX_ERROR); |
gillwei7 | 3:38ec8ad317f4 | 289 | } |
gillwei7 | 3:38ec8ad317f4 | 290 | } else if (cyntecGetCommandTokenCnt() == 2) { |
gillwei7 | 3:38ec8ad317f4 | 291 | uint8_t bleName[BLE_GAP_DEVNAME_MAX_LEN] = {"\0"}; |
gillwei7 | 3:38ec8ad317f4 | 292 | uint16_t bleLen = BLE_GAP_DEVNAME_MAX_LEN - APP_ADV_DATA_OFFSET; |
gillwei7 | 3:38ec8ad317f4 | 293 | uint32_t err_code; |
gillwei7 | 3:38ec8ad317f4 | 294 | |
gillwei7 | 3:38ec8ad317f4 | 295 | err_code = sd_ble_gap_device_name_get(&bleName[APP_ADV_DATA_OFFSET], &bleLen); |
gillwei7 | 3:38ec8ad317f4 | 296 | APP_ERROR_CHECK(err_code); |
gillwei7 | 3:38ec8ad317f4 | 297 | |
gillwei7 | 3:38ec8ad317f4 | 298 | console.printf("\r\nOK;"); |
gillwei7 | 3:38ec8ad317f4 | 299 | console.printf("%s",bleName+APP_ADV_DATA_OFFSET); |
gillwei7 | 3:38ec8ad317f4 | 300 | console.printf(";\r\n"); |
gillwei7 | 3:38ec8ad317f4 | 301 | } else { |
gillwei7 | 3:38ec8ad317f4 | 302 | cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); |
gillwei7 | 3:38ec8ad317f4 | 303 | return; |
gillwei7 | 3:38ec8ad317f4 | 304 | } |
gillwei7 | 0:5c195ab2f696 | 305 | } |
gillwei7 | 3:38ec8ad317f4 | 306 | |
gillwei7 | 0:5c195ab2f696 | 307 | static void cynBLEInfoCommand(void) |
gillwei7 | 0:5c195ab2f696 | 308 | { |
gillwei7 | 5:ee474e3133eb | 309 | console.printf("\r\nOK;"); |
gillwei7 | 3:38ec8ad317f4 | 310 | console.printf(CLI_FWVERION); |
gillwei7 | 5:ee474e3133eb | 311 | console.printf(";"); |
gillwei7 | 3:38ec8ad317f4 | 312 | console.printf(MODULE_NAME); |
gillwei7 | 3:38ec8ad317f4 | 313 | console.printf("\r\n\r\n"); |
gillwei7 | 0:5c195ab2f696 | 314 | } |
gillwei7 | 0:5c195ab2f696 | 315 | |
gillwei7 | 0:5c195ab2f696 | 316 | /**@brief Set the radio's transmit power. |
gillwei7 | 0:5c195ab2f696 | 317 | * |
gillwei7 | 0:5c195ab2f696 | 318 | * @param[in] tx_power Radio transmit power in dBm (accepted values are -40, -30, -20, -16, -12, -8, -4, 0, and 4 dBm). |
gillwei7 | 0:5c195ab2f696 | 319 | * |
gillwei7 | 0:5c195ab2f696 | 320 | * @note -40 dBm will not actually give -40 dBm, but will instead be remapped to -30 dBm. |
gillwei7 | 0:5c195ab2f696 | 321 | */ |
gillwei7 | 0:5c195ab2f696 | 322 | static void cynBLESetTxPowerCommand (void) |
gillwei7 | 0:5c195ab2f696 | 323 | { |
gillwei7 | 4:b52035367aee | 324 | if (cyntecGetCommandTokenCnt() != 3) { |
gillwei7 | 3:38ec8ad317f4 | 325 | cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); |
gillwei7 | 3:38ec8ad317f4 | 326 | return; |
gillwei7 | 3:38ec8ad317f4 | 327 | } |
gillwei7 | 4:b52035367aee | 328 | uint32_t err_code; |
gillwei7 | 4:b52035367aee | 329 | uint8_t txLen=0; |
gillwei7 | 4:b52035367aee | 330 | uint8_t *arg = cyntecGetCommandArgument(0,&txLen); |
gillwei7 | 4:b52035367aee | 331 | const uint8_t NUM_TXPOW = 9; |
gillwei7 | 4:b52035367aee | 332 | bool inputSwitch = false; // if find matched case in TxPow, assign true |
gillwei7 | 4:b52035367aee | 333 | |
gillwei7 | 4:b52035367aee | 334 | int8_t validTxPow[NUM_TXPOW] = {-40, -30, -20, -16, -12, -8, -4, 0, 4}; |
gillwei7 | 4:b52035367aee | 335 | uint8_t i; |
gillwei7 | 4:b52035367aee | 336 | int8_t setValue; |
gillwei7 | 4:b52035367aee | 337 | |
gillwei7 | 4:b52035367aee | 338 | if ( arg != NULL ) { |
gillwei7 | 4:b52035367aee | 339 | setValue = atoi((const char *)arg); |
gillwei7 | 4:b52035367aee | 340 | for (i = 0; i < NUM_TXPOW; i++) { |
gillwei7 | 4:b52035367aee | 341 | if (setValue == validTxPow[i]) { |
gillwei7 | 4:b52035367aee | 342 | err_code = sd_ble_gap_tx_power_set(setValue); |
gillwei7 | 4:b52035367aee | 343 | APP_ERROR_CHECK(err_code); |
gillwei7 | 4:b52035367aee | 344 | inputSwitch = true; |
gillwei7 | 4:b52035367aee | 345 | break; |
gillwei7 | 4:b52035367aee | 346 | } |
gillwei7 | 4:b52035367aee | 347 | } |
gillwei7 | 4:b52035367aee | 348 | |
gillwei7 | 4:b52035367aee | 349 | if ( inputSwitch ) |
gillwei7 | 4:b52035367aee | 350 | cyntecPrintOk(); |
gillwei7 | 4:b52035367aee | 351 | else |
gillwei7 | 4:b52035367aee | 352 | cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); |
gillwei7 | 4:b52035367aee | 353 | |
gillwei7 | 4:b52035367aee | 354 | } |
gillwei7 | 4:b52035367aee | 355 | clearBuffer(); // clear the commandState.buffer |
gillwei7 | 4:b52035367aee | 356 | return; |
gillwei7 | 0:5c195ab2f696 | 357 | } |
gillwei7 | 0:5c195ab2f696 | 358 | |
gillwei7 | 0:5c195ab2f696 | 359 | static void cynBLEAddressCommand(void) |
gillwei7 | 0:5c195ab2f696 | 360 | { |
gillwei7 | 3:38ec8ad317f4 | 361 | if (cyntecGetCommandTokenCnt() == 3) { |
gillwei7 | 3:38ec8ad317f4 | 362 | uint8_t argLen = 0; |
gillwei7 | 3:38ec8ad317f4 | 363 | uint8_t *arg = cyntecGetCommandArgument(0, &argLen); |
gillwei7 | 3:38ec8ad317f4 | 364 | uint32_t err_code; |
gillwei7 | 3:38ec8ad317f4 | 365 | uint8_t addr,i; |
gillwei7 | 3:38ec8ad317f4 | 366 | |
gillwei7 | 3:38ec8ad317f4 | 367 | /* should with "0x" + 12 len addr */ |
gillwei7 | 3:38ec8ad317f4 | 368 | if (argLen == 14) { |
gillwei7 | 3:38ec8ad317f4 | 369 | if (arg[0] != '0' || arg[1] != 'x') { |
gillwei7 | 3:38ec8ad317f4 | 370 | cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_SYNTAX_ERROR); |
gillwei7 | 3:38ec8ad317f4 | 371 | return; |
gillwei7 | 3:38ec8ad317f4 | 372 | } |
gillwei7 | 0:5c195ab2f696 | 373 | |
gillwei7 | 3:38ec8ad317f4 | 374 | m_peer_addr.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC; |
gillwei7 | 3:38ec8ad317f4 | 375 | |
gillwei7 | 3:38ec8ad317f4 | 376 | for ( i = 1 ; i < 7 ; i++) { |
gillwei7 | 3:38ec8ad317f4 | 377 | addr = cyntecArgToUint8(arg+2*i, 2); |
gillwei7 | 3:38ec8ad317f4 | 378 | /* 5 - (i-1) */ |
gillwei7 | 3:38ec8ad317f4 | 379 | m_peer_addr.addr[6-i] = addr; |
gillwei7 | 3:38ec8ad317f4 | 380 | } |
gillwei7 | 3:38ec8ad317f4 | 381 | |
gillwei7 | 3:38ec8ad317f4 | 382 | err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &m_peer_addr); |
gillwei7 | 3:38ec8ad317f4 | 383 | APP_ERROR_CHECK(err_code); |
gillwei7 | 3:38ec8ad317f4 | 384 | |
gillwei7 | 3:38ec8ad317f4 | 385 | } else { //argLen != 14 |
gillwei7 | 3:38ec8ad317f4 | 386 | cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_SYNTAX_ERROR); |
gillwei7 | 3:38ec8ad317f4 | 387 | return; |
gillwei7 | 3:38ec8ad317f4 | 388 | } |
gillwei7 | 0:5c195ab2f696 | 389 | |
gillwei7 | 5:ee474e3133eb | 390 | //#ifdef BLE_DEBUG |
gillwei7 | 0:5c195ab2f696 | 391 | // uint8_t buf2[20]; |
gillwei7 | 0:5c195ab2f696 | 392 | // console.printf("\r\nSet BLE Address: "); |
gillwei7 | 3:38ec8ad317f4 | 393 | // sprintf((char *)&buf2[0],(const char *)"[%02X %02X %02X %02X %02X %02X]", |
gillwei7 | 3:38ec8ad317f4 | 394 | // m_peer_addr.addr[0], m_peer_addr.addr[1], m_peer_addr.addr[2], |
gillwei7 | 3:38ec8ad317f4 | 395 | // m_peer_addr.addr[3], m_peer_addr.addr[4], m_peer_addr.addr[5]); |
gillwei7 | 0:5c195ab2f696 | 396 | // console.printf(buf2); |
gillwei7 | 3:38ec8ad317f4 | 397 | //#endif |
gillwei7 | 3:38ec8ad317f4 | 398 | |
gillwei7 | 3:38ec8ad317f4 | 399 | cyntecPrintOk(); |
gillwei7 | 3:38ec8ad317f4 | 400 | } else if (cyntecGetCommandTokenCnt() == 2) { |
gillwei7 | 0:5c195ab2f696 | 401 | |
gillwei7 | 3:38ec8ad317f4 | 402 | uint32_t err_code; |
gillwei7 | 3:38ec8ad317f4 | 403 | err_code = sd_ble_gap_address_get(&m_peer_addr); |
gillwei7 | 3:38ec8ad317f4 | 404 | APP_ERROR_CHECK(err_code); |
gillwei7 | 3:38ec8ad317f4 | 405 | |
gillwei7 | 3:38ec8ad317f4 | 406 | cyntecPrintOk(); |
gillwei7 | 3:38ec8ad317f4 | 407 | |
gillwei7 | 3:38ec8ad317f4 | 408 | console.printf("[%02X %02X %02X %02X %02X %02X]", |
gillwei7 | 3:38ec8ad317f4 | 409 | m_peer_addr.addr[5], m_peer_addr.addr[4], m_peer_addr.addr[3], |
gillwei7 | 3:38ec8ad317f4 | 410 | m_peer_addr.addr[2], m_peer_addr.addr[1], m_peer_addr.addr[0]); |
gillwei7 | 3:38ec8ad317f4 | 411 | } else { //cyntecGetCommandTokenCnt() not equal to 2 or 3 |
gillwei7 | 3:38ec8ad317f4 | 412 | cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); |
gillwei7 | 3:38ec8ad317f4 | 413 | } |
gillwei7 | 3:38ec8ad317f4 | 414 | return; |
gillwei7 | 0:5c195ab2f696 | 415 | } |
gillwei7 | 0:5c195ab2f696 | 416 | |
gillwei7 | 4:b52035367aee | 417 | static uint32_t adv_report_parse(uint8_t type, data_t * p_advdata, data_t * p_typedata) |
gillwei7 | 4:b52035367aee | 418 | { |
gillwei7 | 4:b52035367aee | 419 | uint32_t index = 0; |
gillwei7 | 4:b52035367aee | 420 | const uint8_t * p_data; |
gillwei7 | 4:b52035367aee | 421 | |
gillwei7 | 4:b52035367aee | 422 | p_data = p_advdata->p_data; |
gillwei7 | 4:b52035367aee | 423 | |
gillwei7 | 4:b52035367aee | 424 | while (index < p_advdata->data_len) { |
gillwei7 | 4:b52035367aee | 425 | uint8_t field_length = p_data[index]; |
gillwei7 | 4:b52035367aee | 426 | uint8_t field_type = p_data[index+1]; |
gillwei7 | 4:b52035367aee | 427 | |
gillwei7 | 4:b52035367aee | 428 | if (field_type == type) { |
gillwei7 | 4:b52035367aee | 429 | p_typedata->p_data = &p_data[index+2]; |
gillwei7 | 4:b52035367aee | 430 | p_typedata->data_len = field_length-1; |
gillwei7 | 4:b52035367aee | 431 | return NRF_SUCCESS; |
gillwei7 | 4:b52035367aee | 432 | } |
gillwei7 | 4:b52035367aee | 433 | index += field_length+1; |
gillwei7 | 4:b52035367aee | 434 | } |
gillwei7 | 4:b52035367aee | 435 | return NRF_ERROR_NOT_FOUND; |
gillwei7 | 4:b52035367aee | 436 | } |
gillwei7 | 4:b52035367aee | 437 | |
gillwei7 | 4:b52035367aee | 438 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) |
gillwei7 | 4:b52035367aee | 439 | { |
gillwei7 | 4:b52035367aee | 440 | bool flagRepeat=false; |
gillwei7 | 4:b52035367aee | 441 | Gap::Address_t newAddr[6]; |
gillwei7 | 4:b52035367aee | 442 | memcpy(newAddr,params->peerAddr,6); |
gillwei7 | 4:b52035367aee | 443 | |
gillwei7 | 4:b52035367aee | 444 | for(int i=0; i<BLE_MAX_ADDRESS_NUMBER; i++) { |
gillwei7 | 4:b52035367aee | 445 | if (memcmp(newAddr,saveAddr[i],6)==0) { |
gillwei7 | 4:b52035367aee | 446 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 447 | console.printf("Repeated\r\n"); |
gillwei7 | 4:b52035367aee | 448 | #endif |
gillwei7 | 4:b52035367aee | 449 | flagRepeat = true; |
gillwei7 | 4:b52035367aee | 450 | //return; |
gillwei7 | 4:b52035367aee | 451 | } |
gillwei7 | 4:b52035367aee | 452 | } |
gillwei7 | 4:b52035367aee | 453 | |
gillwei7 | 4:b52035367aee | 454 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 455 | console.printf("addr cmp result :%i\r\n",memcmp(newAddr,params->peerAddr,6)); |
gillwei7 | 4:b52035367aee | 456 | console.printf("ADV data:%X,%i\r\n",&(params->advertisingData),params->advertisingDataLen); |
gillwei7 | 4:b52035367aee | 457 | #endif |
gillwei7 | 4:b52035367aee | 458 | data_t adv_data; |
gillwei7 | 4:b52035367aee | 459 | data_t type_data; |
gillwei7 | 4:b52035367aee | 460 | //Initialize advertisement report for parsing. |
gillwei7 | 4:b52035367aee | 461 | adv_data.p_data = params->advertisingData; |
gillwei7 | 4:b52035367aee | 462 | adv_data.data_len = params->advertisingDataLen; |
gillwei7 | 4:b52035367aee | 463 | // Parsing Device Name |
gillwei7 | 4:b52035367aee | 464 | uint32_t err_code = adv_report_parse(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, |
gillwei7 | 4:b52035367aee | 465 | &adv_data, |
gillwei7 | 4:b52035367aee | 466 | &type_data); |
gillwei7 | 4:b52035367aee | 467 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 468 | console.printf("error code:%X\r\n",err_code); |
gillwei7 | 4:b52035367aee | 469 | console.printf("type_data.data_len:%i\r\n",type_data.data_len); |
gillwei7 | 4:b52035367aee | 470 | #endif |
gillwei7 | 4:b52035367aee | 471 | if (flagRepeat == false) { |
gillwei7 | 4:b52035367aee | 472 | if (err_code == 0) { |
gillwei7 | 4:b52035367aee | 473 | for (int i=0; i<type_data.data_len; i++) { |
gillwei7 | 4:b52035367aee | 474 | console.printf("%c",type_data.p_data[i]); |
gillwei7 | 4:b52035367aee | 475 | } |
gillwei7 | 4:b52035367aee | 476 | //console.printf("\r\n"); |
gillwei7 | 4:b52035367aee | 477 | } |
gillwei7 | 4:b52035367aee | 478 | console.printf(",ADV,[%02X %02X %02X %02X %02X %02X],%d,%u\r\n",params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], |
gillwei7 | 4:b52035367aee | 479 | params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],params->rssi,params->type); |
gillwei7 | 4:b52035367aee | 480 | memcpy(saveAddr[bleDevInd],params->peerAddr,6); |
gillwei7 | 4:b52035367aee | 481 | bleDevInd++; |
gillwei7 | 4:b52035367aee | 482 | } |
gillwei7 | 4:b52035367aee | 483 | |
gillwei7 | 4:b52035367aee | 484 | /* |
gillwei7 | 4:b52035367aee | 485 | Check short name, not implemented |
gillwei7 | 4:b52035367aee | 486 | */ |
gillwei7 | 4:b52035367aee | 487 | //else |
gillwei7 | 4:b52035367aee | 488 | // { |
gillwei7 | 4:b52035367aee | 489 | // uint32_t err_code_2 = adv_report_parse(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, |
gillwei7 | 4:b52035367aee | 490 | // &adv_data, |
gillwei7 | 4:b52035367aee | 491 | // &type_data); |
gillwei7 | 4:b52035367aee | 492 | // console.printf("error code:%X\r\n",err_code_2); |
gillwei7 | 4:b52035367aee | 493 | // console.printf("%i\r\n",type_data.data_len); |
gillwei7 | 4:b52035367aee | 494 | // console.printf("%s\r\n",type_data.p_data); |
gillwei7 | 4:b52035367aee | 495 | // |
gillwei7 | 4:b52035367aee | 496 | // } |
gillwei7 | 4:b52035367aee | 497 | if (conn_action == true) { |
gillwei7 | 4:b52035367aee | 498 | conn_action = false; |
gillwei7 | 4:b52035367aee | 499 | deltaBLE.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
gillwei7 | 4:b52035367aee | 500 | } |
gillwei7 | 4:b52035367aee | 501 | } |
gillwei7 | 4:b52035367aee | 502 | |
gillwei7 | 3:38ec8ad317f4 | 503 | |
gillwei7 | 3:38ec8ad317f4 | 504 | /**@brief Function to start scanning. |
gillwei7 | 0:5c195ab2f696 | 505 | */ |
gillwei7 | 0:5c195ab2f696 | 506 | static void scan_start(void) |
gillwei7 | 0:5c195ab2f696 | 507 | { |
gillwei7 | 3:38ec8ad317f4 | 508 | deltaBLE.gap().startScan(advertisementCallback); |
gillwei7 | 3:38ec8ad317f4 | 509 | //APP_ERROR_CHECK(err_code); |
gillwei7 | 0:5c195ab2f696 | 510 | } |
gillwei7 | 0:5c195ab2f696 | 511 | |
gillwei7 | 0:5c195ab2f696 | 512 | static void scan_stop(void) |
gillwei7 | 0:5c195ab2f696 | 513 | { |
gillwei7 | 3:38ec8ad317f4 | 514 | deltaBLE.stopScan(); |
gillwei7 | 3:38ec8ad317f4 | 515 | //APP_ERROR_CHECK(err_code); |
gillwei7 | 0:5c195ab2f696 | 516 | } |
gillwei7 | 0:5c195ab2f696 | 517 | |
gillwei7 | 3:38ec8ad317f4 | 518 | // gill 20150916 modify |
gillwei7 | 0:5c195ab2f696 | 519 | static void cynBLEScanCommand(void) |
gillwei7 | 0:5c195ab2f696 | 520 | { |
gillwei7 | 3:38ec8ad317f4 | 521 | uint16_t setInterval = DEFAULT_SCAN_INTERVAL; |
gillwei7 | 3:38ec8ad317f4 | 522 | uint16_t setWindow = DEFAULT_SCAN_WINDOW; |
gillwei7 | 3:38ec8ad317f4 | 523 | uint16_t setTimeout = DEFAULT_SCAN_TIMEOUT; |
gillwei7 | 4:b52035367aee | 524 | if (cyntecGetCommandTokenCnt()!= 2 & cyntecGetCommandTokenCnt()!= 5) { |
gillwei7 | 4:b52035367aee | 525 | cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); |
gillwei7 | 4:b52035367aee | 526 | return; |
gillwei7 | 4:b52035367aee | 527 | } |
gillwei7 | 5:ee474e3133eb | 528 | // If user input scan parameters, overwrite the default value |
gillwei7 | 4:b52035367aee | 529 | if (cyntecGetCommandTokenCnt()== 5) { |
gillwei7 | 4:b52035367aee | 530 | uint8_t argLen = 0; |
gillwei7 | 4:b52035367aee | 531 | uint8_t *arg = cyntecGetCommandArgument(0,&argLen); |
gillwei7 | 4:b52035367aee | 532 | setInterval = cyntecAtoiUint16( arg, argLen ); |
gillwei7 | 4:b52035367aee | 533 | arg = cyntecGetCommandArgument(1,&argLen); |
gillwei7 | 4:b52035367aee | 534 | setWindow = cyntecAtoiUint16( arg, argLen ); |
gillwei7 | 4:b52035367aee | 535 | arg = cyntecGetCommandArgument(2,&argLen); |
gillwei7 | 4:b52035367aee | 536 | setTimeout = cyntecAtoiUint16( arg, argLen ); |
gillwei7 | 4:b52035367aee | 537 | } |
gillwei7 | 4:b52035367aee | 538 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 539 | console.printf("Interval:%d,Window:%d,timeout:%d\r\n",setInterval,setWindow,setTimeout); |
gillwei7 | 3:38ec8ad317f4 | 540 | #endif |
gillwei7 | 4:b52035367aee | 541 | memset(saveAddr,0,sizeof(saveAddr)); // Clear saving address set |
gillwei7 | 4:b52035367aee | 542 | //deltaBLE.gap().setScanParams(setInterval,setWindow,setTimeout,false); |
gillwei7 | 4:b52035367aee | 543 | deltaBLE.gap().setScanInterval(setInterval); |
gillwei7 | 4:b52035367aee | 544 | deltaBLE.gap().setScanWindow(setWindow); |
gillwei7 | 4:b52035367aee | 545 | deltaBLE.gap().setScanTimeout(setTimeout); |
gillwei7 | 5:ee474e3133eb | 546 | console.printf("Start Scan\r\n"); |
gillwei7 | 3:38ec8ad317f4 | 547 | scan_start(); |
gillwei7 | 4:b52035367aee | 548 | |
gillwei7 | 3:38ec8ad317f4 | 549 | } |
gillwei7 | 3:38ec8ad317f4 | 550 | |
gillwei7 | 3:38ec8ad317f4 | 551 | static void cynBLEScanStopCommand(void) |
gillwei7 | 3:38ec8ad317f4 | 552 | { |
gillwei7 | 4:b52035367aee | 553 | console.printf("\r\nStop Scanning\r\n"); |
gillwei7 | 3:38ec8ad317f4 | 554 | scan_stop(); |
gillwei7 | 0:5c195ab2f696 | 555 | } |
gillwei7 | 0:5c195ab2f696 | 556 | |
gillwei7 | 4:b52035367aee | 557 | static void cynBLEConnectCommand(void) |
gillwei7 | 4:b52035367aee | 558 | { |
gillwei7 | 4:b52035367aee | 559 | if (cyntecGetCommandTokenCnt() != 3) { |
gillwei7 | 4:b52035367aee | 560 | cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); |
gillwei7 | 4:b52035367aee | 561 | return; |
gillwei7 | 4:b52035367aee | 562 | } |
gillwei7 | 4:b52035367aee | 563 | |
gillwei7 | 4:b52035367aee | 564 | uint8_t devNameLen = 0; |
gillwei7 | 4:b52035367aee | 565 | uint8_t *argDevName = cyntecGetCommandArgument(0, &devNameLen); |
gillwei7 | 4:b52035367aee | 566 | |
gillwei7 | 4:b52035367aee | 567 | //if ( argDevName != NULL ) { |
gillwei7 | 3:38ec8ad317f4 | 568 | // uint8_t i; |
gillwei7 | 3:38ec8ad317f4 | 569 | // for (i = 0; i < devNameLen; i++) { |
gillwei7 | 3:38ec8ad317f4 | 570 | // targetDevName[i] = argDevName[i]; |
gillwei7 | 3:38ec8ad317f4 | 571 | // } |
gillwei7 | 3:38ec8ad317f4 | 572 | // if (i < devNameLen) |
gillwei7 | 3:38ec8ad317f4 | 573 | // targetDevName[i] = '\0'; |
gillwei7 | 3:38ec8ad317f4 | 574 | // } |
gillwei7 | 4:b52035367aee | 575 | |
gillwei7 | 4:b52035367aee | 576 | memset( targetDevName , 0, TARGET_DEVNAME_LEN); |
gillwei7 | 4:b52035367aee | 577 | memcpy( targetDevName, argDevName, devNameLen); |
gillwei7 | 4:b52035367aee | 578 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 579 | console.printf("Search for device name:%s\r\n",argDevName); |
gillwei7 | 4:b52035367aee | 580 | console.printf("Target:%s\r\n",targetDevName); |
gillwei7 | 4:b52035367aee | 581 | #endif |
gillwei7 | 4:b52035367aee | 582 | conn_action = true; |
gillwei7 | 4:b52035367aee | 583 | scan_start(); |
gillwei7 | 4:b52035367aee | 584 | |
gillwei7 | 4:b52035367aee | 585 | } |
gillwei7 | 3:38ec8ad317f4 | 586 | |
gillwei7 | 0:5c195ab2f696 | 587 | |
gillwei7 | 0:5c195ab2f696 | 588 | static void cynBLEDisconnectCommand(void) |
gillwei7 | 0:5c195ab2f696 | 589 | { |
gillwei7 | 4:b52035367aee | 590 | ble_error_t err_code; |
gillwei7 | 4:b52035367aee | 591 | err_code = deltaBLE.gap().disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION); |
gillwei7 | 4:b52035367aee | 592 | //err_code = sd_ble_gap_disconnect(test_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); |
gillwei7 | 4:b52035367aee | 593 | //err_code = sd_ble_gap_connect_cancel(); // No function defined currently |
gillwei7 | 4:b52035367aee | 594 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 595 | console.printf("Error:%d\r\n",err_code); |
gillwei7 | 4:b52035367aee | 596 | #endif |
gillwei7 | 4:b52035367aee | 597 | cyntecPrintOk(); |
gillwei7 | 0:5c195ab2f696 | 598 | } |
gillwei7 | 0:5c195ab2f696 | 599 | |
gillwei7 | 3:38ec8ad317f4 | 600 | uint8_t isValidGPIO(uint8_t num) |
gillwei7 | 0:5c195ab2f696 | 601 | { |
gillwei7 | 5:ee474e3133eb | 602 | uint8_t cfgPin[] = {0,4,5,6,7,13,21,23,24,25,29,30,31}; |
gillwei7 | 3:38ec8ad317f4 | 603 | uint8_t i; |
gillwei7 | 3:38ec8ad317f4 | 604 | |
gillwei7 | 3:38ec8ad317f4 | 605 | for ( i = 0; i < sizeof(cfgPin) ; i++ ) { |
gillwei7 | 3:38ec8ad317f4 | 606 | if ( num == cfgPin[i] ) |
gillwei7 | 3:38ec8ad317f4 | 607 | return 1; |
gillwei7 | 3:38ec8ad317f4 | 608 | } |
gillwei7 | 3:38ec8ad317f4 | 609 | |
gillwei7 | 3:38ec8ad317f4 | 610 | return 0; |
gillwei7 | 0:5c195ab2f696 | 611 | } |
gillwei7 | 0:5c195ab2f696 | 612 | |
gillwei7 | 0:5c195ab2f696 | 613 | static void cynBLESystemOffCommand(void) |
gillwei7 | 0:5c195ab2f696 | 614 | { |
gillwei7 | 3:38ec8ad317f4 | 615 | if (cyntecGetCommandTokenCnt() == 3) { |
gillwei7 | 3:38ec8ad317f4 | 616 | uint8_t argLen = 0; |
gillwei7 | 3:38ec8ad317f4 | 617 | uint8_t *arg = cyntecGetCommandArgument(0,&argLen); |
gillwei7 | 3:38ec8ad317f4 | 618 | uint8_t gpioNum = cyntecAtoi( arg, argLen ); |
gillwei7 | 3:38ec8ad317f4 | 619 | // Can only use 0,1,2,3,4,5,6,7,13,16,17,23,24,25,29 |
gillwei7 | 3:38ec8ad317f4 | 620 | if (!isValidGPIO(gpioNum)) { |
gillwei7 | 3:38ec8ad317f4 | 621 | cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); |
gillwei7 | 3:38ec8ad317f4 | 622 | return; |
gillwei7 | 3:38ec8ad317f4 | 623 | } |
gillwei7 | 3:38ec8ad317f4 | 624 | nrf_gpio_cfg_sense_input(gpioNum, |
gillwei7 | 3:38ec8ad317f4 | 625 | NRF_GPIO_PIN_PULLUP, |
gillwei7 | 3:38ec8ad317f4 | 626 | NRF_GPIO_PIN_SENSE_LOW); |
gillwei7 | 3:38ec8ad317f4 | 627 | cyntecPrintOk(); |
gillwei7 | 3:38ec8ad317f4 | 628 | } else if (cyntecGetCommandTokenCnt() == 2) { |
gillwei7 | 3:38ec8ad317f4 | 629 | /* default wake up pin is uart Rx pin */ |
gillwei7 | 3:38ec8ad317f4 | 630 | nrf_gpio_cfg_sense_input(RX_PIN_NUMBER, |
gillwei7 | 3:38ec8ad317f4 | 631 | NRF_GPIO_PIN_PULLUP, |
gillwei7 | 3:38ec8ad317f4 | 632 | NRF_GPIO_PIN_SENSE_LOW); |
gillwei7 | 3:38ec8ad317f4 | 633 | cyntecPrintOk(); |
gillwei7 | 3:38ec8ad317f4 | 634 | } else { |
gillwei7 | 3:38ec8ad317f4 | 635 | cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); |
gillwei7 | 3:38ec8ad317f4 | 636 | } |
gillwei7 | 3:38ec8ad317f4 | 637 | sd_power_system_off(); |
gillwei7 | 0:5c195ab2f696 | 638 | } |
gillwei7 | 0:5c195ab2f696 | 639 | |
gillwei7 | 0:5c195ab2f696 | 640 | static void cynBLEGPIOCommand(void) |
gillwei7 | 0:5c195ab2f696 | 641 | { |
gillwei7 | 3:38ec8ad317f4 | 642 | |
gillwei7 | 3:38ec8ad317f4 | 643 | if (cyntecGetCommandTokenCnt() == 4) { |
gillwei7 | 3:38ec8ad317f4 | 644 | uint8_t argLen = 0; |
gillwei7 | 3:38ec8ad317f4 | 645 | uint8_t *arg = cyntecGetCommandArgument(0,&argLen); |
gillwei7 | 3:38ec8ad317f4 | 646 | uint8_t gpioNum = cyntecAtoi( arg, argLen ); |
gillwei7 | 3:38ec8ad317f4 | 647 | |
gillwei7 | 3:38ec8ad317f4 | 648 | if (!isValidGPIO(gpioNum)) { |
gillwei7 | 3:38ec8ad317f4 | 649 | cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); |
gillwei7 | 3:38ec8ad317f4 | 650 | return; |
gillwei7 | 3:38ec8ad317f4 | 651 | } |
gillwei7 | 3:38ec8ad317f4 | 652 | |
gillwei7 | 3:38ec8ad317f4 | 653 | /* arg2 is set or clear */ |
gillwei7 | 3:38ec8ad317f4 | 654 | uint8_t *action = cyntecGetCommandArgument(1,&argLen); |
gillwei7 | 3:38ec8ad317f4 | 655 | |
gillwei7 | 3:38ec8ad317f4 | 656 | if ( cyntecStrCmp(action,(unsigned char *)"set",argLen) == 1 ) { |
gillwei7 | 3:38ec8ad317f4 | 657 | nrf_gpio_cfg_output(gpioNum); |
gillwei7 | 3:38ec8ad317f4 | 658 | nrf_gpio_pin_set(gpioNum); |
gillwei7 | 3:38ec8ad317f4 | 659 | cyntecPrintOk(); |
gillwei7 | 3:38ec8ad317f4 | 660 | } else if ( cyntecStrCmp(action,(unsigned char *)"clear",argLen) == 1 ) { |
gillwei7 | 3:38ec8ad317f4 | 661 | nrf_gpio_cfg_output(gpioNum); |
gillwei7 | 3:38ec8ad317f4 | 662 | nrf_gpio_pin_clear(gpioNum); |
gillwei7 | 3:38ec8ad317f4 | 663 | cyntecPrintOk(); |
gillwei7 | 3:38ec8ad317f4 | 664 | } else { |
gillwei7 | 3:38ec8ad317f4 | 665 | cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_SYNTAX_ERROR); |
gillwei7 | 3:38ec8ad317f4 | 666 | } |
gillwei7 | 3:38ec8ad317f4 | 667 | } else { |
gillwei7 | 3:38ec8ad317f4 | 668 | cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); |
gillwei7 | 3:38ec8ad317f4 | 669 | } |
gillwei7 | 0:5c195ab2f696 | 670 | } |
gillwei7 | 0:5c195ab2f696 | 671 | |
gillwei7 | 0:5c195ab2f696 | 672 | static void cynResetCommand(void) |
gillwei7 | 0:5c195ab2f696 | 673 | { |
gillwei7 | 3:38ec8ad317f4 | 674 | cyntecPrintOk(); |
gillwei7 | 3:38ec8ad317f4 | 675 | // On assert, the system can only recover with a reset. |
gillwei7 | 3:38ec8ad317f4 | 676 | NVIC_SystemReset(); |
gillwei7 | 0:5c195ab2f696 | 677 | } |
gillwei7 | 0:5c195ab2f696 | 678 | |
gillwei7 | 4:b52035367aee | 679 | void triggerRead(const GattReadCallbackParams *response) |
gillwei7 | 4:b52035367aee | 680 | { |
gillwei7 | 4:b52035367aee | 681 | //if (response->handle == ledCharacteristic.getValueHandle()) { |
gillwei7 | 4:b52035367aee | 682 | #if DUMP_READ_DATA |
gillwei7 | 4:b52035367aee | 683 | printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); |
gillwei7 | 4:b52035367aee | 684 | for (unsigned index = 0; index < response->len; index++) { |
gillwei7 | 4:b52035367aee | 685 | printf("%c[%02x]", response->data[index], response->data[index]); |
gillwei7 | 4:b52035367aee | 686 | } |
gillwei7 | 4:b52035367aee | 687 | printf("\r\n"); |
gillwei7 | 4:b52035367aee | 688 | #endif |
gillwei7 | 4:b52035367aee | 689 | // |
gillwei7 | 4:b52035367aee | 690 | // uint8_t toggledValue = response->data[0] ^ 0x1; |
gillwei7 | 4:b52035367aee | 691 | // ledCharacteristic.write(1, &toggledValue); |
gillwei7 | 4:b52035367aee | 692 | // } |
gillwei7 | 4:b52035367aee | 693 | } |
gillwei7 | 0:5c195ab2f696 | 694 | static void cynBLEUpdateUUIDCommand(void) |
gillwei7 | 0:5c195ab2f696 | 695 | { |
gillwei7 | 3:38ec8ad317f4 | 696 | if (cyntecGetCommandTokenCnt() != 5) { |
gillwei7 | 3:38ec8ad317f4 | 697 | cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); |
gillwei7 | 3:38ec8ad317f4 | 698 | return; |
gillwei7 | 4:b52035367aee | 699 | } |
gillwei7 | 4:b52035367aee | 700 | UUID buf_ser_uuid ; |
gillwei7 | 4:b52035367aee | 701 | uint8_t bufferUuidVs[16]; |
gillwei7 | 4:b52035367aee | 702 | UUID buf_char_uuid; |
gillwei7 | 4:b52035367aee | 703 | uint8_t bufVal[MAX_VALUE_LENGTH] = {0}; |
gillwei7 | 4:b52035367aee | 704 | uint16_t valueLen = 0; |
gillwei7 | 4:b52035367aee | 705 | bool readmatchFlag = false; |
gillwei7 | 4:b52035367aee | 706 | uint8_t argLen = 0; |
gillwei7 | 4:b52035367aee | 707 | uint8_t *arg = cyntecGetCommandArgument(0,&argLen); |
gillwei7 | 3:38ec8ad317f4 | 708 | |
gillwei7 | 4:b52035367aee | 709 | // Handle input parameter - Service UUID |
gillwei7 | 4:b52035367aee | 710 | if ( argLen == 6 && arg[0] == '0' && arg[1] == 'x') { |
gillwei7 | 4:b52035367aee | 711 | buf_ser_uuid = cyntecArgToUint16( (arg + 2), (argLen - 2)); |
gillwei7 | 4:b52035367aee | 712 | } |
gillwei7 | 4:b52035367aee | 713 | if ( argLen == 34 && arg[0] == '0' && arg[1] == 'x' ) { |
gillwei7 | 4:b52035367aee | 714 | for (uint8_t i=0; i<16; i++) { |
gillwei7 | 4:b52035367aee | 715 | bufferUuidVs[i] = cyntecArgToUint8( (arg + 2+2*i), 2); |
gillwei7 | 3:38ec8ad317f4 | 716 | } |
gillwei7 | 4:b52035367aee | 717 | buf_ser_uuid.setupLong(bufferUuidVs); |
gillwei7 | 4:b52035367aee | 718 | } |
gillwei7 | 3:38ec8ad317f4 | 719 | |
gillwei7 | 4:b52035367aee | 720 | // Handle input parameter - Characteristic UUID |
gillwei7 | 4:b52035367aee | 721 | argLen = 0; |
gillwei7 | 4:b52035367aee | 722 | arg = cyntecGetCommandArgument(1,&argLen); |
gillwei7 | 4:b52035367aee | 723 | if ( argLen == 6 && arg[0] == '0' && arg[1] == 'x') { |
gillwei7 | 4:b52035367aee | 724 | buf_char_uuid = cyntecArgToUint16( (arg + 2), (argLen - 2)); |
gillwei7 | 4:b52035367aee | 725 | } |
gillwei7 | 4:b52035367aee | 726 | if ( argLen == 34 && arg[0] == '0' && arg[1] == 'x' ) { |
gillwei7 | 4:b52035367aee | 727 | for (uint8_t i=0; i<16; i++) { |
gillwei7 | 4:b52035367aee | 728 | bufferUuidVs[i] = cyntecArgToUint8( (arg + 2+2*i), 2); |
gillwei7 | 4:b52035367aee | 729 | } |
gillwei7 | 4:b52035367aee | 730 | buf_char_uuid.setupLong(bufferUuidVs); |
gillwei7 | 4:b52035367aee | 731 | } |
gillwei7 | 3:38ec8ad317f4 | 732 | |
gillwei7 | 4:b52035367aee | 733 | // Input value |
gillwei7 | 4:b52035367aee | 734 | arg = cyntecGetCommandArgument(2,&argLen); |
gillwei7 | 4:b52035367aee | 735 | valueLen = (argLen-2)/2; // uint8_t to uint16_t transform |
gillwei7 | 4:b52035367aee | 736 | for (int i=0 ; i < (argLen-2)/2; i++) { |
gillwei7 | 4:b52035367aee | 737 | bufVal[i] = cyntecArgToUint8(arg+2*(i+1), 2); |
gillwei7 | 4:b52035367aee | 738 | } |
gillwei7 | 5:ee474e3133eb | 739 | |
gillwei7 | 5:ee474e3133eb | 740 | |
gillwei7 | 4:b52035367aee | 741 | for ( int i = 0 ; i < service_count ; i++ ) { |
gillwei7 | 4:b52035367aee | 742 | if ( (bufferService[i].ser_uuid == buf_ser_uuid) & (readmatchFlag == false)) { |
gillwei7 | 4:b52035367aee | 743 | for (int j = 0 ; j < CLI_CHAR_MAX_NUM ; j++ ) { |
gillwei7 | 4:b52035367aee | 744 | if (( bufferService[i].bufferGattChar[j].char_uuid == buf_char_uuid)& (readmatchFlag == false)) { |
gillwei7 | 4:b52035367aee | 745 | GattAttribute& valueAttr = charAry[i][j]->getValueAttribute(); |
gillwei7 | 4:b52035367aee | 746 | ble_error_t err; |
gillwei7 | 4:b52035367aee | 747 | if (!connState) |
gillwei7 | 4:b52035367aee | 748 | err = deltaBLE.gattServer().write(valueAttr.getHandle(),bufVal,valueLen,true); |
gillwei7 | 4:b52035367aee | 749 | else |
gillwei7 | 4:b52035367aee | 750 | err = deltaBLE.gattServer().write(test_conn_handle,valueAttr.getHandle(),bufVal,valueLen,false); |
gillwei7 | 5:ee474e3133eb | 751 | |
gillwei7 | 4:b52035367aee | 752 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 753 | console.printf("Write ERR:%i\r\n",err); |
gillwei7 | 4:b52035367aee | 754 | #endif |
gillwei7 | 5:ee474e3133eb | 755 | console.printf("\r\nOK;"); |
gillwei7 | 5:ee474e3133eb | 756 | for (uint16_t n=0; n<valueLen; n++) { |
gillwei7 | 5:ee474e3133eb | 757 | console.printf("%02X",bufVal[n]); |
gillwei7 | 5:ee474e3133eb | 758 | } |
gillwei7 | 4:b52035367aee | 759 | console.printf("\r\n"); |
gillwei7 | 4:b52035367aee | 760 | readmatchFlag = true; |
gillwei7 | 4:b52035367aee | 761 | return; |
gillwei7 | 3:38ec8ad317f4 | 762 | } |
gillwei7 | 3:38ec8ad317f4 | 763 | } |
gillwei7 | 3:38ec8ad317f4 | 764 | } |
gillwei7 | 4:b52035367aee | 765 | } |
gillwei7 | 4:b52035367aee | 766 | // no matched case, can not read |
gillwei7 | 4:b52035367aee | 767 | if (readmatchFlag == false) { |
gillwei7 | 4:b52035367aee | 768 | cyntecPrintError(CYNTEC_CMD_ERR_NO_MATCHED_ARGUMENT); |
gillwei7 | 3:38ec8ad317f4 | 769 | } |
gillwei7 | 0:5c195ab2f696 | 770 | } |
gillwei7 | 0:5c195ab2f696 | 771 | |
gillwei7 | 0:5c195ab2f696 | 772 | static void cynBLEReadDataCommand(void) |
gillwei7 | 3:38ec8ad317f4 | 773 | { |
gillwei7 | 4:b52035367aee | 774 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 775 | printf("Saved Char:\r\n"); |
gillwei7 | 4:b52035367aee | 776 | for (int i=0; i<service_count; i++) { |
gillwei7 | 4:b52035367aee | 777 | console.printf("ser_uuid:%04X",bufferService[service_count-1].ser_uuid.getShortUUID()); |
gillwei7 | 4:b52035367aee | 778 | for (int j=0; j<CLI_CHAR_MAX_NUM; j++) { |
gillwei7 | 4:b52035367aee | 779 | printf("%i,U%04X,",j,bufferService[service_count-1].bufferGattChar[char_count-1].char_uuid.getShortUUID()); |
gillwei7 | 4:b52035367aee | 780 | printf("L%d,",bufferService[service_count-1].bufferGattChar[char_count-1].valueLength); |
gillwei7 | 4:b52035367aee | 781 | printf("V%02X ",bufferService[service_count-1].bufferGattChar[char_count-1].value[0]); |
gillwei7 | 4:b52035367aee | 782 | } |
gillwei7 | 4:b52035367aee | 783 | } |
gillwei7 | 4:b52035367aee | 784 | printf("\r\n"); |
gillwei7 | 4:b52035367aee | 785 | #endif |
gillwei7 | 3:38ec8ad317f4 | 786 | if (cyntecGetCommandTokenCnt() != 4) { |
gillwei7 | 3:38ec8ad317f4 | 787 | cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); |
gillwei7 | 4:b52035367aee | 788 | return; |
gillwei7 | 4:b52035367aee | 789 | } |
gillwei7 | 4:b52035367aee | 790 | UUID buf_ser_uuid ; |
gillwei7 | 4:b52035367aee | 791 | uint8_t bufferUuidVs[16]; |
gillwei7 | 4:b52035367aee | 792 | UUID buf_char_uuid; |
gillwei7 | 4:b52035367aee | 793 | uint8_t bufVal[MAX_VALUE_LENGTH] = {0}; |
gillwei7 | 4:b52035367aee | 794 | uint16_t valueLen=0; |
gillwei7 | 4:b52035367aee | 795 | uint16_t * valueLenPtr; |
gillwei7 | 4:b52035367aee | 796 | bool readmatchFlag = false; |
gillwei7 | 4:b52035367aee | 797 | uint8_t argLen = 0; |
gillwei7 | 4:b52035367aee | 798 | uint8_t *arg = cyntecGetCommandArgument(0,&argLen); |
gillwei7 | 3:38ec8ad317f4 | 799 | |
gillwei7 | 4:b52035367aee | 800 | // Handle input parameter - Service UUID |
gillwei7 | 4:b52035367aee | 801 | if ( argLen == 6 && arg[0] == '0' && arg[1] == 'x') { |
gillwei7 | 4:b52035367aee | 802 | buf_ser_uuid = cyntecArgToUint16( (arg + 2), (argLen - 2)); |
gillwei7 | 4:b52035367aee | 803 | } |
gillwei7 | 4:b52035367aee | 804 | if ( argLen == 34 && arg[0] == '0' && arg[1] == 'x' ) { |
gillwei7 | 4:b52035367aee | 805 | for (uint8_t i=0; i<16; i++) { |
gillwei7 | 4:b52035367aee | 806 | bufferUuidVs[i] = cyntecArgToUint8( (arg + 2+2*i), 2); |
gillwei7 | 3:38ec8ad317f4 | 807 | } |
gillwei7 | 4:b52035367aee | 808 | buf_ser_uuid.setupLong(bufferUuidVs); |
gillwei7 | 4:b52035367aee | 809 | } |
gillwei7 | 3:38ec8ad317f4 | 810 | |
gillwei7 | 4:b52035367aee | 811 | // Handle input parameter - Characteristic UUID |
gillwei7 | 4:b52035367aee | 812 | argLen = 0; |
gillwei7 | 4:b52035367aee | 813 | arg = cyntecGetCommandArgument(1,&argLen); |
gillwei7 | 4:b52035367aee | 814 | if ( argLen == 6 && arg[0] == '0' && arg[1] == 'x') { |
gillwei7 | 4:b52035367aee | 815 | buf_char_uuid = cyntecArgToUint16( (arg + 2), (argLen - 2)); |
gillwei7 | 4:b52035367aee | 816 | } |
gillwei7 | 4:b52035367aee | 817 | if ( argLen == 34 && arg[0] == '0' && arg[1] == 'x' ) { |
gillwei7 | 4:b52035367aee | 818 | for (uint8_t i=0; i<16; i++) { |
gillwei7 | 4:b52035367aee | 819 | bufferUuidVs[i] = cyntecArgToUint8( (arg + 2+2*i), 2); |
gillwei7 | 3:38ec8ad317f4 | 820 | } |
gillwei7 | 4:b52035367aee | 821 | buf_char_uuid.setupLong(bufferUuidVs); |
gillwei7 | 4:b52035367aee | 822 | } |
gillwei7 | 4:b52035367aee | 823 | |
gillwei7 | 4:b52035367aee | 824 | for ( int i = 0 ; i < service_count ; i++ ) { |
gillwei7 | 4:b52035367aee | 825 | if ( (bufferService[i].ser_uuid == buf_ser_uuid) & (readmatchFlag == false)) { |
gillwei7 | 4:b52035367aee | 826 | for (int j = 0 ; j < CLI_CHAR_MAX_NUM ; j++ ) { |
gillwei7 | 4:b52035367aee | 827 | if (( bufferService[i].bufferGattChar[j].char_uuid == buf_char_uuid)& (readmatchFlag == false)) { |
gillwei7 | 4:b52035367aee | 828 | GattAttribute& valueAttr = charAry[i][j]->getValueAttribute(); |
gillwei7 | 4:b52035367aee | 829 | valueLenPtr = valueAttr.getLengthPtr(); |
gillwei7 | 4:b52035367aee | 830 | valueLen = valueAttr.getLength(); |
gillwei7 | 4:b52035367aee | 831 | ble_error_t err = deltaBLE.gattServer().read(valueAttr.getHandle(),bufVal,valueLenPtr); |
gillwei7 | 4:b52035367aee | 832 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 833 | console.printf("Read ERR:%i\r\n",err); |
gillwei7 | 4:b52035367aee | 834 | #endif |
gillwei7 | 5:ee474e3133eb | 835 | console.printf("\r\nOK;"); |
gillwei7 | 4:b52035367aee | 836 | console.printf("0x"); |
gillwei7 | 4:b52035367aee | 837 | for (uint16_t n=0; n<valueLen; n++) { |
gillwei7 | 4:b52035367aee | 838 | console.printf("%02X",bufVal[n]); |
gillwei7 | 3:38ec8ad317f4 | 839 | } |
gillwei7 | 4:b52035367aee | 840 | console.printf("\r\n"); |
gillwei7 | 4:b52035367aee | 841 | readmatchFlag = true; |
gillwei7 | 4:b52035367aee | 842 | return; |
gillwei7 | 3:38ec8ad317f4 | 843 | } |
gillwei7 | 3:38ec8ad317f4 | 844 | } |
gillwei7 | 3:38ec8ad317f4 | 845 | } |
gillwei7 | 4:b52035367aee | 846 | |
gillwei7 | 3:38ec8ad317f4 | 847 | } |
gillwei7 | 4:b52035367aee | 848 | // no matched case, can not read |
gillwei7 | 4:b52035367aee | 849 | if (readmatchFlag == false) { |
gillwei7 | 4:b52035367aee | 850 | cyntecPrintError(CYNTEC_CMD_ERR_NO_MATCHED_ARGUMENT); |
gillwei7 | 4:b52035367aee | 851 | } |
gillwei7 | 0:5c195ab2f696 | 852 | } |
gillwei7 | 0:5c195ab2f696 | 853 | |
gillwei7 | 0:5c195ab2f696 | 854 | static void cynGattCharCommand(void) |
gillwei7 | 0:5c195ab2f696 | 855 | { |
gillwei7 | 4:b52035367aee | 856 | uint8_t i; |
gillwei7 | 4:b52035367aee | 857 | uint8_t valueLengthBuffer; |
gillwei7 | 3:38ec8ad317f4 | 858 | |
gillwei7 | 3:38ec8ad317f4 | 859 | if (cyntecGetCommandTokenCnt() != 5) { |
gillwei7 | 3:38ec8ad317f4 | 860 | cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); |
gillwei7 | 3:38ec8ad317f4 | 861 | return; |
gillwei7 | 4:b52035367aee | 862 | } |
gillwei7 | 4:b52035367aee | 863 | |
gillwei7 | 4:b52035367aee | 864 | /* handle parameter - UUID */ |
gillwei7 | 4:b52035367aee | 865 | /* Only support 16-bit or 128-bit UUID type */ |
gillwei7 | 4:b52035367aee | 866 | uint8_t argLen = 0; |
gillwei7 | 4:b52035367aee | 867 | uint8_t *arg = cyntecGetCommandArgument(0,&argLen); |
gillwei7 | 4:b52035367aee | 868 | if (arg[0] != '0' || arg[1] != 'x' || (argLen != 6 && argLen != 34)) { |
gillwei7 | 4:b52035367aee | 869 | cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_SYNTAX_ERROR); |
gillwei7 | 4:b52035367aee | 870 | return; |
gillwei7 | 4:b52035367aee | 871 | } |
gillwei7 | 4:b52035367aee | 872 | /* len: 6 => 0xXXXX */ |
gillwei7 | 4:b52035367aee | 873 | if ( argLen == 6 && arg[0] == '0' && arg[1] == 'x') { |
gillwei7 | 4:b52035367aee | 874 | UUID::ShortUUIDBytes_t buffer_uuid_short; |
gillwei7 | 4:b52035367aee | 875 | buffer_uuid_short = cyntecArgToUint16( (arg + 2), (argLen - 2)); |
gillwei7 | 4:b52035367aee | 876 | UUID uuid_short(buffer_uuid_short); |
gillwei7 | 4:b52035367aee | 877 | bufferService[service_count-1].bufferGattChar[char_count].char_uuid = uuid_short; |
gillwei7 | 4:b52035367aee | 878 | } |
gillwei7 | 4:b52035367aee | 879 | if ( argLen == 34 && arg[0] == '0' && arg[1] == 'x' ) { |
gillwei7 | 4:b52035367aee | 880 | // Initialize LongUUIDBytes_t, then use default constructor to setupLong(longUUID) |
gillwei7 | 4:b52035367aee | 881 | UUID::LongUUIDBytes_t buffer_uuid_vs; |
gillwei7 | 4:b52035367aee | 882 | for (uint8_t i=0; i<16; i++) { |
gillwei7 | 4:b52035367aee | 883 | buffer_uuid_vs[i] = cyntecArgToUint8( (arg + 2+2*i), 2); |
gillwei7 | 3:38ec8ad317f4 | 884 | } |
gillwei7 | 4:b52035367aee | 885 | bufferService[service_count-1].bufferGattChar[char_count].char_uuid.setupLong(buffer_uuid_vs); |
gillwei7 | 3:38ec8ad317f4 | 886 | } |
gillwei7 | 3:38ec8ad317f4 | 887 | |
gillwei7 | 4:b52035367aee | 888 | /* handle 3rd parameter - attribute mode */ |
gillwei7 | 4:b52035367aee | 889 | argLen = 0; |
gillwei7 | 4:b52035367aee | 890 | arg = cyntecGetCommandArgument(1,&argLen); |
gillwei7 | 4:b52035367aee | 891 | if (arg[0] != '0' || arg[1] != 'x' ) { |
gillwei7 | 4:b52035367aee | 892 | cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_SYNTAX_ERROR); |
gillwei7 | 4:b52035367aee | 893 | return; |
gillwei7 | 4:b52035367aee | 894 | } |
gillwei7 | 4:b52035367aee | 895 | uint32_t prop = cyntecHexToUint32(arg+2,argLen-2); |
gillwei7 | 0:5c195ab2f696 | 896 | |
gillwei7 | 4:b52035367aee | 897 | /* handle 4th parameter - attribute value */ |
gillwei7 | 4:b52035367aee | 898 | argLen = 0; |
gillwei7 | 4:b52035367aee | 899 | arg = cyntecGetCommandArgument(2,&argLen); |
gillwei7 | 4:b52035367aee | 900 | if (arg[0] != '0' || arg[1] != 'x' | argLen%2 != 0) { |
gillwei7 | 4:b52035367aee | 901 | cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_SYNTAX_ERROR); |
gillwei7 | 3:38ec8ad317f4 | 902 | return; |
gillwei7 | 4:b52035367aee | 903 | } |
gillwei7 | 4:b52035367aee | 904 | valueLengthBuffer = (argLen-2)/2; |
gillwei7 | 4:b52035367aee | 905 | memset(bufferService[service_count-1].bufferGattChar[char_count].value,0,MAX_VALUE_LENGTH); |
gillwei7 | 4:b52035367aee | 906 | for (i=0 ; i < (argLen-2)/2; i++) { |
gillwei7 | 4:b52035367aee | 907 | bufferService[service_count-1].bufferGattChar[char_count].value[i] = cyntecArgToUint8(arg+2*(i+1), 2); |
gillwei7 | 4:b52035367aee | 908 | //console.printf("%02X ",bufferService[service_count-1].bufferGattChar[char_count].value[i]); |
gillwei7 | 3:38ec8ad317f4 | 909 | } |
gillwei7 | 4:b52035367aee | 910 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 911 | console.printf("valueLengthBuffer:%d\r\n",valueLengthBuffer); |
gillwei7 | 4:b52035367aee | 912 | console.printf("value:"); |
gillwei7 | 4:b52035367aee | 913 | for (i=0 ; i < valueLengthBuffer; i++) { |
gillwei7 | 4:b52035367aee | 914 | console.printf("%02X",bufferService[service_count-1].bufferGattChar[char_count].value[i]); |
gillwei7 | 4:b52035367aee | 915 | } |
gillwei7 | 4:b52035367aee | 916 | #endif |
gillwei7 | 4:b52035367aee | 917 | bufferService[service_count-1].bufferGattChar[char_count].valueLength = valueLengthBuffer; |
gillwei7 | 4:b52035367aee | 918 | bufferService[service_count-1].bufferGattChar[char_count].props = prop; |
gillwei7 | 4:b52035367aee | 919 | //bufferService[service_count-1].bufferGattChar[char_count].char_value_handle = testHandle; |
gillwei7 | 4:b52035367aee | 920 | if (char_count>CLI_CHAR_MAX_NUM) { |
gillwei7 | 4:b52035367aee | 921 | cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); |
gillwei7 | 4:b52035367aee | 922 | return; |
gillwei7 | 4:b52035367aee | 923 | } |
gillwei7 | 4:b52035367aee | 924 | cyntecPrintOk(); |
gillwei7 | 4:b52035367aee | 925 | char_count++; |
gillwei7 | 0:5c195ab2f696 | 926 | } |
gillwei7 | 0:5c195ab2f696 | 927 | |
gillwei7 | 4:b52035367aee | 928 | static void cynRegServiceCommand(void) |
gillwei7 | 3:38ec8ad317f4 | 929 | { |
gillwei7 | 4:b52035367aee | 930 | //GattCharacteristic *charAry[char_count]; |
gillwei7 | 4:b52035367aee | 931 | //GattCharacteristic **charAry; |
gillwei7 | 4:b52035367aee | 932 | //charAry = new GattCharacteristic *[char_count]; |
gillwei7 | 4:b52035367aee | 933 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 934 | console.printf("Current char_count:%d\r\n",char_count); |
gillwei7 | 4:b52035367aee | 935 | console.printf("Current service_count:%d\r\n",service_count); |
gillwei7 | 4:b52035367aee | 936 | #endif |
gillwei7 | 4:b52035367aee | 937 | for (uint8_t i=0; i<char_count; i++) { |
gillwei7 | 4:b52035367aee | 938 | charAry[service_count-1][i] = new GattCharacteristic ( |
gillwei7 | 4:b52035367aee | 939 | bufferService[service_count-1].bufferGattChar[i].char_uuid, |
gillwei7 | 4:b52035367aee | 940 | bufferService[service_count-1].bufferGattChar[i].value, |
gillwei7 | 4:b52035367aee | 941 | bufferService[service_count-1].bufferGattChar[i].valueLength, |
gillwei7 | 4:b52035367aee | 942 | MAX_VALUE_LENGTH, |
gillwei7 | 4:b52035367aee | 943 | bufferService[service_count-1].bufferGattChar[i].props |
gillwei7 | 4:b52035367aee | 944 | ); |
gillwei7 | 4:b52035367aee | 945 | } |
gillwei7 | 4:b52035367aee | 946 | GattService newService(bufferService[service_count-1].ser_uuid, charAry[service_count-1], char_count ); |
gillwei7 | 4:b52035367aee | 947 | ble_error_t err_code; |
gillwei7 | 4:b52035367aee | 948 | err_code = deltaBLE.addService(newService); |
gillwei7 | 4:b52035367aee | 949 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 950 | console.printf("addService error:%d\r\n",err_code); |
gillwei7 | 4:b52035367aee | 951 | #endif |
gillwei7 | 4:b52035367aee | 952 | cyntecPrintOk(); |
gillwei7 | 4:b52035367aee | 953 | //char_count = 0; // already did in gattService |
gillwei7 | 0:5c195ab2f696 | 954 | } |
gillwei7 | 0:5c195ab2f696 | 955 | |
gillwei7 | 0:5c195ab2f696 | 956 | static void cynGattServiceCommand(void) |
gillwei7 | 0:5c195ab2f696 | 957 | { |
gillwei7 | 3:38ec8ad317f4 | 958 | if (cyntecGetCommandTokenCnt() != 3) { |
gillwei7 | 3:38ec8ad317f4 | 959 | cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); |
gillwei7 | 3:38ec8ad317f4 | 960 | return; |
gillwei7 | 4:b52035367aee | 961 | } |
gillwei7 | 4:b52035367aee | 962 | /* handle first parameter - Service UUID */ |
gillwei7 | 4:b52035367aee | 963 | uint8_t argLen = 0; |
gillwei7 | 4:b52035367aee | 964 | uint8_t *arg = cyntecGetCommandArgument(0,&argLen); |
gillwei7 | 3:38ec8ad317f4 | 965 | |
gillwei7 | 4:b52035367aee | 966 | /* Service uuid is 16 bits */ |
gillwei7 | 4:b52035367aee | 967 | if ( argLen == 6 && arg[0] == '0' && arg[1] == 'x') { |
gillwei7 | 4:b52035367aee | 968 | UUID::ShortUUIDBytes_t buffer_uuid_short; |
gillwei7 | 4:b52035367aee | 969 | buffer_uuid_short = cyntecArgToUint16( (arg + 2), (argLen - 2)); |
gillwei7 | 4:b52035367aee | 970 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 971 | console.printf("%4X",buffer_uuid_short); |
gillwei7 | 0:5c195ab2f696 | 972 | #endif |
gillwei7 | 4:b52035367aee | 973 | UUID uuid_short(buffer_uuid_short); |
gillwei7 | 4:b52035367aee | 974 | bufferService[service_count].ser_uuid = uuid_short; |
gillwei7 | 4:b52035367aee | 975 | //buffer_uuid = cyntecArgToUint16( (arg + 2), (argLen - 2)); |
gillwei7 | 4:b52035367aee | 976 | } |
gillwei7 | 3:38ec8ad317f4 | 977 | |
gillwei7 | 4:b52035367aee | 978 | /* Service uuid is 128 bits */ |
gillwei7 | 4:b52035367aee | 979 | if ( argLen == 34 && arg[0] == '0' && arg[1] == 'x' ) { |
gillwei7 | 4:b52035367aee | 980 | // Initialize LongUUIDBytes_t, then use default constructor to setupLong(longUUID) |
gillwei7 | 4:b52035367aee | 981 | UUID::LongUUIDBytes_t buffer_uuid_vs; |
gillwei7 | 4:b52035367aee | 982 | for (uint8_t i=0; i<16; i++) { |
gillwei7 | 4:b52035367aee | 983 | buffer_uuid_vs[i] = cyntecArgToUint8( (arg + 2+2*i), 2); |
gillwei7 | 4:b52035367aee | 984 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 985 | console.printf("%2X ",buffer_uuid_vs[i]); |
gillwei7 | 4:b52035367aee | 986 | #endif |
gillwei7 | 3:38ec8ad317f4 | 987 | } |
gillwei7 | 4:b52035367aee | 988 | UUID uuid_long(buffer_uuid_vs); |
gillwei7 | 4:b52035367aee | 989 | bufferService[service_count].ser_uuid = uuid_long; |
gillwei7 | 3:38ec8ad317f4 | 990 | } |
gillwei7 | 4:b52035367aee | 991 | cyntecPrintOk(); |
gillwei7 | 4:b52035367aee | 992 | service_count++; |
gillwei7 | 4:b52035367aee | 993 | char_count = 0; |
gillwei7 | 3:38ec8ad317f4 | 994 | } |
gillwei7 | 0:5c195ab2f696 | 995 | |
gillwei7 | 0:5c195ab2f696 | 996 | static void cynBLEInitCommand(void) |
gillwei7 | 0:5c195ab2f696 | 997 | { |
gillwei7 | 3:38ec8ad317f4 | 998 | deltaBLE.init(); |
gillwei7 | 3:38ec8ad317f4 | 999 | deltaBLE.onDisconnection(disconnectionCallback); |
gillwei7 | 3:38ec8ad317f4 | 1000 | deltaBLE.onConnection(onConnectionCallback); |
gillwei7 | 3:38ec8ad317f4 | 1001 | deltaBLE.onTimeout(onTimeoutCallback); |
gillwei7 | 4:b52035367aee | 1002 | deltaBLE.gattClient().onDataRead(triggerRead); |
gillwei7 | 5:ee474e3133eb | 1003 | deltaBLE.gattServer().onDataWritten(onDataWrittenCallback); |
gillwei7 | 3:38ec8ad317f4 | 1004 | cyntecPrintOk(); |
gillwei7 | 0:5c195ab2f696 | 1005 | } |
gillwei7 | 0:5c195ab2f696 | 1006 | |
gillwei7 | 5:ee474e3133eb | 1007 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 1008 | static void cynBLETestCommand(void) |
gillwei7 | 4:b52035367aee | 1009 | { |
gillwei7 | 4:b52035367aee | 1010 | // gill test 1021 |
gillwei7 | 4:b52035367aee | 1011 | uint8_t bufVal[20] = {0}; |
gillwei7 | 4:b52035367aee | 1012 | uint8_t valWrite[2] = {0x22,0x33}; |
gillwei7 | 4:b52035367aee | 1013 | //uint16_t bufhandle = 0x0001; |
gillwei7 | 4:b52035367aee | 1014 | uint8_t valRead[2] = {0,0}; |
gillwei7 | 4:b52035367aee | 1015 | |
gillwei7 | 4:b52035367aee | 1016 | // uint16_t handle = charAry[0][0]->getValueHandle(); |
gillwei7 | 4:b52035367aee | 1017 | // console.printf("Handle:%04X\r\n",handle); |
gillwei7 | 4:b52035367aee | 1018 | GattAttribute& valueAttr = charAry[0][0]->getValueAttribute(); |
gillwei7 | 4:b52035367aee | 1019 | //valueAttr.setHandle(bufhandle); |
gillwei7 | 4:b52035367aee | 1020 | console.printf("Handle:%04X ",valueAttr.getHandle()); |
gillwei7 | 4:b52035367aee | 1021 | console.printf("UUID:%04X ",valueAttr.getUUID().getShortUUID()); |
gillwei7 | 4:b52035367aee | 1022 | |
gillwei7 | 4:b52035367aee | 1023 | uint16_t* valueLenPtr = valueAttr.getLengthPtr(); |
gillwei7 | 4:b52035367aee | 1024 | console.printf("gatt val:%02X %02X\r\n",bufVal[0],bufVal[1]); |
gillwei7 | 4:b52035367aee | 1025 | deltaBLE.gattServer().read(valueAttr.getHandle(),bufVal,valueLenPtr); |
gillwei7 | 4:b52035367aee | 1026 | console.printf("gatt val:%02X %02X\r\n",bufVal[0],bufVal[1]); |
gillwei7 | 4:b52035367aee | 1027 | |
gillwei7 | 4:b52035367aee | 1028 | deltaBLE.gattServer().write(valueAttr.getHandle(),valWrite,2); |
gillwei7 | 4:b52035367aee | 1029 | deltaBLE.gattServer().read(valueAttr.getHandle(),valRead,valueLenPtr); |
gillwei7 | 4:b52035367aee | 1030 | console.printf("gatt val:%02X %02X\r\n",valRead[0],valRead[1]); |
gillwei7 | 4:b52035367aee | 1031 | } |
gillwei7 | 5:ee474e3133eb | 1032 | #endif |
gillwei7 | 4:b52035367aee | 1033 | |
gillwei7 | 0:5c195ab2f696 | 1034 | CyntecCommandEntry bleCommandSets[] = { |
gillwei7 | 4:b52035367aee | 1035 | // General |
gillwei7 | 4:b52035367aee | 1036 | #ifdef BLE_DEBUG |
gillwei7 | 4:b52035367aee | 1037 | {"TEST", cynBLETestCommand, NULL, "test"}, |
gillwei7 | 4:b52035367aee | 1038 | #endif |
gillwei7 | 4:b52035367aee | 1039 | // |
gillwei7 | 4:b52035367aee | 1040 | #if SIMPLE_CMD_NAME |
gillwei7 | 4:b52035367aee | 1041 | {"INT", cynBLEInitCommand, NULL, "Init BLE stack"}, |
gillwei7 | 4:b52035367aee | 1042 | {"GIO", cynBLEGPIOCommand, NULL, "Config gpio, Usage: <GPIO NO> <set|clear>"}, |
gillwei7 | 4:b52035367aee | 1043 | {"SLP", cynBLESystemOffCommand, NULL, "System off mode, Usage: <GPIO NO>"}, |
gillwei7 | 4:b52035367aee | 1044 | {"RST", cynResetCommand, NULL, "Soft reset"}, |
gillwei7 | 4:b52035367aee | 1045 | {"INF", cynBLEInfoCommand, NULL, "Module information"}, |
gillwei7 | 4:b52035367aee | 1046 | {"POW", cynBLESetTxPowerCommand, NULL, "Set BLE tx power, Usage: <POW setting>"}, |
gillwei7 | 4:b52035367aee | 1047 | {"NAM", cynBLENameCommand, NULL, "Set/Get friendly for BLE module, Usage: <name>"}, |
gillwei7 | 4:b52035367aee | 1048 | // GATT |
gillwei7 | 4:b52035367aee | 1049 | {"GRS", cynRegServiceCommand, NULL, "Register standby service"}, |
gillwei7 | 4:b52035367aee | 1050 | {"GAC", cynGattCharCommand, NULL, "Set SIG defined characteristic or Create your own"}, |
gillwei7 | 4:b52035367aee | 1051 | {"GAS", cynGattServiceCommand, NULL, "Set SIG defined service or Create your own"}, |
gillwei7 | 4:b52035367aee | 1052 | {"ADS", cynAdvertiseStartCommand, NULL, "Start broadcast advertise packet"}, |
gillwei7 | 4:b52035367aee | 1053 | {"ADP", cynAdvertiseStopCommand, NULL, "Stop broadcast advertise packet"}, |
gillwei7 | 4:b52035367aee | 1054 | {"SCS", cynBLEScanCommand, NULL, "Start to scan BLE device"}, |
gillwei7 | 4:b52035367aee | 1055 | {"SCP", cynBLEScanStopCommand, NULL, "Stop to scan BLE device"}, |
gillwei7 | 4:b52035367aee | 1056 | {"CON", cynBLEConnectCommand, NULL, "Connect to specific BLE device by Device Name"}, |
gillwei7 | 4:b52035367aee | 1057 | {"DCN", cynBLEDisconnectCommand, NULL, "Disconnection, Usage: cynb disconn"}, |
gillwei7 | 4:b52035367aee | 1058 | {"ADR", cynBLEAddressCommand, NULL, "Set/Get Bluetooth address"}, |
gillwei7 | 4:b52035367aee | 1059 | {"WRT", cynBLEUpdateUUIDCommand, NULL, "Update value of specific characteristics"}, |
gillwei7 | 4:b52035367aee | 1060 | {"RED", cynBLEReadDataCommand, NULL, "Read value of specific characteristics"}, |
gillwei7 | 4:b52035367aee | 1061 | {NULL, NULL, NULL, NULL}, |
gillwei7 | 4:b52035367aee | 1062 | #else |
gillwei7 | 5:ee474e3133eb | 1063 | {"init", cynBLEInitCommand, NULL, "Init BLE stack"}, |
gillwei7 | 5:ee474e3133eb | 1064 | {"gpio", cynBLEGPIOCommand, NULL, "Config gpio, Usage: <GPIO NO> <set|clear>"}, |
gillwei7 | 5:ee474e3133eb | 1065 | {"sleep", cynBLESystemOffCommand, NULL, "System off mode, Usage: <GPIO NO>"}, |
gillwei7 | 5:ee474e3133eb | 1066 | {"reset", cynResetCommand, NULL, "Soft reset"}, |
gillwei7 | 5:ee474e3133eb | 1067 | {"info", cynBLEInfoCommand, NULL, "Get module information"}, |
gillwei7 | 5:ee474e3133eb | 1068 | {"txPow", cynBLESetTxPowerCommand, NULL, "Set BLE tx power, Usage: <POWER>"}, |
gillwei7 | 5:ee474e3133eb | 1069 | {"name", cynBLENameCommand, NULL, "Set/Get friendly for BLE module, Usage: *<NAME>*"}, |
gillwei7 | 4:b52035367aee | 1070 | // GATT |
gillwei7 | 5:ee474e3133eb | 1071 | {"regService", cynRegServiceCommand, NULL, "Register standby service"}, |
gillwei7 | 5:ee474e3133eb | 1072 | {"gattChar", cynGattCharCommand, NULL, "Set SIG defined characteristic or Create your own,Usage: <CHAR UUID> <PROPS> <VALUE>"}, |
gillwei7 | 5:ee474e3133eb | 1073 | {"gattService", cynGattServiceCommand, NULL, "Set SIG defined service or Create your own,Usage: <SERVICE UUID>"}, |
gillwei7 | 5:ee474e3133eb | 1074 | {"advStart", cynAdvertiseStartCommand, NULL, "Start broadcast advertise packet,Usage: <INTERVAL> <WINDOW>"}, |
gillwei7 | 3:38ec8ad317f4 | 1075 | {"advStop", cynAdvertiseStopCommand, NULL, "Stop broadcast advertise packet"}, |
gillwei7 | 5:ee474e3133eb | 1076 | {"scanStart", cynBLEScanCommand, NULL, "Start to scan BLE device, Usage: <INTERVAL> <WINDOW> <TIMEOUT>"}, |
gillwei7 | 3:38ec8ad317f4 | 1077 | {"scanStop", cynBLEScanStopCommand, NULL, "Stop to scan BLE device"}, |
gillwei7 | 5:ee474e3133eb | 1078 | {"connect", cynBLEConnectCommand, NULL, "Connect to specific BLE device by Device Name, Usage: <NAME>"}, |
gillwei7 | 4:b52035367aee | 1079 | {"disconn", cynBLEDisconnectCommand, NULL, "Disconnection, Usage: cynb disconn"}, |
gillwei7 | 5:ee474e3133eb | 1080 | {"bleAddr", cynBLEAddressCommand, NULL, "Set/Get Bluetooth address, Usage: <ADDR>"}, |
gillwei7 | 5:ee474e3133eb | 1081 | {"update", cynBLEUpdateUUIDCommand, NULL, "Update value of specific characteristics, Usage: <UUID> <VALUE>"}, |
gillwei7 | 5:ee474e3133eb | 1082 | {"readData", cynBLEReadDataCommand, NULL, "Read value of specific characteristics, Usage: <SERVICE UUID> <CHAR UUID>"}, |
gillwei7 | 3:38ec8ad317f4 | 1083 | {NULL, NULL, NULL, NULL}, |
gillwei7 | 4:b52035367aee | 1084 | #endif |
gillwei7 | 0:5c195ab2f696 | 1085 | }; |