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