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 * Sender informations to be stored here
208 static u_arraylist_t *g_senderInfo = NULL;
211 * Queue to process the outgoing packets from GATTClient.
213 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
216 * Queue to process the incoming packets to GATT Client.
218 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
221 * Queue to process the outgoing packets from GATTServer.
223 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
226 * Mutex to synchronize the incoming data packets to receiver
228 static ca_mutex g_bleReceiveDataMutex = NULL;
234 static void CALEDataDestroyer(void *data, uint32_t size);
236 void CAInitLEQueues()
238 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
240 CAResult_t result = CAInitLEServerQueues();
241 if (CA_STATUS_OK != result)
243 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
247 result = CAInitLEClientQueues();
248 if (CA_STATUS_OK != result)
250 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
254 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
257 CAResult_t CAInitLEServerQueues()
259 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
261 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
263 CAResult_t result = CAInitLEServerSenderQueue();
264 if (CA_STATUS_OK != result)
266 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
267 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
268 return CA_STATUS_FAILED;
271 result = CAInitLEReceiverQueue();
272 if (CA_STATUS_OK != result)
274 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
275 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
276 return CA_STATUS_FAILED;
279 g_dataReceiverHandlerState = true;
281 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
283 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
287 CAResult_t CAInitLEClientQueues()
289 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
291 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
293 CAResult_t result = CAInitLEClientSenderQueue();
294 if (CA_STATUS_OK != result)
296 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
297 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
298 return CA_STATUS_FAILED;
301 result = CAInitLEReceiverQueue();
302 if (CA_STATUS_OK != result)
304 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
305 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
306 return CA_STATUS_FAILED;
309 g_dataReceiverHandlerState = true;
311 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
313 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
317 CAResult_t CAInitLEReceiverQueue()
319 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
320 // Check if the message queue is already initialized
321 if (g_bleReceiverQueue)
323 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
327 // Create recv message queue
328 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
329 if (!g_bleReceiverQueue)
331 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
332 return CA_MEMORY_ALLOC_FAILED;
335 g_senderInfo = u_arraylist_create();
338 OIC_LOG(ERROR, CALEADAPTER_TAG, "ClientInfo memory allcation failed!");
339 OICFree(g_bleReceiverQueue);
340 g_bleReceiverQueue = NULL;
341 return CA_MEMORY_ALLOC_FAILED;
344 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue, g_bleAdapterThreadPool,
345 CALEDataReceiverHandler, CALEDataDestroyer))
347 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
348 OICFree(g_bleReceiverQueue);
349 g_bleReceiverQueue = NULL;
350 u_arraylist_free(&g_senderInfo);
351 return CA_STATUS_FAILED;
354 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
356 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
357 OICFree(g_bleReceiverQueue);
358 g_bleReceiverQueue = NULL;
359 u_arraylist_free(&g_senderInfo);
360 return CA_STATUS_FAILED;
363 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
367 CAResult_t CAInitLEServerSenderQueue()
369 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
370 // Check if the message queue is already initialized
371 if (g_bleServerSendQueueHandle)
373 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
377 // Create send message queue
378 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
379 if (!g_bleServerSendQueueHandle)
381 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
382 return CA_MEMORY_ALLOC_FAILED;
385 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
386 g_bleAdapterThreadPool,
387 CALEServerSendDataThread, CALEDataDestroyer))
389 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
390 OICFree(g_bleServerSendQueueHandle);
391 g_bleServerSendQueueHandle = NULL;
392 return CA_STATUS_FAILED;
395 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerSendQueueHandle))
397 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
398 OICFree(g_bleServerSendQueueHandle);
399 g_bleServerSendQueueHandle = NULL;
400 return CA_STATUS_FAILED;
403 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
407 void CALEClearSenderInfo()
409 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
411 uint32_t listIndex = 0;
412 uint32_t listLength = u_arraylist_length(g_senderInfo);
413 for (listIndex = 0; listIndex < listLength; listIndex++)
415 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, listIndex);
421 OICFree(info->defragData);
422 CAFreeEndpoint(info->remoteEndpoint);
425 u_arraylist_free(&g_senderInfo);
426 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
429 CAResult_t CAInitLEClientSenderQueue()
431 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
433 if (g_bleClientSendQueueHandle)
435 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
439 // Create send message queue
440 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
441 if (!g_bleClientSendQueueHandle)
443 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
444 return CA_MEMORY_ALLOC_FAILED;
447 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
448 g_bleAdapterThreadPool,
449 CALEClientSendDataThread, CALEDataDestroyer))
451 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
452 OICFree(g_bleClientSendQueueHandle);
453 g_bleClientSendQueueHandle = NULL;
454 return CA_STATUS_FAILED;
457 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
459 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
460 OICFree(g_bleClientSendQueueHandle);
461 g_bleClientSendQueueHandle = NULL;
462 return CA_STATUS_FAILED;
465 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
469 void CAStopLEQueues()
471 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
473 ca_mutex_lock(g_bleClientSendDataMutex);
474 if (NULL != g_bleClientSendQueueHandle)
476 CAQueueingThreadStop(g_bleClientSendQueueHandle);
478 ca_mutex_unlock(g_bleClientSendDataMutex);
480 ca_mutex_lock(g_bleServerSendDataMutex);
481 if (NULL != g_bleServerSendQueueHandle)
483 CAQueueingThreadStop(g_bleServerSendQueueHandle);
485 ca_mutex_unlock(g_bleServerSendDataMutex);
487 ca_mutex_lock(g_bleReceiveDataMutex);
488 if (NULL != g_bleReceiverQueue)
490 CAQueueingThreadStop(g_bleReceiverQueue);
492 ca_mutex_unlock(g_bleReceiveDataMutex);
494 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
497 void CATerminateLEQueues()
499 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
501 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
502 OICFree(g_bleClientSendQueueHandle);
503 g_bleClientSendQueueHandle = NULL;
505 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
506 OICFree(g_bleServerSendQueueHandle);
507 g_bleServerSendQueueHandle = NULL;
509 CAQueueingThreadDestroy(g_bleReceiverQueue);
510 OICFree(g_bleReceiverQueue);
511 g_bleReceiverQueue = NULL;
513 CALEClearSenderInfo();
515 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
518 CAResult_t CALEGetSenderInfo(char *leAddress, CABLESenderInfo_t **senderInfo,
519 uint32_t *senderIndex)
521 VERIFY_NON_NULL_RET(leAddress, CALEADAPTER_TAG, "Ble-Address in-param NULL", CA_STATUS_FAILED);
522 VERIFY_NON_NULL_RET(senderIndex, CALEADAPTER_TAG, "Index in-param NULL", CA_STATUS_FAILED);
524 uint32_t listLength = u_arraylist_length(g_senderInfo);
525 uint32_t addrLength = strlen(leAddress);
526 for (uint32_t index = 0; index < listLength; index++)
528 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, index);
529 if(!info || !(info->remoteEndpoint))
534 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
536 *senderIndex = index;
545 return CA_STATUS_FAILED;
548 void CALEDataReceiverHandler(void *threadData)
550 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
552 ca_mutex_lock(g_bleReceiveDataMutex);
554 if (g_dataReceiverHandlerState)
556 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
558 CALEData_t *bleData = (CALEData_t *) threadData;
561 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
562 ca_mutex_unlock(g_bleReceiveDataMutex);
566 if(!(bleData->remoteEndpoint))
568 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
569 ca_mutex_unlock(g_bleReceiveDataMutex);
573 CABLESenderInfo_t *senderInfo = NULL;
574 uint32_t senderIndex = 0;
576 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
577 &senderInfo, &senderIndex))
579 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
580 bleData->remoteEndpoint->addr);
585 CABLESenderInfo_t *newSender = (CABLESenderInfo_t*)OICMalloc(sizeof(CABLESenderInfo_t));
588 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
589 ca_mutex_unlock(g_bleReceiveDataMutex);
592 newSender->recvDataLen = 0;
593 newSender->totalDataLen = 0;
594 newSender->defragData = NULL;
595 newSender->remoteEndpoint = NULL;
597 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
598 newSender->totalDataLen = CAParseHeader((char*)bleData->data);
599 if(!(newSender->totalDataLen))
601 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
603 ca_mutex_unlock(g_bleReceiveDataMutex);
607 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
608 newSender->totalDataLen);
609 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
612 newSender->defragData = (char *) OICCalloc(newSender->totalDataLen + 1, sizeof(char));
614 if (NULL == newSender->defragData)
616 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
618 ca_mutex_unlock(g_bleReceiveDataMutex);
622 const char *remoteAddress = bleData->remoteEndpoint->addr;
623 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
624 CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
625 if (NULL == newSender->remoteEndpoint)
627 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
628 OICFree(newSender->defragData);
630 ca_mutex_unlock(g_bleReceiveDataMutex);
633 memcpy(newSender->defragData, bleData->data + CA_HEADER_LENGTH,
634 bleData->dataLen - CA_HEADER_LENGTH);
635 newSender->recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
636 u_arraylist_add(g_senderInfo,(void *)newSender);
638 //Getting newSender index position in g_senderInfo array list
640 CALEGetSenderInfo(newSender->remoteEndpoint->addr, NULL, &senderIndex))
642 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
643 OICFree(senderInfo->defragData);
645 ca_mutex_unlock(g_bleReceiveDataMutex);
648 senderInfo = newSender;
652 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
654 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
655 "Data Length exceeding error!! Receiving [%d] total length [%d]",
656 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
657 u_arraylist_remove(g_senderInfo, senderIndex);
658 OICFree(senderInfo->defragData);
660 ca_mutex_unlock(g_bleReceiveDataMutex);
663 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
664 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
666 senderInfo->recvDataLen += bleData->dataLen ;
667 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
668 senderInfo->totalDataLen, senderInfo->recvDataLen);
671 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
673 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
674 if (NULL == g_networkPacketReceivedCallback)
676 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
678 u_arraylist_remove(g_senderInfo, senderIndex);
679 OICFree(senderInfo->defragData);
681 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
682 ca_mutex_unlock(g_bleReceiveDataMutex);
685 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
686 g_networkPacketReceivedCallback(senderInfo->remoteEndpoint,
687 senderInfo->defragData, senderInfo->recvDataLen);
688 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
689 u_arraylist_remove(g_senderInfo, senderIndex);
690 senderInfo->remoteEndpoint = NULL;
691 senderInfo->defragData = NULL;
695 ca_mutex_unlock(g_bleReceiveDataMutex);
696 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
699 void CALEServerSendDataThread(void *threadData)
701 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
703 CALEData_t *bleData = (CALEData_t *) threadData;
706 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
710 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
711 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
713 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
715 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
716 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
717 if (NULL == dataSegment)
719 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
724 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
725 if (CA_STATUS_OK != result )
727 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
729 OICFree(dataSegment);
733 memcpy(dataSegment, header, CA_HEADER_LENGTH);
737 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
739 length = totalLength;
740 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
744 length = CA_SUPPORTED_BLE_MTU_SIZE;
745 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
746 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
749 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
751 // Send the first segment with the header.
752 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
754 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
755 result = CAUpdateCharacteristicsToGattClient(
756 bleData->remoteEndpoint->addr, dataSegment, length);
757 if (CA_STATUS_OK != result)
759 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
760 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
761 OICFree(dataSegment);
765 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
766 for (index = 1; index < iter; index++)
768 // Send the remaining header.
769 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
770 result = CAUpdateCharacteristicsToGattClient(
771 bleData->remoteEndpoint->addr,
772 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
773 CA_SUPPORTED_BLE_MTU_SIZE);
774 if (CA_STATUS_OK != result)
776 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
777 "Update characteristics failed, result [%d]", result);
778 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
779 OICFree(dataSegment);
782 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
783 CA_SUPPORTED_BLE_MTU_SIZE);
786 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
787 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
789 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
790 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
791 result = CAUpdateCharacteristicsToGattClient(
792 bleData->remoteEndpoint->addr,
793 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
795 if (CA_STATUS_OK != result)
797 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
799 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
800 OICFree(dataSegment);
803 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
808 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
809 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
810 if (CA_STATUS_OK != result)
812 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
814 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
815 OICFree(dataSegment);
818 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
819 for (index = 1; index < iter; index++)
821 // Send the remaining header.
822 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
823 result = CAUpdateCharacteristicsToAllGattClients(
824 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
825 CA_SUPPORTED_BLE_MTU_SIZE);
826 if (CA_STATUS_OK != result)
828 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
830 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
831 OICFree(dataSegment);
834 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
837 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
838 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
840 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
841 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
842 result = CAUpdateCharacteristicsToAllGattClients(
843 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
845 if (CA_STATUS_OK != result)
847 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
849 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
850 OICFree(dataSegment);
853 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
856 OICFree(dataSegment);
858 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
861 void CALEClientSendDataThread(void *threadData)
863 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
865 CALEData_t *bleData = (CALEData_t *) threadData;
868 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
872 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
873 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
875 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
876 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
877 if (NULL == dataSegment)
879 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
884 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
885 if (CA_STATUS_OK != result )
887 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
889 OICFree(dataSegment);
892 memcpy(dataSegment, header, CA_HEADER_LENGTH);
896 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
898 length = totalLength;
899 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
900 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
904 length = CA_SUPPORTED_BLE_MTU_SIZE;
905 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
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;
912 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
914 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
915 // Send the first segment with the header.
916 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
921 if (CA_STATUS_OK != result)
923 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
924 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
925 OICFree(dataSegment);
929 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
930 for (index = 1; index < iter; index++)
932 // Send the remaining header.
933 result = CAUpdateCharacteristicsToGattServer(
934 bleData->remoteEndpoint->addr,
935 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
936 CA_SUPPORTED_BLE_MTU_SIZE,
938 if (CA_STATUS_OK != result)
940 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
942 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
943 OICFree(dataSegment);
946 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
947 CA_SUPPORTED_BLE_MTU_SIZE);
950 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
951 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
953 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
954 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
955 result = CAUpdateCharacteristicsToGattServer(
956 bleData->remoteEndpoint->addr,
957 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
961 if (CA_STATUS_OK != result)
963 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
965 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
966 OICFree(dataSegment);
969 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
974 //Sending Mulitcast Data
975 // Send the first segment with the header.
976 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
977 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
978 if (CA_STATUS_OK != result)
980 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
981 "Update characteristics (all) failed, result [%d]", result);
982 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
983 OICFree(dataSegment);
986 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
987 // Send the remaining header.
988 for (index = 1; index < iter; index++)
990 result = CAUpdateCharacteristicsToAllGattServers(
991 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
992 CA_SUPPORTED_BLE_MTU_SIZE);
993 if (CA_STATUS_OK != result)
995 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
997 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
998 OICFree(dataSegment);
1001 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1002 CA_SUPPORTED_BLE_MTU_SIZE);
1005 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1006 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1008 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1009 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1010 result = CAUpdateCharacteristicsToAllGattServers(
1011 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1013 if (CA_STATUS_OK != result)
1015 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1016 "Update characteristics (all) failed, result [%d]", result);
1017 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1018 OICFree(dataSegment);
1021 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1026 OICFree(dataSegment);
1028 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1031 CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint, const void *data,
1032 uint32_t dataLength)
1034 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1037 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1041 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1042 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1043 if (NULL == bleData->data)
1045 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1046 CAFreeLEData(bleData);
1049 memcpy(bleData->data, data, dataLength);
1050 bleData->dataLen = dataLength;
1055 void CAFreeLEData(CALEData_t *bleData)
1057 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1059 CAFreeEndpoint(bleData->remoteEndpoint);
1060 OICFree(bleData->data);
1064 void CALEDataDestroyer(void *data, uint32_t size)
1066 CALEData_t *ledata = (CALEData_t *) data;
1068 CAFreeLEData(ledata);
1072 CAResult_t CAInitLEAdapterMutex()
1074 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1076 if (NULL == g_bleIsServerMutex)
1078 g_bleIsServerMutex = ca_mutex_new();
1079 if (NULL == g_bleIsServerMutex)
1081 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1082 return CA_STATUS_FAILED;
1086 if (NULL == g_bleNetworkCbMutex)
1088 g_bleNetworkCbMutex = ca_mutex_new();
1089 if (NULL == g_bleNetworkCbMutex)
1091 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1092 CATerminateLEAdapterMutex();
1093 return CA_STATUS_FAILED;
1097 if (NULL == g_bleLocalAddressMutex)
1099 g_bleLocalAddressMutex = ca_mutex_new();
1100 if (NULL == g_bleLocalAddressMutex)
1102 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1103 CATerminateLEAdapterMutex();
1104 return CA_STATUS_FAILED;
1108 if (NULL == g_bleAdapterThreadPoolMutex)
1110 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1111 if (NULL == g_bleAdapterThreadPoolMutex)
1113 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1114 CATerminateLEAdapterMutex();
1115 return CA_STATUS_FAILED;
1119 if (NULL == g_bleClientSendDataMutex)
1121 g_bleClientSendDataMutex = ca_mutex_new();
1122 if (NULL == g_bleClientSendDataMutex)
1124 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1125 CATerminateLEAdapterMutex();
1126 return CA_STATUS_FAILED;
1130 if (NULL == g_bleClientReceiveDataMutex)
1132 g_bleClientReceiveDataMutex = ca_mutex_new();
1133 if (NULL == g_bleClientReceiveDataMutex)
1135 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1136 CATerminateLEAdapterMutex();
1137 return CA_STATUS_FAILED;
1141 if (NULL == g_bleServerSendDataMutex)
1143 g_bleServerSendDataMutex = ca_mutex_new();
1144 if (NULL == g_bleServerSendDataMutex)
1146 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1147 CATerminateLEAdapterMutex();
1148 return CA_STATUS_FAILED;
1152 if (NULL == g_bleServerReceiveDataMutex)
1154 g_bleServerReceiveDataMutex = ca_mutex_new();
1155 if (NULL == g_bleServerReceiveDataMutex)
1157 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1158 CATerminateLEAdapterMutex();
1159 return CA_STATUS_FAILED;
1163 if (NULL == g_bleAdapterReqRespCbMutex)
1165 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1166 if (NULL == g_bleAdapterReqRespCbMutex)
1168 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1169 CATerminateLEAdapterMutex();
1170 return CA_STATUS_FAILED;
1174 if (NULL == g_bleReceiveDataMutex)
1176 g_bleReceiveDataMutex = ca_mutex_new();
1177 if (NULL == g_bleReceiveDataMutex)
1179 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1180 return CA_STATUS_FAILED;
1184 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1185 return CA_STATUS_OK;
1188 void CATerminateLEAdapterMutex()
1190 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1192 ca_mutex_free(g_bleIsServerMutex);
1193 g_bleIsServerMutex = NULL;
1195 ca_mutex_free(g_bleNetworkCbMutex);
1196 g_bleNetworkCbMutex = NULL;
1198 ca_mutex_free(g_bleLocalAddressMutex);
1199 g_bleLocalAddressMutex = NULL;
1201 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1202 g_bleAdapterThreadPoolMutex = NULL;
1204 ca_mutex_free(g_bleClientSendDataMutex);
1205 g_bleClientSendDataMutex = NULL;
1207 ca_mutex_free(g_bleClientReceiveDataMutex);
1208 g_bleClientReceiveDataMutex = NULL;
1210 ca_mutex_free(g_bleServerSendDataMutex);
1211 g_bleServerSendDataMutex = NULL;
1213 ca_mutex_free(g_bleServerReceiveDataMutex);
1214 g_bleServerReceiveDataMutex = NULL;
1216 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1217 g_bleAdapterReqRespCbMutex = NULL;
1219 ca_mutex_free(g_bleReceiveDataMutex);
1220 g_bleReceiveDataMutex = NULL;
1222 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1225 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1226 CANetworkPacketReceivedCallback reqRespCallback,
1227 CANetworkChangeCallback netCallback,
1228 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
1230 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1233 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1234 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1235 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1237 CAResult_t result = CA_STATUS_OK;
1238 result = CAInitLEAdapterMutex();
1239 if (CA_STATUS_OK != result)
1241 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1242 return CA_STATUS_FAILED;
1244 result = CAInitializeLENetworkMonitor();
1245 if (CA_STATUS_OK != result)
1247 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1248 return CA_STATUS_FAILED;
1251 CAInitializeLEAdapter();
1253 CASetLEClientThreadPoolHandle(handle);
1254 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1255 CASetLEServerThreadPoolHandle(handle);
1256 CASetLEAdapterThreadPoolHandle(handle);
1257 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1258 CASetLEReqRespAdapterCallback(reqRespCallback);
1260 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1261 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1262 CALERegisterNetworkNotifications(netCallback);
1264 g_errorHandler = errorCallback;
1266 CAConnectivityHandler_t connHandler;
1267 connHandler.startAdapter = CAStartLE;
1268 connHandler.stopAdapter = CAStopLE;
1269 connHandler.startListenServer = CAStartLEListeningServer;
1270 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
1271 connHandler.sendData = CASendLEUnicastData;
1272 connHandler.sendDataToAll = CASendLEMulticastData;
1273 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
1274 connHandler.readData = CAReadLEData;
1275 connHandler.terminate = CATerminateLE;
1276 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1278 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1280 return CA_STATUS_OK;
1283 CAResult_t CAStartLE()
1285 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1286 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE, not implemented");
1287 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1288 return CA_STATUS_OK;
1291 CAResult_t CAStopLE()
1293 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1294 #ifndef SINGLE_THREAD
1298 ca_mutex_lock(g_bleIsServerMutex);
1299 if (true == g_isServer)
1301 CAStopLEGattServer();
1305 CAStopLEGattClient();
1307 ca_mutex_unlock(g_bleIsServerMutex);
1309 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1311 return CA_STATUS_OK;
1314 void CATerminateLE()
1316 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1318 CASetLEReqRespServerCallback(NULL);
1319 CASetLEReqRespClientCallback(NULL);
1320 CALERegisterNetworkNotifications(NULL);
1321 CASetLEReqRespAdapterCallback(NULL);
1322 CATerminateLENetworkMonitor();
1324 ca_mutex_lock(g_bleIsServerMutex);
1325 if (true == g_isServer)
1327 CATerminateLEGattServer();
1331 CATerminateLEGattClient();
1333 ca_mutex_unlock(g_bleIsServerMutex);
1335 #ifndef SINGLE_THREAD
1336 CATerminateLEQueues();
1338 CATerminateLEAdapterMutex();
1340 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1343 CAResult_t CAStartLEListeningServer()
1345 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1346 CAResult_t result = CA_STATUS_OK;
1347 #ifndef SINGLE_THREAD
1348 result = CAInitLEServerQueues();
1349 if (CA_STATUS_OK != result)
1351 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1352 return CA_STATUS_FAILED;
1356 result = CAGetLEAdapterState();
1357 if (CA_ADAPTER_NOT_ENABLED == result)
1359 gLeServerStatus = CA_LISTENING_SERVER;
1360 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1361 return CA_STATUS_OK;
1364 if (CA_STATUS_FAILED == result)
1366 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1367 return CA_STATUS_FAILED;
1370 CAStartLEGattServer();
1372 ca_mutex_lock(g_bleIsServerMutex);
1374 ca_mutex_unlock(g_bleIsServerMutex);
1376 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1377 return CA_STATUS_OK;
1380 CAResult_t CAStartLEDiscoveryServer()
1382 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1383 CAResult_t result = CA_STATUS_OK;
1384 #ifndef SINGLE_THREAD
1385 result = CAInitLEClientQueues();
1386 if (CA_STATUS_OK != result)
1388 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1389 return CA_STATUS_FAILED;
1392 result = CAGetLEAdapterState();
1393 if (CA_ADAPTER_NOT_ENABLED == result)
1395 gLeServerStatus = CA_DISCOVERY_SERVER;
1396 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1397 return CA_STATUS_OK;
1400 if (CA_STATUS_FAILED == result)
1402 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1403 return CA_STATUS_FAILED;
1406 CAStartLEGattClient();
1408 ca_mutex_lock(g_bleIsServerMutex);
1410 ca_mutex_unlock(g_bleIsServerMutex);
1412 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1413 return CA_STATUS_OK;
1416 CAResult_t CAStartLENotifyServer()
1418 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1420 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1421 return CA_STATUS_OK;
1424 uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data,
1427 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1429 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1433 CAResult_t CAReadLEData()
1435 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1436 #ifdef SINGLE_THREAD
1439 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1440 return CA_STATUS_OK;
1443 int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1445 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1448 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1449 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1451 CAResult_t result = CA_STATUS_FAILED;
1453 ca_mutex_lock(g_bleIsServerMutex);
1454 if (true == g_isServer)
1456 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1457 if (CA_STATUS_OK != result)
1459 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1462 g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
1464 ca_mutex_unlock(g_bleIsServerMutex);
1470 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1471 if (CA_STATUS_OK != result)
1473 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1476 g_errorHandler(endpoint, data, dataLen, result);
1478 ca_mutex_unlock(g_bleIsServerMutex);
1482 ca_mutex_unlock(g_bleIsServerMutex);
1484 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1488 int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1490 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1493 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1497 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1501 CAResult_t result = CA_STATUS_FAILED;
1503 ca_mutex_lock(g_bleIsServerMutex);
1504 if (true == g_isServer)
1506 result = CALEAdapterServerSendData(NULL, data, dataLen);
1507 if (CA_STATUS_OK != result)
1509 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
1511 ca_mutex_unlock(g_bleIsServerMutex);
1514 g_errorHandler(endpoint, data, dataLen, result);
1521 result = CALEAdapterClientSendData(NULL, data, dataLen);
1522 if (CA_STATUS_OK != result)
1524 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
1527 g_errorHandler(endpoint, data, dataLen, result);
1529 ca_mutex_unlock(g_bleIsServerMutex);
1533 ca_mutex_unlock(g_bleIsServerMutex);
1535 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1539 CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1541 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1543 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
1545 char *local_address = NULL;
1547 CAResult_t res = CAGetLEAddress(&local_address);
1548 if (CA_STATUS_OK != res)
1550 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
1554 if (NULL == local_address)
1556 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
1557 return CA_STATUS_FAILED;
1561 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
1562 if (NULL == (*info))
1564 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
1565 OICFree(local_address);
1566 return CA_STATUS_FAILED;
1569 size_t local_address_len = strlen(local_address);
1571 if(local_address_len >= sizeof(g_localBLEAddress) ||
1572 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
1574 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
1576 OICFree(local_address);
1577 return CA_STATUS_FAILED;
1580 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
1581 ca_mutex_lock(g_bleLocalAddressMutex);
1582 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
1583 ca_mutex_unlock(g_bleLocalAddressMutex);
1585 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
1587 OICFree(local_address);
1589 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1590 return CA_STATUS_OK;
1593 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
1595 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1597 ca_mutex_lock(g_bleNetworkCbMutex);
1598 g_networkCallback = netCallback;
1599 ca_mutex_unlock(g_bleNetworkCbMutex);
1600 CAResult_t res = CA_STATUS_OK;
1603 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
1604 if (CA_STATUS_OK != res)
1606 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1611 res = CAUnSetLEAdapterStateChangedCb();
1612 if (CA_STATUS_OK != res)
1614 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1618 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1622 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
1624 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1626 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
1627 CAEndpoint_t localEndpoint = {};
1629 ca_mutex_lock(g_bleLocalAddressMutex);
1630 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
1631 ca_mutex_unlock(g_bleLocalAddressMutex);
1633 g_bleAdapterState = adapter_state;
1634 // Start a GattServer/Client if gLeServerStatus is SET
1635 if (CA_LISTENING_SERVER == gLeServerStatus)
1637 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1638 CAStartLEGattServer();
1640 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
1642 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
1643 CAStartLEGattClient();
1645 gLeServerStatus = CA_SERVER_NOTSTARTED;
1647 ca_mutex_lock(g_bleNetworkCbMutex);
1648 if (NULL != g_networkCallback)
1650 g_networkCallback(&localEndpoint, adapter_state);
1654 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
1656 ca_mutex_unlock(g_bleNetworkCbMutex);
1658 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1661 CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1665 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1667 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1668 #ifndef SINGLE_THREAD
1669 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1670 "g_bleClientSendQueueHandle is NULL",
1672 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1673 "g_bleClientSendDataMutex is NULL",
1676 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1679 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1681 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1684 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1685 return CA_MEMORY_ALLOC_FAILED;
1687 // Add message to send queue
1688 ca_mutex_lock(g_bleClientSendDataMutex);
1689 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1690 ca_mutex_unlock(g_bleClientSendDataMutex);
1692 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1693 return CA_STATUS_OK;
1697 CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1701 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1703 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1705 #ifdef SINGLE_THREAD
1706 char header[CA_HEADER_LENGTH] = {0};
1708 CAResult_t result = CAGenerateHeader(header, dataLen);
1710 if (CA_STATUS_OK != result)
1712 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1716 if (!CAIsLEConnected())
1718 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
1722 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
1723 if (CA_STATUS_OK != result)
1725 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1729 int32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1730 for (int32_t iter = 0; iter < dataLimit; iter++)
1732 result = CAUpdateCharacteristicsToAllGattClients((data +
1733 (iter * CA_SUPPORTED_BLE_MTU_SIZE)),
1734 CA_SUPPORTED_BLE_MTU_SIZE);
1735 if (CA_STATUS_OK != result)
1737 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1743 uint8_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
1746 result = CAUpdateCharacteristicsToAllGattClients((data +
1747 (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)),
1749 if (CA_STATUS_OK != result)
1751 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1757 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
1758 "BleClientReceiverQueue is NULL",
1760 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1761 "BleClientSendDataMutex is NULL",
1764 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1767 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1769 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1772 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1773 return CA_MEMORY_ALLOC_FAILED;
1775 // Add message to send queue
1776 ca_mutex_lock(g_bleServerSendDataMutex);
1777 CAQueueingThreadAddData(g_bleServerSendQueueHandle, bleData, sizeof(CALEData_t));
1778 ca_mutex_unlock(g_bleServerSendDataMutex);
1780 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1781 return CA_STATUS_OK;
1784 CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1785 const void *data, uint32_t dataLength,
1786 uint32_t *sentLength)
1788 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1791 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1792 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1793 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1795 #ifdef SINGLE_THREAD
1796 if(g_networkPacketReceivedCallback)
1798 CAEndpoint_t endPoint = { 0 }; // will be filled by upper layer
1799 endPoint.adapter = CA_ADAPTER_GATT_BTLE;
1800 g_networkPacketReceivedCallback(&endPoint, data, dataLength);
1803 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
1806 //Add message to data queue
1807 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1808 CA_ADAPTER_GATT_BTLE,
1810 if (NULL == remoteEndpoint)
1812 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1813 return CA_STATUS_FAILED;
1816 // Create bleData to add to queue
1817 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1819 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1822 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1823 CAFreeEndpoint(remoteEndpoint);
1824 return CA_MEMORY_ALLOC_FAILED;
1827 CAFreeEndpoint(remoteEndpoint);
1828 // Add message to send queue
1829 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
1831 *sentLength = dataLength;
1833 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1834 return CA_STATUS_OK;
1837 CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1838 const void *data, uint32_t dataLength,
1839 uint32_t *sentLength)
1841 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1844 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1845 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1846 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1847 #ifndef SINGLE_THREAD
1848 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
1851 //Add message to data queue
1852 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1853 CA_ADAPTER_GATT_BTLE,
1855 if (NULL == remoteEndpoint)
1857 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1858 return CA_STATUS_FAILED;
1861 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1863 // Create bleData to add to queue
1864 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1867 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1868 CAFreeEndpoint(remoteEndpoint);
1869 return CA_MEMORY_ALLOC_FAILED;
1872 CAFreeEndpoint(remoteEndpoint);
1873 // Add message to send queue
1874 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
1876 *sentLength = dataLength;
1878 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1879 return CA_STATUS_OK;
1882 void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
1884 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1886 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1887 g_bleAdapterThreadPool = handle;
1888 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1890 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1893 void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1895 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1897 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1899 g_networkPacketReceivedCallback = callback;
1901 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1903 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1906 void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
1909 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
1911 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
1912 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
1914 //if required, will be used to build remote end point
1915 g_errorHandler(rep, data, dataLen, result);
1917 CAFreeEndpoint(rep);
1918 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");