From 2cd93cfc5e4b61a084f20f63539c711ae868652b Mon Sep 17 00:00:00 2001 From: "koushik.girijala" Date: Wed, 30 Dec 2015 11:07:57 +0530 Subject: [PATCH 1/1] CoAP over TCP transmission over BT support for Tizen currently, CoAP/UDP message format is used over BT-RFCOMM. but BT-RFCOMM data transmission requires dedicated session between devices and guarantees reliable data transmission by its MAC & PHY layer. So, CoAP/TCP message format might be more efficient for Bluetooth Change-Id: Ifd4e47174734cf21de7245d3e6a055f8952ce730 Signed-off-by: koushik.girijala Reviewed-on: https://gerrit.iotivity.org/gerrit/4739 Tested-by: jenkins-iotivity Reviewed-by: Jon A. Cruz --- resource/csdk/connectivity/src/SConscript | 0 .../src/bt_edr_adapter/android/caedrutils.h | 1 - .../src/bt_edr_adapter/tizen/caedrclient.c | 93 ++++++++++++++++++++-- .../src/bt_edr_adapter/tizen/caedrutils.c | 70 ++++++++++++++++ .../src/bt_edr_adapter/tizen/caedrutils.h | 16 ++++ 5 files changed, 174 insertions(+), 6 deletions(-) mode change 100755 => 100644 resource/csdk/connectivity/src/SConscript diff --git a/resource/csdk/connectivity/src/SConscript b/resource/csdk/connectivity/src/SConscript old mode 100755 new mode 100644 diff --git a/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrutils.h b/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrutils.h index 85b4a52..7d1601e 100644 --- a/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrutils.h +++ b/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrutils.h @@ -27,7 +27,6 @@ #include "cacommon.h" #include "cathreadpool.h" -#include "uarraylist.h" #include "caedrinterface.h" #include "jni.h" diff --git a/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrclient.c b/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrclient.c index fcd1db9..38f5945 100644 --- a/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrclient.c +++ b/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrclient.c @@ -40,6 +40,12 @@ #define MICROSECS_PER_SEC 1000000 /** + * Maximum CoAP over TCP header length + * to know the total data length. + */ +#define EDR_MAX_HEADER_LEN 6 + +/** * Mutex to synchronize the access to Bluetooth device information list. */ static ca_mutex g_edrDeviceListMutex = NULL; @@ -947,18 +953,95 @@ void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData) } ca_mutex_unlock(g_edrDeviceListMutex); + //: TODO Need to check if 'check required for socket still connected or not' if (!device) { OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!"); return; } - uint32_t sentLength = 0; + CAConnectedDeviceInfo_t *deviceInfo = + (CAConnectedDeviceInfo_t *) CAEDRGetDeviceInfoFromAddress(device->remoteAddress); + + if (!deviceInfo) + { + OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Received Data from new device"); + deviceInfo = (CAConnectedDeviceInfo_t *) OICCalloc(1, sizeof(*deviceInfo)); + if (!deviceInfo) + { + OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Out of memory"); + return; + } + + deviceInfo->state = STATE_CONNECTED; + deviceInfo->recvData = NULL; + deviceInfo->recvDataLen = 0; + deviceInfo->totalDataLen = 0; + result = CAEDRAddDeviceInfoToList(device->remoteAddress, deviceInfo); + if (CA_STATUS_OK != result) + { + OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not add device info to list!"); + OICFree(deviceInfo); + return; + } + } + + if (!deviceInfo->recvData) + { + OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Callocing deviceInfo->recvData"); + deviceInfo->recvData = OICCalloc(data->data_size, sizeof(uint8_t)); + if (!deviceInfo->recvData) + { + OIC_LOG(ERROR, EDR_ADAPTER_TAG, "out of memory"); + return; + } + } + + memcpy(deviceInfo->recvData + deviceInfo->recvDataLen, (const char*)data->data, + data->data_size); + deviceInfo->recvDataLen += data->data_size; + + if (!deviceInfo->totalDataLen) + { + coap_transport_type transport = coap_get_tcp_header_type_from_initbyte( + ((unsigned char *)deviceInfo->recvData)[0] >> 4); + size_t headerLen = coap_get_tcp_header_length_for_transport(transport); + + if (deviceInfo->recvDataLen >= headerLen) + { + // get actual data length from coap over tcp header + deviceInfo->totalDataLen = coap_get_total_message_length(deviceInfo->recvData, + deviceInfo->recvDataLen); + OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "total data length [%d] bytes", deviceInfo->totalDataLen); + + uint8_t *newBuf = OICRealloc(deviceInfo->recvData, deviceInfo->totalDataLen); + if (!newBuf) + { + OIC_LOG(ERROR, EDR_ADAPTER_TAG, "out of memory"); + //Memory free + return; + } + deviceInfo->recvData = newBuf; + } + } + + if (deviceInfo->totalDataLen == deviceInfo->recvDataLen) + { + if (g_edrPacketReceivedCallback) + { + OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG,"data will be sent to callback routine: %s, %d", + deviceInfo->recvData, deviceInfo->recvDataLen); + + uint32_t sentLength = 0; + g_edrPacketReceivedCallback(device->remoteAddress, (void*) deviceInfo->recvData, + deviceInfo->recvDataLen, &sentLength); - g_edrPacketReceivedCallback(device->remoteAddress, - (uint8_t *) data->data, - (uint32_t) data->data_size, - &sentLength); + OICFree(deviceInfo->recvData); + deviceInfo->recvData = NULL; + deviceInfo->recvDataLen = 0; + deviceInfo->totalDataLen = 0; + } + } OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT"); } diff --git a/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrutils.c b/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrutils.c index 70073ae..72df325 100644 --- a/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrutils.c +++ b/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrutils.c @@ -29,6 +29,12 @@ #include #include "logger.h" +#include "uarraylist.h" + +/** + * Pending devices list for data transfer. + */ +static u_arraylist_t *g_deviceStateList = NULL; bool CAEDRIsServiceSupported(const char **serviceUUID, int32_t serviceCount, const char *matchService) @@ -54,5 +60,69 @@ bool CAEDRIsServiceSupported(const char **serviceUUID, int32_t serviceCount, return false; } +CAConnectedDeviceInfo_t *CAEDRGetDeviceInfoFromAddress(const char *remoteAddress) +{ + OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "CAEDRGetDeviceInfoFromAddress"); + if (!remoteAddress) + { + OIC_LOG(ERROR, EDR_ADAPTER_TAG, "remoteAddress is null"); + return NULL; + } + + uint32_t length = u_arraylist_length(g_deviceStateList); + for (uint32_t index = 0; index < length; index++) + { + CAConnectedDeviceInfo_t* deviceInfo = + (CAConnectedDeviceInfo_t*) u_arraylist_get(g_deviceStateList, index); + if (!deviceInfo) + { + OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "deviceInfo object is null"); + continue; + } + + if (!strcmp((const char*) deviceInfo->address, remoteAddress)) + { + return deviceInfo; + } + } + return NULL; +} + +CAResult_t CAEDRAddDeviceInfoToList(const char *remoteAddress, CAConnectedDeviceInfo_t *deviceInfo) +{ + if (!remoteAddress) + { + OIC_LOG(ERROR, EDR_ADAPTER_TAG, "remoteAddress is null"); + return CA_STATUS_INVALID_PARAM; + } + + if (!deviceInfo) + { + OIC_LOG(ERROR, EDR_ADAPTER_TAG, "deviceInfo is null"); + return CA_STATUS_INVALID_PARAM; + } + + for (int i = 0; i < CA_MACADDR_SIZE; i++) + { + deviceInfo->address[i] = (uint8_t) remoteAddress[i]; + } + + if (!g_deviceStateList) + { + g_deviceStateList = u_arraylist_create(); + if (!g_deviceStateList) + { + OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create Device list"); + return CA_STATUS_FAILED; + } + } + + if (!u_arraylist_add(g_deviceStateList, (void *) deviceInfo)) + { + OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Add Device to list"); + return CA_STATUS_FAILED; + } + return CA_STATUS_OK; +} \ No newline at end of file diff --git a/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrutils.h b/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrutils.h index 6a7d706..c3afe31 100644 --- a/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrutils.h +++ b/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrutils.h @@ -32,6 +32,7 @@ #include "cacommon.h" #include "oic_malloc.h" +#include "caedrinterface.h" /** * Logging tag for module name. @@ -58,6 +59,21 @@ extern "C" bool CAEDRIsServiceSupported(const char **serviceUUID, int32_t serviceCount, const char *matchService); +/** + * Get device information from list. + * @param[in] remoteAddress remote address. + * @return Device information object or NULL. + */ +CAConnectedDeviceInfo_t *CAEDRGetDeviceInfoFromAddress(const char *remoteAddress); + +/** + * Add device information to list. + * @param[in] remoteAddress remote address. + * @param[in] deviceInfo device information. + * @return ::CA_STATUS_OK or Appropriate error code. + */ +CAResult_t CAEDRAddDeviceInfoToList(const char *remoteAddress, CAConnectedDeviceInfo_t *deviceInfo); + #ifdef __cplusplus } /* extern "C" */ #endif -- 2.7.4