41ec6e0c988a49d46c7fdd813b1f93ebf215125e
[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                 memcpy(params.session_information, session_info, info_length);
112         }
113
114         res = asp_tech_connect_session(ASP_TECH_P2P, &params);
115         if (res != 0 && connect_status_cb) {
116                 ASP_LOGE("request connect is failed");
117                 /* TODO: assign proper error code and connect status. */
118                 connect_status_cb(0, session_mac, session_id,
119                                   ASP_SESSION_CONNECT_STATUS_NETWORK_ROLE_REJECTED,
120                                   connect_status_cb_user_data);
121         }
122         g_free(params.session_information);
123
124         __ASP_LOG_FUNC_EXIT__;
125         return;
126 }
127
128 void asp_p2p_conn_confirm_session(const guint8 *session_mac, guint32 session_id,
129                                   gboolean confirmed, guint32 network_config_pin)
130 {
131         __ASP_LOG_FUNC_ENTER__;
132         gint32 res = 0;
133
134         res = asp_tech_confirm_session(ASP_TECH_P2P, session_mac,
135                                        session_id, confirmed, network_config_pin);
136         if (res < 0) {
137                 ASP_LOGE("confirm session is failed");
138                 return;
139         }
140
141         __ASP_LOG_FUNC_EXIT__;
142         return;
143 }
144
145 void asp_p2p_conn_disconnect_p2p(const guint8 *peer_mac)
146 {
147         __ASP_LOG_FUNC_ENTER__;
148         guint8 mac[MAC_LEN] = {0, };
149         gint32 res = 0;
150
151         memcpy(mac, peer_mac, MAC_LEN);
152         res = asp_tech_destroy_connection(ASP_TECH_P2P, mac, MAC_LEN);
153         if (res < 0) {
154                 ASP_LOGE("asp_tech_destroy_connection is failed");
155                 return;
156         }
157
158         __ASP_LOG_FUNC_EXIT__;
159         return;
160 }
161
162 gboolean asp_p2p_conn_peer_is_connected(const guint8 *peer_mac)
163 {
164         __ASP_LOG_FUNC_ENTER__;
165         gint32 is_peer_connected = 0;
166         gboolean is_connected = 0;
167         guint8 mac[MAC_LEN] = {0, };
168         gint32 res = 0;
169
170         memcpy(mac, peer_mac, MAC_LEN);
171         res = asp_tech_is_peer_connected(ASP_TECH_P2P, mac,
172                                          MAC_LEN, &is_peer_connected);
173         if (res < 0) {
174                 ASP_LOGE("asp_tech_is_peer_connected is failed");
175                 return FALSE;
176         }
177
178         is_connected = is_peer_connected;
179         __ASP_LOG_FUNC_EXIT__;
180         return is_connected;
181 }
182
183 void asp_p2p_conn_set_session_request_cb(asp_p2p_conn_session_request_cb cb,
184                 gpointer user_data)
185 {
186         __ASP_LOG_FUNC_ENTER__;
187
188         session_request_cb = cb;
189         session_request_cb_user_data = user_data;
190
191         asp_tech_set_session_request_cb(ASP_TECH_P2P, cb, user_data);
192
193         __ASP_LOG_FUNC_EXIT__;
194         return;
195 }
196
197 void asp_p2p_conn_set_session_config_request_cb(
198         asp_p2p_conn_session_config_request_cb cb, gpointer user_data)
199 {
200         __ASP_LOG_FUNC_ENTER__;
201
202         session_config_request_cb = cb;
203         session_config_request_cb_user_data = user_data;
204
205         asp_tech_set_session_config_request_cb(ASP_TECH_P2P, cb, user_data);
206
207         __ASP_LOG_FUNC_EXIT__;
208         return;
209 }
210
211 void asp_p2p_conn_set_connect_status_cb(asp_p2p_conn_connect_status_cb cb,
212                                         gpointer user_data)
213 {
214         __ASP_LOG_FUNC_ENTER__;
215
216         connect_status_cb = cb;
217         connect_status_cb_user_data = user_data;
218
219         asp_tech_set_connect_status_cb(ASP_TECH_P2P, cb, user_data);
220
221         __ASP_LOG_FUNC_EXIT__;
222         return;
223 }
224
225 void asp_p2p_conn_set_ip_assigned_cb(asp_p2p_conn_ip_assigned_cb cb,
226                                      gpointer user_data)
227 {
228         __ASP_LOG_FUNC_ENTER__;
229
230         ip_assigned_cb = cb;
231         ip_assigned_cb_user_data = user_data;
232
233         asp_tech_set_ip_assigned_cb(ASP_TECH_P2P, cb, user_data);
234
235         __ASP_LOG_FUNC_EXIT__;
236         return;
237 }
238
239 void asp_p2p_conn_get_p2p_mac(gchar *my_p2p_mac)
240 {
241         __ASP_LOG_FUNC_ENTER__;
242
243         asp_s *asp = asp_get_manager();
244         if (asp == NULL) {
245                 ASP_LOGE("memory allocation for asp-manager is failed");
246                 return;
247         }
248
249         memcpy(my_p2p_mac, asp->p2p_local_address, MAC_LEN);
250
251         __ASP_LOG_FUNC_EXIT__;
252         return;
253 }