fork test

Dependencies:   BLE_API WIFI_API_32kRAM mbed nRF51822

Fork of NNN40_CLI by Delta

Revision:
14:397f6514bb2c
Parent:
13:7eaec228ad23
Child:
15:d627ee60bb78
--- a/CLI_Source/ble_cli.cpp	Thu Feb 25 07:22:37 2016 +0000
+++ b/CLI_Source/ble_cli.cpp	Mon Apr 25 03:46:52 2016 +0000
@@ -8,9 +8,9 @@
 #include "ble_cli.h"   //  All #include define in ble_cli.h
 
 // Genral configuration parameters
-//#define BLE_DEBUG
+#define BLE_DEBUG  0
 #define MAX_DEVNAME_LEN 32
-#define CLI_FWVERION "DELTA_CLI_V1.11"
+#define CLI_FWVERION "DELTA_CLI_V1.12"
 #define MODULE_NAME "DFCM-NNN40-DT1R"
 
 // Advertising configuration parameters
@@ -36,7 +36,7 @@
     UUID char_uuid;
     uint8_t value[MAX_VALUE_LENGTH];
     uint16_t char_value_handle;
-    uint8_t valueLength;
+    uint16_t valueLength;
 } bufferGattChar_t;
 
 typedef struct bufferGattService {
@@ -114,7 +114,7 @@
 {
     test_conn_handle = params->handle;
     connState = true;
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("Connect: connState write to %d\r\n",connState);
 #endif
     // gill test
@@ -138,13 +138,13 @@
 
 void onDataWrittenCallback(const GattWriteCallbackParams *params)
 {
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("onDataWritten\r\n");
 #endif
     // trigger Host GPIO interrupt
     BLEWriteInt = !BLEWriteInt;
     BLEWriteInt = !BLEWriteInt;
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("handle:%04X\r\n",params->handle);
     console.printf("conn_handle:%04X,writeOp:%d,offset:%04X\r\n",params->connHandle,params->writeOp,params->offset);
 #endif
@@ -157,7 +157,7 @@
         for (int j = 0 ; j < CLI_CHAR_MAX_NUM ; j++ ) {
             GattAttribute& tempValueAttr = charAry[i][j]->getValueAttribute();
             if (tempValueAttr.getHandle()==params->handle) {
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
                 console.printf("ser_count,char_count:%d,%d\r\n",i,j);
 #endif
                 if (bufferService[i].ser_uuid.shortOrLong()==0)
@@ -179,6 +179,8 @@
                     }
                 }
                 console.printf(",");
+								// update char value length
+								bufferService[i].bufferGattChar[j].valueLength = params->len;
                 break;
             }
         }
@@ -236,7 +238,7 @@
 //        cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
 //        return;
 //    }
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("advInterval:%d,advTimeout:%d",advInterval,advTimeout);
 #endif
     uint8_t bleName[BLE_GAP_DEVNAME_MAX_LEN] = {"\0"};
@@ -244,7 +246,7 @@
     uint32_t err_code;
 
     err_code = sd_ble_gap_device_name_get(&bleName[APP_ADV_DATA_OFFSET], &bleLen);
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("%08X,%s\r\n",err_code,bleName+APP_ADV_DATA_OFFSET);
 #endif
     deltaBLE.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
@@ -440,7 +442,7 @@
             return;
         }
 
-//#ifdef BLE_DEBUG
+//#if BLE_DEBUG
 //		uint8_t buf2[20];
 //		console.printf("\r\nSet BLE Address: ");
 //		sprintf((char *)&buf2[0],(const char *)"[%02X %02X %02X %02X %02X %02X]",
@@ -496,7 +498,7 @@
 
     for(int i=0; i<BLE_MAX_ADDRESS_NUMBER; i++) {
         if (memcmp(newAddr,saveAddr[i],6)==0) {
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
             console.printf("Repeated\r\n");
 #endif
             flagRepeat = true;
@@ -504,7 +506,7 @@
         }
     }
 
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("addr cmp result :%i\r\n",memcmp(newAddr,params->peerAddr,6));
     console.printf("ADV data:%X,%i\r\n",&(params->advertisingData),params->advertisingDataLen);
 #endif
