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