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 ******************************************************************/
24 * This file contains the APIs for EDR adapter.
27 #include "caedradapter.h"
29 #include "caedrinterface.h"
30 #include "caadapterutils.h"
32 #include "cafragmentation.h"
33 #include "caqueueingthread.h"
34 #include "oic_malloc.h"
35 #include "caremotehandler.h"
38 * @var EDR_ADAPTER_TAG
39 * @brief Logging tag for module name.
41 #define EDR_ADAPTER_TAG "CA_EDR_ADAPTER"
44 * @var g_edrThreadPool
45 * @brief Reference to threadpool.
47 static ca_thread_pool_t g_edrThreadPool = NULL;
50 * @var g_sendQueueHandle
51 * @brief Queue handle for Send Data
53 static CAQueueingThread_t *g_sendQueueHandle = NULL;
56 * @var g_recvQueueHandle
57 * @brief Queue handle for Receive Data
59 static CAQueueingThread_t *g_recvQueueHandle = NULL;
63 * @brief Storing Adapter state information
65 static bool g_adapterState = true;
68 * @var g_networkPacketReceivedCallback
69 * @brief Maintains the callback to be notified on receival of network packets from other
72 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
75 * @var g_networkChangeCallback
76 * @brief Maintains the callback to be notified on local bluetooth adapter status change.
78 static CANetworkChangeCallback g_networkChangeCallback = NULL;
81 * @var g_errorCallback
82 * @brief error Callback to CA adapter
84 static CAErrorHandleCallback g_errorCallback = NULL;
87 * @var g_localConnectivity
88 * @brief Information of local Bluetooth adapter.
90 static CAEndpoint_t *g_localConnectivity = NULL;
94 * @brief Storing Rfcommserver state information
96 static bool g_serverState = false;
99 * Stores information of all the senders.
101 * This structure will be used to track and defragment all incoming
106 uint32_t recvDataLen;
107 uint32_t totalDataLen;
109 CAEndpoint_t *remoteEndpoint;
113 * Sender information.
115 static u_arraylist_t *g_senderInfo = NULL;
117 static CAResult_t CAStartServer();
118 static CAResult_t CAEDRInitializeQueueHandlers();
119 CAResult_t CAEDRInitializeSendHandler();
120 CAResult_t CAEDRInitializeReceiveHandler();
121 void CAAdapterTerminateQueues();
122 void CAAdapterDataSendHandler(void *context);
123 void CAAdapterDataReceiverHandler(void *context);
124 CAResult_t CAAdapterStopQueue();
125 void CAAdapterRecvData(const char *remoteAddress, const uint8_t *data, uint32_t dataLength,
126 uint32_t *sentLength);
127 void CAEDRNotifyNetworkStatus(CANetworkStatus_t status);
128 void CAEDROnNetworkStatusChanged(void *context);
129 CAResult_t CAAdapterSendData(const char *remoteAddress, const char *serviceUUID, const uint8_t *data,
130 uint32_t dataLength, uint32_t *sentLength);
131 CAEDRNetworkEvent *CAEDRCreateNetworkEvent(CAEndpoint_t *connectivity,
132 CANetworkStatus_t status);
134 CAResult_t CAEDRClientSendData(const char *remoteAddress,
136 uint32_t dataLength);
139 * @fn CACreateEDRData
140 * @brief Helper function to create CAEDRData
142 static CAEDRData *CACreateEDRData(const CAEndpoint_t *remoteEndpoint, const uint8_t *data,
143 uint32_t dataLength);
147 * @brief Free the Created EDR data
149 static void CAFreeEDRData(CAEDRData *edrData);
152 * @fn CAEDRFreeNetworkEvent
153 * @brief Free the memory associated with @event.
155 void CAEDRFreeNetworkEvent(CAEDRNetworkEvent *event);
157 static void CAEDRDataDestroyer(void *data, uint32_t size);
159 static void CAEDRErrorHandler(const char *remoteAddress, const uint8_t *data,
160 uint32_t dataLength, CAResult_t result);
162 static void CAEDRClearSenderInfo();
164 CAResult_t CAInitializeEDR(CARegisterConnectivityCallback registerCallback,
165 CANetworkPacketReceivedCallback packetReceivedCallback,
166 CANetworkChangeCallback networkStateChangeCallback,
167 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
169 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
172 VERIFY_NON_NULL(registerCallback, EDR_ADAPTER_TAG, "register callback is NULL");
173 VERIFY_NON_NULL(packetReceivedCallback, EDR_ADAPTER_TAG, "data receive callback is NULL");
174 VERIFY_NON_NULL(networkStateChangeCallback, EDR_ADAPTER_TAG,
175 "network state change callback is NULL");
176 VERIFY_NON_NULL(handle, EDR_ADAPTER_TAG, "Thread pool handle is NULL");
178 // Register the callbacks
180 g_edrThreadPool = handle;
181 g_networkPacketReceivedCallback = packetReceivedCallback;
182 g_networkChangeCallback = networkStateChangeCallback;
183 g_errorCallback = errorCallback;
185 // Initialize EDR Network Monitor
186 CAResult_t err = CAEDRInitializeNetworkMonitor(handle);
187 if (CA_STATUS_OK != err)
189 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "EDR N/w Monitor Initialize failed!, error number [%d]",
194 CAEDRSetNetworkChangeCallback(CAEDRNotifyNetworkStatus);
195 CAEDRSetPacketReceivedCallback(CAAdapterRecvData);
196 CAEDRSetErrorHandler(CAEDRErrorHandler);
197 CAEDRInitializeClient(handle);
199 CAConnectivityHandler_t handler;
200 handler.startAdapter = CAStartEDR;
201 handler.startListenServer = CAStartEDRListeningServer;
202 handler.stopListenServer = CAStopEDRListeningServer;
203 handler.startDiscoveryServer = CAStartEDRDiscoveryServer;
204 handler.sendData = CASendEDRUnicastData;
205 handler.sendDataToAll = CASendEDRMulticastData;
206 handler.GetnetInfo = CAGetEDRInterfaceInformation;
207 handler.readData = CAReadEDRData;
208 handler.stopAdapter = CAStopEDR;
209 handler.terminate = CATerminateEDR;
210 registerCallback(handler, CA_ADAPTER_RFCOMM_BTEDR);
212 // Initialize Send/Receive data message queues
213 if (CA_STATUS_OK != CAEDRInitializeQueueHandlers())
215 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "CAAdapterInitializeQueues API failed");
217 return CA_STATUS_FAILED;
220 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
224 CAResult_t CAStartEDR()
226 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
228 //Start Monitoring EDR Network
229 CAResult_t ret = CAEDRStartNetworkMonitor();
230 if (CA_STATUS_OK != ret)
232 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Start n/w monitor");
235 // Get Bluetooth adapter state
236 bool adapterState = false;
237 if (CA_STATUS_OK != CAEDRGetAdapterEnableState(&adapterState))
239 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get adapter enable state");
240 return CA_STATUS_FAILED;
243 if (false == adapterState)
245 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
246 g_adapterState = false;
247 return CA_ADAPTER_NOT_ENABLED;
250 if (CA_STATUS_OK != (ret = CAEDRClientSetCallbacks()))
252 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Start Network Monitor failed!, error number [%d] ",
256 if (CA_STATUS_OK != (ret = CAAdapterStartQueue()))
258 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "CAAdapterStartQueue failed!, error number [%d] ", ret);
261 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
265 CAResult_t CAStartEDRListeningServer()
267 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
269 return CAStartServer();
272 CAResult_t CAStopEDRListeningServer()
274 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
276 return CAEDRServerStop();
279 CAResult_t CAStartEDRDiscoveryServer()
281 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
283 return CAStartServer();
286 int32_t CASendEDRUnicastData(const CAEndpoint_t *remoteEndpoint, const void *data,
289 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
292 VERIFY_NON_NULL_RET(remoteEndpoint, EDR_ADAPTER_TAG, "Remote endpoint is null", -1);
293 VERIFY_NON_NULL_RET(data, EDR_ADAPTER_TAG, "Data is null", -1);
297 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: data length is zero!");
301 if (0 == strlen(remoteEndpoint->addr))
303 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: EDR Address is empty!");
307 uint32_t sentLength = 0;
308 const char *serviceUUID = OIC_EDR_SERVICE_ID;
309 const char *address = remoteEndpoint->addr;
310 CAResult_t err = CAAdapterSendData(address, serviceUUID, data, dataLength, &sentLength);
311 if (CA_STATUS_OK != err)
313 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Send unicast data failed!, error num [%d]", err);
314 g_errorCallback(remoteEndpoint, data, dataLength, err);
318 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
322 int32_t CASendEDRMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLength)
324 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN - CASendEDRMulticastData");
327 VERIFY_NON_NULL_RET(data, EDR_ADAPTER_TAG, "Data is null", -1);
331 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: data length is zero!");
335 uint32_t sentLen = 0;
336 const char *serviceUUID = OIC_EDR_SERVICE_ID;
337 CAResult_t err = CAAdapterSendData(NULL, serviceUUID, data, dataLength, &sentLen);
338 if (CA_STATUS_OK != err)
340 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Send multicast data failed!, error num [%d]", err);
341 g_errorCallback(endpoint, data, dataLength, err);
345 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT - CASendEDRMulticastData");
349 CAResult_t CAGetEDRInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
351 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
353 VERIFY_NON_NULL(info, EDR_ADAPTER_TAG, "LocalConnectivity info is null");
355 CAResult_t err = CA_STATUS_OK;
357 if (CA_STATUS_OK != (err = CAEDRGetInterfaceInformation(info)))
359 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
360 "Failed to get local interface information!, error num [%d]", err);
365 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
369 CAResult_t CAReadEDRData()
371 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
373 return CAEDRManagerReadData();
376 CAResult_t CAStopEDR()
378 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
380 // Stop RFComm server if it is running
383 // Stop network monitor
384 CAEDRStopNetworkMonitor();
387 CAEDRClientUnsetCallbacks();
389 // Disconnect all the client connections
390 CAEDRClientDisconnectAll();
392 // Stop Send and receive Queue
393 CAAdapterStopQueue();
395 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
399 void CATerminateEDR()
401 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
403 // Terminate EDR Network Monitor
404 CAEDRTerminateNetworkMonitor();
406 // Terminate Send/Receive data messages queues
407 CAAdapterTerminateQueues();
409 g_networkPacketReceivedCallback = NULL;
410 g_networkChangeCallback = NULL;
412 // Terminate thread pool
413 g_edrThreadPool = NULL;
415 // Terminate EDR Client
416 CAEDRClientTerminate();
418 // Terminate EDR Server
419 CAEDRServerTerminate();
421 // Free LocalConnectivity information
422 CAFreeEndpoint(g_localConnectivity);
423 g_localConnectivity = NULL;
425 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
428 CAResult_t CAStartServer()
430 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
432 CAResult_t err = CA_STATUS_OK;
434 if (false == g_adapterState)
436 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
437 // Setting g_serverState for starting Rfcommserver when adapter starts
438 g_serverState = true;
442 if (CA_STATUS_OK != (err = CAEDRServerStart(g_edrThreadPool)))
444 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to start RFCOMM server!, error num [%d]",
449 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
453 CAResult_t CAEDRInitializeQueueHandlers()
455 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
457 if (CA_STATUS_OK == CAEDRInitializeSendHandler()
458 && CA_STATUS_OK == CAEDRInitializeReceiveHandler())
460 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Queue is initialized!");
464 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
465 return CA_STATUS_FAILED;
468 CAResult_t CAEDRInitializeSendHandler()
470 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
471 // Check if the message queue is already initialized
472 if (g_sendQueueHandle)
474 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Already queue is initialized!");
478 g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
479 if (!g_sendQueueHandle)
481 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
482 return CA_MEMORY_ALLOC_FAILED;
485 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_edrThreadPool,
486 CAAdapterDataSendHandler, CAEDRDataDestroyer))
488 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Initialize send queue thread");
489 return CA_STATUS_FAILED;
494 CAResult_t CAEDRInitializeReceiveHandler()
496 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
497 // Check if the message queue is already initialized
498 if (g_recvQueueHandle)
500 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Already queue is initialized!");
504 g_recvQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
505 if (!g_recvQueueHandle)
507 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
508 return CA_MEMORY_ALLOC_FAILED;
511 g_senderInfo = u_arraylist_create();
514 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "ClientInfo memory allcation failed!");
515 OICFree(g_recvQueueHandle);
516 g_recvQueueHandle = NULL;
517 return CA_MEMORY_ALLOC_FAILED;
520 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_recvQueueHandle, g_edrThreadPool,
521 CAAdapterDataReceiverHandler,
524 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Initialize send queue thread");
525 u_arraylist_free(&g_senderInfo);
526 OICFree(g_recvQueueHandle);
527 g_recvQueueHandle = NULL;
528 return CA_STATUS_FAILED;
531 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
535 void CAAdapterTerminateQueues()
537 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
539 if (g_sendQueueHandle)
541 CAQueueingThreadDestroy(g_sendQueueHandle);
542 g_sendQueueHandle = NULL;
544 if (g_recvQueueHandle)
546 CAQueueingThreadDestroy(g_recvQueueHandle);
547 g_recvQueueHandle = NULL;
550 CAEDRClearSenderInfo();
552 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
555 void CAAdapterDataSendHandler(void *context)
557 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN - CAAdapterDataSendHandler");
559 CAEDRData *message = (CAEDRData *) context;
562 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get message!");
566 const char *remoteAddress = NULL;
568 if (NULL == message->remoteEndpoint)
570 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "remoteEndpoint is not available");
575 remoteAddress = message->remoteEndpoint->addr;
580 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDR Send Message error");
581 //Error cannot be sent if remote address is NULL
585 uint32_t dataSegmentLength = message->dataLen + CA_HEADER_LENGTH;
587 uint8_t *header = (uint8_t *) OICCalloc(CA_HEADER_LENGTH, sizeof(uint8_t));
588 VERIFY_NON_NULL_VOID(header, EDR_ADAPTER_TAG, "Memory allocation failed");
590 uint8_t* dataSegment = (uint8_t *) OICCalloc(dataSegmentLength, sizeof(uint8_t));
591 if (NULL == dataSegment)
593 CAEDRErrorHandler(remoteAddress, message->data, message->dataLen, CA_SEND_FAILED);
598 CAResult_t result = CAGenerateHeader(header, CA_HEADER_LENGTH, message->dataLen);
599 if (CA_STATUS_OK != result)
601 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Generate header failed");
603 OICFree(dataSegment);
604 CAEDRErrorHandler(remoteAddress, message->data, message->dataLen, result);
608 memcpy(dataSegment, header, CA_HEADER_LENGTH);
611 memcpy(dataSegment + CA_HEADER_LENGTH, message->data, message->dataLen);
613 result = CAEDRClientSendData(remoteAddress, dataSegment, dataSegmentLength);
614 OICFree(dataSegment);
616 if (CA_STATUS_OK != result)
618 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "CAEDRClientSendData API failed");
619 CAEDRErrorHandler(remoteAddress, message->data, message->dataLen, result);
623 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
626 CAResult_t CAEDRClientSendData(const char *remoteAddress,
631 CAResult_t result = CA_SEND_FAILED;
633 // Send the first segment with the header.
634 if ((NULL != remoteAddress) && (0 < strlen(remoteAddress))) //Unicast data
636 result = CAEDRClientSendUnicastData(remoteAddress, data, dataLength);
637 if (CA_STATUS_OK != result)
639 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send unicast data !");
645 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "sending multicast data : %s", data);
646 result = CAEDRClientSendMulticastData(data, dataLength);
648 if (CA_STATUS_OK != result)
650 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send multicast data !");
657 static void CAEDRClearSenderInfo()
659 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
661 uint32_t listIndex = 0;
662 uint32_t listLength = u_arraylist_length(g_senderInfo);
663 for (listIndex = 0; listIndex < listLength; listIndex++)
665 CAEDRSenderInfo_t *info = (CAEDRSenderInfo_t *) u_arraylist_get(g_senderInfo, listIndex);
671 OICFree(info->defragData);
672 CAFreeEndpoint(info->remoteEndpoint);
675 u_arraylist_free(&g_senderInfo);
676 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
679 static CAResult_t CAEDRGetSenderInfo(const char *edrAddress,
680 CAEDRSenderInfo_t **senderInfo,
681 uint32_t *senderIndex)
683 VERIFY_NON_NULL_RET(edrAddress,
685 "NULL edr address argument",
686 CA_STATUS_INVALID_PARAM);
687 VERIFY_NON_NULL_RET(senderIndex,
689 "NULL index argument",
690 CA_STATUS_INVALID_PARAM);
692 const uint32_t listLength = u_arraylist_length(g_senderInfo);
693 const uint32_t addrLength = strlen(edrAddress);
694 for (uint32_t index = 0; index < listLength; index++)
696 CAEDRSenderInfo_t *info = (CAEDRSenderInfo_t *) u_arraylist_get(g_senderInfo, index);
697 if (!info || !(info->remoteEndpoint))
702 if (!strncmp(info->remoteEndpoint->addr, edrAddress, addrLength))
704 *senderIndex = index;
713 return CA_STATUS_FAILED;
716 void CAAdapterDataReceiverHandler(void *context)
718 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN_CAAdapterDataReceiverHandler");
720 if (NULL == g_networkPacketReceivedCallback)
722 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "g_networkPacketReceivedCallback is NULL");
726 CAEDRData *message = (CAEDRData *) context;
727 if (NULL == message || NULL == message->remoteEndpoint)
729 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get message!");
732 uint32_t dataLen = 0;
734 CAEDRSenderInfo_t *senderInfo = NULL;
735 uint32_t senderIndex = 0;
737 while (dataLen < message->dataLen)
739 if(CA_STATUS_OK != CAEDRGetSenderInfo(message->remoteEndpoint->addr,
740 &senderInfo, &senderIndex))
742 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "This is a new client [%s]",
743 message->remoteEndpoint->addr);
748 CAEDRSenderInfo_t *newSender = OICMalloc(sizeof(CAEDRSenderInfo_t));
753 newSender->recvDataLen = 0;
754 newSender->totalDataLen = 0;
755 newSender->defragData = NULL;
756 newSender->remoteEndpoint = NULL;
758 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Parsing the header");
759 newSender->totalDataLen = CAParseHeader(message->data + dataLen,
760 message->dataLen - dataLen);
761 if(!(newSender->totalDataLen))
763 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Total Data Length is parsed as 0!!!");
768 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Total data to be accumulated [%u] bytes",
769 newSender->totalDataLen);
770 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "data received in the first packet [%u] bytes",
773 newSender->defragData = OICCalloc(newSender->totalDataLen,
774 sizeof(*newSender->defragData));
776 if (NULL == newSender->defragData)
782 newSender->remoteEndpoint = CACloneEndpoint(message->remoteEndpoint);
783 if (NULL == newSender->remoteEndpoint)
785 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "remoteEndpoint is NULL!");
786 OICFree(newSender->defragData);
791 if (message->dataLen - CA_HEADER_LENGTH - dataLen <= newSender->totalDataLen)
793 memcpy(newSender->defragData, message->data + dataLen + CA_HEADER_LENGTH,
794 message->dataLen - dataLen - CA_HEADER_LENGTH);
795 newSender->recvDataLen += message->dataLen - dataLen - CA_HEADER_LENGTH;
796 u_arraylist_add(g_senderInfo,(void *)newSender);
797 dataLen = message->dataLen;
801 memcpy(newSender->defragData, message->data + dataLen + CA_HEADER_LENGTH,
802 newSender->totalDataLen);
803 newSender->recvDataLen = newSender->totalDataLen;
804 u_arraylist_add(g_senderInfo,(void *)newSender);
805 dataLen += newSender->totalDataLen + CA_HEADER_LENGTH;
807 //Getting newSender index position in g_senderInfo array list
809 CAEDRGetSenderInfo(newSender->remoteEndpoint->addr, NULL, &senderIndex))
811 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Existing sender index not found!!");
812 OICFree(newSender->defragData);
813 CAFreeEndpoint(newSender->remoteEndpoint);
817 senderInfo = newSender;
821 if (senderInfo->recvDataLen + message->dataLen - dataLen <= senderInfo->totalDataLen)
823 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Copying the data of length [%d]",
824 message->dataLen - dataLen);
825 memcpy(senderInfo->defragData + senderInfo->recvDataLen, message->data + dataLen,
826 message->dataLen - dataLen);
827 senderInfo->recvDataLen += message->dataLen - dataLen;
828 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
829 senderInfo->totalDataLen, senderInfo->recvDataLen);
830 dataLen = message->dataLen;
834 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Copying the data of length [%d]",
835 senderInfo->totalDataLen - senderInfo->recvDataLen);
836 memcpy(senderInfo->defragData + senderInfo->recvDataLen, message->data + dataLen,
837 senderInfo->totalDataLen - senderInfo->recvDataLen);
838 dataLen += senderInfo->totalDataLen - senderInfo->recvDataLen;
839 senderInfo->recvDataLen = senderInfo->totalDataLen;
840 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
841 senderInfo->totalDataLen, senderInfo->recvDataLen);
845 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
847 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Sending data up !");
849 CASecureEndpoint_t sep = {.endpoint = *(senderInfo->remoteEndpoint)};
850 g_networkPacketReceivedCallback(&sep, senderInfo->defragData, senderInfo->recvDataLen);
851 u_arraylist_remove(g_senderInfo, senderIndex);
852 senderInfo->remoteEndpoint = NULL;
853 senderInfo->defragData = NULL;
858 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT_CAAdapterDataReceiverHandler");
861 CAResult_t CAAdapterStartQueue()
863 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
864 // Start send queue thread
865 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
867 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Start Send Data Thread");
868 CAEDRClientUnsetCallbacks();
869 //Disconnect all the client connections
870 CAEDRClientDisconnectAll();
871 return CA_STATUS_FAILED;
874 // Start receive queue thread
875 if (CA_STATUS_OK != CAQueueingThreadStart(g_recvQueueHandle))
877 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Start Receive Data Thread");
878 CAEDRClientUnsetCallbacks();
879 //Disconnect all the client connections
880 CAEDRClientDisconnectAll();
881 return CA_STATUS_FAILED;
883 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
887 CAResult_t CAAdapterStopQueue()
889 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
890 //Stop send queue thread
891 CAQueueingThreadStop(g_sendQueueHandle);
893 //Stop receive queue thread
894 CAQueueingThreadStop(g_recvQueueHandle);
895 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
899 void CAAdapterRecvData(const char *remoteAddress, const uint8_t *data, uint32_t dataLength,
900 uint32_t *sentLength)
902 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
904 if (false == g_adapterState)
906 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
912 VERIFY_NON_NULL_VOID(data, EDR_ADAPTER_TAG, "Data is null");
913 VERIFY_NON_NULL_VOID(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
915 // Create remote endpoint
916 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
917 CA_ADAPTER_RFCOMM_BTEDR,
919 if (NULL == remoteEndpoint)
921 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create remote endpoint !");
925 // Add message to data queue
926 CAEDRData *edrData = CACreateEDRData(remoteEndpoint, data, dataLength);
927 CAQueueingThreadAddData(g_recvQueueHandle, edrData, sizeof(CAEDRData));
928 *sentLength = dataLength;
930 // Free remote endpoint
931 CAFreeEndpoint(remoteEndpoint);
933 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
936 void CAEDRErrorHandler(const char *remoteAddress, const uint8_t *data,
937 uint32_t dataLength, CAResult_t result)
939 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
942 VERIFY_NON_NULL_VOID(data, EDR_ADAPTER_TAG, "Data is null");
944 // Create remote endpoint
945 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(0, CA_ADAPTER_RFCOMM_BTEDR,
949 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create remote endpoint !");
953 g_errorCallback(remoteEndpoint, data, dataLength, result);
955 // Free remote endpoint
956 CAFreeEndpoint(remoteEndpoint);
958 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
961 CAResult_t CAAdapterSendData(const char *remoteAddress, const char *serviceUUID, const uint8_t *data,
962 uint32_t dataLength, uint32_t *sentLength)
964 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN - CAAdapterSendData");
966 if (false == g_adapterState)
968 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
970 return CA_ADAPTER_NOT_ENABLED;
973 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
974 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
975 VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
977 // Create remote endpoint
978 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
979 CA_ADAPTER_RFCOMM_BTEDR,
981 if (NULL == remoteEndpoint)
983 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create remote endpoint !");
984 return CA_MEMORY_ALLOC_FAILED;
987 // Add message to data queue
988 CAEDRData *edrData = CACreateEDRData(remoteEndpoint, data, dataLength);
989 CAQueueingThreadAddData(g_sendQueueHandle, edrData, sizeof (CAEDRData));
990 *sentLength = dataLength;
992 // Free remote endpoint
993 CAFreeEndpoint(remoteEndpoint);
995 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT - CAAdapterSendData");
999 void CAEDRNotifyNetworkStatus(CANetworkStatus_t status)
1001 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
1003 // Create localconnectivity
1004 if (NULL == g_localConnectivity)
1006 CAEDRGetInterfaceInformation(&g_localConnectivity);
1009 if (CA_INTERFACE_UP == status)
1011 if (false == g_adapterState)
1013 // Get Bluetooth adapter state
1014 bool adapterState = false;
1015 if (CA_STATUS_OK != CAEDRGetAdapterEnableState(&adapterState))
1017 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get adapter enable state");
1021 if (false== adapterState)
1023 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
1024 g_adapterState = false;
1027 CAEDRClientSetCallbacks();
1028 g_adapterState = true;
1029 CAAdapterStartQueue();
1030 // starting RFCommServer
1031 if (true == g_serverState)
1034 g_serverState = false;
1040 g_adapterState = false;
1043 // Notify to upper layer
1044 if (g_networkChangeCallback && g_localConnectivity && g_edrThreadPool)
1046 // Add notification task to thread pool
1047 CAEDRNetworkEvent *event = CAEDRCreateNetworkEvent(g_localConnectivity, status);
1050 if (CA_STATUS_OK != ca_thread_pool_add_task(g_edrThreadPool,
1051 CAEDROnNetworkStatusChanged,event))
1053 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create threadpool!");
1059 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
1062 void CAEDROnNetworkStatusChanged(void *context)
1064 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
1066 if (NULL == context)
1068 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "context is NULL!");
1072 CAEDRNetworkEvent *networkEvent = (CAEDRNetworkEvent *) context;
1074 // Notify to upper layer
1075 if (g_networkChangeCallback)
1077 g_networkChangeCallback(networkEvent->info, networkEvent->status);
1080 // Free the created Network event
1081 CAEDRFreeNetworkEvent(networkEvent);
1083 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
1086 CAEDRNetworkEvent *CAEDRCreateNetworkEvent(CAEndpoint_t *connectivity,
1087 CANetworkStatus_t status)
1089 VERIFY_NON_NULL_RET(connectivity, EDR_ADAPTER_TAG, "connectivity is NULL", NULL);
1091 // Create CAEDRNetworkEvent
1092 CAEDRNetworkEvent *event = (CAEDRNetworkEvent *) OICMalloc(sizeof(CAEDRNetworkEvent));
1095 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to allocate memory to network event!");
1099 // Create duplicate of Local connectivity
1100 event->info = CACloneEndpoint(connectivity);
1101 event->status = status;
1105 void CAEDRFreeNetworkEvent(CAEDRNetworkEvent *event)
1109 CAFreeEndpoint(event->info);
1114 CAEDRData *CACreateEDRData(const CAEndpoint_t *remoteEndpoint,
1115 const uint8_t *data, uint32_t dataLength)
1117 CAEDRData *edrData = (CAEDRData *)OICMalloc(sizeof (CAEDRData));
1120 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
1124 edrData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1126 edrData->data = OICMalloc(dataLength);
1127 if (NULL == edrData->data)
1129 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
1130 CAFreeEDRData(edrData);
1133 memcpy(edrData->data, data, dataLength);
1134 edrData->dataLen = dataLength;
1139 void CAFreeEDRData(CAEDRData *edrData)
1141 VERIFY_NON_NULL_VOID(edrData, EDR_ADAPTER_TAG, "edrData is NULL");
1143 CAFreeEndpoint(edrData->remoteEndpoint);
1144 OICFree(edrData->data);
1148 void CAEDRDataDestroyer(void *data, uint32_t size)
1150 if ((size_t)size < sizeof(CAEDRData))
1152 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Destroy data too small %p %d",
1155 CAEDRData *edrdata = (CAEDRData *) data;
1157 CAFreeEDRData(edrdata);