@@ -517,7 +519,7 @@
     uint32_t err_code = adv_report_parse(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
                                          &adv_data,
                                          &type_data);
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("error code:%X\r\n",err_code);
     console.printf("type_data.data_len:%i\r\n",type_data.data_len);
 #endif
@@ -588,7 +590,7 @@
         arg = cyntecGetCommandArgument(2,&argLen);
         setTimeout = cyntecAtoiUint16( arg, argLen );
     }
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("Interval:%d,Window:%d,timeout:%d\r\n",setInterval,setWindow,setTimeout);
 #endif
     memset(saveAddr,0,sizeof(saveAddr)); // Clear saving address set
@@ -628,7 +630,7 @@
 
     memset( targetDevName , 0, TARGET_DEVNAME_LEN);
     memcpy( targetDevName, argDevName, devNameLen);
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("Search for device name:%s\r\n",argDevName);
     console.printf("Target:%s\r\n",targetDevName);
 #endif
@@ -644,7 +646,7 @@
     err_code = deltaBLE.gap().disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION);
     //err_code = sd_ble_gap_disconnect(test_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
     //err_code = sd_ble_gap_connect_cancel();	// No function defined currently
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("Error:%d\r\n",err_code);
 #endif
     cyntecPrintOk();
@@ -745,9 +747,9 @@
 //        ledCharacteristic.write(1, &toggledValue);
 //    }
 }
-static void cynBLEUpdateUUIDCommand(void)
+static void cynBLEUpdateDataCommand(void)
 {
-    if (cyntecGetCommandTokenCnt() != 5) {
+    if (cyntecGetCommandTokenCnt() != 6) {
         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
         return;
     }
@@ -783,14 +785,38 @@
         }
         buf_char_uuid.setupLong(bufferUuidVs);
     }
-
+		// Input Value Type
+		arg = cyntecGetCommandArgument(2,&argLen);
+		uint8_t type = *arg;
+		//type = cyntecArgToUint8(arg,2);
+		//printf("type:%d\r\n",type);
+		if(type != '0' && type != '1') // Original Data
+	 {
+		 cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
+	 }
+		
     // Input value
