Implementation of connectivity abstraction feature Release v0.61
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / tizen / cabtdevicelist.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  cabtdevicelist.c
23  * @brief  This file provides APIs to access the discovered bluetooth device list
24  */
25
26 #include "cabtdevicelist.h"
27 #include "caadapterutils.h"
28 #include "cabtutils.h"
29 #include "logger.h"
30
31
32 /**
33  * @fn  CACreateBTDevice
34  * @brief  Creates #BTDevice for specified remote address and uuid.
35  *
36  */
37 static CAResult_t CACreateBTDevice(const char *deviceAddress, const char *uuid, BTDevice **device);
38
39
40 /**
41  * @fn  CADestroyBTDevice
42  * @brief  Free all the memory associated with specified device.
43  *
44  */
45 static void CADestroyBTDevice(BTDevice *device);
46
47
48 /**
49  * @fn  CADestroyBTData
50  * @brief  Free all the memory associated with specified data.
51  *
52  */
53 static void CADestroyBTData(BTData *data);
54
55
56 CAResult_t CACreateAndAddToDeviceList(BTDeviceList **deviceList, const char *deviceAddress,
57                                       const char *uuid, BTDevice **device)
58 {
59     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
60
61     VERIFY_NON_NULL(deviceList, BLUETOOTH_ADAPTER_TAG, "Device list is null");
62     VERIFY_NON_NULL(deviceAddress, BLUETOOTH_ADAPTER_TAG, "Remote address is null");
63     VERIFY_NON_NULL(device, BLUETOOTH_ADAPTER_TAG, "Device is null");
64
65     if (CA_STATUS_OK != CACreateBTDevice(deviceAddress, uuid, device) || NULL == *device)
66     {
67         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid or Not bonded device!");
68         return CA_STATUS_FAILED;
69     }
70
71     if (CA_STATUS_OK != CAAddBTDeviceToList(deviceList, *device))
72     {
73         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to add in list!");
74
75         //Remove created BTDevice
76         CADestroyBTDevice(*device);
77         *device = NULL;
78
79         return CA_STATUS_FAILED;
80     }
81
82     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
83     return CA_STATUS_OK;
84 }
85
86 CAResult_t CACreateBTDevice(const char *deviceAddress, const char *uuid, BTDevice **device)
87 {
88     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
89
90     VERIFY_NON_NULL(deviceAddress, BLUETOOTH_ADAPTER_TAG, "Remote address is null");
91     VERIFY_NON_NULL(uuid, BLUETOOTH_ADAPTER_TAG, "uuid is null");
92     VERIFY_NON_NULL(device, BLUETOOTH_ADAPTER_TAG, "Device is null");
93
94     *device = (BTDevice *) OICMalloc(sizeof(BTDevice));
95     if (NULL == *device)
96     {
97         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Out of memory (device)!");
98         return CA_MEMORY_ALLOC_FAILED;
99     }
100
101     //Copy bluetooth address
102     if (strlen(deviceAddress))
103     {
104         (*device)->remoteAddress = strndup(deviceAddress, strlen(deviceAddress));
105         if (NULL == (*device)->remoteAddress)
106         {
107             OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Out of memory (remote address)!");
108
109             OICFree(*device);
110             *device = NULL;
111             return CA_MEMORY_ALLOC_FAILED;
112         }
113     }
114
115     //Copy OIC service uuid
116     if (strlen(uuid))
117     {
118         (*device)->serviceUUID = strndup(uuid, strlen(uuid));
119         if (NULL == (*device)->serviceUUID)
120         {
121             OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG,
122                       "[createBTDevice] Out of memory (service uuid)!");
123
124             OICFree((*device)->remoteAddress);
125             OICFree(*device);
126             *device = NULL;
127             return CA_MEMORY_ALLOC_FAILED;
128         }
129     }
130
131     (*device)->socketFD = -1;
132     (*device)->pendingDataList = NULL;
133     (*device)->serviceSearched = 0;
134
135     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
136     return CA_STATUS_OK;
137 }
138
139 CAResult_t CAAddBTDeviceToList(BTDeviceList **deviceList, BTDevice *device)
140 {
141     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
142
143     VERIFY_NON_NULL(deviceList, BLUETOOTH_ADAPTER_TAG, "Device list is null");
144     VERIFY_NON_NULL(device, BLUETOOTH_ADAPTER_TAG, "Device is null");
145
146     BTDeviceList *node = (BTDeviceList *) OICMalloc(sizeof(BTDeviceList));
147     if (NULL == node)
148     {
149         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Out of memory (device list)!");
150         return CA_MEMORY_ALLOC_FAILED;
151     }
152
153     node->device = device;
154     node->next = NULL;
155
156     if (NULL == *deviceList) //Empty list
157     {
158         *deviceList = node;
159     }
160     else //Add at front end
161     {
162         node->next = *deviceList;
163         *deviceList = node;
164     }
165
166     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
167     return CA_STATUS_OK;
168 }
169
170 CAResult_t CAGetBTDevice(BTDeviceList *deviceList, const char *deviceAddress, BTDevice **device)
171 {
172     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
173
174     VERIFY_NON_NULL(deviceList, BLUETOOTH_ADAPTER_TAG, "Device list is null");
175     VERIFY_NON_NULL(deviceAddress, BLUETOOTH_ADAPTER_TAG, "Remote address is null");
176     VERIFY_NON_NULL(device, BLUETOOTH_ADAPTER_TAG, "Device is null");
177
178     BTDeviceList *curNode = deviceList;
179     *device = NULL;
180     while (curNode != NULL)
181     {
182         if (!strcasecmp(curNode->device->remoteAddress, deviceAddress))
183         {
184             *device = curNode->device;
185             return CA_STATUS_OK;
186         }
187
188         curNode = curNode->next;
189     }
190
191     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT [Device not found!]");
192     return CA_STATUS_FAILED;
193 }
194
195 CAResult_t CAGetBTDeviceBySocketId(BTDeviceList *deviceList, int32_t socketID, BTDevice **device)
196 {
197     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
198
199     VERIFY_NON_NULL(deviceList, BLUETOOTH_ADAPTER_TAG, "Device list is null");
200     VERIFY_NON_NULL(device, BLUETOOTH_ADAPTER_TAG, "Device is null");
201     BTDeviceList *curNode = deviceList;
202     *device = NULL;
203     while (curNode != NULL)
204     {
205         if (curNode->device->socketFD == socketID)
206         {
207             *device = curNode->device;
208             return CA_STATUS_OK;
209         }
210
211         curNode = curNode->next;
212     }
213
214     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
215     return CA_STATUS_FAILED;
216 }
217
218 CAResult_t CARemoveBTDeviceFromList(BTDeviceList **deviceList, const char *deviceAddress)
219 {
220     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
221
222     VERIFY_NON_NULL(deviceList, BLUETOOTH_ADAPTER_TAG, "Device list is null");
223     VERIFY_NON_NULL(deviceAddress, BLUETOOTH_ADAPTER_TAG, "Remote address is null");
224
225     BTDeviceList *curNode = NULL;
226     BTDeviceList *prevNode = NULL;
227
228     curNode = *deviceList;
229     while (curNode != NULL)
230     {
231         if (!strcasecmp(curNode->device->remoteAddress, deviceAddress))
232         {
233             if (curNode == *deviceList)
234             {
235                 *deviceList = curNode->next;
236
237                 curNode->next = NULL;
238                 CADestroyBTDeviceList(&curNode);
239                 return CA_STATUS_OK;
240             }
241             else
242             {
243                 prevNode->next = curNode->next;
244
245                 curNode->next = NULL;
246                 CADestroyBTDeviceList(&curNode);
247                 return CA_STATUS_OK;
248             }
249         }
250         else
251         {
252             prevNode = curNode;
253             curNode = curNode->next;
254         }
255     }
256
257     OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Device not in the list !");
258     return CA_STATUS_FAILED;
259 }
260
261 void CADestroyBTDeviceList(BTDeviceList **deviceList)
262 {
263     while (*deviceList)
264     {
265         BTDeviceList *curNode = *deviceList;
266         *deviceList = (*deviceList)->next;
267
268         CADestroyBTDevice(curNode->device);
269         OICFree(curNode);
270     }
271 }
272
273 void CADestroyBTDevice(BTDevice *device)
274 {
275     if (device)
276     {
277         OICFree(device->remoteAddress);
278         OICFree(device->serviceUUID);
279         CADestroyBTDataList(&device->pendingDataList);
280         OICFree(device);
281     }
282 }
283
284 CAResult_t CAAddBTDataToList(BTDataList **dataList, void *data, uint32_t dataLength)
285 {
286     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
287
288     VERIFY_NON_NULL(dataList, BLUETOOTH_ADAPTER_TAG, "Data list is null");
289     VERIFY_NON_NULL(data, BLUETOOTH_ADAPTER_TAG, "Data is null");
290
291     if (0 == dataLength)
292     {
293         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: data length is zero!");
294         return CA_STATUS_INVALID_PARAM;
295     }
296
297     BTDataList *pending_data = (BTDataList *) OICMalloc(sizeof(BTDataList));
298     if (NULL == pending_data)
299     {
300         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "OICMalloc failed (data list)!");
301         return CA_MEMORY_ALLOC_FAILED;
302     }
303
304     pending_data->data = (BTData *) OICMalloc(sizeof(BTData));
305     if (NULL == pending_data->data)
306     {
307         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "OICMalloc failed (data node)!");
308
309         OICFree(pending_data);
310         return CA_MEMORY_ALLOC_FAILED;
311     }
312     pending_data->next = NULL;
313
314     pending_data->data->data = (void *) OICMalloc(dataLength); //data
315     if (NULL == pending_data->data->data)
316     {
317         OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "OICMalloc failed (data)!");
318
319         OICFree(pending_data->data);
320         OICFree(pending_data);
321         return CA_MEMORY_ALLOC_FAILED;
322     }
323
324     memcpy(pending_data->data->data, data, dataLength);
325     pending_data->data->dataLength = dataLength;
326
327     if (NULL == *dataList) //Empty list
328     {
329         *dataList = pending_data;
330     }
331     else //Add at rear end
332     {
333         BTDataList *curNode = *dataList;
334         while (curNode->next != NULL)
335         {
336             curNode = curNode->next;
337         }
338
339         curNode->next = pending_data;
340     }
341
342     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
343     return CA_STATUS_OK;
344 }
345
346 CAResult_t CARemoveBTDataFromList(BTDataList **dataList)
347 {
348     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
349
350     VERIFY_NON_NULL(dataList, BLUETOOTH_ADAPTER_TAG, "Data list is null");
351
352     if (*dataList)
353     {
354         BTDataList *curNode = *dataList;
355         *dataList = (*dataList)->next;
356
357         //Delete the first node
358         CADestroyBTData(curNode->data);
359         OICFree(curNode);
360     }
361
362     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
363     return CA_STATUS_OK;
364 }
365
366 void CADestroyBTDataList(BTDataList **dataList)
367 {
368     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
369
370     while (*dataList)
371     {
372         BTDataList *curNode = *dataList;
373         *dataList = (*dataList)->next;
374
375         CADestroyBTData(curNode->data);
376         OICFree(curNode);
377     }
378
379     *dataList = NULL;
380
381     OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
382 }
383
384 void CADestroyBTData(BTData *data)
385 {
386     if (data)
387     {
388         OICFree(data->data);
389         OICFree(data);
390     }
391 }
392