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, "clientList");
73 VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
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 CAResult_t CAGetLEServerInfo(LEServerInfoList *serverList, const char *leAddress,
106 LEServerInfo **leServerInfo)
109 OIC_LOG(DEBUG, TAG, "IN");
111 VERIFY_NON_NULL(serverList, TAG, "clientList");
112 VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
113 VERIFY_NON_NULL(leAddress, TAG, "leAddress");
115 LEServerInfoList *cur = serverList;
116 *leServerInfo = NULL;
119 if (!strcasecmp(cur->serverInfo->remoteAddress , leAddress))
121 *leServerInfo = cur->serverInfo;
122 OIC_LOG(DEBUG, TAG, "OUT");
129 OIC_LOG(DEBUG, TAG, " OUT");
130 return CA_STATUS_FAILED;
133 CAResult_t CAGetLEServerInfoByPosition(LEServerInfoList *serverList, int32_t position,
134 LEServerInfo **leServerInfo)
136 OIC_LOG(DEBUG, TAG, "IN");
138 VERIFY_NON_NULL(serverList, TAG, "clientList");
139 VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
143 OIC_LOG(ERROR, TAG, "Position Invalid input !");
144 return CA_STATUS_INVALID_PARAM;
147 *leServerInfo = NULL;
149 LEServerInfoList *cur = serverList;
152 if (position == count)
154 *leServerInfo = cur->serverInfo;
155 OIC_LOG(DEBUG, TAG, "OUT");
161 OIC_LOG(DEBUG, TAG, "Client info not found for the position");
162 return CA_STATUS_FAILED;
165 void CAFreeLEServerList(LEServerInfoList *clientList)
167 OIC_LOG(DEBUG, TAG, "IN");
170 LEServerInfoList *temp = clientList;
171 clientList = clientList->next;
172 CAFreeLEServerInfo(temp->serverInfo);
175 OIC_LOG(DEBUG, TAG, "OUT");
178 void CAFreeLEServerInfo(LEServerInfo *leServerInfo)
180 OIC_LOG(DEBUG, TAG, "IN");
183 if (leServerInfo->remoteAddress)
185 bt_gatt_client_destroy(leServerInfo->clientHandle);
186 int32_t ret = bt_gatt_disconnect(leServerInfo->remoteAddress);
188 if (BT_ERROR_NONE != ret)
190 OIC_LOG_V(ERROR, TAG,
191 "bt_gatt_disconnect Failed with ret value [%d]",
195 OICFree(leServerInfo->remoteAddress);
197 OICFree(leServerInfo);
199 OIC_LOG(DEBUG, TAG, "OUT");
202 const char *CALEGetErrorMsg(bt_error_e err)
204 const char *errStr = NULL;
209 errStr = "BT_ERROR_NONE";
211 case BT_ERROR_CANCELLED:
212 errStr = "BT_ERROR_CANCELLED";
214 case BT_ERROR_INVALID_PARAMETER:
215 errStr = "BT_ERROR_INVALID_PARAMETER";
217 case BT_ERROR_OUT_OF_MEMORY:
218 errStr = "BT_ERROR_OUT_OF_MEMORY";
220 case BT_ERROR_RESOURCE_BUSY:
221 errStr = "BT_ERROR_RESOURCE_BUSY";
223 case BT_ERROR_TIMED_OUT:
224 errStr = "BT_ERROR_TIMED_OUT";
226 case BT_ERROR_NOW_IN_PROGRESS:
227 errStr = "BT_ERROR_NOW_IN_PROGRESS";
229 case BT_ERROR_NOT_INITIALIZED:
230 errStr = "BT_ERROR_NOT_INITIALIZED";
232 case BT_ERROR_NOT_ENABLED:
233 errStr = "BT_ERROR_NOT_ENABLED";
235 case BT_ERROR_ALREADY_DONE:
236 errStr = "BT_ERROR_ALREADY_DONE";
238 case BT_ERROR_OPERATION_FAILED:
239 errStr = "BT_ERROR_OPERATION_FAILED";
241 case BT_ERROR_NOT_IN_PROGRESS:
242 errStr = "BT_ERROR_NOT_IN_PROGRESS";
244 case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
245 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_BONDED";
247 case BT_ERROR_AUTH_REJECTED:
248 errStr = "BT_ERROR_AUTH_REJECTED";
250 case BT_ERROR_AUTH_FAILED:
251 errStr = "BT_ERROR_AUTH_FAILED";
253 case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
254 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_FOUND";
256 case BT_ERROR_SERVICE_SEARCH_FAILED:
257 errStr = "BT_ERROR_SERVICE_SEARCH_FAILED";
259 case BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED:
260 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED";
262 case BT_ERROR_PERMISSION_DENIED:
263 errStr = "BT_ERROR_PERMISSION_DENIED";
265 case BT_ERROR_SERVICE_NOT_FOUND:
266 errStr = "BT_ERROR_SERVICE_NOT_FOUND";
268 case BT_ERROR_NOT_SUPPORTED:
269 errStr = "BT_ERROR_NOT_SUPPORTED";
272 errStr = "NOT Defined";