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 "camsgparser.h"
33 #include "caqueueingthread.h"
34 #include "oic_malloc.h"
37 * @var EDR_ADAPTER_TAG
38 * @brief Logging tag for module name.
40 #define EDR_ADAPTER_TAG "CA_EDR_ADAPTER"
43 * @var g_edrThreadPool
44 * @brief Reference to threadpool.
46 static ca_thread_pool_t g_edrThreadPool = NULL;
49 * @var g_sendQueueHandle
50 * @brief Queue handle for Send Data
52 static CAQueueingThread_t *g_sendQueueHandle = NULL;
55 * @var g_recvQueueHandle
56 * @brief Queue handle for Receive Data
58 static CAQueueingThread_t *g_recvQueueHandle = NULL;
61 * @var g_isHeaderAvailable
62 * @brief to differentiate btw header and data packet.
64 static bool g_isHeaderAvailable = false;
68 * @brief Storing Adapter state information
70 static bool g_adapterState = true;
73 * @var g_networkPacketReceivedCallback
74 * @brief Maintains the callback to be notified on receival of network packets from other
77 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
80 * @var g_networkChangeCallback
81 * @brief Maintains the callback to be notified on local bluetooth adapter status change.
83 static CANetworkChangeCallback g_networkChangeCallback = NULL;
86 * @var g_localConnectivity
87 * @brief Information of local Bluetooth adapter.
89 static CAEndpoint_t *g_localConnectivity = NULL;
93 * @brief Storing RfcommserverUUID
95 static int g_serverId = -1;
99 * @brief Storing Rfcommserver state information
101 static bool g_serverState = false;
103 static CAResult_t CAStartServer();
104 static CAResult_t CAEDRInitializeQueueHandlers();
105 CAResult_t CAEDRInitializeSendHandler();
106 CAResult_t CAEDRInitializeReceiveHandler();
107 void CAAdapterTerminateQueues();
108 void CAAdapterDataSendHandler(void *context);
109 void CAAdapterDataReceiverHandler(void *context);
110 CAResult_t CAAdapterStopQueue();
111 void CAAdapterRecvData(const char *remoteAddress, const void *data, uint32_t dataLength,
112 uint32_t *sentLength);
113 void CAEDRNotifyNetworkStatus(CANetworkStatus_t status);
114 void CAEDROnNetworkStatusChanged(void *context);
115 CAResult_t CAAdapterSendData(const char *remoteAddress, const char *serviceUUID, const void *data,
116 uint32_t dataLength, uint32_t *sentLength);
117 CAEDRNetworkEvent *CAEDRCreateNetworkEvent(CAEndpoint_t *connectivity,
118 CANetworkStatus_t status);
119 CAResult_t CAEDRClientSendData(const char *remoteAddress, const char *serviceUUID,
120 const void *data, uint32_t dataLength, uint32_t *sentLength);
122 * @fn CACreateEDRData
123 * @brief Helper function to create CAEDRData
125 static CAEDRData *CACreateEDRData(const CAEndpoint_t *remoteEndpoint, const void *data,
126 uint32_t dataLength);
130 * @brief Free the Created EDR data
132 static void CAFreeEDRData(CAEDRData *edrData);
135 * @fn CAEDRFreeNetworkEvent
136 * @brief Free the memory associated with @event.
138 void CAEDRFreeNetworkEvent(CAEDRNetworkEvent *event);
140 static void CAEDRDataDestroyer(void *data, uint32_t size);
142 CAResult_t CAInitializeEDR(CARegisterConnectivityCallback registerCallback,
143 CANetworkPacketReceivedCallback packetReceivedCallback,
144 CANetworkChangeCallback networkStateChangeCallback,
145 ca_thread_pool_t handle)
147 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
150 VERIFY_NON_NULL(registerCallback, EDR_ADAPTER_TAG, "register callback is NULL");
151 VERIFY_NON_NULL(packetReceivedCallback, EDR_ADAPTER_TAG, "data receive callback is NULL");
152 VERIFY_NON_NULL(networkStateChangeCallback, EDR_ADAPTER_TAG,
153 "network state change callback is NULL");
154 VERIFY_NON_NULL(handle, EDR_ADAPTER_TAG, "Thread pool handle is NULL");
156 // Register the callbacks
158 g_edrThreadPool = handle;
159 g_networkPacketReceivedCallback = packetReceivedCallback;
160 g_networkChangeCallback = networkStateChangeCallback;
162 // Initialize EDR Network Monitor
163 CAResult_t err = CAEDRInitializeNetworkMonitor(handle);
164 if (CA_STATUS_OK != err)
166 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "EDR N/w Monitor Initialize failed!, error number [%d]",
171 CAEDRSetNetworkChangeCallback(CAEDRNotifyNetworkStatus);
172 CAEDRSetPacketReceivedCallback(CAAdapterRecvData);
173 CAEDRInitializeClient(handle);
175 CAConnectivityHandler_t handler;
176 handler.startAdapter = CAStartEDR;
177 handler.startListenServer = CAStartEDRListeningServer;
178 handler.startDiscoveryServer = CAStartEDRDiscoveryServer;
179 handler.sendData = CASendEDRUnicastData;
180 handler.sendDataToAll = CASendEDRMulticastData;
181 handler.GetnetInfo = CAGetEDRInterfaceInformation;
182 handler.readData = CAReadEDRData;
183 handler.stopAdapter = CAStopEDR;
184 handler.terminate = CATerminateEDR;
185 registerCallback(handler, CA_ADAPTER_RFCOMM_BTEDR);
187 // Initialize Send/Receive data message queues
188 if (CA_STATUS_OK != CAEDRInitializeQueueHandlers())
190 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "CAAdapterInitializeQueues API failed");
192 return CA_STATUS_FAILED;
195 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
199 CAResult_t CAStartEDR()
201 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
203 //Start Monitoring EDR Network
204 CAResult_t ret = CAEDRStartNetworkMonitor();
205 if (CA_STATUS_OK != ret)
207 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Start n/w monitor");
210 // Get Bluetooth adapter state
211 bool adapterState = false;
212 if (CA_STATUS_OK != CAEDRGetAdapterEnableState(&adapterState))
214 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get adapter enable state");
215 return CA_STATUS_FAILED;
218 if (false == adapterState)
220 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
221 g_adapterState = false;
222 return CA_ADAPTER_NOT_ENABLED;
225 if (CA_STATUS_OK != (ret = CAEDRClientSetCallbacks()))
227 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Start Network Monitor failed!, error number [%d] ",
231 if (CA_STATUS_OK != (ret = CAAdapterStartQueue()))
233 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "CAAdapterStartQueue failed!, error number [%d] ", ret);
236 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
240 CAResult_t CAStartEDRListeningServer()
242 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
244 return CAStartServer();
247 CAResult_t CAStartEDRDiscoveryServer()
249 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
251 return CAStartServer();
254 int32_t CASendEDRUnicastData(const CAEndpoint_t *remoteEndpoint, const void *data,
257 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
260 VERIFY_NON_NULL_RET(remoteEndpoint, EDR_ADAPTER_TAG, "Remote endpoint is null", -1);
261 VERIFY_NON_NULL_RET(data, EDR_ADAPTER_TAG, "Data is null", -1);
265 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: data length is zero!");
269 if (0 == strlen(remoteEndpoint->addr))
271 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: EDR Address is empty!");
275 uint32_t sentLength = 0;
276 const char *serviceUUID = OIC_EDR_SERVICE_ID;
277 const char *address = remoteEndpoint->addr;
278 CAResult_t err = CAAdapterSendData(address, serviceUUID, data, dataLength, &sentLength);
279 if (CA_STATUS_OK != err)
281 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Send unicast data failed!, error num [%d]", err);
285 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
289 int32_t CASendEDRMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLength)
291 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN - CASendEDRMulticastData");
294 VERIFY_NON_NULL_RET(data, EDR_ADAPTER_TAG, "Data is null", -1);
298 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: data length is zero!");
302 uint32_t sentLen = 0;
303 const char *serviceUUID = OIC_EDR_SERVICE_ID;
304 CAResult_t err = CAAdapterSendData(NULL, serviceUUID, data, dataLength, &sentLen);
305 if (CA_STATUS_OK != err)
307 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Send multicast data failed!, error num [%d]", err);
311 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT - CASendEDRMulticastData");
315 CAResult_t CAGetEDRInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
317 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
319 VERIFY_NON_NULL(info, EDR_ADAPTER_TAG, "LocalConnectivity info is null");
321 CAResult_t err = CA_STATUS_OK;
323 if (CA_STATUS_OK != (err = CAEDRGetInterfaceInformation(info)))
325 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
326 "Failed to get local interface information!, error num [%d]", err);
331 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
335 CAResult_t CAReadEDRData()
337 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
339 return CAEDRManagerReadData();
342 CAResult_t CAStopEDR()
344 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
346 // Stop RFComm server if it is running
347 if (-1 != g_serverId)
349 CAEDRServerStop(g_serverId);
353 // Stop network monitor
354 CAEDRStopNetworkMonitor();
357 CAEDRClientUnsetCallbacks();
359 // Disconnect all the client connections
360 CAEDRClientDisconnectAll();
362 // Stop Send and receive Queue
363 CAAdapterStopQueue();
365 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
369 void CATerminateEDR()
371 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
375 // Terminate EDR Network Monitor
376 CAEDRTerminateNetworkMonitor();
378 // Terminate Send/Receive data messages queues
379 CAAdapterTerminateQueues();
381 g_networkPacketReceivedCallback = NULL;
382 g_networkChangeCallback = NULL;
384 // Terminate thread pool
385 g_edrThreadPool = NULL;
387 // Terminate EDR Client
388 CAEDRClientTerminate();
390 // Terminate EDR Server
391 CAEDRServerTerminate();
393 // Free LocalConnectivity information
394 CAAdapterFreeEndpoint(g_localConnectivity);
395 g_localConnectivity = NULL;
397 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
400 CAResult_t CAStartServer()
402 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
404 CAResult_t err = CA_STATUS_OK;
406 if (false == g_adapterState)
408 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
409 // Setting g_serverState for starting Rfcommserver when adapter starts
410 g_serverState = true;
416 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Server is already in running state.");
417 return CA_SERVER_STARTED_ALREADY;
420 if (CA_STATUS_OK != (err = CAEDRServerStart(OIC_EDR_SERVICE_ID, &g_serverId, g_edrThreadPool)))
422 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to start RFCOMM server!, error num [%d]",
427 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
431 CAResult_t CAEDRInitializeQueueHandlers()
433 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
435 if (CA_STATUS_OK == CAEDRInitializeSendHandler()
436 && CA_STATUS_OK == CAEDRInitializeReceiveHandler())
438 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Queue is initialized!");
442 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
443 return CA_STATUS_FAILED;
446 CAResult_t CAEDRInitializeSendHandler()
448 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
449 // Check if the message queue is already initialized
450 if (g_sendQueueHandle)
452 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Already queue is initialized!");
456 g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
457 if (!g_sendQueueHandle)
459 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
460 return CA_MEMORY_ALLOC_FAILED;
463 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_edrThreadPool,
464 CAAdapterDataSendHandler, CAEDRDataDestroyer))
466 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Initialize send queue thread");
467 return CA_STATUS_FAILED;
472 CAResult_t CAEDRInitializeReceiveHandler()
474 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
475 // Check if the message queue is already initialized
476 if (g_recvQueueHandle)
478 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Already queue is initialized!");
482 g_recvQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
483 if (!g_recvQueueHandle)
485 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
486 return CA_MEMORY_ALLOC_FAILED;
489 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_recvQueueHandle, g_edrThreadPool,
490 CAAdapterDataReceiverHandler,
493 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Initialize send queue thread");
494 return CA_STATUS_FAILED;
497 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
501 void CAAdapterTerminateQueues()
503 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
505 if (g_sendQueueHandle)
507 CAQueueingThreadDestroy(g_sendQueueHandle);
508 g_sendQueueHandle = NULL;
510 if (g_recvQueueHandle)
512 CAQueueingThreadDestroy(g_recvQueueHandle);
513 g_recvQueueHandle = NULL;
516 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
519 void CAAdapterDataSendHandler(void *context)
521 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN - CAAdapterDataSendHandler");
523 CAEDRData *message = (CAEDRData *) context;
526 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get message!");
530 const char *remoteAddress = NULL;
531 const char *serviceUUID = OIC_EDR_SERVICE_ID;
532 uint32_t sentLength = 0;
534 if (NULL == message->remoteEndpoint)
536 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "remoteEndpoint is not available");
540 remoteAddress = message->remoteEndpoint->addr;
543 uint32_t dataSegmentLength = message->dataLen + CA_HEADER_LENGTH;
544 uint32_t dataLen = message->dataLen;
545 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "checking for fragmentation and the dataLen is %d",
548 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
549 VERIFY_NON_NULL_VOID(header, EDR_ADAPTER_TAG, "Memory allocation failed");
551 char* dataSegment = (char *) OICCalloc(dataSegmentLength, sizeof(char));
552 if (NULL == dataSegment)
554 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed");
559 CAResult_t result = CAGenerateHeader(header, dataLen);
560 if (CA_STATUS_OK != result)
562 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Generate header failed");
564 OICFree(dataSegment);
568 memcpy(dataSegment, header, CA_HEADER_LENGTH);
571 if (CA_SUPPORTED_EDR_MTU_SIZE > dataSegmentLength)
573 length = dataSegmentLength;
574 memcpy(dataSegment + CA_HEADER_LENGTH, message->data, dataLen);
578 length = CA_SUPPORTED_EDR_MTU_SIZE;
579 memcpy(dataSegment + CA_HEADER_LENGTH, message->data,
580 CA_SUPPORTED_EDR_MTU_SIZE - CA_HEADER_LENGTH);
583 uint32_t iter = dataSegmentLength / CA_SUPPORTED_EDR_MTU_SIZE;
585 if (CA_STATUS_OK != CAEDRClientSendData(remoteAddress, serviceUUID, dataSegment, length,
588 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "CAEDRClientSendData API failed");
589 OICFree(dataSegment);
592 OICFree(dataSegment);
594 for (index = 1; index < iter; index++)
596 // Send the remaining header.
597 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Sending the chunk number [%d]", index);
599 if (CA_STATUS_OK != CAEDRClientSendData(remoteAddress, serviceUUID,
600 message->data + ((index * CA_SUPPORTED_EDR_MTU_SIZE) - CA_HEADER_LENGTH),
601 CA_SUPPORTED_EDR_MTU_SIZE, &sentLength))
603 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "CAEDRClientSendData API failed");
608 uint32_t remainingLen = dataSegmentLength % CA_SUPPORTED_EDR_MTU_SIZE;
609 if (remainingLen && (dataSegmentLength > CA_SUPPORTED_EDR_MTU_SIZE))
611 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
612 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Sending the last chunk");
613 if (CA_STATUS_OK != CAEDRClientSendData(remoteAddress, serviceUUID,
614 message->data + (index * CA_SUPPORTED_EDR_MTU_SIZE) - CA_HEADER_LENGTH,
615 remainingLen, &sentLength))
617 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "CAEDRClientSendData API failed");
622 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
625 CAResult_t CAEDRClientSendData(const char *remoteAddress, const char *serviceUUID,
626 const void *data, uint32_t dataLength, uint32_t *sentLength)
629 // Send the first segment with the header.
630 if ((NULL != remoteAddress) && (0 < strlen(remoteAddress))) //Unicast data
632 if (CA_STATUS_OK != CAEDRClientSendUnicastData(remoteAddress, serviceUUID, data,
633 dataLength, sentLength))
635 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send unicast data !");
636 return CA_STATUS_FAILED;
641 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "sending multicast data : %s", data);
642 if (CA_STATUS_OK != CAEDRClientSendMulticastData(serviceUUID, data, dataLength,
645 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send multicast data !");
646 return CA_STATUS_FAILED;
652 void CAAdapterDataReceiverHandler(void *context)
654 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN_CAAdapterDataReceiverHandler");
656 CAEDRData *message = (CAEDRData *) context;
659 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get message!");
663 static uint32_t recvDataLen = 0;
664 static uint32_t totalDataLen = 0;
665 static char *defragData = NULL;
666 static CAEndpoint_t *remoteEndpoint = NULL;
668 if (!g_isHeaderAvailable)
670 totalDataLen = CAParseHeader((char*)message->data);
671 if (0 == totalDataLen)
673 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "totalDataLen is zero");
677 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
679 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
682 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "defragData is null");
686 const char *remoteAddress = message->remoteEndpoint->addr;
687 uint16_t port = message->remoteEndpoint->port;
689 remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_RFCOMM_BTEDR, remoteAddress, port);
691 memcpy(defragData + recvDataLen, message->data + CA_HEADER_LENGTH,
692 message->dataLen - CA_HEADER_LENGTH);
693 recvDataLen += message->dataLen - CA_HEADER_LENGTH;
694 g_isHeaderAvailable = true;
698 memcpy(defragData + recvDataLen, message->data, message->dataLen);
699 recvDataLen += message->dataLen ;
702 if (totalDataLen == recvDataLen)
704 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Sending data up !");
705 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
708 remoteEndpoint = NULL;
709 g_isHeaderAvailable = false;
712 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT_CAAdapterDataReceiverHandler");
715 CAResult_t CAAdapterStartQueue()
717 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
718 // Start send queue thread
719 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
721 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Start Send Data Thread");
722 CAEDRClientUnsetCallbacks();
723 //Disconnect all the client connections
724 CAEDRClientDisconnectAll();
725 return CA_STATUS_FAILED;
728 // Start receive queue thread
729 if (CA_STATUS_OK != CAQueueingThreadStart(g_recvQueueHandle))
731 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Start Receive Data Thread");
732 CAEDRClientUnsetCallbacks();
733 //Disconnect all the client connections
734 CAEDRClientDisconnectAll();
735 return CA_STATUS_FAILED;
737 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
741 CAResult_t CAAdapterStopQueue()
743 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
744 //Stop send queue thread
745 CAQueueingThreadStop(g_sendQueueHandle);
747 //Stop receive queue thread
748 CAQueueingThreadStop(g_recvQueueHandle);
749 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
753 void CAAdapterRecvData(const char *remoteAddress, const void *data, uint32_t dataLength,
754 uint32_t *sentLength)
756 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
758 if (false == g_adapterState)
760 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
766 VERIFY_NON_NULL_VOID(data, EDR_ADAPTER_TAG, "Data is null");
767 VERIFY_NON_NULL_VOID(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
769 // Create remote endpoint
770 CAEndpoint_t *remoteEndpoint = CAAdapterCreateEndpoint(0,
771 CA_ADAPTER_RFCOMM_BTEDR,
774 if (NULL == remoteEndpoint)
776 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create remote endpoint !");
780 // Add message to data queue
781 CAEDRData *edrData = CACreateEDRData(remoteEndpoint, data, dataLength);
782 CAQueueingThreadAddData(g_recvQueueHandle, edrData, sizeof(CAEDRData));
783 *sentLength = dataLength;
785 // Free remote endpoint
786 CAAdapterFreeEndpoint(remoteEndpoint);
788 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
791 CAResult_t CAAdapterSendData(const char *remoteAddress, const char *serviceUUID, const void *data,
792 uint32_t dataLength, uint32_t *sentLength)
794 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN - CAAdapterSendData");
796 if (false == g_adapterState)
798 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
803 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
804 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
805 VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
807 // Create remote endpoint
808 CAEndpoint_t *remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_RFCOMM_BTEDR, remoteAddress, 0);
809 if (NULL == remoteEndpoint)
811 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create remote endpoint !");
812 return CA_STATUS_FAILED;
815 // Add message to data queue
816 CAEDRData *edrData = CACreateEDRData(remoteEndpoint, data, dataLength);
817 CAQueueingThreadAddData(g_sendQueueHandle, edrData, sizeof (CAEDRData));
818 *sentLength = dataLength;
820 // Free remote endpoint
821 CAAdapterFreeEndpoint(remoteEndpoint);
823 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT - CAAdapterSendData");
827 void CAEDRNotifyNetworkStatus(CANetworkStatus_t status)
829 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
831 // Create localconnectivity
832 if (NULL == g_localConnectivity)
834 CAEDRGetInterfaceInformation(&g_localConnectivity);
837 if (CA_INTERFACE_UP == status)
839 if (false == g_adapterState)
841 // Get Bluetooth adapter state
842 bool adapterState = false;
843 if (CA_STATUS_OK != CAEDRGetAdapterEnableState(&adapterState))
845 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to get adapter enable state");
849 if (false== adapterState)
851 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!");
852 g_adapterState = false;
855 CAEDRClientSetCallbacks();
856 g_adapterState = true;
857 CAAdapterStartQueue();
858 // starting RFCommServer
859 if (true == g_serverState)
862 g_serverState = false;
868 g_adapterState = false;
871 // Notify to upper layer
872 if (g_networkChangeCallback && g_localConnectivity && g_edrThreadPool)
874 // Add notification task to thread pool
875 CAEDRNetworkEvent *event = CAEDRCreateNetworkEvent(g_localConnectivity, status);
878 if (CA_STATUS_OK != ca_thread_pool_add_task(g_edrThreadPool,
879 CAEDROnNetworkStatusChanged,event))
881 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create threadpool!");
887 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
890 void CAEDROnNetworkStatusChanged(void *context)
892 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
896 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "context is NULL!");
900 CAEDRNetworkEvent *networkEvent = (CAEDRNetworkEvent *) context;
902 // Notify to upper layer
903 if (g_networkChangeCallback)
905 g_networkChangeCallback(networkEvent->info, networkEvent->status);
908 // Free the created Network event
909 CAEDRFreeNetworkEvent(networkEvent);
911 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
914 CAEDRNetworkEvent *CAEDRCreateNetworkEvent(CAEndpoint_t *connectivity,
915 CANetworkStatus_t status)
917 VERIFY_NON_NULL_RET(connectivity, EDR_ADAPTER_TAG, "connectivity is NULL", NULL);
919 // Create CAEDRNetworkEvent
920 CAEDRNetworkEvent *event = (CAEDRNetworkEvent *) OICMalloc(sizeof(CAEDRNetworkEvent));
923 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to allocate memory to network event!");
927 // Create duplicate of Local connectivity
928 event->info = CAAdapterCloneEndpoint(connectivity);
929 event->status = status;
933 void CAEDRFreeNetworkEvent(CAEDRNetworkEvent *event)
937 CAAdapterFreeEndpoint(event->info);
942 CAEDRData *CACreateEDRData(const CAEndpoint_t *remoteEndpoint,
943 const void *data, uint32_t dataLength)
945 CAEDRData *edrData = (CAEDRData *)OICMalloc(sizeof (CAEDRData));
948 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
952 edrData->remoteEndpoint = CAAdapterCloneEndpoint(remoteEndpoint);
954 edrData->data = OICMalloc(dataLength);
955 if (NULL == edrData->data)
957 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Memory allocation failed!");
958 CAFreeEDRData(edrData);
961 memcpy(edrData->data, data, dataLength);
962 edrData->dataLen = dataLength;
967 void CAFreeEDRData(CAEDRData *edrData)
969 VERIFY_NON_NULL_VOID(edrData, EDR_ADAPTER_TAG, "edrData is NULL");
971 CAAdapterFreeEndpoint(edrData->remoteEndpoint);
972 OICFree(edrData->data);
976 void CAEDRDataDestroyer(void *data, uint32_t size)
978 CAEDRData *edrdata = (CAEDRData *) data;
980 CAFreeEDRData(edrdata);