Imported Upstream version 1.1.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 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, "clientList");
73     VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
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 CAResult_t CAGetLEServerInfo(LEServerInfoList *serverList, const char *leAddress,
106                              LEServerInfo **leServerInfo)
107 {
108
109     OIC_LOG(DEBUG, TAG, "IN");
110
111     VERIFY_NON_NULL(serverList, TAG, "clientList");
112     VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
113     VERIFY_NON_NULL(leAddress, TAG, "leAddress");
114
115     LEServerInfoList *cur = serverList;
116     *leServerInfo = NULL;
117     while (cur != NULL)
118     {
119         if (!strcasecmp(cur->serverInfo->remoteAddress , leAddress))
120         {
121             *leServerInfo = cur->serverInfo;
122             OIC_LOG(DEBUG, TAG, "OUT");
123             return CA_STATUS_OK;
124         }
125
126         cur = cur->next;
127     }
128
129     OIC_LOG(DEBUG, TAG, " OUT");
130     return CA_STATUS_FAILED;
131 }
132
133 CAResult_t CAGetLEServerInfoByPosition(LEServerInfoList *serverList, int32_t position,
134                                        LEServerInfo **leServerInfo)
135 {
136     OIC_LOG(DEBUG, TAG, "IN");
137
138     VERIFY_NON_NULL(serverList, TAG, "clientList");
139     VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
140
141     if (0 > position)
142     {
143         OIC_LOG(ERROR, TAG, "Position Invalid input !");
144         return CA_STATUS_INVALID_PARAM;
145     }
146
147     *leServerInfo = NULL;
148     int32_t count = 0;
149     LEServerInfoList *cur = serverList;
150     while (cur != NULL)
151     {
152         if (position == count)
153         {
154             *leServerInfo = cur->serverInfo;
155             OIC_LOG(DEBUG, TAG, "OUT");
156             return CA_STATUS_OK;
157         }
158         count++;
159         cur = cur->next;
160     }
161     OIC_LOG(DEBUG, TAG, "Client info not found for the position");
162     return CA_STATUS_FAILED;
163 }
164
165 void CAFreeLEServerList(LEServerInfoList *clientList)
166 {
167     OIC_LOG(DEBUG, TAG, "IN");
168     while (clientList)
169     {
170         LEServerInfoList *temp = clientList;
171         clientList = clientList->next;
172         CAFreeLEServerInfo(temp->serverInfo);
173         OICFree(temp);
174     }
175     OIC_LOG(DEBUG, TAG, "OUT");
176 }
177
178 void CAFreeLEServerInfo(LEServerInfo *leServerInfo)
179 {
180     OIC_LOG(DEBUG, TAG, "IN");
181     if (leServerInfo)
182     {
183         if (leServerInfo->remoteAddress)
184         {
185             bt_gatt_client_destroy(leServerInfo->clientHandle);
186             int32_t ret = bt_gatt_disconnect(leServerInfo->remoteAddress);
187
188             if (BT_ERROR_NONE != ret)
189             {
190                 OIC_LOG_V(ERROR, TAG,
191                           "bt_gatt_disconnect Failed with ret value [%d]",
192                           ret);
193                 return;
194             }
195             OICFree(leServerInfo->remoteAddress);
196         }
197         OICFree(leServerInfo);
198     }
199     OIC_LOG(DEBUG, TAG, "OUT");
200 }
201
202 const char *CALEGetErrorMsg(bt_error_e err)
203 {
204     const char *errStr = NULL;
205
206     switch (err)
207     {
208         case BT_ERROR_NONE:
209             errStr = "BT_ERROR_NONE";
210             break;
211         case BT_ERROR_CANCELLED:
212             errStr = "BT_ERROR_CANCELLED";
213             break;
214         case BT_ERROR_INVALID_PARAMETER:
215             errStr = "BT_ERROR_INVALID_PARAMETER";
216             break;
217         case BT_ERROR_OUT_OF_MEMORY:
218             errStr = "BT_ERROR_OUT_OF_MEMORY";
219             break;
220         case BT_ERROR_RESOURCE_BUSY:
221             errStr = "BT_ERROR_RESOURCE_BUSY";
222             break;
223         case BT_ERROR_TIMED_OUT:
224             errStr = "BT_ERROR_TIMED_OUT";
225             break;
226         case BT_ERROR_NOW_IN_PROGRESS:
227             errStr = "BT_ERROR_NOW_IN_PROGRESS";
228             break;
229         case BT_ERROR_NOT_INITIALIZED:
230             errStr = "BT_ERROR_NOT_INITIALIZED";
231             break;
232         case BT_ERROR_NOT_ENABLED:
233             errStr = "BT_ERROR_NOT_ENABLED";
234             break;
235         case BT_ERROR_ALREADY_DONE:
236             errStr = "BT_ERROR_ALREADY_DONE";
237             break;
238         case BT_ERROR_OPERATION_FAILED:
239             errStr = "BT_ERROR_OPERATION_FAILED";
240             break;
241         case BT_ERROR_NOT_IN_PROGRESS:
242             errStr = "BT_ERROR_NOT_IN_PROGRESS";
243             break;
244         case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
245             errStr = "BT_ERROR_REMOTE_DEVICE_NOT_BONDED";
246             break;
247         case BT_ERROR_AUTH_REJECTED:
248             errStr = "BT_ERROR_AUTH_REJECTED";
249             break;
250         case BT_ERROR_AUTH_FAILED:
251             errStr = "BT_ERROR_AUTH_FAILED";
252             break;
253         case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
254             errStr = "BT_ERROR_REMOTE_DEVICE_NOT_FOUND";
255             break;
256         case BT_ERROR_SERVICE_SEARCH_FAILED:
257             errStr = "BT_ERROR_SERVICE_SEARCH_FAILED";
258             break;
259         case BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED:
260             errStr = "BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED";
261             break;
262         case BT_ERROR_PERMISSION_DENIED:
263             errStr = "BT_ERROR_PERMISSION_DENIED";
264             break;
265         case BT_ERROR_SERVICE_NOT_FOUND:
266             errStr = "BT_ERROR_SERVICE_NOT_FOUND";
267             break;
268         case BT_ERROR_NOT_SUPPORTED:
269             errStr = "BT_ERROR_NOT_SUPPORTED";
270             break;
271         default:
272             errStr = "NOT Defined";
273             break;
274     }
275
276     return errStr;
277 }
278
279