
fork test
Dependencies: BLE_API WIFI_API_32kRAM mbed nRF51822
Fork of NNN40_CLI by
Diff: CLI_Source/ble_cli.cpp
- Revision:
- 5:ee474e3133eb
- Parent:
- 4:b52035367aee
- Child:
- 6:1243f9e584f4
--- a/CLI_Source/ble_cli.cpp Sat Nov 07 09:02:38 2015 +0000 +++ b/CLI_Source/ble_cli.cpp Wed Dec 09 11:31:59 2015 +0000 @@ -28,7 +28,6 @@ #include "ble/UUID.h" #include "ble/DiscoveredCharacteristic.h" #include "ble/DiscoveredService.h" -//#include <vector> #define APP_ADV_INTERVAL 40 /**< The advertising interval (in units of ms). */ #define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout (in units of seconds). */ @@ -42,10 +41,10 @@ #define CLI_CHAR_MAX_NUM 10 #define MAX_VALUE_LENGTH 20 -#define CLI_FWVERION "DELTA_CLI_V1.6" +#define CLI_FWVERION "DELTA_CLI_V1.7" #define MODULE_NAME "DFCM-NNN40-DT1R" #define ADVERTISING_LED_PIN_NO LED1 -//#define BLE_DEBUG +//#define BLE_DEBUG //#define LBS_UUID_BASE {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00} #define NEW_VS_SERVICE 0x0001 #define DEFAULT_SCAN_INTERVAL 500 @@ -55,7 +54,6 @@ #define TARGET_DEVNAME_LEN 30 typedef struct bufferGattChar { -// bool flag; uint32_t props; UUID char_uuid; uint8_t value[MAX_VALUE_LENGTH]; @@ -64,7 +62,6 @@ } bufferGattChar_t; typedef struct bufferGattService { -// bool flag; UUID ser_uuid; bufferGattChar_t bufferGattChar[CLI_CHAR_MAX_NUM]; //uint16_t service_handle; @@ -82,31 +79,28 @@ extern Serial console; BLE deltaBLE; //gill 0904 extern const char* cyntecCommandErrorNames[]; -static bool connState = false; // gill 0904 +static bool connState = false; // gill 0904, define currently connecting state static uint8_t service_count=0; static uint8_t char_count=0; static uint16_t test_conn_handle; //Connection handle, assign after connected -extern bool advState; // currently no use +//extern bool advState; // currently no use static Gap::Address_t saveAddr[BLE_MAX_ADDRESS_NUMBER]; // check in advertisementCallback static uint8_t bleDevInd; //static Gap::GapState_t nowState; static char targetDevName[TARGET_DEVNAME_LEN]; -static char DEVICE_NAME[] = "DELTA_CLI"; // default device name +static char DEVICE_NAME[] = "nRF5x"; // default device name, same as defined in Gap.h static bool conn_action = false; // Gill add 20151015 static GattCharacteristic *charAry[CLI_SERVICE_MAX_NUM][CLI_CHAR_MAX_NUM]; DigitalOut led1(p7); DigitalOut led2(p13); +DigitalOut WriteInterrupt(p0); /****************************************************** * Function Definitions ******************************************************/ -DiscoveredCharacteristic ledCharacteristic; -void onTimeoutCallback(Gap::TimeoutSource_t source) -{ - led1 = !led1; -} +//DiscoveredCharacteristic ledCharacteristic; void serviceDiscoveryCallback(const DiscoveredService *service) { @@ -128,16 +122,15 @@ console.printf("characteristicDiscoveryCallback\r\n"); //printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); //if (characteristicP->getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */ - if(1) { - ledCharacteristic = *characteristicP; - } + //if(1) { +// ledCharacteristic = *characteristicP; +// } } void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { printf("terminated SD for handle %u\r\n", connectionHandle); } - void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params) { test_conn_handle = params->handle; @@ -155,6 +148,11 @@ // } } +void onTimeoutCallback(Gap::TimeoutSource_t source) +{ + led1 = !led1; +} + void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { //console.printf("connState:%d\r\n",connState); @@ -162,6 +160,15 @@ connState = false; } +void onDataWrittenCallback(const GattWriteCallbackParams *params) { + //if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { +// actuatedLED = *(params->data); +// } +#ifdef BLE_DEBUG + console.printf("onDataWritten\r\n"); +#endif + WriteInterrupt = !WriteInterrupt; +} static void cyntecPrintOk(void) { @@ -252,24 +259,24 @@ uint32_t err_code; ble_gap_conn_sec_mode_t sec_mode; //console.printf("%02X\r\n",cyntecGetTotalIndex()); - console.printf("\r\nSet BLE Name: "); + //console.printf("\r\nSet BLE Name: "); uint8_t i = 0; - for(i = 0; i < cyntecGetTotalIndex()-12; i++) { - console.printf("%c",argName2[11+i]); - } + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *) &argName2[11], (uint16_t) (cyntecGetTotalIndex()-12)); if (err_code == NRF_SUCCESS) { - cyntecPrintOk(); + console.printf("\r\nOK;"); + for(i = 0; i < cyntecGetTotalIndex()-12; i++) { + console.printf("%c",argName2[11+i]); + } + console.printf("\r\n"); } else { console.printf("ERROR;%04X\r\n",err_code); } APP_ERROR_CHECK(err_code); - - -//#ifdef DELTADEBUG +//#ifdef BLE_DEBUG //console.printf("\r\nSet BLE Name: "); // uint8_t i = 0; // for(i = 0; i < nameLen; i++) @@ -297,12 +304,11 @@ } } - static void cynBLEInfoCommand(void) { - console.printf("\r\nOK;F/W version:"); + console.printf("\r\nOK;"); console.printf(CLI_FWVERION); - console.printf(";Module name:"); + console.printf(";"); console.printf(MODULE_NAME); console.printf("\r\n\r\n"); } @@ -381,7 +387,7 @@ return; } -//#ifdef DELTADEBUG +//#ifdef BLE_DEBUG // uint8_t buf2[20]; // console.printf("\r\nSet BLE Address: "); // sprintf((char *)&buf2[0],(const char *)"[%02X %02X %02X %02X %02X %02X]", @@ -519,7 +525,7 @@ cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); return; } - + // If user input scan parameters, overwrite the default value if (cyntecGetCommandTokenCnt()== 5) { uint8_t argLen = 0; uint8_t *arg = cyntecGetCommandArgument(0,&argLen); @@ -537,7 +543,7 @@ deltaBLE.gap().setScanInterval(setInterval); deltaBLE.gap().setScanWindow(setWindow); deltaBLE.gap().setScanTimeout(setTimeout); - console.printf("Start Scanning\r\n"); + console.printf("Start Scan\r\n"); scan_start(); } @@ -593,7 +599,7 @@ uint8_t isValidGPIO(uint8_t num) { - uint8_t cfgPin[] = {0,1,2,3,4,5,6,7,13,16,17,23,24,25,29}; + uint8_t cfgPin[] = {0,4,5,6,7,13,21,23,24,25,29,30,31}; uint8_t i; for ( i = 0; i < sizeof(cfgPin) ; i++ ) { @@ -730,9 +736,8 @@ for (int i=0 ; i < (argLen-2)/2; i++) { bufVal[i] = cyntecArgToUint8(arg+2*(i+1), 2); } - for (uint16_t n=0; n<valueLen; n++) { - console.printf("%02X",bufVal[n]); - } + + for ( int i = 0 ; i < service_count ; i++ ) { if ( (bufferService[i].ser_uuid == buf_ser_uuid) & (readmatchFlag == false)) { for (int j = 0 ; j < CLI_CHAR_MAX_NUM ; j++ ) { @@ -743,10 +748,14 @@ err = deltaBLE.gattServer().write(valueAttr.getHandle(),bufVal,valueLen,true); else err = deltaBLE.gattServer().write(test_conn_handle,valueAttr.getHandle(),bufVal,valueLen,false); - //wait(5); //NO USE + #ifdef BLE_DEBUG console.printf("Write ERR:%i\r\n",err); #endif + console.printf("\r\nOK;"); + for (uint16_t n=0; n<valueLen; n++) { + console.printf("%02X",bufVal[n]); + } console.printf("\r\n"); readmatchFlag = true; return; @@ -823,6 +832,7 @@ #ifdef BLE_DEBUG console.printf("Read ERR:%i\r\n",err); #endif + console.printf("\r\nOK;"); console.printf("0x"); for (uint16_t n=0; n<valueLen; n++) { console.printf("%02X",bufVal[n]); @@ -990,10 +1000,11 @@ deltaBLE.onConnection(onConnectionCallback); deltaBLE.onTimeout(onTimeoutCallback); deltaBLE.gattClient().onDataRead(triggerRead); + deltaBLE.gattServer().onDataWritten(onDataWrittenCallback); cyntecPrintOk(); } - +#ifdef BLE_DEBUG static void cynBLETestCommand(void) { // gill test 1021 @@ -1018,7 +1029,7 @@ deltaBLE.gattServer().read(valueAttr.getHandle(),valRead,valueLenPtr); console.printf("gatt val:%02X %02X\r\n",valRead[0],valRead[1]); } - +#endif CyntecCommandEntry bleCommandSets[] = { // General @@ -1049,26 +1060,26 @@ {"RED", cynBLEReadDataCommand, NULL, "Read value of specific characteristics"}, {NULL, NULL, NULL, NULL}, #else - {"init", cynBLEInitCommand, NULL, "Init BLE stack, Usage: cynb init"}, - {"gpio", cynBLEGPIOCommand, NULL, "Config gpio, Usage: cynb gpio <GPIO NO> <set|clear>"}, - {"sleep", cynBLESystemOffCommand, NULL, "System off mode, Usage: cynb sleep <GPIO NO>"}, - {"reset", cynResetCommand, NULL, "Soft reset, Usage: cynb reset"}, - {"info", cynBLEInfoCommand, NULL, "Module information, Usage: cynb info"}, - {"txPow", cynBLESetTxPowerCommand, NULL, "Set BLE tx power"}, - {"name", cynBLENameCommand, NULL, "Set/Get friendly for BLE module"}, + {"init", cynBLEInitCommand, NULL, "Init BLE stack"}, + {"gpio", cynBLEGPIOCommand, NULL, "Config gpio, Usage: <GPIO NO> <set|clear>"}, + {"sleep", cynBLESystemOffCommand, NULL, "System off mode, Usage: <GPIO NO>"}, + {"reset", cynResetCommand, NULL, "Soft reset"}, + {"info", cynBLEInfoCommand, NULL, "Get module information"}, + {"txPow", cynBLESetTxPowerCommand, NULL, "Set BLE tx power, Usage: <POWER>"}, + {"name", cynBLENameCommand, NULL, "Set/Get friendly for BLE module, Usage: *<NAME>*"}, // GATT - {"regService", cynRegServiceCommand, NULL, "Test"}, - {"gattChar", cynGattCharCommand, NULL, "Set SIG defined characteristic or Create your own"}, - {"gattService", cynGattServiceCommand, NULL, "Set SIG defined service or Create your own"}, - {"advStart", cynAdvertiseStartCommand, NULL, "Start broadcast advertise packet"}, + {"regService", cynRegServiceCommand, NULL, "Register standby service"}, + {"gattChar", cynGattCharCommand, NULL, "Set SIG defined characteristic or Create your own,Usage: <CHAR UUID> <PROPS> <VALUE>"}, + {"gattService", cynGattServiceCommand, NULL, "Set SIG defined service or Create your own,Usage: <SERVICE UUID>"}, + {"advStart", cynAdvertiseStartCommand, NULL, "Start broadcast advertise packet,Usage: <INTERVAL> <WINDOW>"}, {"advStop", cynAdvertiseStopCommand, NULL, "Stop broadcast advertise packet"}, - {"scanStart", cynBLEScanCommand, NULL, "Start to scan BLE device"}, + {"scanStart", cynBLEScanCommand, NULL, "Start to scan BLE device, Usage: <INTERVAL> <WINDOW> <TIMEOUT>"}, {"scanStop", cynBLEScanStopCommand, NULL, "Stop to scan BLE device"}, - {"connect", cynBLEConnectCommand, NULL, "Connect to specific BLE device by Device Name"}, + {"connect", cynBLEConnectCommand, NULL, "Connect to specific BLE device by Device Name, Usage: <NAME>"}, {"disconn", cynBLEDisconnectCommand, NULL, "Disconnection, Usage: cynb disconn"}, - {"bleAddr", cynBLEAddressCommand, NULL, "Set/Get Bluetooth address"}, - {"update", cynBLEUpdateUUIDCommand, NULL, "Update value of specific characteristics"}, - {"readData", cynBLEReadDataCommand, NULL, "Read value of specific characteristics"}, + {"bleAddr", cynBLEAddressCommand, NULL, "Set/Get Bluetooth address, Usage: <ADDR>"}, + {"update", cynBLEUpdateUUIDCommand, NULL, "Update value of specific characteristics, Usage: <UUID> <VALUE>"}, + {"readData", cynBLEReadDataCommand, NULL, "Read value of specific characteristics, Usage: <SERVICE UUID> <CHAR UUID>"}, {NULL, NULL, NULL, NULL}, #endif };