Implementation of connectivity abstraction feature Release v0.3
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / tizen / caedradapter.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file caedradapter.c
23  * @brief This file contains the APIs for EDR adapters to be implemented
24  */
25
26 #include "caedradapter.h"
27 #include "cabtmanager.h"
28 #include "cabtutils.h"
29 #include "caadapterutils.h"
30 #include "logger.h"
31
32 static int32_t gDiscoveryServerID = -1;
33 static int32_t gListeningServerID = -1;
34 static int32_t gNotificationServerID = -1;
35
36 static CAResult_t CAStartServer(const char *serviceUUID, int32_t *serverID);
37
38 CAResult_t CAInitializeEDR(CARegisterConnectivityCallback registerCallback,
39                            CANetworkPacketReceivedCallback packetReceivedCallback,
40                            CANetworkChangeCallback networkStateChangeCallback,
41                            u_thread_pool_t handle)
42 {
43     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
44
45     CAResult_t err = CA_STATUS_OK;
46
47     //Input validation
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");
55
56     //Register the callbacks with BT Manager
57     CABTManagerSetPacketReceivedCallback(packetReceivedCallback);
58     CABTManagerSetNetworkChangeCallback(networkStateChangeCallback);
59
60     //Initialize BT Manager
61     err = CABTManagerInitialize(handle);
62     if (CA_STATUS_OK != err && CA_ADAPTER_NOT_ENABLED != err)
63     {
64         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "BT Manger initialize failed!, error number [%d]",
65                   err);
66         return err;
67     }
68
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);
80
81     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
82     return err;
83 }
84
85 CAResult_t CAStartEDR(void)
86 {
87     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
88
89     CAResult_t err = CA_STATUS_OK;
90
91     if (CA_STATUS_OK != (err = CABTManagerStart()))
92     {
93         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "BT Manger start failed!, error number [%d] ",
94                   err);
95     }
96
97     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
98     return err;
99 }
100
101 CAResult_t CAStartEDRListeningServer(void)
102 {
103     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
104
105     return CAStartServer(OIC_BT_SERVICE_ID, &gListeningServerID);
106 }
107
108 CAResult_t CAStartEDRDiscoveryServer(void)
109 {
110     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
111
112     return CAStartServer(OIC_BT_SERVICE_ID, &gDiscoveryServerID);
113 }
114
115 uint32_t CASendEDRUnicastData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
116                                uint32_t dataLength)
117 {
118     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
119
120     CAResult_t err = CA_STATUS_OK;
121
122     //Input validation
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);
125
126     if (0 == strlen(remoteEndpoint->addressInfo.BT.btMacAddress))
127     {
128         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: BT Address is empty!");
129         return 0;
130     }
131
132     if (0 == dataLength)
133     {
134         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: data length is zero!");
135         return 0;
136     }
137
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)))
143     {
144         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Send unicast data failed!, error num [%d]", err);
145         return 0;
146     }
147
148     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
149     return sentLength;
150 }
151
152 uint32_t CASendEDRMulticastData(void *data, uint32_t dataLength)
153 {
154     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
155
156     CAResult_t err = CA_STATUS_OK;
157
158     //Input validation
159     VERIFY_NON_NULL_RET(data, BLUETOOTH_ADAPTER_TAG, "Data is null", 0);
160
161     if (0 == dataLength)
162     {
163         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: data length is zero!");
164         return 0;
165     }
166
167     uint32_t sentLen = 0;
168     const char *serviceUUID = OIC_BT_SERVICE_ID;
169     if (CA_STATUS_OK != (err = CABTManagerSendData(NULL, serviceUUID, data, dataLength,
170                                &sentLen)))
171     {
172         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Send multicast data failed!, error num [%d]",
173                   err);
174         return 0;
175     }
176
177     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
178     return sentLen;
179 }
180
181
182 CAResult_t CAGetEDRInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
183 {
184     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
185
186     VERIFY_NON_NULL(info, BLUETOOTH_ADAPTER_TAG, "LocalConnectivity info is null");
187
188     CAResult_t err = CA_STATUS_OK;
189     *size = 0;
190     if (CA_STATUS_OK != (err = CABTManagerGetInterface(info)))
191     {
192         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG,
193                   "Failed to get local interface information!, error num [%d]", err);
194         return err;
195     }
196
197     *size = 1;
198     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
199     return err;
200 }
201
202 CAResult_t CAReadEDRData(void)
203 {
204     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
205
206     return CABTManagerReadData();
207 }
208
209 CAResult_t CAStopEDR(void)
210 {
211     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
212
213     //Stop the Discovery server
214     if (-1 < gDiscoveryServerID)
215     {
216         CABTManagerStopServer(gDiscoveryServerID);
217     }
218
219     //Stop the Listening server
220     if (-1 < gListeningServerID)
221     {
222         CABTManagerStopServer(gListeningServerID);
223     }
224
225     //Stop the Notification server
226     if (-1 < gNotificationServerID)
227     {
228         CABTManagerStopServer(gNotificationServerID);
229     }
230
231     //Stop the adapter
232     CABTManagerStop();
233
234     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
235     return CA_STATUS_OK;
236 }
237
238 void CATerminateEDR(void)
239 {
240     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
241
242     //Terminate BT Manager
243     CABTManagerTerminate();
244
245     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
246 }
247
248 CAResult_t CAStartServer(const char *serviceUUID, int32_t *serverID)
249 {
250     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
251
252     CAResult_t err = CA_STATUS_OK;
253
254     //Input validation
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))
258     {
259         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: Service UUID is empty!");
260         return CA_STATUS_INVALID_PARAM;
261     }
262
263     if (CA_STATUS_OK != (err = CABTManagerStartServer(serviceUUID, serverID)))
264     {
265         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to start RFCOMM server!, error num [%d]",
266                   err);
267         return err;
268     }
269
270     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
271     return err;
272 }