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