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;
62 * @var g_isHeaderAvailable
63 * @brief to differentiate btw header and data packet.
65 static bool g_isHeaderAvailable = false;
69 * @brief Storing Adapter state information
71 static bool g_adapterState = true;
74 * @var g_networkPacketReceivedCallback
75 * @brief Maintains the callback to be notified on receival of network packets from other
78 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
81 * @var g_networkChangeCallback
82 * @brief Maintains the callback to be notified on local bluetooth adapter status change.
84 static CANetworkChangeCallback g_networkChangeCallback = NULL;
87 * @var g_errorCallback
88 * @brief error Callback to CA adapter
90 static CAErrorHandleCallback g_errorCallback = NULL;
93 * @var g_localConnectivity
94 * @brief Information of local Bluetooth adapter.
96 static CAEndpoint_t *g_localConnectivity = NULL;
100 * @brief Storing RfcommserverUUID
102 static int g_serverId = -1;
106 * @brief Storing Rfcommserver state information
108 static bool g_serverState = false;
110 static CAResult_t CAStartServer();
111 static CAResult_t CAEDRInitializeQueueHandlers();
112 CAResult_t CAEDRInitializeSendHandler();
113 CAResult_t CAEDRInitializeReceiveHandler();
114 void CAAdapterTerminateQueues();
115 void CAAdapterDataSendHandler(void *context);
116 void CAAdapterDataReceiverHandler(void *context);
117 CAResult_t CAAdapterStopQueue();
118 void CAAdapterRecvData(const char *remoteAddress, const void *data, uint32_t dataLength,
119 uint32_t *sentLength);
120 void CAEDRNotifyNetworkStatus(CANetworkStatus_t status);
121 void CAEDROnNetworkStatusChanged(void *context);
122 CAResult_t CAAdapterSendData(const char *remoteAddress, const char *serviceUUID, const void *data,
123 uint32_t dataLength, uint32_t *sentLength);
124 CAEDRNetworkEvent *CAEDRCreateNetworkEvent(CAEndpoint_t *connectivity,
125 CANetworkStatus_t status);
126 CAResult_t CAEDRClientSendData(const char *remoteAddress, const char *serviceUUID,
127 const void *data, uint32_t dataLength, uint32_t *sentLength);
129 * @fn CACreateEDRData
130 * @brief Helper function to create CAEDRData
132 static CAEDRData *CACreateEDRData(const CAEndpoint_t *remoteEndpoint, const void *data,
133 uint32_t dataLength);
137 * @brief Free the Created EDR data
139 static void CAFreeEDRData(CAEDRData *edrData);
142 * @fn CAEDRFreeNetworkEvent
143 * @brief Free the memory associated with @event.
145 void CAEDRFreeNetworkEvent(CAEDRNetworkEvent *event);
147 static void CAEDRDataDestroyer(void *data, uint32_t size);
149 static void CAEDRErrorHandler(const char *remoteAddress, const char *serviceUUID, const void *data,
150 uint32_t dataLength, CAResult_t result);
152 CAResult_t CAInitializeEDR(CARegisterConnectivityCallback registerCallback,
153 CANetworkPacketReceivedCallback packetReceivedCallback,
154 CANetworkChangeCallback networkStateChangeCallback,
155 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
157 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
160 VERIFY_NON_NULL(registerCallback, EDR_ADAPTER_TAG, "register callback is NULL");
161 VERIFY_NON_NULL(packetReceivedCallback, EDR_ADAPTER_TAG, "data receive callback is NULL");
162 VERIFY_NON_NULL(networkStateChangeCallback, EDR_ADAPTER_TAG,
163 "network state change callback is NULL");
164 VERIFY_NON_NULL(handle, EDR_ADAPTER_TAG, "Thread pool handle is NULL");
166 // Register the callbacks
168 g_edrThreadPool = handle;
169 g_networkPacketReceivedCallback = packetReceivedCallback;
170 g_networkChangeCallback = networkStateChangeCallback;
171 g_errorCallback = errorCallback;
173 // Initialize EDR Network Monitor
174 CAResult_t err = CAEDRInitializeNetworkMonitor(handle);
175 if (CA_STATUS_OK != err)
177 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "EDR N/w Monitor Initialize failed!, error number [%d]",
182 CAEDRSetNetworkChangeCallback(CAEDRNotifyNetworkStatus);
183 CAEDRSetPacketReceivedCallback(CAAdapterRecvData);
184 CAEDRSetErrorHandler(CAEDRErrorHandler);
185 CAEDRInitializeClient(handle);
187 CAConnectivityHandler_t handler;
188 handler.startAdapter = CAStartEDR;
189 handler.startListenServer = CAStartEDRListeningServer;
190 handler.startDiscoveryServer = CAStartEDRDiscoveryServer;
191 handler.sendData = CASendEDRUnicastData;
192 handler.sendDataToAll = CASendEDRMulticastData;
193 handler.GetnetInfo = CAGetEDRInterfaceInformation;
194 handler.readData = CAReadEDRData;
195 handler.stopAdapter = CAStopEDR;
196 handler.terminate = CATerminateEDR;
197 registerCallback(handler, CA_ADAPTER_RFCOMM_BTEDR);
199 // Initialize Send/Receive data message queues
200 if (CA_STATUS_OK != CAEDRInitializeQueueHandlers())
202 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "CAAdapterInitializeQueues API failed");
204 return CA_STATUS_FAILED;
207 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
211 CAResult_t CAStartEDR()
213 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
215 //Start Monitoring EDR Network
216 CAResult_t ret = CAEDRStartNetworkMonitor();
217 if (CA_STATUS_OK != ret)
219 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Start n/w monitor");
222 // Get Bluetooth adapter state
223 bool adapterState = false;
224 if (CA_STATUS_OK != CAEDRGetAdapterEnableState(&adapterState))
226 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get adapter enable state");
227 return CA_STATUS_FAILED;
230 if (false == adapterState)
232 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
233 g_adapterState = false;
234 return CA_ADAPTER_NOT_ENABLED;
237 if (CA_STATUS_OK != (ret = CAEDRClientSetCallbacks()))
239 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Start Network Monitor failed!, error number [%d] ",
243 if (CA_STATUS_OK != (ret = CAAdapterStartQueue()))
245 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "CAAdapterStartQueue failed!, error number [%d] ", ret);
248 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
252 CAResult_t CAStartEDRListeningServer()
254 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
256 return CAStartServer();
259 CAResult_t CAStartEDRDiscoveryServer()
261 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
263 return CAStartServer();
266 int32_t CASendEDRUnicastData(const CAEndpoint_t *remoteEndpoint, const void *data,
269 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
272 VERIFY_NON_NULL_RET(remoteEndpoint, EDR_ADAPTER_TAG, "Remote endpoint is null", -1);
273 VERIFY_NON_NULL_RET(data, EDR_ADAPTER_TAG, "Data is null", -1);
277 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: data length is zero!");
281 if (0 == strlen(remoteEndpoint->addr))
283 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: EDR Address is empty!");
287 uint32_t sentLength = 0;
288 const char *serviceUUID = OIC_EDR_SERVICE_ID;
289 const char *address = remoteEndpoint->addr;
290 CAResult_t err = CAAdapterSendData(address, serviceUUID, data, dataLength, &sentLength);
291 if (CA_STATUS_OK != err)
293 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Send unicast data failed!, error num [%d]", err);
294 g_errorCallback(remoteEndpoint, data, dataLength, err);
298 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
302 int32_t CASendEDRMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLength)
304 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN - CASendEDRMulticastData");
307 VERIFY_NON_NULL_RET(data, EDR_ADAPTER_TAG, "Data is null", -1);
311 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: data length is zero!");
315 uint32_t sentLen = 0;
316 const char *serviceUUID = OIC_EDR_SERVICE_ID;
317 CAResult_t err = CAAdapterSendData(NULL, serviceUUID, data, dataLength, &sentLen);
318 if (CA_STATUS_OK != err)
320 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Send multicast data failed!, error num [%d]", err);
321 g_errorCallback(endpoint, data, dataLength, err);
325 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT - CASendEDRMulticastData");
329 CAResult_t CAGetEDRInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
331 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
333 VERIFY_NON_NULL(info, EDR_ADAPTER_TAG, "LocalConnectivity info is null");
335 CAResult_t err = CA_STATUS_OK;
337 if (CA_STATUS_OK != (err = CAEDRGetInterfaceInformation(info)))
339 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
340 "Failed to get local interface information!, error num [%d]", err);
345 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
349 CAResult_t CAReadEDRData()
351 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
353 return CAEDRManagerReadData();
356 CAResult_t CAStopEDR()
358 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
360 // Stop RFComm server if it is running
361 if (-1 != g_serverId)
363 CAEDRServerStop(g_serverId);
367 // Stop network monitor
368 CAEDRStopNetworkMonitor();
371 CAEDRClientUnsetCallbacks();
373 // Disconnect all the client connections
374 CAEDRClientDisconnectAll();
376 // Stop Send and receive Queue
377 CAAdapterStopQueue();
379 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
383 void CATerminateEDR()
385 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
389 // Terminate EDR Network Monitor
390 CAEDRTerminateNetworkMonitor();
392 // Terminate Send/Receive data messages queues
393 CAAdapterTerminateQueues();
395 g_networkPacketReceivedCallback = NULL;
396 g_networkChangeCallback = NULL;
398 // Terminate thread pool
399 g_edrThreadPool = NULL;
401 // Terminate EDR Client
402 CAEDRClientTerminate();
404 // Terminate EDR Server
405 CAEDRServerTerminate();
407 // Free LocalConnectivity information
408 CAFreeEndpoint(g_localConnectivity);
409 g_localConnectivity = NULL;
411 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
414 CAResult_t CAStartServer()
416 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
418 CAResult_t err = CA_STATUS_OK;
420 if (false == g_adapterState)
422 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
423 // Setting g_serverState for starting Rfcommserver when adapter starts
424 g_serverState = true;
430 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Server is already in running state.");
431 return CA_SERVER_STARTED_ALREADY;
434 if (CA_STATUS_OK != (err = CAEDRServerStart(OIC_EDR_SERVICE_ID, &g_serverId, g_edrThreadPool)))
436 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to start RFCOMM server!, error num [%d]",
441 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
445 CAResult_t CAEDRInitializeQueueHandlers()
447 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
449 if (CA_STATUS_OK == CAEDRInitializeSendHandler()
450 && CA_STATUS_OK == CAEDRInitializeReceiveHandler())
452 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Queue is initialized!");
456 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
457 return CA_STATUS_FAILED;
460 CAResult_t CAEDRInitializeSendHandler()
462 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
463 // Check if the message queue is already initialized
464 if (g_sendQueueHandle)
466 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Already queue is initialized!");
470 g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
471 if (!g_sendQueueHandle)
473 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
474 return CA_MEMORY_ALLOC_FAILED;
477 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_edrThreadPool,
478 CAAdapterDataSendHandler, CAEDRDataDestroyer))
480 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Initialize send queue thread");
481 return CA_STATUS_FAILED;
486 CAResult_t CAEDRInitializeReceiveHandler()
488 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
489 // Check if the message queue is already initialized
490 if (g_recvQueueHandle)
492 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Already queue is initialized!");
496 g_recvQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
497 if (!g_recvQueueHandle)
499 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
500 return CA_MEMORY_ALLOC_FAILED;
503 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_recvQueueHandle, g_edrThreadPool,
504 CAAdapterDataReceiverHandler,
507 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Initialize send queue thread");
508 return CA_STATUS_FAILED;
511 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
515 void CAAdapterTerminateQueues()
517 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
519 if (g_sendQueueHandle)
521 CAQueueingThreadDestroy(g_sendQueueHandle);
522 g_sendQueueHandle = NULL;
524 if (g_recvQueueHandle)
526 CAQueueingThreadDestroy(g_recvQueueHandle);
527 g_recvQueueHandle = NULL;
530 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
533 void CAAdapterDataSendHandler(void *context)
535 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN - CAAdapterDataSendHandler");
537 CAEDRData *message = (CAEDRData *) context;
540 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get message!");
544 const char *remoteAddress = NULL;
545 const char *serviceUUID = OIC_EDR_SERVICE_ID;
546 uint32_t sentLength = 0;
548 if (NULL == message->remoteEndpoint)
550 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "remoteEndpoint is not available");
555 remoteAddress = message->remoteEndpoint->addr;
558 if(!remoteAddress || !serviceUUID)
560 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDR Send Message error");
561 //Error cannot be sent if remote address is NULL
565 uint32_t dataSegmentLength = message->dataLen + CA_HEADER_LENGTH;
566 uint32_t dataLen = message->dataLen;
567 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "checking for fragmentation and the dataLen is %d",
570 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
571 VERIFY_NON_NULL_VOID(header, EDR_ADAPTER_TAG, "Memory allocation failed");
573 char* dataSegment = (char *) OICCalloc(dataSegmentLength, sizeof(char));
574 if (NULL == dataSegment)
576 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed");
577 CAEDRErrorHandler(remoteAddress, serviceUUID, message->data, message->dataLen, CA_SEND_FAILED);
582 CAResult_t result = CAGenerateHeader(header, dataLen);
583 if (CA_STATUS_OK != result)
585 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Generate header failed");
587 OICFree(dataSegment);
588 CAEDRErrorHandler(remoteAddress, serviceUUID, message->data, message->dataLen, CA_SEND_FAILED);
592 memcpy(dataSegment, header, CA_HEADER_LENGTH);
595 if (CA_SUPPORTED_EDR_MTU_SIZE > dataSegmentLength)
597 length = dataSegmentLength;
598 memcpy(dataSegment + CA_HEADER_LENGTH, message->data, dataLen);
602 length = CA_SUPPORTED_EDR_MTU_SIZE;
603 memcpy(dataSegment + CA_HEADER_LENGTH, message->data,
604 CA_SUPPORTED_EDR_MTU_SIZE - CA_HEADER_LENGTH);
607 uint32_t iter = dataSegmentLength / CA_SUPPORTED_EDR_MTU_SIZE;
609 result = CAEDRClientSendData(remoteAddress, serviceUUID, dataSegment, length,
611 if(CA_STATUS_OK != result)
613 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "CAEDRClientSendData API failed");
614 OICFree(dataSegment);
615 CAEDRErrorHandler(remoteAddress, serviceUUID, message->data, message->dataLen, result);
619 OICFree(dataSegment);
621 for (index = 1; index < iter; index++)
623 // Send the remaining header.
624 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Sending the chunk number [%d]", index);
626 void *dataPtr = message->data + ((index * CA_SUPPORTED_EDR_MTU_SIZE) - CA_HEADER_LENGTH);
627 result = CAEDRClientSendData(remoteAddress, serviceUUID,
628 dataPtr, CA_SUPPORTED_EDR_MTU_SIZE, &sentLength);
629 if(CA_STATUS_OK != result)
631 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "CAEDRClientSendData API failed");
632 CAEDRErrorHandler(remoteAddress, serviceUUID, message->data, message->dataLen, result);
637 uint32_t remainingLen = dataSegmentLength % CA_SUPPORTED_EDR_MTU_SIZE;
638 if (remainingLen && (dataSegmentLength > CA_SUPPORTED_EDR_MTU_SIZE))
640 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
641 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Sending the last chunk");
642 void *dataPtr = message->data + ((index * CA_SUPPORTED_EDR_MTU_SIZE) - CA_HEADER_LENGTH);
643 result = CAEDRClientSendData(remoteAddress, serviceUUID, dataPtr,
644 remainingLen, &sentLength);
645 if(CA_STATUS_OK != result)
647 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "CAEDRClientSendData API failed");
648 CAEDRErrorHandler(remoteAddress, serviceUUID, message->data, message->dataLen, result);
653 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
656 CAResult_t CAEDRClientSendData(const char *remoteAddress, const char *serviceUUID,
657 const void *data, uint32_t dataLength, uint32_t *sentLength)
660 CAResult_t result = CA_SEND_FAILED;
662 // Send the first segment with the header.
663 if ((NULL != remoteAddress) && (0 < strlen(remoteAddress))) //Unicast data
665 result = CAEDRClientSendUnicastData(remoteAddress, serviceUUID, data,
666 dataLength, sentLength);
667 if (CA_STATUS_OK != result)
669 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send unicast data !");
675 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "sending multicast data : %s", data);
676 result = CAEDRClientSendMulticastData(serviceUUID, data, dataLength,
679 if (CA_STATUS_OK != result)
681 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send multicast data !");
688 void CAAdapterDataReceiverHandler(void *context)
690 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN_CAAdapterDataReceiverHandler");
692 CAEDRData *message = (CAEDRData *) context;
695 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get message!");
699 static uint32_t recvDataLen = 0;
700 static uint32_t totalDataLen = 0;
701 static char *defragData = NULL;
702 static CAEndpoint_t *remoteEndpoint = NULL;
704 if (!g_isHeaderAvailable)
706 totalDataLen = CAParseHeader((char*)message->data);
707 if (0 == totalDataLen)
709 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "totalDataLen is zero");
713 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
715 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
718 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "defragData is null");
722 const char *remoteAddress = message->remoteEndpoint->addr;
724 remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_RFCOMM_BTEDR,
727 memcpy(defragData + recvDataLen, message->data + CA_HEADER_LENGTH,
728 message->dataLen - CA_HEADER_LENGTH);
729 recvDataLen += message->dataLen - CA_HEADER_LENGTH;
730 g_isHeaderAvailable = true;
734 memcpy(defragData + recvDataLen, message->data, message->dataLen);
735 recvDataLen += message->dataLen ;
738 if (totalDataLen == recvDataLen)
740 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Sending data up !");
741 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
744 remoteEndpoint = NULL;
745 g_isHeaderAvailable = false;
748 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT_CAAdapterDataReceiverHandler");
751 CAResult_t CAAdapterStartQueue()
753 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
754 // Start send queue thread
755 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
757 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Start Send Data Thread");
758 CAEDRClientUnsetCallbacks();
759 //Disconnect all the client connections
760 CAEDRClientDisconnectAll();
761 return CA_STATUS_FAILED;
764 // Start receive queue thread
765 if (CA_STATUS_OK != CAQueueingThreadStart(g_recvQueueHandle))
767 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Start Receive Data Thread");
768 CAEDRClientUnsetCallbacks();
769 //Disconnect all the client connections
770 CAEDRClientDisconnectAll();
771 return CA_STATUS_FAILED;
773 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
777 CAResult_t CAAdapterStopQueue()
779 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
780 //Stop send queue thread
781 CAQueueingThreadStop(g_sendQueueHandle);
783 //Stop receive queue thread
784 CAQueueingThreadStop(g_recvQueueHandle);
785 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
789 void CAAdapterRecvData(const char *remoteAddress, const void *data, uint32_t dataLength,
790 uint32_t *sentLength)
792 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
794 if (false == g_adapterState)
796 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
802 VERIFY_NON_NULL_VOID(data, EDR_ADAPTER_TAG, "Data is null");
803 VERIFY_NON_NULL_VOID(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
805 // Create remote endpoint
806 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
807 CA_ADAPTER_RFCOMM_BTEDR,
809 if (NULL == remoteEndpoint)
811 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create remote endpoint !");
815 // Add message to data queue
816 CAEDRData *edrData = CACreateEDRData(remoteEndpoint, data, dataLength);
817 CAQueueingThreadAddData(g_recvQueueHandle, edrData, sizeof(CAEDRData));
818 *sentLength = dataLength;
820 // Free remote endpoint
821 CAFreeEndpoint(remoteEndpoint);
823 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
826 void CAEDRErrorHandler(const char *remoteAddress, const char *serviceUUID, const void *data,
827 uint32_t dataLength, CAResult_t result)
829 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
832 VERIFY_NON_NULL_VOID(data, EDR_ADAPTER_TAG, "Data is null");
834 // Create remote endpoint
835 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(0, CA_ADAPTER_RFCOMM_BTEDR,
839 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create remote endpoint !");
843 g_errorCallback(remoteEndpoint, data, dataLength, result);
845 // Free remote endpoint
846 CAFreeEndpoint(remoteEndpoint);
848 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
851 CAResult_t CAAdapterSendData(const char *remoteAddress, const char *serviceUUID, const void *data,
852 uint32_t dataLength, uint32_t *sentLength)
854 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN - CAAdapterSendData");
856 if (false == g_adapterState)
858 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
860 return CA_ADAPTER_NOT_ENABLED;
863 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
864 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
865 VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
867 // Create remote endpoint
868 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
869 CA_ADAPTER_RFCOMM_BTEDR,
871 if (NULL == remoteEndpoint)
873 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create remote endpoint !");
874 return CA_MEMORY_ALLOC_FAILED;
877 // Add message to data queue
878 CAEDRData *edrData = CACreateEDRData(remoteEndpoint, data, dataLength);
879 CAQueueingThreadAddData(g_sendQueueHandle, edrData, sizeof (CAEDRData));
880 *sentLength = dataLength;
882 // Free remote endpoint
883 CAFreeEndpoint(remoteEndpoint);
885 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT - CAAdapterSendData");
889 void CAEDRNotifyNetworkStatus(CANetworkStatus_t status)
891 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
893 // Create localconnectivity
894 if (NULL == g_localConnectivity)
896 CAEDRGetInterfaceInformation(&g_localConnectivity);
899 if (CA_INTERFACE_UP == status)
901 if (false == g_adapterState)
903 // Get Bluetooth adapter state
904 bool adapterState = false;
905 if (CA_STATUS_OK != CAEDRGetAdapterEnableState(&adapterState))
907 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get adapter enable state");
911 if (false== adapterState)
913 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
914 g_adapterState = false;
917 CAEDRClientSetCallbacks();
918 g_adapterState = true;
919 CAAdapterStartQueue();
920 // starting RFCommServer
921 if (true == g_serverState)
924 g_serverState = false;
930 g_adapterState = false;
933 // Notify to upper layer
934 if (g_networkChangeCallback && g_localConnectivity && g_edrThreadPool)
936 // Add notification task to thread pool
937 CAEDRNetworkEvent *event = CAEDRCreateNetworkEvent(g_localConnectivity, status);
940 if (CA_STATUS_OK != ca_thread_pool_add_task(g_edrThreadPool,
941 CAEDROnNetworkStatusChanged,event))
943 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create threadpool!");
949 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
952 void CAEDROnNetworkStatusChanged(void *context)
954 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
958 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "context is NULL!");
962 CAEDRNetworkEvent *networkEvent = (CAEDRNetworkEvent *) context;
964 // Notify to upper layer
965 if (g_networkChangeCallback)
967 g_networkChangeCallback(networkEvent->info, networkEvent->status);
970 // Free the created Network event
971 CAEDRFreeNetworkEvent(networkEvent);
973 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
976 CAEDRNetworkEvent *CAEDRCreateNetworkEvent(CAEndpoint_t *connectivity,
977 CANetworkStatus_t status)
979 VERIFY_NON_NULL_RET(connectivity, EDR_ADAPTER_TAG, "connectivity is NULL", NULL);
981 // Create CAEDRNetworkEvent
982 CAEDRNetworkEvent *event = (CAEDRNetworkEvent *) OICMalloc(sizeof(CAEDRNetworkEvent));
985 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to allocate memory to network event!");
989 // Create duplicate of Local connectivity
990 event->info = CACloneEndpoint(connectivity);
991 event->status = status;
995 void CAEDRFreeNetworkEvent(CAEDRNetworkEvent *event)
999 CAFreeEndpoint(event->info);
1004 CAEDRData *CACreateEDRData(const CAEndpoint_t *remoteEndpoint,
1005 const void *data, uint32_t dataLength)
1007 CAEDRData *edrData = (CAEDRData *)OICMalloc(sizeof (CAEDRData));
1010 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
1014 edrData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1016 edrData->data = OICMalloc(dataLength);
1017 if (NULL == edrData->data)
1019 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
1020 CAFreeEDRData(edrData);
1023 memcpy(edrData->data, data, dataLength);
1024 edrData->dataLen = dataLength;
1029 void CAFreeEDRData(CAEDRData *edrData)
1031 VERIFY_NON_NULL_VOID(edrData, EDR_ADAPTER_TAG, "edrData is NULL");
1033 CAFreeEndpoint(edrData->remoteEndpoint);
1034 OICFree(edrData->data);
1038 void CAEDRDataDestroyer(void *data, uint32_t size)
1040 CAEDRData *edrdata = (CAEDRData *) data;
1042 CAFreeEDRData(edrdata);