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 "OIC_LE_ADAP"
43 * Stores information of all the senders.
45 * This structure will be used to track and defragment all incoming
51 uint32_t totalDataLen;
53 CAEndpoint_t *remoteEndpoint;
59 ADAPTER_BOTH_CLIENT_SERVER,
65 * Stores the information of the Data to be sent from the queues.
67 * This structure will be pushed to the sender/receiver queue for
72 /// Remote endpoint contains the information of remote device.
73 CAEndpoint_t *remoteEndpoint;
75 /// Data to be transmitted over LE transport.
78 /// Length of the data being transmitted.
81 /// Sender information list
82 u_arraylist_t * senderInfo;
86 * Callback to provide the status of the network change to CA layer.
88 static CANetworkChangeCallback g_networkCallback = NULL;
91 * bleAddress of the local adapter. Value will be initialized to zero,
92 * and will be updated later.
94 static char g_localBLEAddress[18] = { 0 };
97 * Variable to differentiate btw GattServer and GattClient.
99 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
102 * Mutex to synchronize the task to be executed on the GattServer
105 static ca_mutex g_bleIsServerMutex = NULL;
108 * Mutex to synchronize the callback to be called for the network
111 static ca_mutex g_bleNetworkCbMutex = NULL;
114 * Mutex to synchronize the updates of the local LE address of the
117 static ca_mutex g_bleLocalAddressMutex = NULL;
120 * Reference to thread pool.
122 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
125 * Mutex to synchronize the task to be pushed to thread pool.
127 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
130 * Mutex to synchronize the queing of the data from SenderQueue.
132 static ca_mutex g_bleClientSendDataMutex = NULL;
135 * Mutex to synchronize the queing of the data from ReceiverQueue.
137 static ca_mutex g_bleReceiveDataMutex = NULL;
140 * Mutex to synchronize the queing of the data from SenderQueue.
142 static ca_mutex g_bleServerSendDataMutex = NULL;
145 * Mutex to synchronize the callback to be called for the
146 * adapterReqResponse.
148 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
151 * Callback to be called when network packet received from either
152 * GattServer or GattClient.
154 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
157 * Callback to notify error from the BLE adapter.
159 static CAErrorHandleCallback g_errorHandler = NULL;
162 * Register network change notification callback.
164 * @param[in] netCallback CANetworkChangeCallback callback which will
165 * be set for the change in network.
167 * @return 0 on success otherwise a positive error value.
168 * @retval ::CA_STATUS_OK Successful.
169 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
170 * @retval ::CA_STATUS_FAILED Operation failed.
173 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
176 * Set the thread pool handle which is required for spawning new
179 * @param[in] handle Thread pool handle which is given by above layer
180 * for using thread creation task.
183 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
186 * Call the callback to the upper layer when the device state gets
189 * @param[in] adapter_state New state of the adapter to be notified to
192 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
195 * Used to initialize all required mutex variable for LE Adapter
198 * @return 0 on success otherwise a positive error value.
199 * @retval ::CA_STATUS_OK Successful.
200 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
201 * @retval ::CA_STATUS_FAILED Operation failed.
204 static CAResult_t CAInitLEAdapterMutex();
207 * Terminate all required mutex variables for LE adapter
210 static void CATerminateLEAdapterMutex();
213 * Prepares and notify error through error callback.
215 static void CALEErrorHandler(const char *remoteAddress,
220 #ifndef SINGLE_THREAD
222 * Stop condition of recvhandler.
224 static bool g_dataBleReceiverHandlerState = false;
227 * Sender information.
229 static u_arraylist_t *g_bleServerSenderInfo = NULL;
231 static u_arraylist_t *g_bleClientSenderInfo = NULL;
234 * Queue to process the outgoing packets from GATTClient.
236 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
239 * Queue to process the incoming packets.
241 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
244 * Queue to process the outgoing packets from GATTServer.
246 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
249 * This function will be associated with the sender queue for
252 * This function will fragment the data to the MTU of the transport
253 * and send the data in fragments to the adapters. The function will
254 * be blocked until all data is sent out from the adapter.
256 * @param[in] threadData Data pushed to the queue which contains the
257 * info about RemoteEndpoint and Data.
259 static void CALEServerSendDataThread(void *threadData);
262 * This function will be associated with the sender queue for
265 * This function will fragment the data to the MTU of the transport
266 * and send the data in fragments to the adapters. The function will
267 * be blocked until all data is sent out from the adapter.
269 * @param[in] threadData Data pushed to the queue which contains the
270 * info about RemoteEndpoint and Data.
272 static void CALEClientSendDataThread(void *threadData);
275 * This function will be associated with the receiver queue.
277 * This function will defragment the received data from each sender
278 * respectively and will send it up to CA layer. Respective sender's
279 * header will provide the length of the data sent.
281 * @param[in] threadData Data pushed to the queue which contains the
282 * info about RemoteEndpoint and Data.
284 static void CALEDataReceiverHandler(void *threadData);
287 * This function will stop all queues created for GattServer and
288 * GattClient. All four queues will be be stopped with this function
291 static void CAStopLEQueues();
294 * This function will terminate all queues created for GattServer and
295 * GattClient. All four queues will be be terminated with this
296 * function invocations.
298 static void CATerminateLEQueues();
301 * This function will initalize the Receiver and Sender queues for
302 * GattServer. This function will in turn call the functions
303 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
304 * initialize the queues.
306 * @return ::CA_STATUS_OK or Appropriate error code.
307 * @retval ::CA_STATUS_OK Successful.
308 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
309 * @retval ::CA_STATUS_FAILED Operation failed.
311 static CAResult_t CAInitLEServerQueues();
314 * This function will initalize the Receiver and Sender queues for
315 * GattClient. This function will inturn call the functions
316 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
317 * initialize the queues.
319 * @return ::CA_STATUS_OK or Appropriate error code.
320 * @retval ::CA_STATUS_OK Successful.
321 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
322 * @retval ::CA_STATUS_FAILED Operation failed.
325 static CAResult_t CAInitLEClientQueues();
328 * This function will initalize the Receiver queue for
329 * GattServer. This will initialize the queue to process the function
330 * CABLEServerSendDataThread() when ever the task is added to this
333 * @return ::CA_STATUS_OK or Appropriate error code.
334 * @retval ::CA_STATUS_OK Successful.
335 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
336 * @retval ::CA_STATUS_FAILED Operation failed.
338 static CAResult_t CAInitLEServerSenderQueue();
341 * This function will initalize the Receiver queue for
342 * GattClient. This will initialize the queue to process the function
343 * CABLEClientSendDataThread() when ever the task is added to this
346 * @return ::CA_STATUS_OK or Appropriate error code.
347 * @retval ::CA_STATUS_OK Successful.
348 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
349 * @retval ::CA_STATUS_FAILED Operation failed.
351 static CAResult_t CAInitLEClientSenderQueue();
354 * This function will initialize the Receiver queue for
355 * LEAdapter. This will initialize the queue to process the function
356 * CABLEDataReceiverHandler() when ever the task is added to this
359 * @return ::CA_STATUS_OK or Appropriate error code
360 * @retval ::CA_STATUS_OK Successful
361 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
362 * @retval ::CA_STATUS_FAILED Operation failed
365 static CAResult_t CAInitLEReceiverQueue();
368 * This function will create the Data required to send it in the
371 * @param[in] remoteEndpoint Remote endpoint information of the
373 * @param[in] data Data to be transmitted from LE.
374 * @param[in] dataLength Length of the Data being transmitted.
376 * @return ::CA_STATUS_OK or Appropriate error code.
377 * @retval ::CA_STATUS_OK Successful.
378 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
379 * @retval ::CA_STATUS_FAILED Operation failed.
381 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
384 u_arraylist_t *senderInfo);
387 * Used to free the BLE information stored in the sender/receiver
390 * @param[in] bleData Information for a particular data segment.
392 static void CAFreeLEData(CALEData_t *bleData);
397 static void CALEDataDestroyer(void *data, uint32_t size);
399 static CAResult_t CAInitLEServerQueues()
401 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
403 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
405 CAResult_t result = CAInitLEServerSenderQueue();
406 if (CA_STATUS_OK != result)
408 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
409 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
410 return CA_STATUS_FAILED;
413 g_bleServerSenderInfo = u_arraylist_create();
414 if (!g_bleServerSenderInfo)
416 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
417 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
418 return CA_MEMORY_ALLOC_FAILED;
421 result = CAInitLEReceiverQueue();
422 if (CA_STATUS_OK != result)
424 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
425 u_arraylist_free(&g_bleServerSenderInfo);
426 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
427 return CA_STATUS_FAILED;
430 g_dataBleReceiverHandlerState = true;
432 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
434 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
438 static CAResult_t CAInitLEClientQueues()
440 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
442 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
444 CAResult_t result = CAInitLEClientSenderQueue();
445 if (CA_STATUS_OK != result)
447 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
448 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
449 return CA_STATUS_FAILED;
452 g_bleClientSenderInfo = u_arraylist_create();
453 if (!g_bleClientSenderInfo)
455 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
456 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
457 return CA_MEMORY_ALLOC_FAILED;
460 result = CAInitLEReceiverQueue();
461 if (CA_STATUS_OK != result)
463 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
464 u_arraylist_free(&g_bleClientSenderInfo);
465 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
466 return CA_STATUS_FAILED;
469 g_dataBleReceiverHandlerState = true;
471 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
473 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
477 static CAResult_t CAInitLEReceiverQueue()
479 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
480 // Check if the message queue is already initialized
481 if (g_bleReceiverQueue)
483 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
487 // Create recv message queue
488 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
489 if (!g_bleReceiverQueue)
491 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
492 return CA_MEMORY_ALLOC_FAILED;
495 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
496 g_bleAdapterThreadPool,
497 CALEDataReceiverHandler,
500 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
501 OICFree(g_bleReceiverQueue);
502 g_bleReceiverQueue = NULL;
503 return CA_STATUS_FAILED;
506 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
508 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
509 OICFree(g_bleReceiverQueue);
510 g_bleReceiverQueue = NULL;
511 return CA_STATUS_FAILED;
514 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
518 static CAResult_t CAInitLEServerSenderQueue()
520 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
521 // Check if the message queue is already initialized
522 if (g_bleServerSendQueueHandle)
524 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
528 // Create send message queue
529 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
530 if (!g_bleServerSendQueueHandle)
532 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
533 return CA_MEMORY_ALLOC_FAILED;
536 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
537 g_bleAdapterThreadPool,
538 CALEServerSendDataThread,
541 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
542 OICFree(g_bleServerSendQueueHandle);
543 g_bleServerSendQueueHandle = NULL;
544 return CA_STATUS_FAILED;
547 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
551 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
553 const size_t length = u_arraylist_length(*list);
554 for (size_t i = 0; i < length; ++i)
556 CABLESenderInfo_t * const info =
557 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
560 OICFree(info->defragData);
561 CAFreeEndpoint(info->remoteEndpoint);
565 u_arraylist_free(list);
568 static void CALEClearSenderInfo()
570 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
571 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
574 static CAResult_t CAInitLEClientSenderQueue()
576 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
578 if (g_bleClientSendQueueHandle)
580 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
584 // Create send message queue
585 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
586 if (!g_bleClientSendQueueHandle)
588 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
589 return CA_MEMORY_ALLOC_FAILED;
592 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
593 g_bleAdapterThreadPool,
594 CALEClientSendDataThread, CALEDataDestroyer))
596 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
597 OICFree(g_bleClientSendQueueHandle);
598 g_bleClientSendQueueHandle = NULL;
599 return CA_STATUS_FAILED;
602 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
606 static void CAStopLEQueues()
608 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
610 ca_mutex_lock(g_bleReceiveDataMutex);
611 if (NULL != g_bleReceiverQueue)
613 CAQueueingThreadStop(g_bleReceiverQueue);
615 ca_mutex_unlock(g_bleReceiveDataMutex);
617 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
620 static void CATerminateLEQueues()
622 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
624 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
625 OICFree(g_bleClientSendQueueHandle);
626 g_bleClientSendQueueHandle = NULL;
628 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
629 OICFree(g_bleServerSendQueueHandle);
630 g_bleServerSendQueueHandle = NULL;
632 CAQueueingThreadDestroy(g_bleReceiverQueue);
633 OICFree(g_bleReceiverQueue);
634 g_bleReceiverQueue = NULL;
636 CALEClearSenderInfo();
638 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
641 static CAResult_t CALEGetSenderInfo(const char *leAddress,
642 u_arraylist_t *senderInfoList,
643 CABLESenderInfo_t **senderInfo,
644 uint32_t *senderIndex)
646 VERIFY_NON_NULL_RET(leAddress,
648 "NULL BLE address argument",
649 CA_STATUS_INVALID_PARAM);
650 VERIFY_NON_NULL_RET(senderIndex,
652 "NULL index argument",
653 CA_STATUS_INVALID_PARAM);
655 const uint32_t listLength = u_arraylist_length(senderInfoList);
656 const uint32_t addrLength = strlen(leAddress);
657 for (uint32_t index = 0; index < listLength; index++)
659 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
660 if(!info || !(info->remoteEndpoint))
665 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
667 *senderIndex = index;
676 return CA_STATUS_FAILED;
679 static void CALEDataReceiverHandler(void *threadData)
681 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
683 ca_mutex_lock(g_bleReceiveDataMutex);
685 if (g_dataBleReceiverHandlerState)
687 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
689 CALEData_t *bleData = (CALEData_t *) threadData;
692 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
693 ca_mutex_unlock(g_bleReceiveDataMutex);
697 if (!(bleData->senderInfo))
699 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
700 ca_mutex_unlock(g_bleReceiveDataMutex);
704 if (!(bleData->remoteEndpoint))
706 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
707 ca_mutex_unlock(g_bleReceiveDataMutex);
711 CABLESenderInfo_t *senderInfo = NULL;
712 uint32_t senderIndex = 0;
714 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
716 &senderInfo, &senderIndex))
718 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
719 bleData->remoteEndpoint->addr);
724 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
727 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
728 ca_mutex_unlock(g_bleReceiveDataMutex);
731 newSender->recvDataLen = 0;
732 newSender->totalDataLen = 0;
733 newSender->defragData = NULL;
734 newSender->remoteEndpoint = NULL;
736 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
737 newSender->totalDataLen = CAParseHeader(bleData->data,
739 if(!(newSender->totalDataLen))
741 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
743 ca_mutex_unlock(g_bleReceiveDataMutex);
747 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
748 newSender->totalDataLen);
749 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
752 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
753 sizeof(*newSender->defragData));
755 if (NULL == newSender->defragData)
757 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
759 ca_mutex_unlock(g_bleReceiveDataMutex);
763 const char *remoteAddress = bleData->remoteEndpoint->addr;
764 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
765 CA_ADAPTER_GATT_BTLE,
768 if (NULL == newSender->remoteEndpoint)
770 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
771 OICFree(newSender->defragData);
773 ca_mutex_unlock(g_bleReceiveDataMutex);
776 memcpy(newSender->defragData, bleData->data + CA_HEADER_LENGTH,
777 bleData->dataLen - CA_HEADER_LENGTH);
778 newSender->recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
779 u_arraylist_add(bleData->senderInfo, (void *)newSender);
781 //Getting newSender index position in bleSenderInfo array list
783 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
786 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
787 OICFree(newSender->defragData);
788 CAFreeEndpoint(newSender->remoteEndpoint);
790 ca_mutex_unlock(g_bleReceiveDataMutex);
793 senderInfo = newSender;
797 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
799 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
800 "Data Length exceeding error!! Receiving [%d] total length [%d]",
801 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
802 u_arraylist_remove(bleData->senderInfo, senderIndex);
803 OICFree(senderInfo->defragData);
805 ca_mutex_unlock(g_bleReceiveDataMutex);
808 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
810 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
812 senderInfo->recvDataLen += bleData->dataLen ;
813 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Data lenhth [%d]",
814 senderInfo->totalDataLen, senderInfo->recvDataLen);
817 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
819 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
820 if (NULL == g_networkPacketReceivedCallback)
822 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
824 u_arraylist_remove(bleData->senderInfo, senderIndex);
825 OICFree(senderInfo->defragData);
827 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
828 ca_mutex_unlock(g_bleReceiveDataMutex);
832 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
834 const CASecureEndpoint_t tmp =
836 .endpoint = *senderInfo->remoteEndpoint
839 g_networkPacketReceivedCallback(&tmp,
840 senderInfo->defragData,
841 senderInfo->recvDataLen);
842 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
843 u_arraylist_remove(bleData->senderInfo, senderIndex);
844 senderInfo->remoteEndpoint = NULL;
845 senderInfo->defragData = NULL;
849 ca_mutex_unlock(g_bleReceiveDataMutex);
850 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
853 static void CALEServerSendDataThread(void *threadData)
855 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
857 CALEData_t * const bleData = (CALEData_t *) threadData;
860 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
864 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
865 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
867 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
871 "Server total Data length with header is [%u]",
874 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
876 if (NULL == dataSegment)
878 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
883 CAResult_t result = CAGenerateHeader(header,
886 if (CA_STATUS_OK != result )
888 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
890 OICFree(dataSegment);
894 memcpy(dataSegment, header, CA_HEADER_LENGTH);
898 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
900 length = totalLength;
901 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
905 length = CA_SUPPORTED_BLE_MTU_SIZE;
906 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
907 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
910 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
913 // Send the first segment with the header.
914 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
916 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
918 result = CAUpdateCharacteristicsToGattClient(
919 bleData->remoteEndpoint->addr, dataSegment, length);
921 if (CA_STATUS_OK != result)
925 "Update characteristics failed, result [%d]",
928 g_errorHandler(bleData->remoteEndpoint,
932 OICFree(dataSegment);
938 "Server Sent data length [%u]",
940 for (index = 1; index < iter; index++)
942 // Send the remaining header.
945 "Sending the chunk number [%u]",
949 CAUpdateCharacteristicsToGattClient(
950 bleData->remoteEndpoint->addr,
951 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
952 CA_SUPPORTED_BLE_MTU_SIZE);
953 if (CA_STATUS_OK != result)
955 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
956 "Update characteristics failed, result [%d]", result);
957 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
958 OICFree(dataSegment);
961 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
962 CA_SUPPORTED_BLE_MTU_SIZE);
965 const uint32_t remainingLen =
966 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
968 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
970 // send the last segment of the data (Ex: 22 bytes of 622
971 // bytes of data when MTU is 200)
972 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
973 result = CAUpdateCharacteristicsToGattClient(
974 bleData->remoteEndpoint->addr,
975 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
977 if (CA_STATUS_OK != result)
981 "Update characteristics failed, result [%d]",
983 g_errorHandler(bleData->remoteEndpoint,
987 OICFree(dataSegment);
990 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
995 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
996 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
997 if (CA_STATUS_OK != result)
999 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1001 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1002 OICFree(dataSegment);
1005 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1006 for (index = 1; index < iter; index++)
1008 // Send the remaining header.
1009 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1010 result = CAUpdateCharacteristicsToAllGattClients(
1011 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1012 CA_SUPPORTED_BLE_MTU_SIZE);
1013 if (CA_STATUS_OK != result)
1015 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1017 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1018 OICFree(dataSegment);
1021 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1022 CA_SUPPORTED_BLE_MTU_SIZE);
1025 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1026 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1028 // send the last segment of the data
1029 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1030 result = CAUpdateCharacteristicsToAllGattClients(
1031 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1033 if (CA_STATUS_OK != result)
1035 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1037 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1038 OICFree(dataSegment);
1041 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1044 OICFree(dataSegment);
1046 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1049 static void CALEClientSendDataThread(void *threadData)
1051 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1053 CALEData_t *bleData = (CALEData_t *) threadData;
1056 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1060 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
1061 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1063 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1064 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1065 if (NULL == dataSegment)
1067 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1072 CAResult_t result = CAGenerateHeader(header,
1075 if (CA_STATUS_OK != result )
1077 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1079 OICFree(dataSegment);
1082 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1085 uint32_t length = 0;
1086 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1088 length = totalLength;
1089 memcpy(dataSegment + CA_HEADER_LENGTH,
1095 length = CA_SUPPORTED_BLE_MTU_SIZE;
1096 memcpy(dataSegment + CA_HEADER_LENGTH,
1098 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1101 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1103 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1105 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1106 // Send the first segment with the header.
1108 CAUpdateCharacteristicsToGattServer(
1109 bleData->remoteEndpoint->addr,
1115 if (CA_STATUS_OK != result)
1119 "Update characteristics failed, result [%d]",
1121 g_errorHandler(bleData->remoteEndpoint,
1125 OICFree(dataSegment);
1131 "Client Sent Data length is [%u]",
1134 for (index = 1; index < iter; index++)
1136 // Send the remaining header.
1137 result = CAUpdateCharacteristicsToGattServer(
1138 bleData->remoteEndpoint->addr,
1139 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1140 CA_SUPPORTED_BLE_MTU_SIZE,
1142 if (CA_STATUS_OK != result)
1146 "Update characteristics failed, result [%d]",
1148 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1149 OICFree(dataSegment);
1152 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1153 CA_SUPPORTED_BLE_MTU_SIZE);
1156 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1157 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1159 // send the last segment of the data (Ex: 22 bytes of 622
1160 // bytes of data when MTU is 200)
1161 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1162 result = CAUpdateCharacteristicsToGattServer(
1163 bleData->remoteEndpoint->addr,
1164 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1168 if (CA_STATUS_OK != result)
1170 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1172 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1173 OICFree(dataSegment);
1176 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1181 //Sending Mulitcast Data
1182 // Send the first segment with the header.
1183 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1184 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1185 if (CA_STATUS_OK != result)
1187 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1188 "Update characteristics (all) failed, result [%d]", result);
1189 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1190 OICFree(dataSegment);
1193 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1194 // Send the remaining header.
1195 for (index = 1; index < iter; index++)
1197 result = CAUpdateCharacteristicsToAllGattServers(
1198 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1199 CA_SUPPORTED_BLE_MTU_SIZE);
1200 if (CA_STATUS_OK != result)
1202 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1204 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1205 OICFree(dataSegment);
1208 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1209 CA_SUPPORTED_BLE_MTU_SIZE);
1212 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1213 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1215 // send the last segment of the data (Ex: 22 bytes of 622
1216 // bytes of data when MTU is 200)
1217 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1219 CAUpdateCharacteristicsToAllGattServers(
1220 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1222 if (CA_STATUS_OK != result)
1224 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1225 "Update characteristics (all) failed, result [%d]", result);
1226 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1227 OICFree(dataSegment);
1230 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1235 OICFree(dataSegment);
1237 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1240 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1241 const uint8_t *data,
1242 uint32_t dataLength,
1243 u_arraylist_t *senderInfo)
1245 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1249 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1253 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1254 bleData->data = OICCalloc(dataLength + 1, 1);
1256 if (NULL == bleData->data)
1258 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1259 CAFreeLEData(bleData);
1263 memcpy(bleData->data, data, dataLength);
1264 bleData->dataLen = dataLength;
1267 bleData->senderInfo = senderInfo;
1273 static void CAFreeLEData(CALEData_t *bleData)
1275 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1277 CAFreeEndpoint(bleData->remoteEndpoint);
1278 OICFree(bleData->data);
1282 static void CALEDataDestroyer(void *data, uint32_t size)
1284 if ((size_t)size < sizeof(CALEData_t *))
1286 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1287 "Destroy data too small %p %d", data, size);
1289 CALEData_t *ledata = (CALEData_t *) data;
1291 CAFreeLEData(ledata);
1295 static CAResult_t CAInitLEAdapterMutex()
1297 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1299 if (NULL == g_bleIsServerMutex)
1301 g_bleIsServerMutex = ca_mutex_new();
1302 if (NULL == g_bleIsServerMutex)
1304 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1305 return CA_STATUS_FAILED;
1309 if (NULL == g_bleNetworkCbMutex)
1311 g_bleNetworkCbMutex = ca_mutex_new();
1312 if (NULL == g_bleNetworkCbMutex)
1314 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1315 CATerminateLEAdapterMutex();
1316 return CA_STATUS_FAILED;
1320 if (NULL == g_bleLocalAddressMutex)
1322 g_bleLocalAddressMutex = ca_mutex_new();
1323 if (NULL == g_bleLocalAddressMutex)
1325 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1326 CATerminateLEAdapterMutex();
1327 return CA_STATUS_FAILED;
1331 if (NULL == g_bleAdapterThreadPoolMutex)
1333 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1334 if (NULL == g_bleAdapterThreadPoolMutex)
1336 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1337 CATerminateLEAdapterMutex();
1338 return CA_STATUS_FAILED;
1342 if (NULL == g_bleClientSendDataMutex)
1344 g_bleClientSendDataMutex = ca_mutex_new();
1345 if (NULL == g_bleClientSendDataMutex)
1347 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1348 CATerminateLEAdapterMutex();
1349 return CA_STATUS_FAILED;
1353 if (NULL == g_bleServerSendDataMutex)
1355 g_bleServerSendDataMutex = ca_mutex_new();
1356 if (NULL == g_bleServerSendDataMutex)
1358 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1359 CATerminateLEAdapterMutex();
1360 return CA_STATUS_FAILED;
1364 if (NULL == g_bleAdapterReqRespCbMutex)
1366 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1367 if (NULL == g_bleAdapterReqRespCbMutex)
1369 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1370 CATerminateLEAdapterMutex();
1371 return CA_STATUS_FAILED;
1375 if (NULL == g_bleReceiveDataMutex)
1377 g_bleReceiveDataMutex = ca_mutex_new();
1378 if (NULL == g_bleReceiveDataMutex)
1380 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1381 return CA_STATUS_FAILED;
1385 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1386 return CA_STATUS_OK;
1389 static void CATerminateLEAdapterMutex()
1391 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1393 ca_mutex_free(g_bleIsServerMutex);
1394 g_bleIsServerMutex = NULL;
1396 ca_mutex_free(g_bleNetworkCbMutex);
1397 g_bleNetworkCbMutex = NULL;
1399 ca_mutex_free(g_bleLocalAddressMutex);
1400 g_bleLocalAddressMutex = NULL;
1402 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1403 g_bleAdapterThreadPoolMutex = NULL;
1405 ca_mutex_free(g_bleClientSendDataMutex);
1406 g_bleClientSendDataMutex = NULL;
1408 ca_mutex_free(g_bleServerSendDataMutex);
1409 g_bleServerSendDataMutex = NULL;
1411 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1412 g_bleAdapterReqRespCbMutex = NULL;
1414 ca_mutex_free(g_bleReceiveDataMutex);
1415 g_bleReceiveDataMutex = NULL;
1417 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1421 * Starting LE connectivity adapters.
1423 * As its peer to peer it does not require to start any servers.
1425 * @return ::CA_STATUS_OK or Appropriate error code.
1427 static CAResult_t CAStartLE();
1430 * Start listening server for receiving multicast search requests.
1432 * Transport Specific Behavior:
1433 * LE Starts GATT Server with prefixed UUID and Characteristics
1434 * per OIC Specification.
1435 * @return ::CA_STATUS_OK or Appropriate error code.
1437 static CAResult_t CAStartLEListeningServer();
1440 * Stops listening server from receiving multicast search requests.
1442 * Transport Specific Behavior:
1443 * LE Starts GATT Server with prefixed UUID and Characteristics
1444 * per OIC Specification.
1445 * @return ::CA_STATUS_OK or Appropriate error code.
1447 static CAResult_t CAStopLEListeningServer();
1450 * Sarting discovery of servers for receiving multicast
1453 * Transport Specific Behavior:
1454 * LE Starts GATT Server with prefixed UUID and Characteristics
1455 * per OIC Specification.
1457 * @return ::CA_STATUS_OK or Appropriate error code
1459 static CAResult_t CAStartLEDiscoveryServer();
1462 * Send data to the endpoint using the adapter connectivity.
1464 * @param[in] endpoint Remote Endpoint information (like MAC address,
1465 * reference URI and connectivity type) to which
1466 * the unicast data has to be sent.
1467 * @param[in] data Data which required to be sent.
1468 * @param[in] dataLen Size of data to be sent.
1470 * @note dataLen must be > 0.
1472 * @return The number of bytes sent on the network, or -1 on error.
1474 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1479 * Send multicast data to the endpoint using the LE connectivity.
1481 * @param[in] endpoint Remote Endpoint information to which the
1482 * multicast data has to be sent.
1483 * @param[in] data Data which required to be sent.
1484 * @param[in] dataLen Size of data to be sent.
1486 * @note dataLen must be > 0.
1488 * @return The number of bytes sent on the network, or -1 on error.
1490 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1495 * Get LE Connectivity network information.
1497 * @param[out] info Local connectivity information structures.
1498 * @param[out] size Number of local connectivity structures.
1500 * @return ::CA_STATUS_OK or Appropriate error code.
1502 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1506 * Read Synchronous API callback.
1508 * @return ::CA_STATUS_OK or Appropriate error code.
1510 static CAResult_t CAReadLEData();
1513 * Stopping the adapters and close socket connections.
1515 * LE Stops all GATT servers and GATT Clients.
1517 * @return ::CA_STATUS_OK or Appropriate error code.
1519 static CAResult_t CAStopLE();
1522 * Terminate the LE connectivity adapter.
1524 * Configuration information will be deleted from further use.
1526 static void CATerminateLE();
1529 * This function will receive the data from the GattServer and add the
1530 * data to the Server receiver queue.
1532 * @param[in] remoteAddress Remote address of the device from where
1534 * @param[in] data Actual data recevied from the remote
1536 * @param[in] dataLength Length of the data received from the
1538 * @param[in] sentLength Length of the data sent from the remote
1541 * @return ::CA_STATUS_OK or Appropriate error code.
1542 * @retval ::CA_STATUS_OK Successful.
1543 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1544 * @retval ::CA_STATUS_FAILED Operation failed.
1547 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1548 const uint8_t *data,
1549 uint32_t dataLength,
1550 uint32_t *sentLength);
1553 * This function will receive the data from the GattClient and add the
1554 * data into the Client receiver queue.
1556 * @param[in] remoteAddress Remote address of the device from where
1558 * @param[in] data Actual data recevied from the remote
1560 * @param[in] dataLength Length of the data received from the
1562 * @param[in] sentLength Length of the data sent from the remote
1565 * @return ::CA_STATUS_OK or Appropriate error code.
1566 * @retval ::CA_STATUS_OK Successful.
1567 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1568 * @retval ::CA_STATUS_FAILED Operation failed.
1570 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1571 const uint8_t *data,
1572 uint32_t dataLength,
1573 uint32_t *sentLength);
1576 * Set the NetworkPacket received callback to CA layer from adapter
1579 * @param[in] callback Callback handle sent from the upper layer.
1581 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1584 * Push the data from CA layer to the Sender processor queue.
1586 * @param[in] remoteEndpoint Remote endpoint information of the
1588 * @param[in] data Data to be transmitted from LE.
1589 * @param[in] dataLen Length of the Data being transmitted.
1591 * @return ::CA_STATUS_OK or Appropriate error code.
1592 * @retval ::CA_STATUS_OK Successful.
1593 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1594 * @retval ::CA_STATUS_FAILED Operation failed.
1596 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1597 const uint8_t *data,
1601 * Push the data from CA layer to the Sender processor queue.
1603 * @param[in] remoteEndpoint Remote endpoint information of the
1605 * @param[in] data Data to be transmitted from LE.
1606 * @param[in] dataLen Length of the Data being transmitted.
1608 * @return ::CA_STATUS_OK or Appropriate error code.
1609 * @retval ::CA_STATUS_OK Successful.
1610 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1611 * @retval ::CA_STATUS_FAILED Operation failed.
1613 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1614 const uint8_t *data,
1617 static CAResult_t CALEAdapterGattServerStart()
1619 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1621 CAResult_t result = CAStartLEGattServer();
1623 #ifndef SINGLE_THREAD
1625 Don't start the server side sending queue thread until the
1626 server itself has actually started.
1628 if (CA_STATUS_OK == result)
1630 ca_mutex_lock(g_bleServerSendDataMutex);
1631 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1632 ca_mutex_unlock(g_bleServerSendDataMutex);
1634 if (CA_STATUS_OK != result)
1638 "Unable to start server queuing thread (%d)",
1647 static CAResult_t CALEAdapterGattServerStop()
1649 #ifndef SINGLE_THREAD
1650 ca_mutex_lock(g_bleServerSendDataMutex);
1651 CAResult_t result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1652 ca_mutex_unlock(g_bleServerSendDataMutex);
1653 if (CA_STATUS_OK == result)
1655 result = CAStopLEGattServer();
1660 return CAStopLEGattServer();
1664 static CAResult_t CALEAdapterGattClientStart()
1666 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1668 CAResult_t result = CAStartLEGattClient();
1670 #ifndef SINGLE_THREAD
1672 Don't start the client side sending queue thread until the
1673 client itself has actually started.
1675 if (CA_STATUS_OK == result)
1677 ca_mutex_lock(g_bleClientSendDataMutex);
1678 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1679 ca_mutex_unlock(g_bleClientSendDataMutex);
1681 if (CA_STATUS_OK != result)
1685 "Unable to start client queuing thread");
1693 static CAResult_t CALEAdapterGattClientStop()
1695 #ifndef SINGLE_THREAD
1696 ca_mutex_lock(g_bleClientSendDataMutex);
1697 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1698 ca_mutex_unlock(g_bleClientSendDataMutex);
1699 if (CA_STATUS_OK == result)
1701 CAStopLEGattClient();
1706 CAStopLEGattClient();
1708 return CA_STATUS_OK;
1712 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1713 CANetworkPacketReceivedCallback reqRespCallback,
1714 CANetworkChangeCallback netCallback,
1715 CAErrorHandleCallback errorCallback,
1716 ca_thread_pool_t handle)
1718 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1721 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1722 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1723 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1725 CAResult_t result = CA_STATUS_OK;
1726 result = CAInitLEAdapterMutex();
1727 if (CA_STATUS_OK != result)
1729 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1730 return CA_STATUS_FAILED;
1733 result = CAInitializeLENetworkMonitor();
1734 if (CA_STATUS_OK != result)
1736 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1737 return CA_STATUS_FAILED;
1740 CAInitializeLEAdapter();
1742 CASetLEClientThreadPoolHandle(handle);
1744 result = CAInitializeLEGattClient();
1745 if (CA_STATUS_OK != result)
1747 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1748 return CA_STATUS_FAILED;
1751 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1752 CASetLEServerThreadPoolHandle(handle);
1753 result = CAInitializeLEGattServer();
1754 if (CA_STATUS_OK != result)
1756 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1757 return CA_STATUS_FAILED;
1760 CASetLEAdapterThreadPoolHandle(handle);
1761 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1762 CASetLEReqRespAdapterCallback(reqRespCallback);
1764 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1765 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1766 CALERegisterNetworkNotifications(netCallback);
1768 g_errorHandler = errorCallback;
1770 static const CAConnectivityHandler_t connHandler =
1772 .startAdapter = CAStartLE,
1773 .stopAdapter = CAStopLE,
1774 .startListenServer = CAStartLEListeningServer,
1775 .stopListenServer = CAStopLEListeningServer,
1776 .startDiscoveryServer = CAStartLEDiscoveryServer,
1777 .sendData = CASendLEUnicastData,
1778 .sendDataToAll = CASendLEMulticastData,
1779 .GetnetInfo = CAGetLEInterfaceInformation,
1780 .readData = CAReadLEData,
1781 .terminate = CATerminateLE
1784 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1786 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1788 return CA_STATUS_OK;
1791 static CAResult_t CAStartLE()
1793 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1795 return CAStartLEAdapter();
1798 static CAResult_t CAStopLE()
1800 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1801 #ifndef SINGLE_THREAD
1805 ca_mutex_lock(g_bleIsServerMutex);
1806 switch (g_adapterType)
1808 case ADAPTER_SERVER:
1809 CALEAdapterGattServerStop();
1811 case ADAPTER_CLIENT:
1812 CALEAdapterGattClientStop();
1814 case ADAPTER_BOTH_CLIENT_SERVER:
1815 CALEAdapterGattServerStop();
1816 CALEAdapterGattClientStop();
1821 ca_mutex_unlock(g_bleIsServerMutex);
1823 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1825 return CAStopLEAdapter();
1828 static void CATerminateLE()
1830 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1832 CASetLEReqRespServerCallback(NULL);
1833 CASetLEReqRespClientCallback(NULL);
1834 CALERegisterNetworkNotifications(NULL);
1835 CASetLEReqRespAdapterCallback(NULL);
1836 CATerminateLENetworkMonitor();
1838 ca_mutex_lock(g_bleIsServerMutex);
1839 switch (g_adapterType)
1841 case ADAPTER_SERVER:
1842 CATerminateLEGattServer();
1844 case ADAPTER_CLIENT:
1845 CATerminateLEGattClient();
1847 case ADAPTER_BOTH_CLIENT_SERVER:
1848 CATerminateLEGattServer();
1849 CATerminateLEGattClient();
1854 g_adapterType = ADAPTER_EMPTY;
1855 ca_mutex_unlock(g_bleIsServerMutex);
1857 #ifndef SINGLE_THREAD
1858 CATerminateLEQueues();
1860 CATerminateLEAdapterMutex();
1862 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1865 static CAResult_t CAStartLEListeningServer()
1867 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1868 #ifndef ROUTING_GATEWAY
1869 CAResult_t result = CA_STATUS_OK;
1870 #ifndef SINGLE_THREAD
1871 result = CAInitLEServerQueues();
1872 if (CA_STATUS_OK != result)
1874 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1879 ca_mutex_lock(g_bleIsServerMutex);
1880 switch (g_adapterType)
1882 case ADAPTER_CLIENT:
1883 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1885 case ADAPTER_BOTH_CLIENT_SERVER:
1888 g_adapterType = ADAPTER_SERVER;
1890 ca_mutex_unlock(g_bleIsServerMutex);
1892 result = CAGetLEAdapterState();
1893 if (CA_STATUS_OK != result)
1895 if (CA_ADAPTER_NOT_ENABLED == result)
1899 "Listen Server will be started once BT Adapter is enabled");
1904 result = CALEAdapterGattServerStart();
1907 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1910 // Routing Gateway only supports BLE client mode.
1911 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1912 return CA_NOT_SUPPORTED;
1916 static CAResult_t CAStopLEListeningServer()
1918 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1919 return CA_NOT_SUPPORTED;
1922 static CAResult_t CAStartLEDiscoveryServer()
1924 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1925 CAResult_t result = CA_STATUS_OK;
1926 #ifndef SINGLE_THREAD
1927 result = CAInitLEClientQueues();
1928 if (CA_STATUS_OK != result)
1930 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1935 ca_mutex_lock(g_bleIsServerMutex);
1936 switch (g_adapterType)
1938 case ADAPTER_SERVER:
1939 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1941 case ADAPTER_BOTH_CLIENT_SERVER:
1944 g_adapterType = ADAPTER_CLIENT;
1946 ca_mutex_unlock(g_bleIsServerMutex);
1948 result = CAGetLEAdapterState();
1949 if (CA_STATUS_OK != result)
1951 if (CA_ADAPTER_NOT_ENABLED == result)
1955 "Discovery Server will be started once BT Adapter is enabled");
1960 result = CALEAdapterGattClientStart();
1963 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1967 static CAResult_t CAReadLEData()
1969 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1970 #ifdef SINGLE_THREAD
1973 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1974 return CA_STATUS_OK;
1977 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1981 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
1984 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1985 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1987 CAResult_t result = CA_STATUS_FAILED;
1989 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
1990 if (ADAPTER_EMPTY == g_adapterType)
1992 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
1995 ca_mutex_lock(g_bleIsServerMutex);
1996 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1998 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1999 if (CA_STATUS_OK != result)
2001 ca_mutex_unlock(g_bleIsServerMutex);
2002 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
2005 g_errorHandler(endpoint, data, dataLen, result);
2012 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2014 result = CALEAdapterClientSendData(endpoint, data, dataLen);
2015 if (CA_STATUS_OK != result)
2017 ca_mutex_unlock(g_bleIsServerMutex);
2018 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2022 g_errorHandler(endpoint, data, dataLen, result);
2027 ca_mutex_unlock(g_bleIsServerMutex);
2029 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2033 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2037 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2040 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2044 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2048 CAResult_t result = CA_STATUS_FAILED;
2050 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2051 if (ADAPTER_EMPTY == g_adapterType)
2053 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2056 ca_mutex_lock(g_bleIsServerMutex);
2057 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2059 result = CALEAdapterServerSendData(NULL, data, dataLen);
2060 if (CA_STATUS_OK != result)
2062 ca_mutex_unlock(g_bleIsServerMutex);
2064 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2068 g_errorHandler(endpoint, data, dataLen, result);
2074 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2076 result = CALEAdapterClientSendData(NULL, data, dataLen);
2077 if (CA_STATUS_OK != result)
2079 ca_mutex_unlock(g_bleIsServerMutex);
2081 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2085 g_errorHandler(endpoint, data, dataLen, result);
2090 ca_mutex_unlock(g_bleIsServerMutex);
2092 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2096 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2098 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2100 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2102 char *local_address = NULL;
2104 CAResult_t res = CAGetLEAddress(&local_address);
2105 if (CA_STATUS_OK != res)
2107 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2111 if (NULL == local_address)
2113 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2114 return CA_STATUS_FAILED;
2118 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2119 if (NULL == (*info))
2121 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2122 OICFree(local_address);
2123 return CA_STATUS_FAILED;
2126 size_t local_address_len = strlen(local_address);
2128 if(local_address_len >= sizeof(g_localBLEAddress) ||
2129 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2131 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2133 OICFree(local_address);
2134 return CA_STATUS_FAILED;
2137 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2138 ca_mutex_lock(g_bleLocalAddressMutex);
2139 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2140 ca_mutex_unlock(g_bleLocalAddressMutex);
2142 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2144 OICFree(local_address);
2146 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2147 return CA_STATUS_OK;
2150 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2152 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2154 ca_mutex_lock(g_bleNetworkCbMutex);
2155 g_networkCallback = netCallback;
2156 ca_mutex_unlock(g_bleNetworkCbMutex);
2157 CAResult_t res = CA_STATUS_OK;
2160 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2161 if (CA_STATUS_OK != res)
2163 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2168 res = CAUnSetLEAdapterStateChangedCb();
2169 if (CA_STATUS_OK != res)
2171 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2175 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2179 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2181 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2183 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2184 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2186 ca_mutex_lock(g_bleLocalAddressMutex);
2187 OICStrcpy(localEndpoint.addr,
2188 sizeof(localEndpoint.addr),
2190 ca_mutex_unlock(g_bleLocalAddressMutex);
2192 if (CA_ADAPTER_ENABLED == adapter_state)
2194 ca_mutex_lock(g_bleIsServerMutex);
2195 switch (g_adapterType)
2197 case ADAPTER_SERVER:
2198 CALEAdapterGattServerStart();
2200 case ADAPTER_CLIENT:
2201 CALEAdapterGattClientStart();
2203 case ADAPTER_BOTH_CLIENT_SERVER:
2204 CALEAdapterGattServerStart();
2205 CALEAdapterGattClientStart();
2210 ca_mutex_unlock(g_bleIsServerMutex);
2214 ca_mutex_lock(g_bleIsServerMutex);
2215 switch (g_adapterType)
2217 case ADAPTER_SERVER:
2218 CALEAdapterGattServerStop();
2220 case ADAPTER_CLIENT:
2221 CALEAdapterGattClientStop();
2223 case ADAPTER_BOTH_CLIENT_SERVER:
2224 CALEAdapterGattServerStop();
2225 CALEAdapterGattClientStop();
2230 ca_mutex_unlock(g_bleIsServerMutex);
2233 ca_mutex_lock(g_bleNetworkCbMutex);
2234 if (NULL != g_networkCallback)
2236 g_networkCallback(&localEndpoint, adapter_state);
2240 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2242 ca_mutex_unlock(g_bleNetworkCbMutex);
2244 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2247 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2248 const uint8_t *data,
2251 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2253 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2254 #ifndef SINGLE_THREAD
2255 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2256 "g_bleClientSendQueueHandle is NULL",
2258 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2259 "g_bleClientSendDataMutex is NULL",
2262 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2263 "g_bleClientSendQueueHandle",
2266 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2268 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2271 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2272 return CA_MEMORY_ALLOC_FAILED;
2274 // Add message to send queue
2275 ca_mutex_lock(g_bleClientSendDataMutex);
2276 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2277 ca_mutex_unlock(g_bleClientSendDataMutex);
2279 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2280 return CA_STATUS_OK;
2283 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2284 const uint8_t *data,
2287 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2289 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2291 #ifdef SINGLE_THREAD
2292 uint8_t header[CA_HEADER_LENGTH] = { 0 };
2295 CAGenerateHeader(header, CA_HEADER_LENGTH, dataLen);
2297 if (CA_STATUS_OK != result)
2299 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
2300 return CA_STATUS_FAILED;
2303 if (!CAIsLEConnected())
2305 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2306 return CA_STATUS_FAILED;
2309 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
2310 if (CA_STATUS_OK != result)
2312 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2313 return CA_STATUS_FAILED;
2316 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2317 for (uint32_t iter = 0; iter < dataLimit; iter++)
2320 CAUpdateCharacteristicsToAllGattClients(
2321 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2322 CA_SUPPORTED_BLE_MTU_SIZE);
2324 if (CA_STATUS_OK != result)
2326 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2327 return CA_STATUS_FAILED;
2333 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2337 CAUpdateCharacteristicsToAllGattClients(
2338 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2340 if (CA_STATUS_OK != result)
2342 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2343 return CA_STATUS_FAILED;
2348 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2349 "BleClientReceiverQueue is NULL",
2351 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2352 "BleClientSendDataMutex is NULL",
2355 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2358 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2360 CALEData_t * const bleData =
2361 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2365 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2366 return CA_MEMORY_ALLOC_FAILED;
2369 // Add message to send queue
2370 ca_mutex_lock(g_bleServerSendDataMutex);
2371 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2373 sizeof(CALEData_t));
2374 ca_mutex_unlock(g_bleServerSendDataMutex);
2376 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2377 return CA_STATUS_OK;
2380 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2381 const uint8_t *data,
2382 uint32_t dataLength,
2383 uint32_t *sentLength)
2385 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2388 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2389 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2391 #ifdef SINGLE_THREAD
2392 if(g_networkPacketReceivedCallback)
2394 // will be filled by upper layer
2395 const CASecureEndpoint_t endpoint =
2396 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2399 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2402 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2404 "g_bleReceiverQueue",
2407 //Add message to data queue
2408 CAEndpoint_t * const remoteEndpoint =
2409 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2410 CA_ADAPTER_GATT_BTLE,
2414 if (NULL == remoteEndpoint)
2416 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2417 return CA_STATUS_FAILED;
2420 // Create bleData to add to queue
2423 "Data received from LE layer [%d]",
2426 CALEData_t * const bleData =
2427 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2431 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2432 CAFreeEndpoint(remoteEndpoint);
2433 return CA_MEMORY_ALLOC_FAILED;
2436 CAFreeEndpoint(remoteEndpoint);
2437 // Add message to receiver queue
2438 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2440 *sentLength = dataLength;
2442 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2443 return CA_STATUS_OK;
2446 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2447 const uint8_t *data,
2448 uint32_t dataLength,
2449 uint32_t *sentLength)
2451 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2454 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2455 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2456 #ifndef SINGLE_THREAD
2457 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2458 "g_bleReceiverQueue",
2461 //Add message to data queue
2462 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2463 CA_ADAPTER_GATT_BTLE,
2465 if (NULL == remoteEndpoint)
2467 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2468 return CA_STATUS_FAILED;
2471 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2473 // Create bleData to add to queue
2474 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2475 dataLength, g_bleClientSenderInfo);
2478 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2479 CAFreeEndpoint(remoteEndpoint);
2480 return CA_MEMORY_ALLOC_FAILED;
2483 CAFreeEndpoint(remoteEndpoint);
2484 // Add message to receiver queue
2485 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2487 *sentLength = dataLength;
2489 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2490 return CA_STATUS_OK;
2493 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2495 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2497 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2498 g_bleAdapterThreadPool = handle;
2499 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2501 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2504 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2506 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2508 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2510 g_networkPacketReceivedCallback = callback;
2512 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2514 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2517 static void CALEErrorHandler(const char *remoteAddress,
2518 const uint8_t *data,
2522 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2524 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2526 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2527 CA_ADAPTER_GATT_BTLE,
2531 // if required, will be used to build remote endpoint
2532 g_errorHandler(rep, data, dataLen, result);
2534 CAFreeEndpoint(rep);
2536 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");