From f7d26054edfb58e57e0897a36758a2cf88cc626f Mon Sep 17 00:00:00 2001 From: "vimala.v" Date: Mon, 4 May 2015 09:19:33 +0530 Subject: [PATCH] BLE Adapter implementation for Arduino Change-Id: I99c3dc7074f255e6b5549977844a477d4b331a7d Signed-off-by: vimala.v Reviewed-on: https://gerrit.iotivity.org/gerrit/734 Tested-by: jenkins-iotivity Reviewed-by: Joseph Morrow Reviewed-by: Erich Keane --- extlibs/arduino/SConscript | 6 +++ .../build/arduino/Arduino_Setup_README.txt | 6 +-- .../connectivity/inc/caleadapter_singlethread.h | 5 +- .../connectivity/inc/caleinterface_singlethread.h | 17 +++--- .../src/bt_le_adapter/arduino/cablenwmonitor.cpp | 8 +-- .../src/bt_le_adapter/arduino/cableserver.cpp | 9 +--- .../src/bt_le_adapter/arduino/cableserver.h | 2 +- .../src/bt_le_adapter/caleadapter_singlethread.c | 60 +++++++++++++++++----- 8 files changed, 76 insertions(+), 37 deletions(-) diff --git a/extlibs/arduino/SConscript b/extlibs/arduino/SConscript index 22311a5..268c061 100644 --- a/extlibs/arduino/SConscript +++ b/extlibs/arduino/SConscript @@ -140,6 +140,12 @@ if target_os == 'arduino': shutil.move('ble-sdk-arduino-0.9.5.beta/libraries/BLE/', '.') shutil.rmtree('ble-sdk-arduino-0.9.5.beta') + # Apply Nordic lib patches + print 'Patching Nordic library...' + os.chdir(arduinolib_dir + '/libraries/BLE/') + os.system("find . -type f -exec dos2unix {} \;") + os.system("patch -p1 < " + src_dir + "/resource/csdk/connectivity/lib/arduino/arduino_due_ble.patch") + # Remove downloaded file os.remove(nordiclib_zip_file) diff --git a/resource/csdk/connectivity/build/arduino/Arduino_Setup_README.txt b/resource/csdk/connectivity/build/arduino/Arduino_Setup_README.txt index f3217e2..359ded0 100644 --- a/resource/csdk/connectivity/build/arduino/Arduino_Setup_README.txt +++ b/resource/csdk/connectivity/build/arduino/Arduino_Setup_README.txt @@ -11,7 +11,7 @@ STEPS to setup Arduino on linux machine for building CA code: 4.1) ONE TIME ONLY - ADDITIONAL LIBRARIES (Time) Arduino builds are dependent on latest Time library. Download it from here: - I. http://playground.arduino.cc/Code/Time + I. http://playground.arduino.cc/uploads/Code/Time.zip Place this library in the "ARDUINO_DIR/libraries/Time" folder where other libraries are present. [P.S - The Time library files path should be as follows "ARDUINO_DIR/libraries/Time/Time/{***files***}"] Some files in this library has windows style line endings, for patch to run properly, please execute following command: @@ -20,14 +20,14 @@ STEPS to setup Arduino on linux machine for building CA code: 4.2) ONE TIME ONLY - ADDITIONAL LIBRARIES (BLE) Note: Prior to building for BLE Transport, following additional steps are required: - (1) Download RedBearLab Arduino RBL_nRF8001 Library from link(https://github.com/RedBearLab/nRF8001/tree/25643e7b1b7da3740406325a471e3faa4b948747) + (1) Download RedBearLab Arduino RBL_nRF8001 Library from link(https://github.com/RedBearLab/nRF8001/archive/25643e7b1b7da3740406325a471e3faa4b948747.zip) (2) Extract zip file and apply RBL patch(connectivity/lib/arduino/RBL_nRF8001.patch) to "RBL_nRF8001" folder by following below steps, i) Go to downloaded RBL_Library Folder ("nRF8001-master/Arduino/libraries/RBL_nRF8001") ii) Apply dos2unix for RBL_Library Folder and RBL Patch File (RBL_nRF8001.patch), if we have downloaded Library in Non UNIX Platform. iii) Apply RBL Patch[connectivity\lib\arduino\RBL_nRF8001.patch] by using the below command patch -p1 < RBL_Patch_File_Location(RBL_nRF8001.patch) (3) Copy the patched RBL_nRF8001 Library and place it in "arduino-/libraries" folder - (4) Download Nordic Arduino BLE library [Tag: "0.9.5.Beta" Version] from the link (https://github.com/NordicSemiconductor/ble-sdk-arduino). + (4) Download Nordic Arduino BLE library [Tag: "0.9.5.Beta" Version] from the link (https://github.com/NordicSemiconductor/ble-sdk-arduino/archive/0.9.5.beta.zip). Extract the zip file and copy the "BLE" folder available under "libraries" folder and place it in "arduino-/libraries" folder i) Apply dos2unix for the "BLE" folder. ii) Go to the BLE folder and apply the patch[connectivity\lib\arduino\arduino_due_ble.patch] using the below command. diff --git a/resource/csdk/connectivity/inc/caleadapter_singlethread.h b/resource/csdk/connectivity/inc/caleadapter_singlethread.h index 36ced7c..4ca59c1 100644 --- a/resource/csdk/connectivity/inc/caleadapter_singlethread.h +++ b/resource/csdk/connectivity/inc/caleadapter_singlethread.h @@ -372,7 +372,7 @@ void CABLEServerSendDataThread(void *threadData); * @retval #CA_STATUS_FAILED Operation failed */ CALEData_t *CACreateBLEData(const CARemoteEndpoint_t *remoteEndpoint, const void *data, - uint32_t dataLength); + uint32_t dataLength); /** * @brief Used to free the BLE information stored in the sender/receiver queues. @@ -401,7 +401,8 @@ typedef void (*CALEDeviceStateChangedCallback)(CAAdapterState_t adapter_state); * @retval #CA_STATUS_FAILED Operation failed */ typedef CAResult_t (*CABLEClientDataReceivedCallback)(const char *remoteAddress, - const char *serviceUUID, const void *data, uint32_t dataLength, uint32_t *sentLength); + const char *serviceUUID, const void *data, uint32_t dataLength, + uint32_t *sentLength); /** * @brief This will be used to notify that network packet recieved from GATTServer to adapter layer. diff --git a/resource/csdk/connectivity/inc/caleinterface_singlethread.h b/resource/csdk/connectivity/inc/caleinterface_singlethread.h index c9d9339..06fe17c 100644 --- a/resource/csdk/connectivity/inc/caleinterface_singlethread.h +++ b/resource/csdk/connectivity/inc/caleinterface_singlethread.h @@ -89,11 +89,14 @@ CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback) /** * @brief Provides the BD address of the local adapter. - * @param local_address [IN] pointer to the location where bd address needs to be stored. + * @param local_address [OUT] pointer to the location where bd address needs to be stored. * - * @return NONE + * @return #CA_STATUS_OK or Appropriate error code + * @retval #CA_STATUS_OK Successful + * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets + * @retval #CA_STATUS_FAILED Operation failed */ -void CAGetLEAddress(char **local_address); +CAResult_t CAGetLEAddress(char **local_address); /** * @brief Used to start Gatt Server thread for service creation and advertise ble service. @@ -136,8 +139,8 @@ void CASetBLEReqRespServerCallback(CABLEServerDataReceivedCallback callback); * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets * @retval #CA_STATUS_FAILED Operation failed */ -CAResult_t CAUpdateCharacteristicsInGattServer(const char *charValue, - uint32_t charValueLen); +CAResult_t CAUpdateCharacteristicsToAllGattClients(const char *charValue, + uint8_t charValueLen); /** * @brief Used to start CAStartBleGattClientThread for initializing Gatt Client @@ -180,7 +183,7 @@ void CATerminateBLEGattClient(); * @retval #CA_STATUS_FAILED Operation failed */ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, const char *data, - const int32_t dataLen, CALETransferType_t type, + const uint32_t dataLen, CALETransferType_t type, const int32_t position); /** @@ -194,7 +197,7 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, const * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets * @retval #CA_STATUS_FAILED Operation failed */ -CAResult_t CAUpdateCharacteristicsToAllGattServers(const char *data, int32_t dataLen); +CAResult_t CAUpdateCharacteristicsToAllGattServers(const char *data, uint32_t dataLen); /** * @brief Used to store upper layer callback locally which will be used to send the data to diff --git a/resource/csdk/connectivity/src/bt_le_adapter/arduino/cablenwmonitor.cpp b/resource/csdk/connectivity/src/bt_le_adapter/arduino/cablenwmonitor.cpp index 1a2b5a5..a61c483 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/arduino/cablenwmonitor.cpp +++ b/resource/csdk/connectivity/src/bt_le_adapter/arduino/cablenwmonitor.cpp @@ -70,10 +70,10 @@ CAResult_t CAGetLEAdapterState() return CA_STATUS_OK; } -void CAGetLEAddress(char **leAddress) +CAResult_t CAGetLEAddress(char **leAddress) { OIC_LOG(DEBUG, TAG, "IN"); - VERIFY_NON_NULL_VOID(leAddress, TAG, "leAddress"); + VERIFY_NON_NULL(leAddress, TAG, "leAddress"); g_leAddress = ble_getAddress(); /** @@ -83,11 +83,11 @@ void CAGetLEAddress(char **leAddress) if (NULL == *leAddress) { OIC_LOG(ERROR, TAG, "malloc fail"); - return; + return CA_MEMORY_ALLOC_FAILED; } memcpy(*leAddress, g_leAddress, CA_MACADDR_SIZE); OIC_LOG(DEBUG, TAG, "OUT"); - return; + return CA_STATUS_OK; } CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback) diff --git a/resource/csdk/connectivity/src/bt_le_adapter/arduino/cableserver.cpp b/resource/csdk/connectivity/src/bt_le_adapter/arduino/cableserver.cpp index 5c6f748..1ad39b3 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/arduino/cableserver.cpp +++ b/resource/csdk/connectivity/src/bt_le_adapter/arduino/cableserver.cpp @@ -79,16 +79,11 @@ CAResult_t CABleDoEvents() return CA_STATUS_OK; } -CAResult_t CAUpdateCharacteristicsInGattServer(const char *char_value, - uint32_t valueLength) +CAResult_t CAUpdateCharacteristicsToAllGattClients(const char *char_value, + uint8_t valueLength) { // ble_write_bytes() api can send only max of 255 bytes at a time // This function shall never be called to send more than 255 bytes by the fragmentation logic. - if(valueLength > 255) - { - OIC_LOG_V(ERROR, TAG, "Error:Max 255"); - return CA_STATUS_INVALID_PARAM; - } // Currently ble_write_bytes api returns void. ble_write_bytes((unsigned char *)char_value, (unsigned char)valueLength); return CA_STATUS_OK; diff --git a/resource/csdk/connectivity/src/bt_le_adapter/arduino/cableserver.h b/resource/csdk/connectivity/src/bt_le_adapter/arduino/cableserver.h index 04d18e7..1fa94d6 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/arduino/cableserver.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/arduino/cableserver.h @@ -22,7 +22,7 @@ * @file * * This file contains the APIs related to the GATT Server functionalities. - * Creation of the GattServer with the characteristics. Enabling the + * Creation of the GATT Server with the characteristics. Enabling the * advertisement and updating the characteristics for the response and * notifying the change of characteristcs for the request will be done here. */ diff --git a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter_singlethread.c b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter_singlethread.c index a33e260..018c9bc 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter_singlethread.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter_singlethread.c @@ -121,12 +121,11 @@ CAResult_t CAStartLEListeningServer() OIC_LOG_V(ERROR, TAG, "ble init fail: %d", result); return CA_STATUS_FAILED; } - uint32_t iter = 0; /** * Below for loop is to process the BLE Events received from BLE Shield. * BLE Events includes BLE Shield Address Added as a patch to RBL Library. */ - for (iter = 0; iter < MAX_EVENT_COUNT; iter++) + for (int iter = 0; iter < MAX_EVENT_COUNT; iter++) { CACheckData(); } @@ -195,7 +194,19 @@ CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t **info, uint32_t *s } char *leAddress = NULL; - CAGetLEAddress(&leAddress); + CAResult_t res = CAGetLEAddress(&leAddress); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CAGetLEAddress has failed"); + return res; + } + + if (NULL == leAddress) + { + OIC_LOG(ERROR, TAG, "Failed to get Le addr"); + return CA_STATUS_FAILED; + } + OIC_LOG_V(DEBUG, TAG, "leAddress = %s", leAddress); /** @@ -316,7 +327,7 @@ void CACheckData() return; } - g_coapBuffer = (char *)OICCalloc(g_packetDataLen, sizeof(char)); + g_coapBuffer = (char *)OICCalloc((size_t)g_packetDataLen, sizeof(char)); if (NULL == g_coapBuffer) { OIC_LOG(ERROR, TAG, "malloc"); @@ -365,28 +376,51 @@ int32_t CASendLEData(const void *data, uint32_t dataLen) if (CA_STATUS_OK != result) { + OIC_LOG(ERROR, TAG, "Generate header failed"); return -1; } if (!CAIsBleConnected()) { - OIC_LOG(DEBUG, TAG, "le not conn"); + OIC_LOG(ERROR, TAG, "le not conn"); + return -1; + } + + result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH); + if (CA_STATUS_OK != result) + { + OIC_LOG(ERROR, TAG, "Update characteristics failed"); return -1; } - CAUpdateCharacteristicsInGattServer(header, CA_HEADER_LENGTH); int32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE; - int32_t iter = 0; - for (iter = 0; iter < dataLimit; iter++) + for (int32_t iter = 0; iter < dataLimit; iter++) { - CAUpdateCharacteristicsInGattServer((data + (iter * CA_SUPPORTED_BLE_MTU_SIZE)), - CA_SUPPORTED_BLE_MTU_SIZE); + result = CAUpdateCharacteristicsToAllGattClients((data + + (iter * CA_SUPPORTED_BLE_MTU_SIZE)), + CA_SUPPORTED_BLE_MTU_SIZE); + if (CA_STATUS_OK != result) + { + OIC_LOG(ERROR, TAG, "Update characteristics failed"); + return -1; + } + CABleDoEvents(); + } + + uint8_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE; + if(remainingLen) + { + result = CAUpdateCharacteristicsToAllGattClients((data + + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)), + remainingLen); + if (CA_STATUS_OK != result) + { + OIC_LOG(ERROR, TAG, "Update characteristics failed"); + return -1; + } CABleDoEvents(); } - CAUpdateCharacteristicsInGattServer((data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)), - dataLen % CA_SUPPORTED_BLE_MTU_SIZE); - CABleDoEvents(); OIC_LOG(DEBUG, TAG, "writebytes done"); OIC_LOG(DEBUG, TAG, "OUT"); // Arduino BLEWrite doesnot return value. So, Return the received DataLength -- 2.7.4