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 ******************************************************************/
28 #include<sys/socket.h>
29 #include<netinet/in.h>
32 #include "caadapterutils.h"
33 #include "oic_string.h"
34 #include "oic_malloc.h"
37 * Logging tag for module name
39 #define TAG "OIC_CA_LE_UTIL"
42 * Number of services connected.
44 static int32_t g_numberOfServiceConnected = 0;
46 void CAIncrementRegisteredServiceCount()
48 g_numberOfServiceConnected++;
51 void CADecrementRegisteredServiceCount()
53 g_numberOfServiceConnected--;
56 void CAResetRegisteredServiceCount()
58 g_numberOfServiceConnected = 0;
61 int32_t CAGetRegisteredServiceCount()
63 return g_numberOfServiceConnected ;
66 CAResult_t CAAddLEServerInfoToList(LEServerInfoList **serverList,
67 LEServerInfo *leServerInfo)
70 OIC_LOG(DEBUG, TAG, "IN");
72 VERIFY_NON_NULL(serverList, TAG, "serverList");
73 VERIFY_NON_NULL(leServerInfo, TAG, "leServerInfo");
75 LEServerInfoList *node = (LEServerInfoList *) OICCalloc(1, sizeof(LEServerInfoList));
78 OIC_LOG(ERROR, TAG, "Malloc failed!");
79 return CA_STATUS_FAILED;
82 node->serverInfo = leServerInfo;
85 if (*serverList == NULL) // Empty list
89 else // Add at front end
91 node->next = *serverList;
95 CAIncrementRegisteredServiceCount();
97 OIC_LOG_V(DEBUG, TAG, "Device [%s] added to list",
98 leServerInfo->remoteAddress);
100 OIC_LOG(DEBUG, TAG, "OUT");
105 void CARemoveLEServerInfoFromList(LEServerInfoList **serverList,
106 const char *remoteAddress)
108 OIC_LOG(DEBUG, TAG, "IN");
109 VERIFY_NON_NULL_VOID(serverList, TAG, "serverList");
110 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remoteAddress");
112 LEServerInfoList *temp = *serverList;
113 LEServerInfoList *prev = NULL;
116 if (!strcasecmp(temp->serverInfo->remoteAddress, remoteAddress))
120 *serverList = temp->next;
124 prev->next = temp->next;
126 CADecrementRegisteredServiceCount();
127 bt_gatt_client_destroy(temp->serverInfo->clientHandle);
128 OICFree(temp->serverInfo->remoteAddress);
129 OICFree(temp->serverInfo);
131 OIC_LOG_V(DEBUG, TAG, "Device [%s] removed from list", remoteAddress);
138 OIC_LOG(DEBUG, TAG, "OUT");
141 CAResult_t CAGetLEServerInfo(LEServerInfoList *serverList, const char *leAddress,
142 LEServerInfo **leServerInfo)
145 OIC_LOG(DEBUG, TAG, "IN");
147 VERIFY_NON_NULL(serverList, TAG, "clientList");
148 VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
149 VERIFY_NON_NULL(leAddress, TAG, "leAddress");
151 LEServerInfoList *cur = serverList;
152 *leServerInfo = NULL;
155 if (!strcasecmp(cur->serverInfo->remoteAddress , leAddress))
157 *leServerInfo = cur->serverInfo;
158 OIC_LOG(DEBUG, TAG, "OUT");
165 OIC_LOG(DEBUG, TAG, " OUT");
166 return CA_STATUS_FAILED;
169 CAResult_t CAGetLEServerInfoByPosition(LEServerInfoList *serverList, int32_t position,
170 LEServerInfo **leServerInfo)
172 OIC_LOG(DEBUG, TAG, "IN");
174 VERIFY_NON_NULL(serverList, TAG, "clientList");
175 VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
179 OIC_LOG(ERROR, TAG, "Position Invalid input !");
180 return CA_STATUS_INVALID_PARAM;
183 *leServerInfo = NULL;
185 LEServerInfoList *cur = serverList;
188 if (position == count)
190 *leServerInfo = cur->serverInfo;
191 OIC_LOG(DEBUG, TAG, "OUT");
197 OIC_LOG(DEBUG, TAG, "Client info not found for the position");
198 return CA_STATUS_FAILED;
201 void CAFreeLEServerList(LEServerInfoList *serverList)
203 OIC_LOG(DEBUG, TAG, "IN");
206 LEServerInfoList *temp = serverList;
207 serverList = serverList->next;
208 CAFreeLEServerInfo(temp->serverInfo);
211 OIC_LOG(DEBUG, TAG, "OUT");
214 void CAFreeLEServerInfo(LEServerInfo *leServerInfo)
216 OIC_LOG(DEBUG, TAG, "IN");
219 if (leServerInfo->remoteAddress)
221 bt_gatt_client_destroy(leServerInfo->clientHandle);
222 int32_t ret = bt_gatt_disconnect(leServerInfo->remoteAddress);
224 if (BT_ERROR_NONE != ret)
226 OIC_LOG_V(ERROR, TAG,
227 "bt_gatt_disconnect Failed with ret value [%d]",
231 OICFree(leServerInfo->remoteAddress);
233 OICFree(leServerInfo);
235 OIC_LOG(DEBUG, TAG, "OUT");
238 CAResult_t CAAddLEClientInfoToList(LEClientInfoList **clientList,
241 OIC_LOG(DEBUG, TAG, "IN");
242 VERIFY_NON_NULL(clientList, TAG, "clientList");
243 VERIFY_NON_NULL(clientAddress, TAG, "clientAddress");
245 LEClientInfoList *node = (LEClientInfoList *) OICCalloc(1, sizeof(LEClientInfoList));
248 OIC_LOG(ERROR, TAG, "Malloc failed!");
249 return CA_STATUS_FAILED;
252 node->remoteAddress= clientAddress;
255 if (*clientList == NULL) // Empty list
259 else // Add at front end
261 node->next = *clientList;
265 OIC_LOG_V(DEBUG, TAG, "Device [%s] added to list", clientAddress);
266 OIC_LOG(DEBUG, TAG, "OUT");
270 void CARemoveLEClientInfoFromList(LEClientInfoList **clientList,
271 const char *clientAddress)
273 OIC_LOG(DEBUG, TAG, "IN");
274 VERIFY_NON_NULL_VOID(clientAddress, TAG, "clientAddress");
276 LEClientInfoList *temp = *clientList;
277 LEClientInfoList *prev = NULL;
280 if (!strcasecmp(temp->remoteAddress, clientAddress))
284 *clientList = temp->next;
288 prev->next = temp->next;
290 OICFree(temp->remoteAddress);
292 OIC_LOG_V(DEBUG, TAG, "Device [%s] removed from list", clientAddress);
299 OIC_LOG(DEBUG, TAG, "OUT");
302 void CADisconnectAllClient(LEClientInfoList *clientList)
304 OIC_LOG(DEBUG, TAG, "IN");
307 LEClientInfoList *temp = clientList;
308 clientList = clientList->next;
309 if (temp->remoteAddress)
311 int32_t ret = bt_gatt_disconnect(temp->remoteAddress);
313 if (BT_ERROR_NONE != ret)
315 OIC_LOG_V(ERROR, TAG,
316 "bt_gatt_disconnect Failed with ret value [%d]",
320 OICFree(temp->remoteAddress);
324 OIC_LOG(DEBUG, TAG, "OUT");
327 const char *CALEGetErrorMsg(bt_error_e err)
329 const char *errStr = NULL;
334 errStr = "BT_ERROR_NONE";
336 case BT_ERROR_CANCELLED:
337 errStr = "BT_ERROR_CANCELLED";
339 case BT_ERROR_INVALID_PARAMETER:
340 errStr = "BT_ERROR_INVALID_PARAMETER";
342 case BT_ERROR_OUT_OF_MEMORY:
343 errStr = "BT_ERROR_OUT_OF_MEMORY";
345 case BT_ERROR_RESOURCE_BUSY:
346 errStr = "BT_ERROR_RESOURCE_BUSY";
348 case BT_ERROR_TIMED_OUT:
349 errStr = "BT_ERROR_TIMED_OUT";
351 case BT_ERROR_NOW_IN_PROGRESS:
352 errStr = "BT_ERROR_NOW_IN_PROGRESS";
354 case BT_ERROR_NOT_INITIALIZED:
355 errStr = "BT_ERROR_NOT_INITIALIZED";
357 case BT_ERROR_NOT_ENABLED:
358 errStr = "BT_ERROR_NOT_ENABLED";
360 case BT_ERROR_ALREADY_DONE:
361 errStr = "BT_ERROR_ALREADY_DONE";
363 case BT_ERROR_OPERATION_FAILED:
364 errStr = "BT_ERROR_OPERATION_FAILED";
366 case BT_ERROR_NOT_IN_PROGRESS:
367 errStr = "BT_ERROR_NOT_IN_PROGRESS";
369 case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
370 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_BONDED";
372 case BT_ERROR_AUTH_REJECTED:
373 errStr = "BT_ERROR_AUTH_REJECTED";
375 case BT_ERROR_AUTH_FAILED:
376 errStr = "BT_ERROR_AUTH_FAILED";
378 case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
379 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_FOUND";
381 case BT_ERROR_SERVICE_SEARCH_FAILED:
382 errStr = "BT_ERROR_SERVICE_SEARCH_FAILED";
384 case BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED:
385 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED";
387 case BT_ERROR_PERMISSION_DENIED:
388 errStr = "BT_ERROR_PERMISSION_DENIED";
390 case BT_ERROR_SERVICE_NOT_FOUND:
391 errStr = "BT_ERROR_SERVICE_NOT_FOUND";
393 case BT_ERROR_NOT_SUPPORTED:
394 errStr = "BT_ERROR_NOT_SUPPORTED";
396 case BT_ERROR_QUOTA_EXCEEDED:
397 errStr = "BT_ERROR_QUOTA_EXCEEDED";
399 case BT_ERROR_NO_DATA:
400 errStr = "BT_ERROR_NO_DATA";
403 errStr = "BT_ERROR_AGAIN";
406 errStr = "NOT Defined";