Fix memory leak
[platform/core/connectivity/asp-manager.git] / src / session / asp-p2p-conn.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 <gio/gio.h>
31
32 #include<dlog.h>
33
34 /*****************************************************************************
35  * Application Service Platform Daemon headers
36  *****************************************************************************/
37 #include "asp-manager.h"
38 #include "asp-p2p-conn.h"
39 #include "asp-service.h"
40 #include "asp-tech.h"
41
42 /*****************************************************************************
43  * Macros and Typedefs
44  *****************************************************************************/
45
46 /*****************************************************************************
47  * Global Variables
48  *****************************************************************************/
49
50 static asp_p2p_conn_session_request_cb session_request_cb = NULL;
51 static gpointer session_request_cb_user_data = NULL;
52 static asp_p2p_conn_session_config_request_cb session_config_request_cb = NULL;
53 static gpointer session_config_request_cb_user_data = NULL;
54 static asp_p2p_conn_connect_status_cb connect_status_cb = NULL;
55 static gpointer connect_status_cb_user_data = NULL;
56 static asp_p2p_conn_ip_assigned_cb ip_assigned_cb = NULL;
57 static gpointer ip_assigned_cb_user_data = NULL;
58
59 /*****************************************************************************
60  * Local Functions Definition
61  *****************************************************************************/
62
63 void asp_p2p_conn_initialize()
64 {
65         __ASP_LOG_FUNC_ENTER__;
66
67         session_request_cb = NULL;
68         session_request_cb_user_data = NULL;
69         session_config_request_cb = NULL;
70         session_config_request_cb_user_data = NULL;
71         connect_status_cb = NULL;
72         connect_status_cb_user_data = NULL;
73
74         __ASP_LOG_FUNC_EXIT__;
75         return;
76 }
77
78 void asp_p2p_conn_deinitialize()
79 {
80         __ASP_LOG_FUNC_ENTER__;
81
82         session_request_cb = NULL;
83         session_request_cb_user_data = NULL;
84         session_config_request_cb = NULL;
85         session_config_request_cb_user_data = NULL;
86         connect_status_cb = NULL;
87         connect_status_cb_user_data = NULL;
88
89         __ASP_LOG_FUNC_EXIT__;
90         return;
91 }
92
93 void asp_p2p_conn_connect_session(const guint8 *session_mac, guint32 session_id,
94                                   const guint8 *service_mac,
95                                   guint32 adv_id, const guint8 *session_info, size_t info_length,
96                                   guint8 network_role, guint8 network_config)
97 {
98         __ASP_LOG_FUNC_ENTER__;
99         asp_tech_session_request_params_s params = {{0, } };
100         gint32 res = 0;
101
102         /* Make connect parameter for session request */
103         memcpy(params.session_mac, session_mac, MAC_LEN);
104         params.session_id = session_id;
105         memcpy(params.service_mac, service_mac, MAC_LEN);
106         params.advertisement_id = adv_id;
107         params.network_role = network_role;
108         params.network_config = network_config;
109         if (info_length != 0) {
110                 params.session_information = g_try_malloc0(info_length + 1);
111                 if (params.session_information)
112                         memcpy(params.session_information, session_info, info_length);
113         }
114
115         res = asp_tech_connect_session(ASP_TECH_P2P, &params);
116         if (res != 0 && connect_status_cb) {
117                 ASP_LOGE("request connect is failed");
118                 /* TODO: assign proper error code and connect status. */
119                 connect_status_cb(0, session_mac, session_id,
120                                   ASP_SESSION_CONNECT_STATUS_NETWORK_ROLE_REJECTED,
121                                   connect_status_cb_user_data);
122         }
123         g_free(params.session_information);
124
125         __ASP_LOG_FUNC_EXIT__;
126         return;
127 }
128
129 void asp_p2p_conn_confirm_session(const guint8 *session_mac, guint32 session_id,
130                                   gboolean confirmed, guint32 network_config_pin)
131 {
132         __ASP_LOG_FUNC_ENTER__;
133         gint32 res = 0;
134
135         res = asp_tech_confirm_session(ASP_TECH_P2P, session_mac,
136                                        session_id, confirmed, network_config_pin);
137         if (res < 0) {
138                 ASP_LOGE("confirm session is failed");
139                 return;
140         }
141
142         __ASP_LOG_FUNC_EXIT__;
143         return;
144 }
145
146 void asp_p2p_conn_disconnect_p2p(const guint8 *peer_mac)
147 {
148         __ASP_LOG_FUNC_ENTER__;
149         guint8 mac[MAC_LEN] = {0, };
150         gint32 res = 0;
151
152         memcpy(mac, peer_mac, MAC_LEN);
153         res = asp_tech_destroy_connection(ASP_TECH_P2P, mac, MAC_LEN);
154         if (res < 0) {
155                 ASP_LOGE("asp_tech_destroy_connection is failed");
156                 return;
157         }
158
159         __ASP_LOG_FUNC_EXIT__;
160         return;
161 }
162
163 gboolean asp_p2p_conn_peer_is_connected(const guint8 *peer_mac)
164 {
165         __ASP_LOG_FUNC_ENTER__;
166         gint32 is_peer_connected = 0;
167         gboolean is_connected = 0;
168         guint8 mac[MAC_LEN] = {0, };
169         gint32 res = 0;
170
171         memcpy(mac, peer_mac, MAC_LEN);
172         res = asp_tech_is_peer_connected(ASP_TECH_P2P, mac,
173                                          MAC_LEN, &is_peer_connected);
174         if (res < 0) {
175                 ASP_LOGE("asp_tech_is_peer_connected is failed");
176                 return FALSE;
177         }
178
179         is_connected = is_peer_connected;
180         __ASP_LOG_FUNC_EXIT__;
181         return is_connected;
182 }
183
184 void asp_p2p_conn_set_session_request_cb(asp_p2p_conn_session_request_cb cb,
185                 gpointer user_data)
186 {
187         __ASP_LOG_FUNC_ENTER__;
188
189         session_request_cb = cb;
190         session_request_cb_user_data = user_data;
191
192         asp_tech_set_session_request_cb(ASP_TECH_P2P, cb, user_data);
193
194         __ASP_LOG_FUNC_EXIT__;
195         return;
196 }
197
198 void asp_p2p_conn_set_session_config_request_cb(
199         asp_p2p_conn_session_config_request_cb cb, gpointer user_data)
200 {
201         __ASP_LOG_FUNC_ENTER__;
202
203         session_config_request_cb = cb;
204         session_config_request_cb_user_data = user_data;
205
206         asp_tech_set_session_config_request_cb(ASP_TECH_P2P, cb, user_data);
207
208         __ASP_LOG_FUNC_EXIT__;
209         return;
210 }
211
212 void asp_p2p_conn_set_connect_status_cb(asp_p2p_conn_connect_status_cb cb,
213                                         gpointer user_data)
214 {
215         __ASP_LOG_FUNC_ENTER__;
216
217         connect_status_cb = cb;
218         connect_status_cb_user_data = user_data;
219
220         asp_tech_set_connect_status_cb(ASP_TECH_P2P, cb, user_data);
221
222         __ASP_LOG_FUNC_EXIT__;
223         return;
224 }
225
226 void asp_p2p_conn_set_ip_assigned_cb(asp_p2p_conn_ip_assigned_cb cb,
227                                      gpointer user_data)
228 {
229         __ASP_LOG_FUNC_ENTER__;
230
231         ip_assigned_cb = cb;
232         ip_assigned_cb_user_data = user_data;
233
234         asp_tech_set_ip_assigned_cb(ASP_TECH_P2P, cb, user_data);
235
236         __ASP_LOG_FUNC_EXIT__;
237         return;
238 }
239
240 void asp_p2p_conn_get_p2p_mac(gchar *my_p2p_mac)
241 {
242         __ASP_LOG_FUNC_ENTER__;
243
244         asp_s *asp = asp_get_manager();
245         if (asp == NULL) {
246                 ASP_LOGE("memory allocation for asp-manager is failed");
247                 return;
248         }
249
250         memcpy(my_p2p_mac, asp->p2p_local_address, MAC_LEN);
251
252         __ASP_LOG_FUNC_EXIT__;
253         return;
254 }