Fix memory leak
[platform/core/connectivity/asp-manager.git] / src / asp-tech.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*****************************************************************************
18  * Standard headers
19  *****************************************************************************/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25
26 /*****************************************************************************
27  * System headers
28  *****************************************************************************/
29
30 #include <glib.h>
31 #include <dlog.h>
32
33 /*****************************************************************************
34  * Application Service Platform Daemon headers
35  *****************************************************************************/
36 #include "asp-manager-util.h"
37 #include "asp-service.h"
38 #include "asp-session.h"
39 #include "asp-tech.h"
40
41 /*****************************************************************************
42  * Macros and Typedefs
43  *****************************************************************************/
44
45 /* TODO : define service data structure */
46
47 /*****************************************************************************
48  * Global Variables
49  *****************************************************************************/
50
51 /* TODO : define local memory for service data structure */
52
53 extern asp_tech_ops_s asp_tech_p2p_ops;
54 extern asp_tech_ops_s asp_tech_ble_ops;
55 extern asp_tech_ops_s asp_tech_nfc_ops;
56 extern asp_tech_ops_s asp_tech_infra_ops;
57 extern asp_tech_ops_s asp_tech_nan_ops;
58
59 asp_tech_ops_s *asp_techs[] = {
60                 NULL,
61                 &asp_tech_p2p_ops,
62                 &asp_tech_ble_ops,
63                 &asp_tech_nfc_ops,
64                 &asp_tech_infra_ops,
65                 &asp_tech_nan_ops,
66                 NULL
67 };
68
69 /*****************************************************************************
70  * Local Functions Definition
71  *****************************************************************************/
72
73 void asp_tech_init()
74 {
75         __ASP_LOG_FUNC_ENTER__;
76         gint32 result = 0;
77
78         result = asp_techs[ASP_TECH_P2P]->init();
79         if (result < 0) {
80                 ASP_LOGE("Failed to initialize p2p");
81                 /* TODO : exclude p2p technology */
82         }
83
84         result = asp_techs[ASP_TECH_INFRA]->init();
85         if (result < 0) {
86                 ASP_LOGE("Failed to initialize infra");
87                 /* TODO : exclude infra technology */
88         }
89
90         __ASP_LOG_FUNC_EXIT__;
91         return;
92 }
93
94 void asp_tech_deinit()
95 {
96         __ASP_LOG_FUNC_ENTER__;
97         gint32 result = 0;
98
99         result = asp_techs[ASP_TECH_P2P]->deinit();
100         if (result < 0) {
101                 ASP_LOGE("Failed to deinitialize p2p");
102                 /* TODO : exclude p2p technology */
103         }
104
105         result = asp_techs[ASP_TECH_INFRA]->deinit();
106         if (result < 0) {
107                 ASP_LOGE("Failed to deinitialize infra");
108                 /* TODO : exclude infra technology */
109         }
110
111         __ASP_LOG_FUNC_EXIT__;
112         return;
113 }
114
115 gint32 asp_tech_advertise(gint32 tech, asp_service_advertise_s *service, gint32 replace)
116 {
117         __ASP_LOG_FUNC_ENTER__;
118         gint32 tech_idx = 0;
119         gint32 i = 0;
120         gint32 result = 0;
121
122         for (i = ASP_TECH_P2P; i != ASP_TECH_MAX; i++) {
123                 tech_idx =  1 << (i - 1);
124                 if (tech & tech_idx) {
125                         ASP_LOGD("Advertise using tech %d", tech);
126                         result = asp_techs[i]->advertise(service, replace);
127                 }
128                 /* TODO */
129                 /* error handling */
130         }
131
132         __ASP_LOG_FUNC_EXIT__;
133         return result;
134 }
135
136 gint32 asp_tech_cancel_advertise(gint32 tech, asp_service_advertise_s *service)
137 {
138         __ASP_LOG_FUNC_ENTER__;
139         gint32 tech_idx = 0;
140         gint32 i = 0;
141         gint32 result = 0;
142
143         for (i = ASP_TECH_P2P; i != ASP_TECH_MAX; i++) {
144                 tech_idx =  1 << (i - 1);
145                 if (tech & tech_idx) {
146                         ASP_LOGD("Cancel advertise using tech %d", tech_idx);
147                         result = asp_techs[i]->cancel_advertise(service);
148                 }
149                 /* TODO */
150                 /* error handling */
151         }
152         __ASP_LOG_FUNC_EXIT__;
153         return result;
154 }
155
156 gint32 asp_tech_seek(gint32 tech, asp_service_seek_s *service)
157 {
158         __ASP_LOG_FUNC_ENTER__;
159         gint32 tech_idx = 0;
160         gint32 i = 0;
161         gint32 result = 0;
162
163         for (i = 1; i != ASP_TECH_MAX; i++) {
164                 tech_idx =  1 << (i - 1);
165                 if (tech & tech_idx) {
166                         ASP_LOGD("Seek service using tech %d", tech_idx);
167                         result = asp_techs[i]->seek(service);
168                 }
169                 /* TODO */
170                 /* error handling */
171         }
172         __ASP_LOG_FUNC_EXIT__;
173         return result;
174 }
175
176 gint32 asp_tech_cancel_seek(gint32 tech, asp_service_seek_s *service)
177 {
178         __ASP_LOG_FUNC_ENTER__;
179         gint32 tech_idx = 0;
180         gint32 i = 0;
181         gint32 result = 0;
182
183         for (i = 1; i != ASP_TECH_MAX; i++) {
184                 tech_idx =  1 << (i - 1);
185                 if (tech & tech_idx) {
186                         ASP_LOGD("Cancel seek service using tech %d", tech_idx);
187                         result = asp_techs[i]->cancel_seek(service);
188                 }
189                 /* TODO */
190                 /* error handling */
191         }
192         __ASP_LOG_FUNC_EXIT__;
193         return result;
194 }
195
196 gint32 asp_tech_connect_session(gint32 tech, asp_tech_session_request_params_s *params)
197 {
198         __ASP_LOG_FUNC_ENTER__;
199         gint32 tech_idx = 0;
200         gint32 i = 0;
201         gint32 result = 0;
202
203         for (i = 1; i != ASP_TECH_MAX; i++) {
204                 tech_idx =  1 << (i - 1);
205                 if (tech & tech_idx) {
206                         ASP_LOGD("Connect session using tech %d", tech_idx);
207                         result = asp_techs[i]->connect_session(params);
208                         /* TODO */
209                         /* error handling */
210                         break;
211                 }
212         }
213         __ASP_LOG_FUNC_EXIT__;
214         return result;
215 }
216
217 gint32 asp_tech_confirm_session(gint32 tech, const guint8 *session_mac, gint32 session_id, gint32 confirm, guint32 pin)
218 {
219         __ASP_LOG_FUNC_ENTER__;
220         gint32 tech_idx = 0;
221         gint32 i = 0;
222         gint32 result = 0;
223
224         for (i = 1; i != ASP_TECH_MAX; i++) {
225                 tech_idx =  1 << (i - 1);
226                 if (tech & tech_idx) {
227                         ASP_LOGD("Confirm session using tech %d", tech_idx);
228                         result = asp_techs[i]->confirm_session(session_mac, session_id, confirm, pin);
229                         /* TODO */
230                         /* error handling */
231                         break;
232                 }
233         }
234         __ASP_LOG_FUNC_EXIT__;
235         return result;
236 }
237
238 gint32 asp_tech_destroy_connection(gint32 tech, const guint8 *peer_id, gint32 peer_id_length)
239 {
240         __ASP_LOG_FUNC_ENTER__;
241         gint32 tech_idx = 0;
242         gint32 i = 0;
243         gint32 result = 0;
244
245         for (i = 1; i != ASP_TECH_MAX; i++) {
246                 tech_idx =  1 << (i - 1);
247                 if (tech & tech_idx) {
248                         ASP_LOGD("Destroy connection using tech %d", tech_idx);
249                         result = asp_techs[i]->destroy_connection(peer_id, peer_id_length);
250                 }
251                 /* TODO */
252                 /* error handling */
253         }
254         __ASP_LOG_FUNC_EXIT__;
255         return result;
256 }
257
258 gint32 asp_tech_is_peer_connected(gint32 tech, const guint8 *peer_id, gint32 peer_id_length, gint32 *is_connected)
259 {
260         __ASP_LOG_FUNC_ENTER__;
261         gint32 tech_idx = 0;
262         gint32 i = 0;
263         gint32 result = 0;
264
265         for (i = 1; i != ASP_TECH_MAX; i++) {
266                 tech_idx =  1 << (i - 1);
267                 if (tech & tech_idx) {
268                         ASP_LOGD("Is peer connected using tech %d", tech_idx);
269                         result = asp_techs[i]->is_peer_connected(peer_id, peer_id_length, is_connected);
270                 }
271                 /* TODO */
272                 /* error handling */
273         }
274         __ASP_LOG_FUNC_EXIT__;
275         return result;
276 }
277
278 void asp_tech_set_session_request_cb(gint32 tech, asp_tech_session_request_cb cb, gpointer user_data)
279 {
280         __ASP_LOG_FUNC_ENTER__;
281         gint32 tech_idx = 0;
282         gint32 i = 0;
283
284         for (i = 1; i != ASP_TECH_MAX; i++) {
285                 tech_idx =  1 << (i - 1);
286                 if (tech & tech_idx) {
287                         ASP_LOGD("Set session_request_cb using tech %d", tech_idx);
288                         asp_techs[i]->session_request_cb = cb;
289                         asp_techs[i]->session_request_cb_user_data = user_data;
290                 }
291                 /* TODO */
292                 /* error handling */
293         }
294         __ASP_LOG_FUNC_EXIT__;
295         return;
296 }
297
298 void asp_tech_set_session_config_request_cb(gint32 tech, asp_tech_session_config_request_cb cb, gpointer user_data)
299 {
300         __ASP_LOG_FUNC_ENTER__;
301         gint32 tech_idx = 0;
302         gint32 i = 0;
303
304         for (i = 1; i != ASP_TECH_MAX; i++) {
305                 tech_idx =  1 << (i - 1);
306                 if (tech & tech_idx) {
307                         ASP_LOGD("Set session_config_request_cb using tech %d", tech_idx);
308                         asp_techs[i]->session_config_request_cb = cb;
309                         asp_techs[i]->session_config_request_cb_user_data = user_data;
310                 }
311                 /* TODO */
312                 /* error handling */
313         }
314         __ASP_LOG_FUNC_EXIT__;
315         return;
316 }
317
318 void asp_tech_set_connect_status_cb(gint32 tech, asp_tech_connect_status_cb cb, gpointer user_data)
319 {
320         __ASP_LOG_FUNC_ENTER__;
321         gint32 tech_idx = 0;
322         gint32 i = 0;
323
324         for (i = 1; i != ASP_TECH_MAX; i++) {
325                 tech_idx =  1 << (i - 1);
326                 if (tech & tech_idx) {
327                         ASP_LOGD("Set connect_status_cb using tech %d", tech_idx);
328                         asp_techs[i]->connect_status_cb = cb;
329                         asp_techs[i]->connect_status_cb_user_data = user_data;
330                 }
331                 /* TODO */
332                 /* error handling */
333         }
334         __ASP_LOG_FUNC_EXIT__;
335         return;
336 }
337
338 void asp_tech_set_ip_assigned_cb(gint32 tech, asp_tech_ip_assigned_cb cb, gpointer user_data)
339 {
340         __ASP_LOG_FUNC_ENTER__;
341         gint32 tech_idx = 0;
342         gint32 i = 0;
343
344         for (i = 1; i != ASP_TECH_MAX; i++) {
345                 tech_idx =  1 << (i - 1);
346                 if (tech & tech_idx) {
347                         ASP_LOGD("Set ip_assigned_cb using tech %d", tech_idx);
348                         asp_techs[i]->ip_assigned_cb = cb;
349                         asp_techs[i]->ip_assigned_cb_user_data = user_data;
350                 }
351                 /* TODO */
352                 /* error handling */
353         }
354         __ASP_LOG_FUNC_EXIT__;
355         return;
356 }