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