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_bleReceiveDataMutex = NULL;
96 * Mutex to synchronize the queing of the data from SenderQueue.
98 static ca_mutex g_bleServerSendDataMutex = NULL;
101 * Mutex to synchronize the callback to be called for the adapterReqResponse.
103 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
106 * Callback to be called when network packet recieved from either
107 * GattServer or GattClient.
109 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
112 * Callback to notify error from the BLE adapter.
114 static CAErrorHandleCallback g_errorHandler = NULL;
117 * Storing Adapter state information.
119 static CAAdapterState_t g_bleAdapterState = CA_ADAPTER_DISABLED;
122 * status of BLE Server Status.
123 * This ENUM provides information of LE Adapter Server status.
127 CA_SERVER_NOTSTARTED = 0,
133 * structure to maintain the status of the server.
135 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
138 * This function is used to register network change notification callback.
140 * @param[in] netCallback CANetworkChangeCallback callback which will be
141 * set for the change in nwk.
143 * @return 0 on success otherwise a positive error value.
144 * @retval CA_STATUS_OK Successful.
145 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets.
146 * @retval CA_STATUS_FAILED Operation failed.
149 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
152 * Used to Set the gThreadPool handle which is required for spawning new thread.
154 * @param[in] handle - Thread pool handle which is given by above layer for
155 * using thread creation task.
158 void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
161 * This function is used to call the callback to the upper layer when the
162 * device state gets changed.
164 * @param[in] adapter_state New state of the adapter to be notified to the
168 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
171 * Used to initialize all required mutex variable for LE Adapter implementation.
173 * @return 0 on success otherwise a positive error value.
174 * @retval CA_STATUS_OK Successful.
175 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets.
176 * @retval CA_STATUS_FAILED Operation failed.
179 CAResult_t CAInitLEAdapterMutex();
182 * Used to terminate all required mutex variable for LE adapter implementation.
185 void CATerminateLEAdapterMutex();
188 * prepares and notify error through error callback.
191 static void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
194 #ifndef SINGLE_THREAD
196 * Stop condition of recvhandler.
198 static bool g_dataReceiverHandlerState = false;
201 * Sender informations to be stored here
203 static u_arraylist_t *g_senderInfo = NULL;
206 * Queue to process the outgoing packets from GATTClient.
208 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
211 * Queue to process the incoming packets to GATT Client.
213 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
216 * Queue to process the outgoing packets from GATTServer.
218 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
224 static void CALEDataDestroyer(void *data, uint32_t size);
226 void CAInitLEQueues()
228 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
230 CAResult_t result = CAInitLEServerQueues();
231 if (CA_STATUS_OK != result)
233 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
237 result = CAInitLEClientQueues();
238 if (CA_STATUS_OK != result)
240 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
244 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
247 CAResult_t CAInitLEServerQueues()
249 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
251 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
253 CAResult_t result = CAInitLEServerSenderQueue();
254 if (CA_STATUS_OK != result)
256 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
257 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
258 return CA_STATUS_FAILED;
261 result = CAInitLEReceiverQueue();
262 if (CA_STATUS_OK != result)
264 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
265 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
266 return CA_STATUS_FAILED;
269 g_dataReceiverHandlerState = true;
271 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
273 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
277 CAResult_t CAInitLEClientQueues()
279 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
281 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
283 CAResult_t result = CAInitLEClientSenderQueue();
284 if (CA_STATUS_OK != result)
286 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
287 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
288 return CA_STATUS_FAILED;
291 result = CAInitLEReceiverQueue();
292 if (CA_STATUS_OK != result)
294 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
295 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
296 return CA_STATUS_FAILED;
299 g_dataReceiverHandlerState = true;
301 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
303 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
307 CAResult_t CAInitLEReceiverQueue()
309 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
310 // Check if the message queue is already initialized
311 if (g_bleReceiverQueue)
313 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
317 // Create recv message queue
318 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
319 if (!g_bleReceiverQueue)
321 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
322 return CA_MEMORY_ALLOC_FAILED;
325 g_senderInfo = u_arraylist_create();
328 OIC_LOG(ERROR, CALEADAPTER_TAG, "ClientInfo memory allcation failed!");
329 OICFree(g_bleReceiverQueue);
330 g_bleReceiverQueue = NULL;
331 return CA_MEMORY_ALLOC_FAILED;
334 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue, g_bleAdapterThreadPool,
335 CALEDataReceiverHandler, CALEDataDestroyer))
337 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
338 OICFree(g_bleReceiverQueue);
339 g_bleReceiverQueue = NULL;
340 u_arraylist_free(&g_senderInfo);
341 return CA_STATUS_FAILED;
344 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
346 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
347 OICFree(g_bleReceiverQueue);
348 g_bleReceiverQueue = NULL;
349 u_arraylist_free(&g_senderInfo);
350 return CA_STATUS_FAILED;
353 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
357 CAResult_t CAInitLEServerSenderQueue()
359 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
360 // Check if the message queue is already initialized
361 if (g_bleServerSendQueueHandle)
363 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
367 // Create send message queue
368 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
369 if (!g_bleServerSendQueueHandle)
371 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
372 return CA_MEMORY_ALLOC_FAILED;
375 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
376 g_bleAdapterThreadPool,
377 CALEServerSendDataThread, CALEDataDestroyer))
379 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
380 OICFree(g_bleServerSendQueueHandle);
381 g_bleServerSendQueueHandle = NULL;
382 return CA_STATUS_FAILED;
385 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerSendQueueHandle))
387 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
388 OICFree(g_bleServerSendQueueHandle);
389 g_bleServerSendQueueHandle = NULL;
390 return CA_STATUS_FAILED;
393 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
397 void CALEClearSenderInfo()
399 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
401 uint32_t listIndex = 0;
402 uint32_t listLength = u_arraylist_length(g_senderInfo);
403 for (listIndex = 0; listIndex < listLength; listIndex++)
405 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, listIndex);
411 OICFree(info->defragData);
412 CAFreeEndpoint(info->remoteEndpoint);
415 u_arraylist_free(&g_senderInfo);
416 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
419 CAResult_t CAInitLEClientSenderQueue()
421 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
423 if (g_bleClientSendQueueHandle)
425 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
429 // Create send message queue
430 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
431 if (!g_bleClientSendQueueHandle)
433 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
434 return CA_MEMORY_ALLOC_FAILED;
437 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
438 g_bleAdapterThreadPool,
439 CALEClientSendDataThread, CALEDataDestroyer))
441 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
442 OICFree(g_bleClientSendQueueHandle);
443 g_bleClientSendQueueHandle = NULL;
444 return CA_STATUS_FAILED;
447 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
449 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
450 OICFree(g_bleClientSendQueueHandle);
451 g_bleClientSendQueueHandle = NULL;
452 return CA_STATUS_FAILED;
455 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
459 void CAStopLEQueues()
461 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
463 ca_mutex_lock(g_bleClientSendDataMutex);
464 if (NULL != g_bleClientSendQueueHandle)
466 CAQueueingThreadStop(g_bleClientSendQueueHandle);
468 ca_mutex_unlock(g_bleClientSendDataMutex);
470 ca_mutex_lock(g_bleServerSendDataMutex);
471 if (NULL != g_bleServerSendQueueHandle)
473 CAQueueingThreadStop(g_bleServerSendQueueHandle);
475 ca_mutex_unlock(g_bleServerSendDataMutex);
477 ca_mutex_lock(g_bleReceiveDataMutex);
478 if (NULL != g_bleReceiverQueue)
480 CAQueueingThreadStop(g_bleReceiverQueue);
482 ca_mutex_unlock(g_bleReceiveDataMutex);
484 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
487 void CATerminateLEQueues()
489 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
491 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
492 OICFree(g_bleClientSendQueueHandle);
493 g_bleClientSendQueueHandle = NULL;
495 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
496 OICFree(g_bleServerSendQueueHandle);
497 g_bleServerSendQueueHandle = NULL;
499 CAQueueingThreadDestroy(g_bleReceiverQueue);
500 OICFree(g_bleReceiverQueue);
501 g_bleReceiverQueue = NULL;
503 CALEClearSenderInfo();
505 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
508 CAResult_t CALEGetSenderInfo(char *leAddress, CABLESenderInfo_t **senderInfo,
509 uint32_t *senderIndex)
511 VERIFY_NON_NULL_RET(leAddress, CALEADAPTER_TAG, "Ble-Address in-param NULL", CA_STATUS_FAILED);
512 VERIFY_NON_NULL_RET(senderIndex, CALEADAPTER_TAG, "Index in-param NULL", CA_STATUS_FAILED);
514 uint32_t listLength = u_arraylist_length(g_senderInfo);
515 uint32_t addrLength = strlen(leAddress);
516 for (uint32_t index = 0; index < listLength; index++)
518 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, index);
519 if(!info || !(info->remoteEndpoint))
524 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
526 *senderIndex = index;
535 return CA_STATUS_FAILED;
538 void CALEDataReceiverHandler(void *threadData)
540 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
542 ca_mutex_lock(g_bleReceiveDataMutex);
544 if (g_dataReceiverHandlerState)
546 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
548 CALEData_t *bleData = (CALEData_t *) threadData;
551 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
552 ca_mutex_unlock(g_bleReceiveDataMutex);
556 if(!(bleData->remoteEndpoint))
558 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
559 ca_mutex_unlock(g_bleReceiveDataMutex);
563 CABLESenderInfo_t *senderInfo = NULL;
564 uint32_t senderIndex = 0;
566 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
567 &senderInfo, &senderIndex))
569 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
570 bleData->remoteEndpoint->addr);
575 CABLESenderInfo_t *newSender = (CABLESenderInfo_t*)OICMalloc(sizeof(CABLESenderInfo_t));
578 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
579 ca_mutex_unlock(g_bleReceiveDataMutex);
582 newSender->recvDataLen = 0;
583 newSender->totalDataLen = 0;
584 newSender->defragData = NULL;
585 newSender->remoteEndpoint = NULL;
587 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
588 newSender->totalDataLen = CAParseHeader((char*)bleData->data);
589 if(!(newSender->totalDataLen))
591 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
593 ca_mutex_unlock(g_bleReceiveDataMutex);
597 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
598 newSender->totalDataLen);
599 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
602 newSender->defragData = (char *) OICCalloc(newSender->totalDataLen + 1, sizeof(char));
604 if (NULL == newSender->defragData)
606 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
608 ca_mutex_unlock(g_bleReceiveDataMutex);
612 const char *remoteAddress = bleData->remoteEndpoint->addr;
613 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
614 CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
615 if (NULL == newSender->remoteEndpoint)
617 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
618 OICFree(newSender->defragData);
620 ca_mutex_unlock(g_bleReceiveDataMutex);
623 memcpy(newSender->defragData, bleData->data + CA_HEADER_LENGTH,
624 bleData->dataLen - CA_HEADER_LENGTH);
625 newSender->recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
626 u_arraylist_add(g_senderInfo,(void *)newSender);
628 //Getting newSender index position in g_senderInfo array list
630 CALEGetSenderInfo(newSender->remoteEndpoint->addr, NULL, &senderIndex))
632 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
633 OICFree(senderInfo->defragData);
635 ca_mutex_unlock(g_bleReceiveDataMutex);
638 senderInfo = newSender;
642 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
644 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
645 "Data Length exceeding error!! Receiving [%d] total length [%d]",
646 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
647 u_arraylist_remove(g_senderInfo, senderIndex);
648 OICFree(senderInfo->defragData);
650 ca_mutex_unlock(g_bleReceiveDataMutex);
653 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
654 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
656 senderInfo->recvDataLen += bleData->dataLen ;
657 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
658 senderInfo->totalDataLen, senderInfo->recvDataLen);
661 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
663 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
664 if (NULL == g_networkPacketReceivedCallback)
666 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
668 u_arraylist_remove(g_senderInfo, senderIndex);
669 OICFree(senderInfo->defragData);
671 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
672 ca_mutex_unlock(g_bleReceiveDataMutex);
675 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
676 g_networkPacketReceivedCallback(senderInfo->remoteEndpoint,
677 senderInfo->defragData, senderInfo->recvDataLen);
678 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
679 u_arraylist_remove(g_senderInfo, senderIndex);
680 senderInfo->remoteEndpoint = NULL;
681 senderInfo->defragData = NULL;
685 ca_mutex_unlock(g_bleReceiveDataMutex);
686 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
689 void CALEServerSendDataThread(void *threadData)
691 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
693 CALEData_t *bleData = (CALEData_t *) threadData;
696 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
700 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
701 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
703 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
705 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
706 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
707 if (NULL == dataSegment)
709 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
714 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
715 if (CA_STATUS_OK != result )
717 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
719 OICFree(dataSegment);
723 memcpy(dataSegment, header, CA_HEADER_LENGTH);
727 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
729 length = totalLength;
730 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
734 length = CA_SUPPORTED_BLE_MTU_SIZE;
735 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
736 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
739 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
741 // Send the first segment with the header.
742 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
744 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
745 result = CAUpdateCharacteristicsToGattClient(
746 bleData->remoteEndpoint->addr, dataSegment, length);
747 if (CA_STATUS_OK != result)
749 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
750 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
751 OICFree(dataSegment);
755 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
756 for (index = 1; index < iter; index++)
758 // Send the remaining header.
759 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
760 result = CAUpdateCharacteristicsToGattClient(
761 bleData->remoteEndpoint->addr,
762 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
763 CA_SUPPORTED_BLE_MTU_SIZE);
764 if (CA_STATUS_OK != result)
766 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
767 "Update characteristics failed, result [%d]", result);
768 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
769 OICFree(dataSegment);
772 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
773 CA_SUPPORTED_BLE_MTU_SIZE);
776 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
777 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
779 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
780 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
781 result = CAUpdateCharacteristicsToGattClient(
782 bleData->remoteEndpoint->addr,
783 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
785 if (CA_STATUS_OK != result)
787 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
789 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
790 OICFree(dataSegment);
793 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
798 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
799 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
800 if (CA_STATUS_OK != result)
802 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
804 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
805 OICFree(dataSegment);
808 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
809 for (index = 1; index < iter; index++)
811 // Send the remaining header.
812 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
813 result = CAUpdateCharacteristicsToAllGattClients(
814 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
815 CA_SUPPORTED_BLE_MTU_SIZE);
816 if (CA_STATUS_OK != result)
818 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
820 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
821 OICFree(dataSegment);
824 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
827 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
828 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
830 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
831 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
832 result = CAUpdateCharacteristicsToAllGattClients(
833 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
835 if (CA_STATUS_OK != result)
837 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
839 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
840 OICFree(dataSegment);
843 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
846 OICFree(dataSegment);
848 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
851 void CALEClientSendDataThread(void *threadData)
853 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
855 CALEData_t *bleData = (CALEData_t *) threadData;
858 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
862 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
863 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
865 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
866 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
867 if (NULL == dataSegment)
869 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
874 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
875 if (CA_STATUS_OK != result )
877 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
879 OICFree(dataSegment);
882 memcpy(dataSegment, header, CA_HEADER_LENGTH);
886 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
888 length = totalLength;
889 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
890 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
894 length = CA_SUPPORTED_BLE_MTU_SIZE;
895 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
896 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
897 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
900 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
902 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
904 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
905 // Send the first segment with the header.
906 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
911 if (CA_STATUS_OK != result)
913 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
914 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
915 OICFree(dataSegment);
919 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
920 for (index = 1; index < iter; index++)
922 // Send the remaining header.
923 result = CAUpdateCharacteristicsToGattServer(
924 bleData->remoteEndpoint->addr,
925 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
926 CA_SUPPORTED_BLE_MTU_SIZE,
928 if (CA_STATUS_OK != result)
930 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
932 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
933 OICFree(dataSegment);
936 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
937 CA_SUPPORTED_BLE_MTU_SIZE);
940 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
941 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
943 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
944 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
945 result = CAUpdateCharacteristicsToGattServer(
946 bleData->remoteEndpoint->addr,
947 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
951 if (CA_STATUS_OK != result)
953 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
955 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
956 OICFree(dataSegment);
959 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
964 //Sending Mulitcast Data
965 // Send the first segment with the header.
966 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
967 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
968 if (CA_STATUS_OK != result)
970 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
971 "Update characteristics (all) failed, result [%d]", result);
972 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
973 OICFree(dataSegment);
976 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
977 // Send the remaining header.
978 for (index = 1; index < iter; index++)
980 result = CAUpdateCharacteristicsToAllGattServers(
981 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
982 CA_SUPPORTED_BLE_MTU_SIZE);
983 if (CA_STATUS_OK != result)
985 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
987 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
988 OICFree(dataSegment);
991 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
992 CA_SUPPORTED_BLE_MTU_SIZE);
995 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
996 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
998 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
999 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1000 result = CAUpdateCharacteristicsToAllGattServers(
1001 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1003 if (CA_STATUS_OK != result)
1005 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1006 "Update characteristics (all) failed, result [%d]", result);
1007 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1008 OICFree(dataSegment);
1011 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1016 OICFree(dataSegment);
1018 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1021 CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint, const void *data,
1022 uint32_t dataLength)
1024 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1027 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1031 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1032 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1033 if (NULL == bleData->data)
1035 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1036 CAFreeLEData(bleData);
1039 memcpy(bleData->data, data, dataLength);
1040 bleData->dataLen = dataLength;
1045 void CAFreeLEData(CALEData_t *bleData)
1047 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1049 CAFreeEndpoint(bleData->remoteEndpoint);
1050 OICFree(bleData->data);
1054 void CALEDataDestroyer(void *data, uint32_t size)
1056 CALEData_t *ledata = (CALEData_t *) data;
1058 CAFreeLEData(ledata);
1062 CAResult_t CAInitLEAdapterMutex()
1064 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1066 if (NULL == g_bleIsServerMutex)
1068 g_bleIsServerMutex = ca_mutex_new();
1069 if (NULL == g_bleIsServerMutex)
1071 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1072 return CA_STATUS_FAILED;
1076 if (NULL == g_bleNetworkCbMutex)
1078 g_bleNetworkCbMutex = ca_mutex_new();
1079 if (NULL == g_bleNetworkCbMutex)
1081 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1082 CATerminateLEAdapterMutex();
1083 return CA_STATUS_FAILED;
1087 if (NULL == g_bleLocalAddressMutex)
1089 g_bleLocalAddressMutex = ca_mutex_new();
1090 if (NULL == g_bleLocalAddressMutex)
1092 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1093 CATerminateLEAdapterMutex();
1094 return CA_STATUS_FAILED;
1098 if (NULL == g_bleAdapterThreadPoolMutex)
1100 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1101 if (NULL == g_bleAdapterThreadPoolMutex)
1103 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1104 CATerminateLEAdapterMutex();
1105 return CA_STATUS_FAILED;
1109 if (NULL == g_bleClientSendDataMutex)
1111 g_bleClientSendDataMutex = ca_mutex_new();
1112 if (NULL == g_bleClientSendDataMutex)
1114 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1115 CATerminateLEAdapterMutex();
1116 return CA_STATUS_FAILED;
1120 if (NULL == g_bleServerSendDataMutex)
1122 g_bleServerSendDataMutex = ca_mutex_new();
1123 if (NULL == g_bleServerSendDataMutex)
1125 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1126 CATerminateLEAdapterMutex();
1127 return CA_STATUS_FAILED;
1131 if (NULL == g_bleAdapterReqRespCbMutex)
1133 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1134 if (NULL == g_bleAdapterReqRespCbMutex)
1136 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1137 CATerminateLEAdapterMutex();
1138 return CA_STATUS_FAILED;
1142 if (NULL == g_bleReceiveDataMutex)
1144 g_bleReceiveDataMutex = ca_mutex_new();
1145 if (NULL == g_bleReceiveDataMutex)
1147 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1148 return CA_STATUS_FAILED;
1152 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1153 return CA_STATUS_OK;
1156 void CATerminateLEAdapterMutex()
1158 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1160 ca_mutex_free(g_bleIsServerMutex);
1161 g_bleIsServerMutex = NULL;
1163 ca_mutex_free(g_bleNetworkCbMutex);
1164 g_bleNetworkCbMutex = NULL;
1166 ca_mutex_free(g_bleLocalAddressMutex);
1167 g_bleLocalAddressMutex = NULL;
1169 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1170 g_bleAdapterThreadPoolMutex = NULL;
1172 ca_mutex_free(g_bleClientSendDataMutex);
1173 g_bleClientSendDataMutex = NULL;
1175 ca_mutex_free(g_bleServerSendDataMutex);
1176 g_bleServerSendDataMutex = NULL;
1178 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1179 g_bleAdapterReqRespCbMutex = NULL;
1181 ca_mutex_free(g_bleReceiveDataMutex);
1182 g_bleReceiveDataMutex = NULL;
1184 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1187 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1188 CANetworkPacketReceivedCallback reqRespCallback,
1189 CANetworkChangeCallback netCallback,
1190 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
1192 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1195 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1196 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1197 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1199 CAResult_t result = CA_STATUS_OK;
1200 result = CAInitLEAdapterMutex();
1201 if (CA_STATUS_OK != result)
1203 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1204 return CA_STATUS_FAILED;
1206 result = CAInitializeLENetworkMonitor();
1207 if (CA_STATUS_OK != result)
1209 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1210 return CA_STATUS_FAILED;
1213 CAInitializeLEAdapter();
1215 CASetLEClientThreadPoolHandle(handle);
1216 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1217 CASetLEServerThreadPoolHandle(handle);
1218 CASetLEAdapterThreadPoolHandle(handle);
1219 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1220 CASetLEReqRespAdapterCallback(reqRespCallback);
1222 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1223 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1224 CALERegisterNetworkNotifications(netCallback);
1226 g_errorHandler = errorCallback;
1228 CAConnectivityHandler_t connHandler;
1229 connHandler.startAdapter = CAStartLE;
1230 connHandler.stopAdapter = CAStopLE;
1231 connHandler.startListenServer = CAStartLEListeningServer;
1232 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
1233 connHandler.sendData = CASendLEUnicastData;
1234 connHandler.sendDataToAll = CASendLEMulticastData;
1235 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
1236 connHandler.readData = CAReadLEData;
1237 connHandler.terminate = CATerminateLE;
1238 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1240 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1242 return CA_STATUS_OK;
1245 CAResult_t CAStartLE()
1247 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1248 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE, not implemented");
1249 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1250 return CA_STATUS_OK;
1253 CAResult_t CAStopLE()
1255 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1256 #ifndef SINGLE_THREAD
1260 ca_mutex_lock(g_bleIsServerMutex);
1261 if (true == g_isServer)
1263 CAStopLEGattServer();
1267 CAStopLEGattClient();
1269 ca_mutex_unlock(g_bleIsServerMutex);
1271 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1273 return CA_STATUS_OK;
1276 void CATerminateLE()
1278 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1280 CASetLEReqRespServerCallback(NULL);
1281 CASetLEReqRespClientCallback(NULL);
1282 CALERegisterNetworkNotifications(NULL);
1283 CASetLEReqRespAdapterCallback(NULL);
1284 CATerminateLENetworkMonitor();
1286 ca_mutex_lock(g_bleIsServerMutex);
1287 if (true == g_isServer)
1289 CATerminateLEGattServer();
1293 CATerminateLEGattClient();
1295 ca_mutex_unlock(g_bleIsServerMutex);
1297 #ifndef SINGLE_THREAD
1298 CATerminateLEQueues();
1300 CATerminateLEAdapterMutex();
1302 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1305 CAResult_t CAStartLEListeningServer()
1307 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1308 CAResult_t result = CA_STATUS_OK;
1309 #ifndef SINGLE_THREAD
1310 result = CAInitLEServerQueues();
1311 if (CA_STATUS_OK != result)
1313 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1314 return CA_STATUS_FAILED;
1318 result = CAGetLEAdapterState();
1319 if (CA_ADAPTER_NOT_ENABLED == result)
1321 gLeServerStatus = CA_LISTENING_SERVER;
1322 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1323 return CA_STATUS_OK;
1326 if (CA_STATUS_FAILED == result)
1328 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1329 return CA_STATUS_FAILED;
1332 CAStartLEGattServer();
1334 ca_mutex_lock(g_bleIsServerMutex);
1336 ca_mutex_unlock(g_bleIsServerMutex);
1338 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1339 return CA_STATUS_OK;
1342 CAResult_t CAStartLEDiscoveryServer()
1344 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1345 CAResult_t result = CA_STATUS_OK;
1346 #ifndef SINGLE_THREAD
1347 result = CAInitLEClientQueues();
1348 if (CA_STATUS_OK != result)
1350 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1351 return CA_STATUS_FAILED;
1354 result = CAGetLEAdapterState();
1355 if (CA_ADAPTER_NOT_ENABLED == result)
1357 gLeServerStatus = CA_DISCOVERY_SERVER;
1358 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1359 return CA_STATUS_OK;
1362 if (CA_STATUS_FAILED == result)
1364 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1365 return CA_STATUS_FAILED;
1368 CAStartLEGattClient();
1370 ca_mutex_lock(g_bleIsServerMutex);
1372 ca_mutex_unlock(g_bleIsServerMutex);
1374 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1375 return CA_STATUS_OK;
1378 CAResult_t CAStartLENotifyServer()
1380 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1382 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1383 return CA_STATUS_OK;
1386 uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data,
1389 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1391 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1395 CAResult_t CAReadLEData()
1397 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1398 #ifdef SINGLE_THREAD
1401 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1402 return CA_STATUS_OK;
1405 int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1407 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1410 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1411 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1413 CAResult_t result = CA_STATUS_FAILED;
1415 ca_mutex_lock(g_bleIsServerMutex);
1416 if (true == g_isServer)
1418 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1419 if (CA_STATUS_OK != result)
1421 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1424 g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
1426 ca_mutex_unlock(g_bleIsServerMutex);
1432 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1433 if (CA_STATUS_OK != result)
1435 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1438 g_errorHandler(endpoint, data, dataLen, result);
1440 ca_mutex_unlock(g_bleIsServerMutex);
1444 ca_mutex_unlock(g_bleIsServerMutex);
1446 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1450 int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1452 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1455 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1459 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1463 CAResult_t result = CA_STATUS_FAILED;
1465 ca_mutex_lock(g_bleIsServerMutex);
1466 if (true == g_isServer)
1468 result = CALEAdapterServerSendData(NULL, data, dataLen);
1469 if (CA_STATUS_OK != result)
1471 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
1473 ca_mutex_unlock(g_bleIsServerMutex);
1476 g_errorHandler(endpoint, data, dataLen, result);
1483 result = CALEAdapterClientSendData(NULL, data, dataLen);
1484 if (CA_STATUS_OK != result)
1486 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
1489 g_errorHandler(endpoint, data, dataLen, result);
1491 ca_mutex_unlock(g_bleIsServerMutex);
1495 ca_mutex_unlock(g_bleIsServerMutex);
1497 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1501 CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1503 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1505 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
1507 char *local_address = NULL;
1509 CAResult_t res = CAGetLEAddress(&local_address);
1510 if (CA_STATUS_OK != res)
1512 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
1516 if (NULL == local_address)
1518 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
1519 return CA_STATUS_FAILED;
1523 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
1524 if (NULL == (*info))
1526 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
1527 OICFree(local_address);
1528 return CA_STATUS_FAILED;
1531 size_t local_address_len = strlen(local_address);
1533 if(local_address_len >= sizeof(g_localBLEAddress) ||
1534 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
1536 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
1538 OICFree(local_address);
1539 return CA_STATUS_FAILED;
1542 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
1543 ca_mutex_lock(g_bleLocalAddressMutex);
1544 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
1545 ca_mutex_unlock(g_bleLocalAddressMutex);
1547 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
1549 OICFree(local_address);
1551 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1552 return CA_STATUS_OK;
1555 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
1557 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1559 ca_mutex_lock(g_bleNetworkCbMutex);
1560 g_networkCallback = netCallback;
1561 ca_mutex_unlock(g_bleNetworkCbMutex);
1562 CAResult_t res = CA_STATUS_OK;
1565 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
1566 if (CA_STATUS_OK != res)
1568 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1573 res = CAUnSetLEAdapterStateChangedCb();
1574 if (CA_STATUS_OK != res)
1576 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1580 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1584 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
1586 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1588 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
1589 CAEndpoint_t localEndpoint = {};
1591 ca_mutex_lock(g_bleLocalAddressMutex);
1592 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
1593 ca_mutex_unlock(g_bleLocalAddressMutex);
1595 g_bleAdapterState = adapter_state;
1596 // Start a GattServer/Client if gLeServerStatus is SET
1597 if (CA_LISTENING_SERVER == gLeServerStatus)
1599 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1600 CAStartLEGattServer();
1602 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
1604 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
1605 CAStartLEGattClient();
1607 gLeServerStatus = CA_SERVER_NOTSTARTED;
1609 ca_mutex_lock(g_bleNetworkCbMutex);
1610 if (NULL != g_networkCallback)
1612 g_networkCallback(&localEndpoint, adapter_state);
1616 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
1618 ca_mutex_unlock(g_bleNetworkCbMutex);
1620 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1623 CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1627 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1629 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1630 #ifndef SINGLE_THREAD
1631 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1632 "g_bleClientSendQueueHandle is NULL",
1634 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1635 "g_bleClientSendDataMutex is NULL",
1638 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1641 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1643 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1646 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1647 return CA_MEMORY_ALLOC_FAILED;
1649 // Add message to send queue
1650 ca_mutex_lock(g_bleClientSendDataMutex);
1651 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1652 ca_mutex_unlock(g_bleClientSendDataMutex);
1654 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1655 return CA_STATUS_OK;
1659 CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1663 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1665 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1667 #ifdef SINGLE_THREAD
1668 char header[CA_HEADER_LENGTH] = {0};
1670 CAResult_t result = CAGenerateHeader(header, dataLen);
1672 if (CA_STATUS_OK != result)
1674 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1678 if (!CAIsLEConnected())
1680 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
1684 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
1685 if (CA_STATUS_OK != result)
1687 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1691 int32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1692 for (int32_t iter = 0; iter < dataLimit; iter++)
1694 result = CAUpdateCharacteristicsToAllGattClients((data +
1695 (iter * CA_SUPPORTED_BLE_MTU_SIZE)),
1696 CA_SUPPORTED_BLE_MTU_SIZE);
1697 if (CA_STATUS_OK != result)
1699 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1705 uint8_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
1708 result = CAUpdateCharacteristicsToAllGattClients((data +
1709 (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)),
1711 if (CA_STATUS_OK != result)
1713 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1719 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
1720 "BleClientReceiverQueue is NULL",
1722 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1723 "BleClientSendDataMutex is NULL",
1726 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1729 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1731 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1734 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1735 return CA_MEMORY_ALLOC_FAILED;
1737 // Add message to send queue
1738 ca_mutex_lock(g_bleServerSendDataMutex);
1739 CAQueueingThreadAddData(g_bleServerSendQueueHandle, bleData, sizeof(CALEData_t));
1740 ca_mutex_unlock(g_bleServerSendDataMutex);
1742 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1743 return CA_STATUS_OK;
1746 CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1747 const void *data, uint32_t dataLength,
1748 uint32_t *sentLength)
1750 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1753 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1754 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1755 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1757 #ifdef SINGLE_THREAD
1758 if(g_networkPacketReceivedCallback)
1760 CAEndpoint_t endPoint = { 0 }; // will be filled by upper layer
1761 endPoint.adapter = CA_ADAPTER_GATT_BTLE;
1762 g_networkPacketReceivedCallback(&endPoint, data, dataLength);
1765 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
1768 //Add message to data queue
1769 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1770 CA_ADAPTER_GATT_BTLE,
1772 if (NULL == remoteEndpoint)
1774 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1775 return CA_STATUS_FAILED;
1778 // Create bleData to add to queue
1779 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1781 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1784 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1785 CAFreeEndpoint(remoteEndpoint);
1786 return CA_MEMORY_ALLOC_FAILED;
1789 CAFreeEndpoint(remoteEndpoint);
1790 // Add message to send queue
1791 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
1793 *sentLength = dataLength;
1795 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1796 return CA_STATUS_OK;
1799 CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1800 const void *data, uint32_t dataLength,
1801 uint32_t *sentLength)
1803 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1806 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1807 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1808 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1809 #ifndef SINGLE_THREAD
1810 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
1813 //Add message to data queue
1814 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1815 CA_ADAPTER_GATT_BTLE,
1817 if (NULL == remoteEndpoint)
1819 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1820 return CA_STATUS_FAILED;
1823 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1825 // Create bleData to add to queue
1826 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1829 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1830 CAFreeEndpoint(remoteEndpoint);
1831 return CA_MEMORY_ALLOC_FAILED;
1834 CAFreeEndpoint(remoteEndpoint);
1835 // Add message to send queue
1836 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
1838 *sentLength = dataLength;
1840 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1841 return CA_STATUS_OK;
1844 void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
1846 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1848 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1849 g_bleAdapterThreadPool = handle;
1850 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1852 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1855 void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1857 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1859 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1861 g_networkPacketReceivedCallback = callback;
1863 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1865 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1868 void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
1871 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
1873 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
1874 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
1876 //if required, will be used to build remote end point
1877 g_errorHandler(rep, data, dataLen, result);
1879 CAFreeEndpoint(rep);
1880 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");