1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "caleadapter.h"
25 #include "caleinterface.h"
28 #include "caadapterutils.h"
30 #include "caqueueingthread.h"
32 #include "cafragmentation.h"
33 #include "oic_malloc.h"
34 #include "oic_string.h"
35 #include "caremotehandler.h"
38 * Logging tag for module name.
40 #define CALEADAPTER_TAG "LAD"
43 * Callback to provide the status of the network change to CA layer.
45 static CANetworkChangeCallback g_networkCallback = NULL;
48 * bleAddress of the local adapter. Value will be initialized to zero, and will
51 static char g_localBLEAddress[18] = {0};
54 * Variable to differentiate btw GattServer and GattClient.
56 static bool g_isServer = false;
59 * Mutex to synchronize the task to be executed on the GattServer function
62 static ca_mutex g_bleIsServerMutex = NULL;
65 * Mutex to synchronize the callback to be called for the network changes.
67 static ca_mutex g_bleNetworkCbMutex = NULL;
70 * Mutex to synchronize the updation of the local LE address of the adapter.
72 static ca_mutex g_bleLocalAddressMutex = NULL;
75 * reference to threadpool.
77 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
80 * Mutex to synchronize the task to be pushed to thread pool.
82 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
85 * Mutex to synchronize the queing of the data from SenderQueue.
87 static ca_mutex g_bleClientSendDataMutex = NULL;
90 * Mutex to synchronize the queing of the data from ReceiverQueue.
92 static ca_mutex g_bleClientReceiveDataMutex = NULL;
96 * Mutex to synchronize the queing of the data from SenderQueue.
98 static ca_mutex g_bleServerSendDataMutex = NULL;
101 * Mutex to synchronize the queing of the data from ReceiverQueue.
103 static ca_mutex g_bleServerReceiveDataMutex = NULL;
106 * Mutex to synchronize the callback to be called for the adapterReqResponse.
108 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
111 * Callback to be called when network packet recieved from either
112 * GattServer or GattClient.
114 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
117 * Callback to notify error from the BLE adapter.
119 static CAErrorHandleCallback g_errorHandler = NULL;
122 * Storing Adapter state information.
124 static CAAdapterState_t g_bleAdapterState = CA_ADAPTER_DISABLED;
127 * status of BLE Server Status.
128 * This ENUM provides information of LE Adapter Server status.
132 CA_SERVER_NOTSTARTED = 0,
138 * structure to maintain the status of the server.
140 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
143 * This function is used to register network change notification callback.
145 * @param[in] netCallback CANetworkChangeCallback callback which will be
146 * set for the change in nwk.
148 * @return 0 on success otherwise a positive error value.
149 * @retval CA_STATUS_OK Successful.
150 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets.
151 * @retval CA_STATUS_FAILED Operation failed.
154 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
157 * Used to Set the gThreadPool handle which is required for spawning new thread.
159 * @param[in] handle - Thread pool handle which is given by above layer for
160 * using thread creation task.
163 void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
166 * This function is used to call the callback to the upper layer when the
167 * device state gets changed.
169 * @param[in] adapter_state New state of the adapter to be notified to the
173 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
176 * Used to initialize all required mutex variable for LE Adapter implementation.
178 * @return 0 on success otherwise a positive error value.
179 * @retval CA_STATUS_OK Successful.
180 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets.
181 * @retval CA_STATUS_FAILED Operation failed.
184 CAResult_t CAInitLEAdapterMutex();
187 * Used to terminate all required mutex variable for LE adapter implementation.
190 void CATerminateLEAdapterMutex();
193 * prepares and notify error through error callback.
196 static void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
199 #ifndef SINGLE_THREAD
201 * Stop condition of recvhandler.
203 static bool g_dataReceiverHandlerState = false;
206 * Queue to process the outgoing packets from GATTClient.
208 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
211 * Queue to process the incoming packets to GATT Client.
213 static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
216 * Queue to process the outgoing packets from GATTServer.
218 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
221 * Queue to process the incoming packets to GATTServer.
223 static CAQueueingThread_t *g_bleServerReceiverQueue = NULL;
229 static void CALEDataDestroyer(void *data, uint32_t size);
231 void CAInitLEQueues()
233 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
235 CAResult_t result = CAInitLEServerQueues();
236 if (CA_STATUS_OK != result)
238 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
242 result = CAInitLEClientQueues();
243 if (CA_STATUS_OK != result)
245 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
249 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
252 CAResult_t CAInitLEServerQueues()
254 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
256 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
258 CAResult_t result = CAInitLEServerSenderQueue();
259 if (CA_STATUS_OK != result)
261 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
262 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
263 return CA_STATUS_FAILED;
266 result = CAInitLEServerReceiverQueue();
267 if (CA_STATUS_OK != result)
269 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
270 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
271 return CA_STATUS_FAILED;
274 g_dataReceiverHandlerState = true;
276 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
278 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
282 CAResult_t CAInitLEClientQueues()
284 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
286 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
288 CAResult_t result = CAInitLEClientSenderQueue();
289 if (CA_STATUS_OK != result)
291 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
292 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
293 return CA_STATUS_FAILED;
296 result = CAInitLEClientReceiverQueue();
297 if (CA_STATUS_OK != result)
299 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
300 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
301 return CA_STATUS_FAILED;
304 g_dataReceiverHandlerState = true;
306 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
308 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
312 CAResult_t CAInitLEServerSenderQueue()
314 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
315 // Check if the message queue is already initialized
316 if (g_bleServerSendQueueHandle)
318 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
322 // Create send message queue
323 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
324 if (!g_bleServerSendQueueHandle)
326 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
327 return CA_MEMORY_ALLOC_FAILED;
330 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
331 g_bleAdapterThreadPool,
332 CALEServerSendDataThread, CALEDataDestroyer))
334 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
335 OICFree(g_bleServerSendQueueHandle);
336 g_bleServerSendQueueHandle = NULL;
337 return CA_STATUS_FAILED;
340 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerSendQueueHandle))
342 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
343 OICFree(g_bleServerSendQueueHandle);
344 g_bleServerSendQueueHandle = NULL;
345 return CA_STATUS_FAILED;
348 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
352 CAResult_t CAInitLEClientSenderQueue()
354 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
356 if (g_bleClientSendQueueHandle)
358 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
362 // Create send message queue
363 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
364 if (!g_bleClientSendQueueHandle)
366 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
367 return CA_MEMORY_ALLOC_FAILED;
370 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
371 g_bleAdapterThreadPool,
372 CALEClientSendDataThread, CALEDataDestroyer))
374 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
375 OICFree(g_bleClientSendQueueHandle);
376 g_bleClientSendQueueHandle = NULL;
377 return CA_STATUS_FAILED;
380 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
382 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
383 OICFree(g_bleClientSendQueueHandle);
384 g_bleClientSendQueueHandle = NULL;
385 return CA_STATUS_FAILED;
388 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
392 CAResult_t CAInitLEServerReceiverQueue()
394 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
395 // Check if the message queue is already initialized
396 if (g_bleServerReceiverQueue)
398 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
402 // Create send message queue
403 g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
404 if (!g_bleServerReceiverQueue)
406 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
407 OICFree(g_bleServerSendQueueHandle);
408 return CA_MEMORY_ALLOC_FAILED;
411 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
412 CALEServerDataReceiverHandler, CALEDataDestroyer))
414 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
415 OICFree(g_bleServerReceiverQueue);
416 g_bleServerReceiverQueue = NULL;
417 return CA_STATUS_FAILED;
420 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
422 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
423 OICFree(g_bleServerReceiverQueue);
424 g_bleServerReceiverQueue = NULL;
425 return CA_STATUS_FAILED;
428 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
432 CAResult_t CAInitLEClientReceiverQueue()
434 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
436 // Check if the message queue is already initialized
437 if (g_bleClientReceiverQueue)
439 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
443 // Create send message queue
444 g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
445 if (!g_bleClientReceiverQueue)
447 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
448 OICFree(g_bleClientSendQueueHandle);
449 return CA_MEMORY_ALLOC_FAILED;
452 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue,
453 g_bleAdapterThreadPool,
454 CALEClientDataReceiverHandler, NULL))
456 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
457 OICFree(g_bleClientSendQueueHandle);
458 OICFree(g_bleClientReceiverQueue);
459 g_bleClientReceiverQueue = NULL;
460 return CA_STATUS_FAILED;
463 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
465 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
466 OICFree(g_bleClientReceiverQueue);
467 g_bleClientReceiverQueue = NULL;
468 return CA_STATUS_FAILED;
471 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
475 void CAStopLEQueues()
477 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
479 ca_mutex_lock(g_bleClientSendDataMutex);
480 if (NULL != g_bleClientSendQueueHandle)
482 CAQueueingThreadStop(g_bleClientSendQueueHandle);
484 ca_mutex_unlock(g_bleClientSendDataMutex);
486 ca_mutex_lock(g_bleClientReceiveDataMutex);
487 if (NULL != g_bleClientReceiverQueue)
489 CAQueueingThreadStop(g_bleClientReceiverQueue);
491 ca_mutex_unlock(g_bleClientReceiveDataMutex);
493 ca_mutex_lock(g_bleServerSendDataMutex);
494 if (NULL != g_bleServerSendQueueHandle)
496 CAQueueingThreadStop(g_bleServerSendQueueHandle);
498 ca_mutex_unlock(g_bleServerSendDataMutex);
500 ca_mutex_lock(g_bleServerReceiveDataMutex);
501 if (NULL != g_bleServerReceiverQueue)
503 CAQueueingThreadStop(g_bleServerReceiverQueue);
505 ca_mutex_unlock(g_bleServerReceiveDataMutex);
507 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
510 void CATerminateLEQueues()
512 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
514 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
515 OICFree(g_bleClientSendQueueHandle);
516 g_bleClientSendQueueHandle = NULL;
519 CAQueueingThreadDestroy(g_bleClientReceiverQueue);
520 OICFree(g_bleClientReceiverQueue);
521 g_bleClientReceiverQueue = NULL;
524 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
525 OICFree(g_bleServerSendQueueHandle);
526 g_bleServerSendQueueHandle = NULL;
529 CAQueueingThreadDestroy(g_bleServerReceiverQueue);
530 OICFree(g_bleServerReceiverQueue);
531 g_bleServerReceiverQueue = NULL;
533 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
536 void CALEServerDataReceiverHandler(void *threadData)
538 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
540 static uint32_t recvDataLen = 0;
541 static uint32_t totalDataLen = 0;
542 static char *defragData = NULL;
543 static bool isHeaderAvailable = false;
544 static CAEndpoint_t *remoteEndpoint = NULL;
546 ca_mutex_lock(g_bleServerReceiveDataMutex);
548 if (g_dataReceiverHandlerState)
550 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
552 CALEData_t *bleData = (CALEData_t *) threadData;
555 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
556 ca_mutex_unlock(g_bleServerReceiveDataMutex);
560 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
562 if (!isHeaderAvailable)
564 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
565 totalDataLen = CAParseHeader((char*)bleData->data);
567 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
568 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
570 defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
571 if (NULL == defragData)
573 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
574 ca_mutex_unlock(g_bleServerReceiveDataMutex);
578 const char *remoteAddress = bleData->remoteEndpoint->addr;
580 remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
583 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
584 bleData->dataLen - CA_HEADER_LENGTH);
585 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
586 isHeaderAvailable = true;
590 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
591 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
592 recvDataLen += bleData->dataLen ;
593 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
594 totalDataLen, recvDataLen);
596 if (totalDataLen == recvDataLen)
598 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
599 if (NULL == g_networkPacketReceivedCallback)
601 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
603 CAFreeEndpoint(remoteEndpoint);
604 remoteEndpoint = NULL;
606 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
607 ca_mutex_unlock(g_bleServerReceiveDataMutex);
610 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
611 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
614 CAFreeEndpoint(remoteEndpoint);
618 isHeaderAvailable = false;
619 remoteEndpoint = NULL;
621 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
624 if (false == g_dataReceiverHandlerState)
626 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
629 isHeaderAvailable = false;
631 CAFreeEndpoint(remoteEndpoint);
632 remoteEndpoint = NULL;
634 ca_mutex_unlock(g_bleServerReceiveDataMutex);
638 ca_mutex_unlock(g_bleServerReceiveDataMutex);
639 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
642 void CALEClientDataReceiverHandler(void *threadData)
644 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
646 static const char *remoteAddress = NULL;
647 static uint32_t recvDataLen = 0;
648 static uint32_t totalDataLen = 0;
649 static char *defragData = NULL;
650 static bool isHeaderAvailable = false;
651 static CAEndpoint_t *remoteEndpoint = NULL;
653 ca_mutex_lock(g_bleClientReceiveDataMutex);
655 if (g_dataReceiverHandlerState)
657 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
659 CALEData_t *bleData = (CALEData_t *) threadData;
662 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
663 ca_mutex_unlock(g_bleClientReceiveDataMutex);
667 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
669 if (!isHeaderAvailable)
671 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
673 totalDataLen = CAParseHeader(bleData->data);
674 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
676 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
679 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
680 if (NULL == defragData)
682 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
683 ca_mutex_unlock(g_bleClientReceiveDataMutex);
687 remoteAddress = bleData->remoteEndpoint->addr;
689 remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
692 memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
693 bleData->dataLen - CA_HEADER_LENGTH);
694 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
695 isHeaderAvailable = true;
699 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
700 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
701 recvDataLen += bleData->dataLen ;
702 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
703 totalDataLen, recvDataLen);
705 if (totalDataLen == recvDataLen)
707 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
708 if (NULL == g_networkPacketReceivedCallback)
710 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
712 CAFreeEndpoint(remoteEndpoint);
713 remoteEndpoint = NULL;
715 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
716 ca_mutex_unlock(g_bleClientReceiveDataMutex);
719 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
720 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
723 isHeaderAvailable = false;
725 CAFreeEndpoint(remoteEndpoint);
726 remoteEndpoint = NULL;
728 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
731 if (false == g_dataReceiverHandlerState)
733 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
735 CAFreeEndpoint(remoteEndpoint);
736 remoteEndpoint = NULL;
738 ca_mutex_unlock(g_bleClientReceiveDataMutex);
742 ca_mutex_unlock(g_bleClientReceiveDataMutex);
743 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
746 void CALEServerSendDataThread(void *threadData)
748 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
750 CALEData_t *bleData = (CALEData_t *) threadData;
753 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
757 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
758 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
760 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
762 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
763 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
764 if (NULL == dataSegment)
766 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
771 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
772 if (CA_STATUS_OK != result )
774 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
776 OICFree(dataSegment);
780 memcpy(dataSegment, header, CA_HEADER_LENGTH);
784 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
786 length = totalLength;
787 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
791 length = CA_SUPPORTED_BLE_MTU_SIZE;
792 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
793 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
796 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
798 // Send the first segment with the header.
799 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
801 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
802 result = CAUpdateCharacteristicsToGattClient(
803 bleData->remoteEndpoint->addr, dataSegment, length);
804 if (CA_STATUS_OK != result)
806 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
807 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
808 OICFree(dataSegment);
812 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
813 for (index = 1; index < iter; index++)
815 // Send the remaining header.
816 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
817 result = CAUpdateCharacteristicsToGattClient(
818 bleData->remoteEndpoint->addr,
819 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
820 CA_SUPPORTED_BLE_MTU_SIZE);
821 if (CA_STATUS_OK != result)
823 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
824 "Update characteristics failed, result [%d]", result);
825 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
826 OICFree(dataSegment);
829 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
830 CA_SUPPORTED_BLE_MTU_SIZE);
833 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
834 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
836 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
837 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
838 result = CAUpdateCharacteristicsToGattClient(
839 bleData->remoteEndpoint->addr,
840 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
842 if (CA_STATUS_OK != result)
844 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
846 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
847 OICFree(dataSegment);
850 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
855 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
856 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
857 if (CA_STATUS_OK != result)
859 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
861 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
862 OICFree(dataSegment);
865 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
866 for (index = 1; index < iter; index++)
868 // Send the remaining header.
869 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
870 result = CAUpdateCharacteristicsToAllGattClients(
871 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
872 CA_SUPPORTED_BLE_MTU_SIZE);
873 if (CA_STATUS_OK != result)
875 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
877 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
878 OICFree(dataSegment);
881 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
884 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
885 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
887 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
888 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
889 result = CAUpdateCharacteristicsToAllGattClients(
890 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
892 if (CA_STATUS_OK != result)
894 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
896 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
897 OICFree(dataSegment);
900 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
903 OICFree(dataSegment);
905 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
908 void CALEClientSendDataThread(void *threadData)
910 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
912 CALEData_t *bleData = (CALEData_t *) threadData;
915 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
919 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
920 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
922 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
923 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
924 if (NULL == dataSegment)
926 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
931 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
932 if (CA_STATUS_OK != result )
934 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
936 OICFree(dataSegment);
939 memcpy(dataSegment, header, CA_HEADER_LENGTH);
943 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
945 length = totalLength;
946 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
947 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
951 length = CA_SUPPORTED_BLE_MTU_SIZE;
952 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
953 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
954 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
957 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
959 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
961 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
962 // Send the first segment with the header.
963 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
968 if (CA_STATUS_OK != result)
970 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
971 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
972 OICFree(dataSegment);
976 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
977 for (index = 1; index < iter; index++)
979 // Send the remaining header.
980 result = CAUpdateCharacteristicsToGattServer(
981 bleData->remoteEndpoint->addr,
982 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
983 CA_SUPPORTED_BLE_MTU_SIZE,
985 if (CA_STATUS_OK != result)
987 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
989 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
990 OICFree(dataSegment);
993 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
994 CA_SUPPORTED_BLE_MTU_SIZE);
997 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
998 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1000 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1001 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1002 result = CAUpdateCharacteristicsToGattServer(
1003 bleData->remoteEndpoint->addr,
1004 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1008 if (CA_STATUS_OK != result)
1010 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1012 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1013 OICFree(dataSegment);
1016 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1021 //Sending Mulitcast Data
1022 // Send the first segment with the header.
1023 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1024 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1025 if (CA_STATUS_OK != result)
1027 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1028 "Update characteristics (all) failed, result [%d]", result);
1029 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1030 OICFree(dataSegment);
1033 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1034 // Send the remaining header.
1035 for (index = 1; index < iter; index++)
1037 result = CAUpdateCharacteristicsToAllGattServers(
1038 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1039 CA_SUPPORTED_BLE_MTU_SIZE);
1040 if (CA_STATUS_OK != result)
1042 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1044 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1045 OICFree(dataSegment);
1048 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1049 CA_SUPPORTED_BLE_MTU_SIZE);
1052 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1053 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1055 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1056 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1057 result = CAUpdateCharacteristicsToAllGattServers(
1058 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1060 if (CA_STATUS_OK != result)
1062 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1063 "Update characteristics (all) failed, result [%d]", result);
1064 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1065 OICFree(dataSegment);
1068 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1073 OICFree(dataSegment);
1075 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1078 CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint, const void *data,
1079 uint32_t dataLength)
1081 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1084 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1088 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1089 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1090 if (NULL == bleData->data)
1092 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1093 CAFreeLEData(bleData);
1096 memcpy(bleData->data, data, dataLength);
1097 bleData->dataLen = dataLength;
1102 void CAFreeLEData(CALEData_t *bleData)
1104 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1106 CAFreeEndpoint(bleData->remoteEndpoint);
1107 OICFree(bleData->data);
1111 void CALEDataDestroyer(void *data, uint32_t size)
1113 CALEData_t *ledata = (CALEData_t *) data;
1115 CAFreeLEData(ledata);
1119 CAResult_t CAInitLEAdapterMutex()
1121 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1123 if (NULL == g_bleIsServerMutex)
1125 g_bleIsServerMutex = ca_mutex_new();
1126 if (NULL == g_bleIsServerMutex)
1128 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1129 return CA_STATUS_FAILED;
1133 if (NULL == g_bleNetworkCbMutex)
1135 g_bleNetworkCbMutex = ca_mutex_new();
1136 if (NULL == g_bleNetworkCbMutex)
1138 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1139 CATerminateLEAdapterMutex();
1140 return CA_STATUS_FAILED;
1144 if (NULL == g_bleLocalAddressMutex)
1146 g_bleLocalAddressMutex = ca_mutex_new();
1147 if (NULL == g_bleLocalAddressMutex)
1149 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1150 CATerminateLEAdapterMutex();
1151 return CA_STATUS_FAILED;
1155 if (NULL == g_bleAdapterThreadPoolMutex)
1157 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1158 if (NULL == g_bleAdapterThreadPoolMutex)
1160 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1161 CATerminateLEAdapterMutex();
1162 return CA_STATUS_FAILED;
1166 if (NULL == g_bleClientSendDataMutex)
1168 g_bleClientSendDataMutex = ca_mutex_new();
1169 if (NULL == g_bleClientSendDataMutex)
1171 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1172 CATerminateLEAdapterMutex();
1173 return CA_STATUS_FAILED;
1177 if (NULL == g_bleClientReceiveDataMutex)
1179 g_bleClientReceiveDataMutex = ca_mutex_new();
1180 if (NULL == g_bleClientReceiveDataMutex)
1182 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1183 CATerminateLEAdapterMutex();
1184 return CA_STATUS_FAILED;
1188 if (NULL == g_bleServerSendDataMutex)
1190 g_bleServerSendDataMutex = ca_mutex_new();
1191 if (NULL == g_bleServerSendDataMutex)
1193 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1194 CATerminateLEAdapterMutex();
1195 return CA_STATUS_FAILED;
1199 if (NULL == g_bleServerReceiveDataMutex)
1201 g_bleServerReceiveDataMutex = ca_mutex_new();
1202 if (NULL == g_bleServerReceiveDataMutex)
1204 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1205 CATerminateLEAdapterMutex();
1206 return CA_STATUS_FAILED;
1210 if (NULL == g_bleAdapterReqRespCbMutex)
1212 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1213 if (NULL == g_bleAdapterReqRespCbMutex)
1215 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1216 CATerminateLEAdapterMutex();
1217 return CA_STATUS_FAILED;
1221 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1222 return CA_STATUS_OK;
1225 void CATerminateLEAdapterMutex()
1227 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1229 ca_mutex_free(g_bleIsServerMutex);
1230 g_bleIsServerMutex = NULL;
1232 ca_mutex_free(g_bleNetworkCbMutex);
1233 g_bleNetworkCbMutex = NULL;
1235 ca_mutex_free(g_bleLocalAddressMutex);
1236 g_bleLocalAddressMutex = NULL;
1238 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1239 g_bleAdapterThreadPoolMutex = NULL;
1241 ca_mutex_free(g_bleClientSendDataMutex);
1242 g_bleClientSendDataMutex = NULL;
1244 ca_mutex_free(g_bleClientReceiveDataMutex);
1245 g_bleClientReceiveDataMutex = NULL;
1247 ca_mutex_free(g_bleServerSendDataMutex);
1248 g_bleServerSendDataMutex = NULL;
1250 ca_mutex_free(g_bleServerReceiveDataMutex);
1251 g_bleServerReceiveDataMutex = NULL;
1253 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1254 g_bleAdapterReqRespCbMutex = NULL;
1256 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1259 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1260 CANetworkPacketReceivedCallback reqRespCallback,
1261 CANetworkChangeCallback netCallback,
1262 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
1264 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1267 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1268 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1269 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1271 CAResult_t result = CA_STATUS_OK;
1272 result = CAInitLEAdapterMutex();
1273 if (CA_STATUS_OK != result)
1275 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1276 return CA_STATUS_FAILED;
1278 result = CAInitializeLENetworkMonitor();
1279 if (CA_STATUS_OK != result)
1281 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1282 return CA_STATUS_FAILED;
1285 CAInitializeLEAdapter();
1287 CASetLEClientThreadPoolHandle(handle);
1288 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1289 CASetLEServerThreadPoolHandle(handle);
1290 CASetLEAdapterThreadPoolHandle(handle);
1291 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1292 CASetLEReqRespAdapterCallback(reqRespCallback);
1294 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1295 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1296 CALERegisterNetworkNotifications(netCallback);
1298 g_errorHandler = errorCallback;
1300 CAConnectivityHandler_t connHandler;
1301 connHandler.startAdapter = CAStartLE;
1302 connHandler.stopAdapter = CAStopLE;
1303 connHandler.startListenServer = CAStartLEListeningServer;
1304 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
1305 connHandler.sendData = CASendLEUnicastData;
1306 connHandler.sendDataToAll = CASendLEMulticastData;
1307 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
1308 connHandler.readData = CAReadLEData;
1309 connHandler.terminate = CATerminateLE;
1310 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1312 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1314 return CA_STATUS_OK;
1317 CAResult_t CAStartLE()
1319 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1320 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE, not implemented");
1321 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1322 return CA_STATUS_OK;
1325 CAResult_t CAStopLE()
1327 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1328 #ifndef SINGLE_THREAD
1332 ca_mutex_lock(g_bleIsServerMutex);
1333 if (true == g_isServer)
1335 CAStopLEGattServer();
1339 CAStopLEGattClient();
1341 ca_mutex_unlock(g_bleIsServerMutex);
1343 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1345 return CA_STATUS_OK;
1348 void CATerminateLE()
1350 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1352 CASetLEReqRespServerCallback(NULL);
1353 CASetLEReqRespClientCallback(NULL);
1354 CALERegisterNetworkNotifications(NULL);
1355 CASetLEReqRespAdapterCallback(NULL);
1356 CATerminateLENetworkMonitor();
1358 ca_mutex_lock(g_bleIsServerMutex);
1359 if (true == g_isServer)
1361 CATerminateLEGattServer();
1365 CATerminateLEGattClient();
1367 ca_mutex_unlock(g_bleIsServerMutex);
1369 #ifndef SINGLE_THREAD
1370 CATerminateLEQueues();
1372 CATerminateLEAdapterMutex();
1374 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1377 CAResult_t CAStartLEListeningServer()
1379 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1380 CAResult_t result = CA_STATUS_OK;
1381 #ifndef SINGLE_THREAD
1382 result = CAInitLEServerQueues();
1383 if (CA_STATUS_OK != result)
1385 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1386 return CA_STATUS_FAILED;
1390 result = CAGetLEAdapterState();
1391 if (CA_ADAPTER_NOT_ENABLED == result)
1393 gLeServerStatus = CA_LISTENING_SERVER;
1394 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1395 return CA_STATUS_OK;
1398 if (CA_STATUS_FAILED == result)
1400 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1401 return CA_STATUS_FAILED;
1404 CAStartLEGattServer();
1406 ca_mutex_lock(g_bleIsServerMutex);
1408 ca_mutex_unlock(g_bleIsServerMutex);
1410 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1411 return CA_STATUS_OK;
1414 CAResult_t CAStartLEDiscoveryServer()
1416 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1417 CAResult_t result = CA_STATUS_OK;
1418 #ifndef SINGLE_THREAD
1419 result = CAInitLEClientQueues();
1420 if (CA_STATUS_OK != result)
1422 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1423 return CA_STATUS_FAILED;
1426 result = CAGetLEAdapterState();
1427 if (CA_ADAPTER_NOT_ENABLED == result)
1429 gLeServerStatus = CA_DISCOVERY_SERVER;
1430 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1431 return CA_STATUS_OK;
1434 if (CA_STATUS_FAILED == result)
1436 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1437 return CA_STATUS_FAILED;
1440 CAStartLEGattClient();
1442 ca_mutex_lock(g_bleIsServerMutex);
1444 ca_mutex_unlock(g_bleIsServerMutex);
1446 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1447 return CA_STATUS_OK;
1450 CAResult_t CAStartLENotifyServer()
1452 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1454 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1455 return CA_STATUS_OK;
1458 uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data,
1461 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1463 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1467 CAResult_t CAReadLEData()
1469 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1470 #ifdef SINGLE_THREAD
1473 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1474 return CA_STATUS_OK;
1477 int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1479 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1482 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1483 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1485 CAResult_t result = CA_STATUS_FAILED;
1487 ca_mutex_lock(g_bleIsServerMutex);
1488 if (true == g_isServer)
1490 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1491 if (CA_STATUS_OK != result)
1493 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1496 g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
1498 ca_mutex_unlock(g_bleIsServerMutex);
1504 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1505 if (CA_STATUS_OK != result)
1507 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1510 g_errorHandler(endpoint, data, dataLen, result);
1512 ca_mutex_unlock(g_bleIsServerMutex);
1516 ca_mutex_unlock(g_bleIsServerMutex);
1518 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1522 int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1524 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1527 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1531 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1535 CAResult_t result = CA_STATUS_FAILED;
1537 ca_mutex_lock(g_bleIsServerMutex);
1538 if (true == g_isServer)
1540 result = CALEAdapterServerSendData(NULL, data, dataLen);
1541 if (CA_STATUS_OK != result)
1543 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
1545 ca_mutex_unlock(g_bleIsServerMutex);
1548 g_errorHandler(endpoint, data, dataLen, result);
1555 result = CALEAdapterClientSendData(NULL, data, dataLen);
1556 if (CA_STATUS_OK != result)
1558 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
1561 g_errorHandler(endpoint, data, dataLen, result);
1563 ca_mutex_unlock(g_bleIsServerMutex);
1567 ca_mutex_unlock(g_bleIsServerMutex);
1569 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1573 CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1575 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1577 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
1579 char *local_address = NULL;
1581 CAResult_t res = CAGetLEAddress(&local_address);
1582 if (CA_STATUS_OK != res)
1584 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
1588 if (NULL == local_address)
1590 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
1591 return CA_STATUS_FAILED;
1595 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
1596 if (NULL == (*info))
1598 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
1599 OICFree(local_address);
1600 return CA_STATUS_FAILED;
1603 size_t local_address_len = strlen(local_address);
1605 if(local_address_len >= sizeof(g_localBLEAddress) ||
1606 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
1608 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
1610 OICFree(local_address);
1611 return CA_STATUS_FAILED;
1614 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
1615 ca_mutex_lock(g_bleLocalAddressMutex);
1616 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
1617 ca_mutex_unlock(g_bleLocalAddressMutex);
1619 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
1621 OICFree(local_address);
1623 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1624 return CA_STATUS_OK;
1627 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
1629 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1631 ca_mutex_lock(g_bleNetworkCbMutex);
1632 g_networkCallback = netCallback;
1633 ca_mutex_unlock(g_bleNetworkCbMutex);
1634 CAResult_t res = CA_STATUS_OK;
1637 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
1638 if (CA_STATUS_OK != res)
1640 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1645 res = CAUnSetLEAdapterStateChangedCb();
1646 if (CA_STATUS_OK != res)
1648 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1652 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1656 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
1658 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1660 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
1661 CAEndpoint_t localEndpoint = {};
1663 ca_mutex_lock(g_bleLocalAddressMutex);
1664 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
1665 ca_mutex_unlock(g_bleLocalAddressMutex);
1667 g_bleAdapterState = adapter_state;
1668 // Start a GattServer/Client if gLeServerStatus is SET
1669 if (CA_LISTENING_SERVER == gLeServerStatus)
1671 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1672 CAStartLEGattServer();
1674 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
1676 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
1677 CAStartLEGattClient();
1679 gLeServerStatus = CA_SERVER_NOTSTARTED;
1681 ca_mutex_lock(g_bleNetworkCbMutex);
1682 if (NULL != g_networkCallback)
1684 g_networkCallback(&localEndpoint, adapter_state);
1688 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
1690 ca_mutex_unlock(g_bleNetworkCbMutex);
1692 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1695 CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1699 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1701 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1702 #ifndef SINGLE_THREAD
1703 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1704 "g_bleClientSendQueueHandle is NULL",
1706 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1707 "g_bleClientSendDataMutex is NULL",
1710 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1713 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1715 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1718 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1719 return CA_MEMORY_ALLOC_FAILED;
1721 // Add message to send queue
1722 ca_mutex_lock(g_bleClientSendDataMutex);
1723 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1724 ca_mutex_unlock(g_bleClientSendDataMutex);
1726 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1727 return CA_STATUS_OK;
1731 CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1735 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1737 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1739 #ifdef SINGLE_THREAD
1740 char header[CA_HEADER_LENGTH] = {0};
1742 CAResult_t result = CAGenerateHeader(header, dataLen);
1744 if (CA_STATUS_OK != result)
1746 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1750 if (!CAIsLEConnected())
1752 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
1756 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
1757 if (CA_STATUS_OK != result)
1759 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1763 int32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1764 for (int32_t iter = 0; iter < dataLimit; iter++)
1766 result = CAUpdateCharacteristicsToAllGattClients((data +
1767 (iter * CA_SUPPORTED_BLE_MTU_SIZE)),
1768 CA_SUPPORTED_BLE_MTU_SIZE);
1769 if (CA_STATUS_OK != result)
1771 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1777 uint8_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
1780 result = CAUpdateCharacteristicsToAllGattClients((data +
1781 (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)),
1783 if (CA_STATUS_OK != result)
1785 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1791 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
1792 "BleClientReceiverQueue is NULL",
1794 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1795 "BleClientSendDataMutex is NULL",
1798 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1801 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1803 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1806 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1807 return CA_MEMORY_ALLOC_FAILED;
1809 // Add message to send queue
1810 ca_mutex_lock(g_bleServerSendDataMutex);
1811 CAQueueingThreadAddData(g_bleServerSendQueueHandle, bleData, sizeof(CALEData_t));
1812 ca_mutex_unlock(g_bleServerSendDataMutex);
1814 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1815 return CA_STATUS_OK;
1818 CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1819 const void *data, uint32_t dataLength,
1820 uint32_t *sentLength)
1822 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1825 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1826 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1827 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1829 #ifdef SINGLE_THREAD
1830 if(g_networkPacketReceivedCallback)
1832 CAEndpoint_t endPoint = { 0 }; // will be filled by upper layer
1833 endPoint.adapter = CA_ADAPTER_GATT_BTLE;
1834 g_networkPacketReceivedCallback(&endPoint, data, dataLength);
1837 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1840 //Add message to data queue
1841 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1842 CA_ADAPTER_GATT_BTLE,
1844 if (NULL == remoteEndpoint)
1846 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1847 return CA_STATUS_FAILED;
1850 // Create bleData to add to queue
1851 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1853 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1856 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1857 CAFreeEndpoint(remoteEndpoint);
1858 return CA_MEMORY_ALLOC_FAILED;
1861 CAFreeEndpoint(remoteEndpoint);
1862 // Add message to send queue
1863 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1865 *sentLength = dataLength;
1867 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1868 return CA_STATUS_OK;
1871 CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1872 const void *data, uint32_t dataLength,
1873 uint32_t *sentLength)
1875 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1878 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1879 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1880 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1881 #ifndef SINGLE_THREAD
1882 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1885 //Add message to data queue
1886 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1887 CA_ADAPTER_GATT_BTLE,
1889 if (NULL == remoteEndpoint)
1891 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1892 return CA_STATUS_FAILED;
1895 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1897 // Create bleData to add to queue
1898 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1901 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1902 CAFreeEndpoint(remoteEndpoint);
1903 return CA_MEMORY_ALLOC_FAILED;
1906 CAFreeEndpoint(remoteEndpoint);
1907 // Add message to send queue
1908 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1910 *sentLength = dataLength;
1912 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1913 return CA_STATUS_OK;
1916 void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
1918 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1920 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1921 g_bleAdapterThreadPool = handle;
1922 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1924 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1927 void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1929 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1931 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1933 g_networkPacketReceivedCallback = callback;
1935 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1937 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1940 void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
1943 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
1945 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
1946 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
1948 //if required, will be used to build remote end point
1949 g_errorHandler(rep, data, dataLen, result);
1951 CAFreeEndpoint(rep);
1952 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");