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