[Tizen]Fix for LE Server termination issue
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / tizen / caleutil.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 #include "caleutil.h"
22
23 #include<stdio.h>
24 #include<stdlib.h>
25 #include<string.h>
26 #include<arpa/inet.h>
27 #include<sys/types.h>
28 #include<sys/socket.h>
29 #include<netinet/in.h>
30
31
32 #include "caadapterutils.h"
33 #include "oic_string.h"
34 #include "oic_malloc.h"
35
36 /**
37  * Logging tag for module name
38  */
39 #define TAG "OIC_CA_LE_UTIL"
40
41 /**
42  * Number of services connected.
43  */
44 static int32_t g_numberOfServiceConnected = 0;
45
46 void CAIncrementRegisteredServiceCount()
47 {
48     g_numberOfServiceConnected++;
49 }
50
51 void CADecrementRegisteredServiceCount()
52 {
53     g_numberOfServiceConnected--;
54 }
55
56 void CAResetRegisteredServiceCount()
57 {
58     g_numberOfServiceConnected = 0;
59 }
60
61 int32_t  CAGetRegisteredServiceCount()
62 {
63     return g_numberOfServiceConnected ;
64 }
65
66 CAResult_t CAAddLEServerInfoToList(LEServerInfoList **serverList,
67                                    LEServerInfo *leServerInfo)
68 {
69
70     OIC_LOG(DEBUG, TAG, "IN");
71
72     VERIFY_NON_NULL(serverList, TAG, "serverList");
73     VERIFY_NON_NULL(leServerInfo, TAG, "leServerInfo");
74
75     LEServerInfoList *node = (LEServerInfoList *) OICCalloc(1, sizeof(LEServerInfoList));
76     if (NULL == node)
77     {
78         OIC_LOG(ERROR, TAG, "Malloc failed!");
79         return CA_STATUS_FAILED;
80     }
81
82     node->serverInfo = leServerInfo;
83     node->next = NULL;
84
85     if (*serverList == NULL)   // Empty list
86     {
87         *serverList = node;
88     }
89     else     // Add at front end
90     {
91         node->next = *serverList;
92         *serverList = node;
93     }
94
95     CAIncrementRegisteredServiceCount();
96
97     OIC_LOG_V(DEBUG, TAG, "Device [%s] added to list",
98               leServerInfo->remoteAddress);
99
100     OIC_LOG(DEBUG, TAG, "OUT");
101
102     return CA_STATUS_OK;
103 }
104
105 void CARemoveLEServerInfoFromList(LEServerInfoList **serverList,
106                                         const char *remoteAddress)
107 {
108     OIC_LOG(DEBUG, TAG, "IN");
109     VERIFY_NON_NULL_VOID(serverList, TAG, "serverList");
110     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remoteAddress");
111
112     LEServerInfoList *temp = *serverList;
113     LEServerInfoList *prev = NULL;
114     while (temp)
115     {
116         if (!strcasecmp(temp->serverInfo->remoteAddress, remoteAddress))
117         {
118             if (NULL == prev)
119             {
120                 *serverList = temp->next;
121             }
122             else
123             {
124                 prev->next = temp->next;
125             }
126             CADecrementRegisteredServiceCount();
127             bt_gatt_client_destroy(temp->serverInfo->clientHandle);
128             OICFree(temp->serverInfo->remoteAddress);
129             OICFree(temp->serverInfo);
130             OICFree(temp);
131             OIC_LOG_V(DEBUG, TAG, "Device [%s] removed from list", remoteAddress);
132             break;
133         }
134         prev = temp;
135         temp = temp->next;
136     }
137
138     OIC_LOG(DEBUG, TAG, "OUT");
139 }
140
141 CAResult_t CAGetLEServerInfo(LEServerInfoList *serverList, const char *leAddress,
142                              LEServerInfo **leServerInfo)
143 {
144
145     OIC_LOG(DEBUG, TAG, "IN");
146
147     VERIFY_NON_NULL(serverList, TAG, "clientList");
148     VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
149     VERIFY_NON_NULL(leAddress, TAG, "leAddress");
150
151     LEServerInfoList *cur = serverList;
152     *leServerInfo = NULL;
153     while (cur != NULL)
154     {
155         if (!strcasecmp(cur->serverInfo->remoteAddress , leAddress))
156         {
157             *leServerInfo = cur->serverInfo;
158             OIC_LOG(DEBUG, TAG, "OUT");
159             return CA_STATUS_OK;
160         }
161
162         cur = cur->next;
163     }
164
165     OIC_LOG(DEBUG, TAG, " OUT");
166     return CA_STATUS_FAILED;
167 }
168
169 CAResult_t CAGetLEServerInfoByPosition(LEServerInfoList *serverList, int32_t position,
170                                        LEServerInfo **leServerInfo)
171 {
172     OIC_LOG(DEBUG, TAG, "IN");
173
174     VERIFY_NON_NULL(serverList, TAG, "clientList");
175     VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
176
177     if (0 > position)
178     {
179         OIC_LOG(ERROR, TAG, "Position Invalid input !");
180         return CA_STATUS_INVALID_PARAM;
181     }
182
183     *leServerInfo = NULL;
184     int32_t count = 0;
185     LEServerInfoList *cur = serverList;
186     while (cur != NULL)
187     {
188         if (position == count)
189         {
190             *leServerInfo = cur->serverInfo;
191             OIC_LOG(DEBUG, TAG, "OUT");
192             return CA_STATUS_OK;
193         }
194         count++;
195         cur = cur->next;
196     }
197     OIC_LOG(DEBUG, TAG, "Client info not found for the position");
198     return CA_STATUS_FAILED;
199 }
200
201 void CAFreeLEServerList(LEServerInfoList *serverList)
202 {
203     OIC_LOG(DEBUG, TAG, "IN");
204     while (serverList)
205     {
206         LEServerInfoList *temp = serverList;
207         serverList = serverList->next;
208         CAFreeLEServerInfo(temp->serverInfo);
209         OICFree(temp);
210     }
211     OIC_LOG(DEBUG, TAG, "OUT");
212 }
213
214 void CAFreeLEServerInfo(LEServerInfo *leServerInfo)
215 {
216     OIC_LOG(DEBUG, TAG, "IN");
217     if (leServerInfo)
218     {
219         if (leServerInfo->remoteAddress)
220         {
221             bt_gatt_client_destroy(leServerInfo->clientHandle);
222             int32_t ret = bt_gatt_disconnect(leServerInfo->remoteAddress);
223
224             if (BT_ERROR_NONE != ret)
225             {
226                 OIC_LOG_V(ERROR, TAG,
227                           "bt_gatt_disconnect Failed with ret value [%d]",
228                           ret);
229                 return;
230             }
231             OICFree(leServerInfo->remoteAddress);
232         }
233         OICFree(leServerInfo);
234     }
235     OIC_LOG(DEBUG, TAG, "OUT");
236 }
237
238 CAResult_t CAAddLEClientInfoToList(LEClientInfoList **clientList,
239                                    char *clientAddress)
240 {
241     OIC_LOG(DEBUG, TAG, "IN");
242     VERIFY_NON_NULL(clientList, TAG, "clientList");
243     VERIFY_NON_NULL(clientAddress, TAG, "clientAddress");
244
245     LEClientInfoList *node = (LEClientInfoList *) OICCalloc(1, sizeof(LEClientInfoList));
246     if (NULL == node)
247     {
248         OIC_LOG(ERROR, TAG, "Malloc failed!");
249         return CA_STATUS_FAILED;
250     }
251
252     node->remoteAddress= clientAddress;
253     node->next = NULL;
254
255     if (*clientList == NULL)   // Empty list
256     {
257         *clientList = node;
258     }
259     else     // Add at front end
260     {
261         node->next = *clientList;
262         *clientList = node;
263     }
264
265     OIC_LOG_V(DEBUG, TAG, "Device [%s] added to list", clientAddress);
266     OIC_LOG(DEBUG, TAG, "OUT");
267     return CA_STATUS_OK;
268 }
269
270 void CARemoveLEClientInfoFromList(LEClientInfoList **clientList,
271                                   const char *clientAddress)
272 {
273     OIC_LOG(DEBUG, TAG, "IN");
274     VERIFY_NON_NULL_VOID(clientAddress, TAG, "clientAddress");
275
276     LEClientInfoList *temp = *clientList;
277     LEClientInfoList *prev = NULL;
278     while (temp)
279     {
280         if (!strcasecmp(temp->remoteAddress, clientAddress))
281         {
282             if (NULL == prev)
283             {
284                 *clientList = temp->next;
285             }
286             else
287             {
288                 prev->next = temp->next;
289             }
290             OICFree(temp->remoteAddress);
291             OICFree(temp);
292             OIC_LOG_V(DEBUG, TAG, "Device [%s] removed from list", clientAddress);
293             break;
294         }
295         prev = temp;
296         temp = temp->next;
297     }
298
299     OIC_LOG(DEBUG, TAG, "OUT");
300 }
301
302 void CADisconnectAllClient(LEClientInfoList *clientList)
303 {
304     OIC_LOG(DEBUG, TAG, "IN");
305     while (clientList)
306     {
307         LEClientInfoList *temp = clientList;
308         clientList = clientList->next;
309         if (temp->remoteAddress)
310         {
311             int32_t ret = bt_gatt_disconnect(temp->remoteAddress);
312
313             if (BT_ERROR_NONE != ret)
314             {
315                 OIC_LOG_V(ERROR, TAG,
316                           "bt_gatt_disconnect Failed with ret value [%d]",
317                           ret);
318                 return;
319             }
320             OICFree(temp->remoteAddress);
321         }
322         OICFree(temp);
323     }
324     OIC_LOG(DEBUG, TAG, "OUT");
325 }
326
327 const char *CALEGetErrorMsg(bt_error_e err)
328 {
329     const char *errStr = NULL;
330
331     switch (err)
332     {
333         case BT_ERROR_NONE:
334             errStr = "BT_ERROR_NONE";
335             break;
336         case BT_ERROR_CANCELLED:
337             errStr = "BT_ERROR_CANCELLED";
338             break;
339         case BT_ERROR_INVALID_PARAMETER:
340             errStr = "BT_ERROR_INVALID_PARAMETER";
341             break;
342         case BT_ERROR_OUT_OF_MEMORY:
343             errStr = "BT_ERROR_OUT_OF_MEMORY";
344             break;
345         case BT_ERROR_RESOURCE_BUSY:
346             errStr = "BT_ERROR_RESOURCE_BUSY";
347             break;
348         case BT_ERROR_TIMED_OUT:
349             errStr = "BT_ERROR_TIMED_OUT";
350             break;
351         case BT_ERROR_NOW_IN_PROGRESS:
352             errStr = "BT_ERROR_NOW_IN_PROGRESS";
353             break;
354         case BT_ERROR_NOT_INITIALIZED:
355             errStr = "BT_ERROR_NOT_INITIALIZED";
356             break;
357         case BT_ERROR_NOT_ENABLED:
358             errStr = "BT_ERROR_NOT_ENABLED";
359             break;
360         case BT_ERROR_ALREADY_DONE:
361             errStr = "BT_ERROR_ALREADY_DONE";
362             break;
363         case BT_ERROR_OPERATION_FAILED:
364             errStr = "BT_ERROR_OPERATION_FAILED";
365             break;
366         case BT_ERROR_NOT_IN_PROGRESS:
367             errStr = "BT_ERROR_NOT_IN_PROGRESS";
368             break;
369         case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
370             errStr = "BT_ERROR_REMOTE_DEVICE_NOT_BONDED";
371             break;
372         case BT_ERROR_AUTH_REJECTED:
373             errStr = "BT_ERROR_AUTH_REJECTED";
374             break;
375         case BT_ERROR_AUTH_FAILED:
376             errStr = "BT_ERROR_AUTH_FAILED";
377             break;
378         case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
379             errStr = "BT_ERROR_REMOTE_DEVICE_NOT_FOUND";
380             break;
381         case BT_ERROR_SERVICE_SEARCH_FAILED:
382             errStr = "BT_ERROR_SERVICE_SEARCH_FAILED";
383             break;
384         case BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED:
385             errStr = "BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED";
386             break;
387         case BT_ERROR_PERMISSION_DENIED:
388             errStr = "BT_ERROR_PERMISSION_DENIED";
389             break;
390         case BT_ERROR_SERVICE_NOT_FOUND:
391             errStr = "BT_ERROR_SERVICE_NOT_FOUND";
392             break;
393         case BT_ERROR_NOT_SUPPORTED:
394             errStr = "BT_ERROR_NOT_SUPPORTED";
395             break;
396         case BT_ERROR_QUOTA_EXCEEDED:
397             errStr = "BT_ERROR_QUOTA_EXCEEDED";
398             break;
399         case BT_ERROR_NO_DATA:
400             errStr = "BT_ERROR_NO_DATA";
401             break;
402         case BT_ERROR_AGAIN:
403             errStr = "BT_ERROR_AGAIN";
404             break;
405         default:
406             errStr = "NOT Defined";
407             break;
408     }
409
410     return errStr;
411 }
412
413