-    arg = cyntecGetCommandArgument(2,&argLen);
-    valueLen = (argLen-2)/2; // uint8_t to uint16_t transform
-    for (int i=0 ; i < (argLen-2)/2; i++) {
-        bufVal[i]  = cyntecArgToUint8(arg+2*(i+1), 2);
-    }
-
+    arg = cyntecGetCommandArgument(3,&argLen);
+	 if(type == '0') // Original Data
+	 {
+			valueLen = argLen; 
+			memcpy(bufVal,arg,argLen);
+	 }
+	 if(type == '1') // Char Data
+	 {
+			valueLen = (argLen-2)/2; // uint8_t to uint16_t transform
+			for (int i=0 ; i < valueLen; i++) {
+				bufVal[i] = cyntecArgToUint8(arg+2+2*i,2);
+			}
+	 }
+#if BLE_DEBUG
+	 printf("type:%i,bufVal:",type);
+	 printf("valueLength:%i\r\n",valueLen);
+		for (int i=0 ; i < valueLen; i++) {
+				printf("%02X ",bufVal[i]);
+			}
+		printf("\r\n");
+#endif
 
     for ( int i = 0 ; i < service_count ; i++ ) {
         if ( (bufferService[i].ser_uuid == buf_ser_uuid) & (readmatchFlag == false)) {
@@ -802,9 +828,13 @@
                         err = deltaBLE.gattServer().write(valueAttr.getHandle(),bufVal,valueLen,true);
                     else
                         err = deltaBLE.gattServer().write(test_conn_handle,valueAttr.getHandle(),bufVal,valueLen,false);
+										
+										// update valueLength
+										bufferService[i].bufferGattChar[j].valueLength = valueLen;
 
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
                     console.printf("Write ERR:%i\r\n",err);
+										printf("valueLen:%i\r\n",valueLen);
 #endif
                     console.printf("\r\nOK;");
                     for (uint16_t n=0; n<valueLen; n++) {
@@ -825,18 +855,18 @@
 
 static void cynBLEReadDataCommand(void)
 {
-#ifdef BLE_DEBUG
-    printf("Saved Char:\r\n");
-    for (int i=0; i<service_count; i++) {
-        console.printf("ser_uuid:%04X",bufferService[service_count-1].ser_uuid.getShortUUID());
-        for (int j=0; j<CLI_CHAR_MAX_NUM; j++) {
-            printf(" %i,char_uuid%04X,",j,bufferService[service_count-1].bufferGattChar[char_count-1].char_uuid.getShortUUID());
-            printf("len%d,",bufferService[service_count-1].bufferGattChar[char_count-1].valueLength);
-            printf("val%02X;",bufferService[service_count-1].bufferGattChar[char_count-1].value[0]);
-        }
-    }
-    printf("\r\n");
-#endif
+//#if BLE_DEBUG
+//    printf("Saved Char:\r\n");
+//    for (int i=0; i<service_count; i++) {
+//        console.printf("ser_uuid:%04X",bufferService[service_count-1].ser_uuid.getShortUUID());
+//        for (int j=0; j<CLI_CHAR_MAX_NUM; j++) {
+//            printf(" %i,char_uuid%04X,",j,bufferService[service_count-1].bufferGattChar[char_count-1].char_uuid.getShortUUID());
+//            printf("len%d,",bufferService[service_count-1].bufferGattChar[char_count-1].valueLength);
+//            printf("val%02X;",bufferService[service_count-1].bufferGattChar[char_count-1].value[0]);
+//        }
+//    }
+//    printf("\r\n");
+//#endif
     if (cyntecGetCommandTokenCnt() != 4) {
         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
         return;
@@ -846,7 +876,7 @@
     UUID buf_char_uuid;
     uint8_t bufVal[MAX_VALUE_LENGTH] = {0};
     uint16_t valueLen=0;
-    uint16_t * valueLenPtr;
+    //uint16_t * valueLenPtr;
     bool readmatchFlag = false;
     uint8_t argLen = 0;
     uint8_t *arg = cyntecGetCommandArgument(0,&argLen);
@@ -874,17 +904,19 @@
         }
         buf_char_uuid.setupLong(bufferUuidVs);
     }
-
+		
     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++ ) {
                 if (( bufferService[i].bufferGattChar[j].char_uuid == buf_char_uuid)& (readmatchFlag == false)) {
                     GattAttribute& valueAttr = charAry[i][j]->getValueAttribute();
-                    valueLenPtr = valueAttr.getLengthPtr();
-                    valueLen = valueAttr.getLength();
+//                    valueLenPtr = valueAttr.getLengthPtr();
+                  valueLen = bufferService[i].bufferGattChar[j].valueLength;
+										uint16_t * valueLenPtr = &bufferService[i].bufferGattChar[j].valueLength;
                     ble_error_t err = deltaBLE.gattServer().read(valueAttr.getHandle(),bufVal,valueLenPtr);
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
                     console.printf("Read ERR:%i\r\n",err);
+										printf("valueLen:%i\r\n",valueLen);
 #endif
                     console.printf("\r\nOK;");
                     console.printf("0x");
@@ -961,7 +993,7 @@
         bufferService[service_count-1].bufferGattChar[char_count].value[i]  = cyntecArgToUint8(arg+2*(i+1), 2);
         //console.printf("%02X ",bufferService[service_count-1].bufferGattChar[char_count].value[i]);
     }
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("valueLengthBuffer:%d\r\n",valueLengthBuffer);
     console.printf("value:");
     for (i=0 ; i < valueLengthBuffer; i++) {
@@ -973,7 +1005,7 @@
     //bufferService[service_count-1].bufferGattChar[char_count].char_value_handle = testHandle;
     if (char_count>CLI_CHAR_MAX_NUM) {
         cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
         console.printf("Error: char_count>CLI_CHAR_MAX_NUM\r\n");
 #endif
         return;
@@ -987,7 +1019,7 @@
     //GattCharacteristic *charAry[char_count];
     //GattCharacteristic **charAry;
     //charAry = new GattCharacteristic *[char_count];
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     console.printf("Current char_count:%d\r\n",char_count);
     console.printf("Current service_count:%d\r\n",service_count);
 #endif
@@ -1004,7 +1036,7 @@
     ble_error_t err_code;
     err_code = deltaBLE.addService(newService);
     if (err_code != 0) {
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
         console.printf("addService error:%d\r\n",err_code);
 #endif
         cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
@@ -1027,7 +1059,7 @@
     if ( argLen == 6 && arg[0] == '0' && arg[1] == 'x') {
         UUID::ShortUUIDBytes_t buffer_uuid_short;
         buffer_uuid_short = cyntecArgToUint16( (arg + 2), (argLen - 2));
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
         console.printf("%4X",buffer_uuid_short);
 #endif
         UUID uuid_short(buffer_uuid_short);
@@ -1041,7 +1073,7 @@
         UUID::LongUUIDBytes_t buffer_uuid_vs;
         for (uint8_t i=0; i<16; i++) {
             buffer_uuid_vs[i] = cyntecArgToUint8( (arg + 2+2*i), 2);
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
             console.printf("%2X ",buffer_uuid_vs[i]);
 #endif
         }
@@ -1074,7 +1106,7 @@
     deltaBLE.gattServer().onDataWritten().detach(onDataWrittenCallback);
 }
 
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
 static void cynBLETestCommand(void)
 {
     // gill test 1021
@@ -1097,7 +1129,7 @@
 
 CyntecCommandEntry bleCommandSets[] = {
 // General
-#ifdef BLE_DEBUG
+#if BLE_DEBUG
     {"TEST", cynBLETestCommand, NULL, "test"},
 #endif
 //
@@ -1120,7 +1152,7 @@
     {"CON", cynBLEConnectCommand, NULL, "Connect to specific BLE device by Device Name"},
     {"DCN", cynBLEDisconnectCommand, NULL, "Disconnection, Usage: cynb disconn"},
     {"ADR", cynBLEAddressCommand, NULL, "Set/Get Bluetooth address"},
-    {"WRT", cynBLEUpdateUUIDCommand, NULL, "Update value of specific characteristics"},
+    {"WRT", cynBLEUpdateDataCommand, NULL, "Update value of specific characteristics"},
     {"RED", cynBLEReadDataCommand, NULL, "Read value of specific characteristics"},
     {"EDI", cynBLEDataIntCommand, NULL, "Trigger remote write detection, give interrupt to Host."},
     {"DDI", cynBLEDisDataIntCommand, NULL, "Disable remote write detection."},
@@ -1144,7 +1176,7 @@
     {"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, Usage: <ADDR>"},
-    {"update", cynBLEUpdateUUIDCommand, NULL, "Update value of specific characteristics, Usage: <UUID> <VALUE>"},
+    {"update", cynBLEUpdateDataCommand, NULL, "Update value of specific characteristics, Usage: <UUID> <VALUE>"},
     {"readData", cynBLEReadDataCommand, NULL, "Read value of specific characteristics, Usage: <SERVICE UUID> <CHAR UUID>"},
     {"enInt", cynBLEDataIntCommand, NULL, "Trigger remote write detection, give interrupt to Host."},
     {"disInt", cynBLEDisDataIntCommand, NULL, "Disable remote write detection."},