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 ******************************************************************/
22 * @file caedradapter.c
23 * @brief This file contains the APIs for EDR adapters to be implemented
26 #include "caedradapter.h"
27 #include "cabtmanager.h"
28 #include "cabtutils.h"
29 #include "caadapterutils.h"
32 static int32_t gDiscoveryServerID = -1;
33 static int32_t gListeningServerID = -1;
34 static int32_t gNotificationServerID = -1;
36 static CAResult_t CAStartServer(const char *serviceUUID, int32_t *serverID);
38 CAResult_t CAInitializeEDR(CARegisterConnectivityCallback registerCallback,
39 CANetworkPacketReceivedCallback packetReceivedCallback,
40 CANetworkChangeCallback networkStateChangeCallback,
41 u_thread_pool_t handle)
43 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
45 CAResult_t err = CA_STATUS_OK;
48 VERIFY_NON_NULL(registerCallback, BLUETOOTH_ADAPTER_TAG,
49 "register callback is NULL");
50 VERIFY_NON_NULL(packetReceivedCallback, BLUETOOTH_ADAPTER_TAG,
51 "data receive callback is NULL");
52 VERIFY_NON_NULL(networkStateChangeCallback, BLUETOOTH_ADAPTER_TAG,
53 "network state change callback is NULL");
54 VERIFY_NON_NULL(handle, BLUETOOTH_ADAPTER_TAG, "Thread pool hanlde is NULL");
56 //Register the callbacks with BT Manager
57 CABTManagerSetPacketReceivedCallback(packetReceivedCallback);
58 CABTManagerSetNetworkChangeCallback(networkStateChangeCallback);
60 //Initialize BT Manager
61 err = CABTManagerInitialize(handle);
62 if (CA_STATUS_OK != err && CA_ADAPTER_NOT_ENABLED != err)
64 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "BT Manger initialize failed!, error number [%d]",
69 CAConnectivityHandler_t handler;
70 handler.startAdapter = CAStartEDR;
71 handler.startListenServer = CAStartEDRListeningServer;
72 handler.startDiscoverServer = CAStartEDRDiscoveryServer;
73 handler.sendData = CASendEDRUnicastData;
74 handler.sendDataToAll = CASendEDRMulticastData;
75 handler.GetnetInfo = CAGetEDRInterfaceInformation;
76 handler.readData = CAReadEDRData;
77 handler.stopAdapter = CAStopEDR;
78 handler.terminate = CATerminateEDR;
79 registerCallback(handler, CA_EDR);
81 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
85 CAResult_t CAStartEDR(void)
87 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
89 CAResult_t err = CA_STATUS_OK;
91 if (CA_STATUS_OK != (err = CABTManagerStart()))
93 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "BT Manger start failed!, error number [%d] ",
97 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
101 CAResult_t CAStartEDRListeningServer(void)
103 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
105 return CAStartServer(OIC_BT_SERVICE_ID, &gListeningServerID);
108 CAResult_t CAStartEDRDiscoveryServer(void)
110 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
112 return CAStartServer(OIC_BT_SERVICE_ID, &gDiscoveryServerID);
115 uint32_t CASendEDRUnicastData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
118 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
120 CAResult_t err = CA_STATUS_OK;
123 VERIFY_NON_NULL_RET(remoteEndpoint, BLUETOOTH_ADAPTER_TAG, "Remote endpoint is null", 0);
124 VERIFY_NON_NULL_RET(data, BLUETOOTH_ADAPTER_TAG, "Data is null", 0);
126 if (0 == strlen(remoteEndpoint->addressInfo.BT.btMacAddress))
128 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: BT Address is empty!");
134 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: data length is zero!");
138 uint32_t sentLength = 0;
139 const char *serviceUUID = OIC_BT_SERVICE_ID;
140 const char *address = remoteEndpoint->addressInfo.BT.btMacAddress;
141 if (CA_STATUS_OK != (err = CABTManagerSendData(address, serviceUUID, data,
142 dataLength, &sentLength)))
144 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Send unicast data failed!, error num [%d]", err);
148 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
152 uint32_t CASendEDRMulticastData(void *data, uint32_t dataLength)
154 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
156 CAResult_t err = CA_STATUS_OK;
159 VERIFY_NON_NULL_RET(data, BLUETOOTH_ADAPTER_TAG, "Data is null", 0);
163 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: data length is zero!");
167 uint32_t sentLen = 0;
168 const char *serviceUUID = OIC_BT_SERVICE_ID;
169 if (CA_STATUS_OK != (err = CABTManagerSendData(NULL, serviceUUID, data, dataLength,
172 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Send multicast data failed!, error num [%d]",
177 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
182 CAResult_t CAGetEDRInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
184 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
186 VERIFY_NON_NULL(info, BLUETOOTH_ADAPTER_TAG, "LocalConnectivity info is null");
188 CAResult_t err = CA_STATUS_OK;
190 if (CA_STATUS_OK != (err = CABTManagerGetInterface(info)))
192 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG,
193 "Failed to get local interface information!, error num [%d]", err);
198 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
202 CAResult_t CAReadEDRData(void)
204 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
206 return CABTManagerReadData();
209 CAResult_t CAStopEDR(void)
211 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
213 //Stop the Discovery server
214 if (-1 < gDiscoveryServerID)
216 CABTManagerStopServer(gDiscoveryServerID);
219 //Stop the Listening server
220 if (-1 < gListeningServerID)
222 CABTManagerStopServer(gListeningServerID);
225 //Stop the Notification server
226 if (-1 < gNotificationServerID)
228 CABTManagerStopServer(gNotificationServerID);
234 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
238 void CATerminateEDR(void)
240 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
242 //Terminate BT Manager
243 CABTManagerTerminate();
245 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
248 CAResult_t CAStartServer(const char *serviceUUID, int32_t *serverID)
250 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
252 CAResult_t err = CA_STATUS_OK;
255 VERIFY_NON_NULL(serviceUUID, BLUETOOTH_ADAPTER_TAG, "Service UUID is NULL");
256 VERIFY_NON_NULL(serverID, BLUETOOTH_ADAPTER_TAG, "Server ID is NULL");
257 if (0 == strlen(serviceUUID))
259 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: Service UUID is empty!");
260 return CA_STATUS_INVALID_PARAM;
263 if (CA_STATUS_OK != (err = CABTManagerStartServer(serviceUUID, serverID)))
265 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to start RFCOMM server!, error num [%d]",
270 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");