BLE Adapter code updated for Tizen-4.0
[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 CAResult_t CAAddLEDataToList(LEDataList **dataList, const void *data, uint32_t dataLength)
52 {
53     OIC_LOG(DEBUG, TAG, "IN");
54
55     VERIFY_NON_NULL(dataList, TAG, "Data list is null");
56     VERIFY_NON_NULL(data, TAG, "data is null");
57
58     if (0 == dataLength)
59     {
60         OIC_LOG(ERROR, TAG, "Invalid input: data length is zero!");
61         return CA_STATUS_INVALID_PARAM;
62     }
63
64     LEDataList *pending_data = (LEDataList *) OICMalloc(sizeof(LEDataList));
65     if (NULL == pending_data)
66     {
67         OIC_LOG(ERROR, TAG, "OICMalloc failed (data list)!");
68         return CA_MEMORY_ALLOC_FAILED;
69     }
70
71     pending_data->data = (LEData *) OICMalloc(sizeof(LEData));
72     if (NULL == pending_data->data)
73     {
74         OIC_LOG(ERROR, TAG, "OICMalloc failed (data node)!");
75         OICFree(pending_data);
76         return CA_MEMORY_ALLOC_FAILED;
77     }
78
79     pending_data->next = NULL;
80     pending_data->data->data = (void *) OICMalloc(dataLength); //data
81     if (NULL == pending_data->data->data)
82     {
83         OIC_LOG(ERROR, TAG, "OICMalloc failed (data)!");
84         OICFree(pending_data->data);
85         OICFree(pending_data);
86         return CA_MEMORY_ALLOC_FAILED;
87     }
88
89     memcpy(pending_data->data->data, data, dataLength);
90     pending_data->data->dataLength = dataLength;
91
92     if (NULL == *dataList)
93     {
94         *dataList = pending_data;
95     }
96     else
97     {
98         LEDataList *curNode = *dataList;
99         while (curNode->next != NULL)
100         {
101             curNode = curNode->next;
102         }
103         curNode->next = pending_data;
104     }
105
106     OIC_LOG(DEBUG, TAG, "OUT");
107     return CA_STATUS_OK;
108 }
109
110
111 void CARemoveLEDataFromList(LEDataList **dataList)
112 {
113     OIC_LOG(DEBUG, TAG, "IN");
114
115     VERIFY_NON_NULL(dataList, TAG, "Data list is null");
116
117     if (*dataList)
118     {
119         LEDataList *curNode = *dataList;
120         *dataList = (*dataList)->next;
121
122         //Delete the first node
123         CADestroyLEData(curNode->data);
124         OICFree(curNode);
125     }
126
127     OIC_LOG(DEBUG, TAG, "OUT");
128 }
129
130 void CADestroyLEDataList(LEDataList **dataList)
131 {
132     OIC_LOG(DEBUG, TAG, "IN");
133
134     VERIFY_NON_NULL_VOID(dataList, TAG, "Data list is null");
135
136     while (*dataList)
137     {
138         LEDataList *curNode = *dataList;
139         *dataList = (*dataList)->next;
140
141         CADestroyLEData(curNode->data);
142         OICFree(curNode);
143     }
144
145     *dataList = NULL;
146
147     OIC_LOG(DEBUG, TAG, "OUT");
148 }
149
150 void CADestroyLEData(LEData *data)
151 {
152     if (data)
153     {
154         OICFree(data->data);
155         OICFree(data);
156     }
157 }
158
159 CAResult_t CAAddLEServerInfoToList(LEServerInfoList **serverList,
160                                    LEServerInfo *leServerInfo)
161 {
162
163     OIC_LOG(DEBUG, TAG, "IN");
164
165     VERIFY_NON_NULL(serverList, TAG, "serverList");
166     VERIFY_NON_NULL(leServerInfo, TAG, "leServerInfo");
167
168     LEServerInfoList *node = (LEServerInfoList *) OICCalloc(1, sizeof(LEServerInfoList));
169     if (NULL == node)
170     {
171         OIC_LOG(ERROR, TAG, "Calloc failed!");
172         return CA_STATUS_FAILED;
173     }
174
175     node->serverInfo = leServerInfo;
176     node->next = NULL;
177
178     if (*serverList == NULL)   // Empty list
179     {
180         *serverList = node;
181     }
182     else     // Add at front end
183     {
184         node->next = *serverList;
185         *serverList = node;
186     }
187
188     OIC_LOG_V(DEBUG, TAG, "Device [%s] added to list",
189               leServerInfo->remoteAddress);
190
191     OIC_LOG(DEBUG, TAG, "OUT");
192
193     return CA_STATUS_OK;
194 }
195
196 void CARemoveLEServerInfoFromList(LEServerInfoList **serverList,
197                                         const char *remoteAddress)
198 {
199     OIC_LOG(DEBUG, TAG, "IN");
200     VERIFY_NON_NULL_VOID(serverList, TAG, "serverList");
201     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remoteAddress");
202
203     LEServerInfoList *temp = *serverList;
204     LEServerInfoList *prev = NULL;
205     while (temp)
206     {
207         if (!strcasecmp(temp->serverInfo->remoteAddress, remoteAddress))
208         {
209             if (NULL == prev)
210             {
211                 *serverList = temp->next;
212             }
213             else
214             {
215                 prev->next = temp->next;
216             }
217             
218             CAFreeLEServerInfo(temp->serverInfo);
219             OICFree(temp);
220             OIC_LOG_V(DEBUG, TAG, "Device [%s] removed from list", remoteAddress);
221             break;
222         }
223         prev = temp;
224         temp = temp->next;
225     }
226
227     OIC_LOG(DEBUG, TAG, "OUT");
228 }
229
230 CAResult_t CAGetLEServerInfo(LEServerInfoList *serverList, const char *leAddress,
231                              LEServerInfo **leServerInfo)
232 {
233
234     OIC_LOG(DEBUG, TAG, "IN");
235
236     VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
237     VERIFY_NON_NULL(leAddress, TAG, "leAddress");
238
239     if (NULL == serverList)
240     {
241         OIC_LOG(DEBUG, TAG, "Server list is empty");
242         return CA_STATUS_FAILED;
243     }
244
245     LEServerInfoList *cur = serverList;
246     *leServerInfo = NULL;
247     while (cur != NULL)
248     {
249         if (!strcasecmp(cur->serverInfo->remoteAddress , leAddress))
250         {
251             *leServerInfo = cur->serverInfo;
252             OIC_LOG(DEBUG, TAG, "OUT");
253             return CA_STATUS_OK;
254         }
255
256         cur = cur->next;
257     }
258
259     OIC_LOG(DEBUG, TAG, " OUT");
260     return CA_STATUS_FAILED;
261 }
262
263
264
265 void CAFreeLEServerList(LEServerInfoList *serverList)
266 {
267     OIC_LOG(DEBUG, TAG, "IN");
268     while (serverList)
269     {
270         LEServerInfoList *temp = serverList;
271         serverList = serverList->next;
272         CAFreeLEServerInfo(temp->serverInfo);
273         OICFree(temp);
274     }
275     OIC_LOG(DEBUG, TAG, "OUT");
276 }
277
278 void CAFreeLEServerInfo(LEServerInfo *leServerInfo)
279 {
280     OIC_LOG(DEBUG, TAG, "IN");
281     if (leServerInfo)
282     {
283         if (leServerInfo->clientHandle)
284         {
285             bt_gatt_client_destroy(leServerInfo->clientHandle);
286         }
287
288         if (leServerInfo->pendingDataList)
289         {
290             CADestroyLEDataList(&(leServerInfo->pendingDataList));
291         }
292
293         if (leServerInfo->status > LE_STATUS_CONNECTED)
294         {    int32_t ret = bt_gatt_disconnect(leServerInfo->remoteAddress);
295
296             if (BT_ERROR_NONE != ret)
297             {
298                 OIC_LOG_V(ERROR, TAG,
299                           "bt_gatt_disconnect Failed with ret value [%d]",
300                           ret);
301                 return;
302             }
303         }
304         OICFree(leServerInfo->remoteAddress);
305         OICFree(leServerInfo);
306     }
307     OIC_LOG(DEBUG, TAG, "OUT");
308 }
309
310 CAResult_t CAAddLEClientInfoToList(LEClientInfoList **clientList,
311                                    char *clientAddress)
312 {
313     OIC_LOG(DEBUG, TAG, "IN");
314     VERIFY_NON_NULL(clientList, TAG, "clientList");
315     VERIFY_NON_NULL(clientAddress, TAG, "clientAddress");
316
317     LEClientInfoList *node = (LEClientInfoList *) OICCalloc(1, sizeof(LEClientInfoList));
318     if (NULL == node)
319     {
320         OIC_LOG(ERROR, TAG, "Malloc failed!");
321         return CA_STATUS_FAILED;
322     }
323
324     node->remoteAddress= clientAddress;
325     node->next = NULL;
326
327     if (*clientList == NULL)   // Empty list
328     {
329         *clientList = node;
330     }
331     else     // Add at front end
332     {
333         node->next = *clientList;
334         *clientList = node;
335     }
336
337     OIC_LOG_V(DEBUG, TAG, "Device [%s] added to list", clientAddress);
338     OIC_LOG(DEBUG, TAG, "OUT");
339     return CA_STATUS_OK;
340 }
341
342 CAResult_t CAIsLEClientInfoInList(LEClientInfoList *clientList,
343                                         const char *clientAddress)
344 {
345     OIC_LOG(DEBUG, TAG, "IN");
346
347     LEClientInfoList *temp = clientList;
348     while (temp)
349     {
350         if (!strcasecmp(temp->remoteAddress, clientAddress))
351         {
352             return CA_STATUS_OK;
353         }
354         temp = temp->next;
355     }
356
357     OIC_LOG(DEBUG, TAG, "OUT");
358     return CA_STATUS_FAILED;
359 }
360
361 void CARemoveLEClientInfoFromList(LEClientInfoList **clientList,
362                                   const char *clientAddress)
363 {
364     OIC_LOG(DEBUG, TAG, "IN");
365     VERIFY_NON_NULL_VOID(clientAddress, TAG, "clientAddress");
366
367     LEClientInfoList *temp = *clientList;
368     LEClientInfoList *prev = NULL;
369     while (temp)
370     {
371         if (!strcasecmp(temp->remoteAddress, clientAddress))
372         {
373             if (NULL == prev)
374             {
375                 *clientList = temp->next;
376             }
377             else
378             {
379                 prev->next = temp->next;
380             }
381             OICFree(temp->remoteAddress);
382             OICFree(temp);
383             OIC_LOG_V(DEBUG, TAG, "Device [%s] removed from list", clientAddress);
384             break;
385         }
386         prev = temp;
387         temp = temp->next;
388     }
389
390     OIC_LOG(DEBUG, TAG, "OUT");
391 }
392
393 void CADisconnectAllClient(LEClientInfoList *clientList)
394 {
395     OIC_LOG(DEBUG, TAG, "IN");
396     while (clientList)
397     {
398         LEClientInfoList *temp = clientList;
399         clientList = clientList->next;
400         if (temp->remoteAddress)
401         {
402             int32_t ret = bt_gatt_disconnect(temp->remoteAddress);
403
404             if (BT_ERROR_NONE != ret)
405             {
406                 OIC_LOG_V(ERROR, TAG,
407                           "bt_gatt_disconnect Failed with ret value [%d]",
408                           ret);
409                 return;
410             }
411             OICFree(temp->remoteAddress);
412         }
413         OICFree(temp);
414     }
415     OIC_LOG(DEBUG, TAG, "OUT");
416 }
417
418 const char *CALEGetErrorMsg(bt_error_e err)
419 {
420     const char *errStr = NULL;
421
422     switch (err)
423     {
424         case BT_ERROR_NONE:
425             errStr = "BT_ERROR_NONE";
426             break;
427         case BT_ERROR_CANCELLED:
428             errStr = "BT_ERROR_CANCELLED";
429             break;
430         case BT_ERROR_INVALID_PARAMETER:
431             errStr = "BT_ERROR_INVALID_PARAMETER";
432             break;
433         case BT_ERROR_OUT_OF_MEMORY:
434             errStr = "BT_ERROR_OUT_OF_MEMORY";
435             break;
436         case BT_ERROR_RESOURCE_BUSY:
437             errStr = "BT_ERROR_RESOURCE_BUSY";
438             break;
439         case BT_ERROR_TIMED_OUT:
440             errStr = "BT_ERROR_TIMED_OUT";
441             break;
442         case BT_ERROR_NOW_IN_PROGRESS:
443             errStr = "BT_ERROR_NOW_IN_PROGRESS";
444             break;
445         case BT_ERROR_NOT_INITIALIZED:
446             errStr = "BT_ERROR_NOT_INITIALIZED";
447             break;
448         case BT_ERROR_NOT_ENABLED:
449             errStr = "BT_ERROR_NOT_ENABLED";
450             break;
451         case BT_ERROR_ALREADY_DONE:
452             errStr = "BT_ERROR_ALREADY_DONE";
453             break;
454         case BT_ERROR_OPERATION_FAILED:
455             errStr = "BT_ERROR_OPERATION_FAILED";
456             break;
457         case BT_ERROR_NOT_IN_PROGRESS:
458             errStr = "BT_ERROR_NOT_IN_PROGRESS";
459             break;
460         case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
461             errStr = "BT_ERROR_REMOTE_DEVICE_NOT_BONDED";
462             break;
463         case BT_ERROR_AUTH_REJECTED:
464             errStr = "BT_ERROR_AUTH_REJECTED";
465             break;
466         case BT_ERROR_AUTH_FAILED:
467             errStr = "BT_ERROR_AUTH_FAILED";
468             break;
469         case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
470             errStr = "BT_ERROR_REMOTE_DEVICE_NOT_FOUND";
471             break;
472         case BT_ERROR_SERVICE_SEARCH_FAILED:
473             errStr = "BT_ERROR_SERVICE_SEARCH_FAILED";
474             break;
475         case BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED:
476             errStr = "BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED";
477             break;
478         case BT_ERROR_PERMISSION_DENIED:
479             errStr = "BT_ERROR_PERMISSION_DENIED";
480             break;
481         case BT_ERROR_SERVICE_NOT_FOUND:
482             errStr = "BT_ERROR_SERVICE_NOT_FOUND";
483             break;
484         case BT_ERROR_NOT_SUPPORTED:
485             errStr = "BT_ERROR_NOT_SUPPORTED";
486             break;
487         case BT_ERROR_QUOTA_EXCEEDED:
488             errStr = "BT_ERROR_QUOTA_EXCEEDED";
489             break;
490         case BT_ERROR_NO_DATA:
491             errStr = "BT_ERROR_NO_DATA";
492             break;
493         case BT_ERROR_AGAIN:
494             errStr = "BT_ERROR_AGAIN";
495             break;
496         default:
497             errStr = "NOT Defined";
498             break;
499     }
500
501     return errStr;
502 }
503
504