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 cabtdevicelist.c
23 * @brief This file provides APIs to access the discovered bluetooth device list
26 #include "cabtdevicelist.h"
27 #include "caadapterutils.h"
28 #include "cabtutils.h"
33 * @fn CACreateBTDevice
34 * @brief Creates #BTDevice for specified remote address and uuid.
37 static CAResult_t CACreateBTDevice(const char *deviceAddress, const char *uuid, BTDevice **device);
41 * @fn CADestroyBTDevice
42 * @brief Free all the memory associated with specified device.
45 static void CADestroyBTDevice(BTDevice *device);
50 * @brief Free all the memory associated with specified data.
53 static void CADestroyBTData(BTData *data);
56 CAResult_t CACreateAndAddToDeviceList(BTDeviceList **deviceList, const char *deviceAddress,
57 const char *uuid, BTDevice **device)
59 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
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");
65 if (CA_STATUS_OK != CACreateBTDevice(deviceAddress, uuid, device) || NULL == *device)
67 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid or Not bonded device!");
68 return CA_STATUS_FAILED;
71 if (CA_STATUS_OK != CAAddBTDeviceToList(deviceList, *device))
73 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to add in list!");
75 //Remove created BTDevice
76 CADestroyBTDevice(*device);
79 return CA_STATUS_FAILED;
82 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
86 CAResult_t CACreateBTDevice(const char *deviceAddress, const char *uuid, BTDevice **device)
88 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
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");
94 *device = (BTDevice *) OICMalloc(sizeof(BTDevice));
97 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Out of memory (device)!");
98 return CA_MEMORY_ALLOC_FAILED;
101 //Copy bluetooth address
102 if (strlen(deviceAddress))
104 (*device)->remoteAddress = strndup(deviceAddress, strlen(deviceAddress));
105 if (NULL == (*device)->remoteAddress)
107 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Out of memory (remote address)!");
111 return CA_MEMORY_ALLOC_FAILED;
115 //Copy OIC service uuid
118 (*device)->serviceUUID = strndup(uuid, strlen(uuid));
119 if (NULL == (*device)->serviceUUID)
121 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG,
122 "[createBTDevice] Out of memory (service uuid)!");
124 OICFree((*device)->remoteAddress);
127 return CA_MEMORY_ALLOC_FAILED;
131 (*device)->socketFD = -1;
132 (*device)->pendingDataList = NULL;
133 (*device)->serviceSearched = 0;
135 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
139 CAResult_t CAAddBTDeviceToList(BTDeviceList **deviceList, BTDevice *device)
141 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
143 VERIFY_NON_NULL(deviceList, BLUETOOTH_ADAPTER_TAG, "Device list is null");
144 VERIFY_NON_NULL(device, BLUETOOTH_ADAPTER_TAG, "Device is null");
146 BTDeviceList *node = (BTDeviceList *) OICMalloc(sizeof(BTDeviceList));
149 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Out of memory (device list)!");
150 return CA_MEMORY_ALLOC_FAILED;
153 node->device = device;
156 if (NULL == *deviceList) //Empty list
160 else //Add at front end
162 node->next = *deviceList;
166 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
170 CAResult_t CAGetBTDevice(BTDeviceList *deviceList, const char *deviceAddress, BTDevice **device)
172 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
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");
178 BTDeviceList *curNode = deviceList;
180 while (curNode != NULL)
182 if (!strcasecmp(curNode->device->remoteAddress, deviceAddress))
184 *device = curNode->device;
188 curNode = curNode->next;
191 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT [Device not found!]");
192 return CA_STATUS_FAILED;
195 CAResult_t CAGetBTDeviceBySocketId(BTDeviceList *deviceList, int32_t socketID, BTDevice **device)
197 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
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;
203 while (curNode != NULL)
205 if (curNode->device->socketFD == socketID)
207 *device = curNode->device;
211 curNode = curNode->next;
214 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
215 return CA_STATUS_FAILED;
218 CAResult_t CARemoveBTDeviceFromList(BTDeviceList **deviceList, const char *deviceAddress)
220 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
222 VERIFY_NON_NULL(deviceList, BLUETOOTH_ADAPTER_TAG, "Device list is null");
223 VERIFY_NON_NULL(deviceAddress, BLUETOOTH_ADAPTER_TAG, "Remote address is null");
225 BTDeviceList *curNode = NULL;
226 BTDeviceList *prevNode = NULL;
228 curNode = *deviceList;
229 while (curNode != NULL)
231 if (!strcasecmp(curNode->device->remoteAddress, deviceAddress))
233 if (curNode == *deviceList)
235 *deviceList = curNode->next;
237 curNode->next = NULL;
238 CADestroyBTDeviceList(&curNode);
243 prevNode->next = curNode->next;
245 curNode->next = NULL;
246 CADestroyBTDeviceList(&curNode);
253 curNode = curNode->next;
257 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Device not in the list !");
258 return CA_STATUS_FAILED;
261 void CADestroyBTDeviceList(BTDeviceList **deviceList)
265 BTDeviceList *curNode = *deviceList;
266 *deviceList = (*deviceList)->next;
268 CADestroyBTDevice(curNode->device);
273 void CADestroyBTDevice(BTDevice *device)
277 OICFree(device->remoteAddress);
278 OICFree(device->serviceUUID);
279 CADestroyBTDataList(&device->pendingDataList);
284 CAResult_t CAAddBTDataToList(BTDataList **dataList, void *data, uint32_t dataLength)
286 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
288 VERIFY_NON_NULL(dataList, BLUETOOTH_ADAPTER_TAG, "Data list is null");
289 VERIFY_NON_NULL(data, BLUETOOTH_ADAPTER_TAG, "Data is null");
293 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: data length is zero!");
294 return CA_STATUS_INVALID_PARAM;
297 BTDataList *pending_data = (BTDataList *) OICMalloc(sizeof(BTDataList));
298 if (NULL == pending_data)
300 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "OICMalloc failed (data list)!");
301 return CA_MEMORY_ALLOC_FAILED;
304 pending_data->data = (BTData *) OICMalloc(sizeof(BTData));
305 if (NULL == pending_data->data)
307 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "OICMalloc failed (data node)!");
309 OICFree(pending_data);
310 return CA_MEMORY_ALLOC_FAILED;
312 pending_data->next = NULL;
314 pending_data->data->data = (void *) OICMalloc(dataLength); //data
315 if (NULL == pending_data->data->data)
317 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "OICMalloc failed (data)!");
319 OICFree(pending_data->data);
320 OICFree(pending_data);
321 return CA_MEMORY_ALLOC_FAILED;
324 memcpy(pending_data->data->data, data, dataLength);
325 pending_data->data->dataLength = dataLength;
327 if (NULL == *dataList) //Empty list
329 *dataList = pending_data;
331 else //Add at rear end
333 BTDataList *curNode = *dataList;
334 while (curNode->next != NULL)
336 curNode = curNode->next;
339 curNode->next = pending_data;
342 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
346 CAResult_t CARemoveBTDataFromList(BTDataList **dataList)
348 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
350 VERIFY_NON_NULL(dataList, BLUETOOTH_ADAPTER_TAG, "Data list is null");
354 BTDataList *curNode = *dataList;
355 *dataList = (*dataList)->next;
357 //Delete the first node
358 CADestroyBTData(curNode->data);
362 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
366 void CADestroyBTDataList(BTDataList **dataList)
368 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
372 BTDataList *curNode = *dataList;
373 *dataList = (*dataList)->next;
375 CADestroyBTData(curNode->data);
381 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
384 void CADestroyBTData(BTData *data)