+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__);
+}
+
+#ifndef SINGLE_THREAD
+static bool CALEClearQueueAddressDataContext(void *data, uint32_t size, void *ctx)
+{
+ if (NULL == data || NULL == ctx)
+ {
+ return false;
+ }
+
+ CALEData_t *caLeData = (CALEData_t *)data;
+ const char *address = (const char *)ctx;
+
+ if (NULL != caLeData && NULL != caLeData->remoteEndpoint)
+ {
+ if (!strcasecmp(caLeData->remoteEndpoint->addr, address))
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, const char* address)
+{
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
+
+ VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle");
+ VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
+
+ CAResult_t res = CAQueueingThreadClearContextData(queueHandle,
+ CALEClearQueueAddressDataContext,
+ address);
+ if (CA_STATUS_OK != res)
+ {
+
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Could not clear the send queue");
+ }
+
+}
+
+static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList, const char* address)
+{
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
+
+ VERIFY_NON_NULL_VOID(dataInfoList, CALEADAPTER_TAG, "dataInfoList");
+ VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
+
+ CABLESenderInfo_t *senderInfo = NULL;
+ uint32_t senderIndex = 0;
+
+ u_arraylist_t *portList = u_arraylist_create();
+ if (CA_STATUS_OK == CALEGetPortsFromSenderInfo(address, dataInfoList, portList))
+ {
+ uint32_t arrayLength = u_arraylist_length(portList);
+ for (uint32_t i = 0; i < arrayLength; i++)
+ {
+ uint16_t *port = (uint16_t *)u_arraylist_get(portList, i);
+ if (!port)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to get port from sender info !");
+ u_arraylist_destroy(portList);
+ return;
+ }
+
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "port : %X", *port);
+
+ if (CA_STATUS_OK == CALEGetSenderInfo(address, *port,
+ dataInfoList, &senderInfo,
+ &senderIndex))
+ {
+ OIC_LOG(DEBUG, CALEADAPTER_TAG,
+ "SenderInfo is removed for disconnection");
+ CALERemoveSenderInfoFromList(dataInfoList, senderInfo);
+ CALEFreeSenderInfo(senderInfo);
+ }
+ else
+ {
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");
+ }
+ }
+ }
+ u_arraylist_destroy(portList);
+}
+
+static CAResult_t CALEGetPortsFromSenderInfo(const char *leAddress,
+ u_arraylist_t *senderInfoList,
+ u_arraylist_t *portList)
+{
+ VERIFY_NON_NULL(leAddress,
+ CALEADAPTER_TAG,
+ "NULL BLE address argument");
+
+ const uint32_t listLength = u_arraylist_length(senderInfoList);
+ const uint32_t addrLength = strlen(leAddress);
+
+ for (uint32_t index = 0; index < listLength; index++)
+ {
+ CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
+ if (!info || !(info->remoteEndpoint))
+ {
+ continue;
+ }
+
+ if (!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
+ {
+ uint16_t *port = (uint16_t *)OICMalloc(sizeof(uint16_t));
+ if (!port)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
+ return CA_MEMORY_ALLOC_FAILED;
+ }
+ *port = info->remoteEndpoint->port;
+ u_arraylist_add(portList, (void *)port);
+ }
+ }
+
+ if (u_arraylist_length(portList) != 0)
+ {
+ return CA_STATUS_OK;
+ }
+ else
+ {
+ return CA_STATUS_FAILED;
+ }
+}
+#endif
+
+void CALEStartGattServer()
+{
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__);
+
+ oc_mutex_lock(g_bleIsServerMutex);
+ switch (g_adapterType)
+ {
+ case ADAPTER_SERVER:
+ CALEAdapterGattServerStart();
+ break;
+ case ADAPTER_CLIENT:
+ CALEAdapterGattClientStart();
+ break;
+ case ADAPTER_BOTH_CLIENT_SERVER:
+ CALEAdapterGattServerStart();
+ CALEAdapterGattClientStart();
+ break;
+ default:
+ break;
+ }
+ oc_mutex_unlock(g_bleIsServerMutex);
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__);
+}
+
+void CALEStopGattServer()
+{
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__);
+
+ oc_mutex_lock(g_bleIsServerMutex);
+ switch (g_adapterType)
+ {
+ case ADAPTER_SERVER:
+ CALEAdapterGattServerStop();
+ break;
+ case ADAPTER_CLIENT:
+ CALEAdapterGattClientStop();
+ break;
+ case ADAPTER_BOTH_CLIENT_SERVER:
+ CALEAdapterGattServerStop();
+ CALEAdapterGattClientStop();
+ break;
+ default:
+ break;
+ }
+ oc_mutex_unlock(g_bleIsServerMutex);
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__);