9aff5c8f00d42d17f6704faa0203581613d85e53
[platform/core/connectivity/asp-manager.git] / src / session / asp-session.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 <gio/gio.h>
32
33 #include<dlog.h>
34
35 /*****************************************************************************
36  * Application Service Platform Daemon headers
37  *****************************************************************************/
38 #include "asp-manager.h"
39 #include "asp-manager-util.h"
40 #include "asp-service.h"
41 #include "asp-session.h"
42 #include "asp-manager-gdbus.h"
43 #include "asp-tech.h"
44
45 #include "../generated-code.h"
46
47 /*****************************************************************************
48  * Macros and Typedefs
49  *****************************************************************************/
50
51 /* TODO : define session data structure */
52
53 /*****************************************************************************
54  * Global Variables
55  *****************************************************************************/
56
57 GList *g_sessions = NULL;
58
59 /*****************************************************************************
60  * Local Functions Definition
61  *****************************************************************************/
62
63 /* Allocate memory for session and initialized it */
64 asp_session_s * asp_session_create(unsigned char *session_mac, int session_id, unsigned int adv_id)
65 {
66         __ASP_LOG_FUNC_ENTER__;
67         asp_session_s *session = NULL;
68
69         session = (asp_session_s *)g_try_malloc0(sizeof(asp_session_s));
70         if (session == NULL) {
71                 ASP_LOGE("memory allocation for session is failed");
72                 return NULL;
73         }
74
75         memcpy(session->service_mac, session_mac, MAC_LEN);
76         session->session_id = session_id;
77         session->advertisement_id = adv_id;
78         session->state = ASP_SESSION_CLOESD;
79
80         g_sessions = g_list_append(g_sessions, session);
81
82         __ASP_LOG_FUNC_EXIT__;
83         return session;
84 }
85
86 /* Deallocate memory for session */
87 int asp_session_destroy(unsigned char *session_mac, int session_id)
88 {
89         __ASP_LOG_FUNC_ENTER__;
90         asp_session_s *session = NULL;
91         int res = 0;
92
93         session = asp_session_get_session(session_mac, session_id);
94         if (session == NULL) {
95                 ASP_LOGE("There's no session");
96                 return -1;
97         }
98
99         g_sessions = g_list_remove(g_sessions, session);
100         g_free(session);
101
102         __ASP_LOG_FUNC_EXIT__;
103         return res;
104 }
105
106 int asp_session_connect_session(asp_connect_session_params *params, asp_session_s **session)
107 {
108         __ASP_LOG_FUNC_ENTER__;
109         asp_s *asp = NULL;
110         asp_session_s * local_session = NULL;
111         int tech = 0;
112         int res = 0;
113
114         asp = asp_get_manager();
115         if (asp == NULL) {
116                 ASP_LOGE("memory allocation for session is failed");
117                 return -1;
118         }
119
120         /* Make local p2p address as session mac */
121         local_session = asp_session_create(asp->p2p_local_address,
122                         params->session_id, params->advertisement_id);
123         if (local_session == NULL) {
124                 ASP_LOGE("memory allocation for session is failed");
125                 return ASP_ERROR_MEMORY;
126         }
127
128         /* TODO : request connection on proper technology */
129         tech = ASP_TECH_P2P;
130         memcpy(params->session_mac, local_session->session_mac, MAC_LEN);
131         res = asp_tech_connect_session(tech, params);
132         if (res < 0) {
133                 ASP_LOGE("request connect is failed");
134                 asp_session_destroy(local_session->session_mac, local_session->session_id);
135                 return -1;
136         }
137         *session = local_session;
138         asp_state_set(local_session, ASP_SESSION_INITIATED, 0, NULL);
139
140         __ASP_LOG_FUNC_EXIT__;
141         return res;
142 }
143
144 int asp_session_confirm_session(unsigned char *session_mac, int session_id, int confirm, const char *pin)
145 {
146         __ASP_LOG_FUNC_ENTER__;
147         asp_session_s *session = NULL;
148         int res = 0;
149
150         session = asp_session_get_session(session_mac, session_id);
151         if (session == NULL) {
152                 ASP_LOGE("There's no session");
153                 return -1;
154         }
155         res = asp_tech_confirm_session(ASP_TECH_P2P, session_mac,
156                         session_id, confirm, pin);
157         if (res < 0) {
158                 ASP_LOGE("confirm session is failed");
159                 asp_state_set(session, ASP_SESSION_CLOESD, ASP_SESSION_CLOSE_SYSTEM_FAILURE, NULL);
160                 asp_session_destroy(session_mac, session_id);
161                 return -1;
162         }
163
164         __ASP_LOG_FUNC_EXIT__;
165         return res;
166 }
167
168 asp_session_s *asp_session_get_session(unsigned char *session_mac, int session_id)
169 {
170         __ASP_LOG_FUNC_ENTER__;
171         GList *list;
172         asp_session_s *session = NULL;
173
174         for (list = g_sessions; list != NULL; list = list->next) {
175                 session = list->data;
176                 if (session != NULL) {
177                         if (session->session_id == session_id &&
178                                         memcmp(session->session_mac, session_mac, MAC_LEN)) {
179                                 break;
180                         }
181                 }
182                 session = NULL;
183         }
184
185         __ASP_LOG_FUNC_EXIT__;
186         return session;
187 }
188
189 void asp_session_set_session_ready(unsigned char *session_mac, int session_id)
190 {
191         __ASP_LOG_FUNC_ENTER__;
192
193         __ASP_LOG_FUNC_EXIT__;
194         return;
195 }
196
197 int asp_session_close_session(unsigned char *session_mac, int session_id)
198 {
199         __ASP_LOG_FUNC_ENTER__;
200         asp_session_s *session = NULL;
201         int res = 0;
202
203         session = asp_session_get_session(session_mac, session_id);
204         if (session == NULL) {
205                 ASP_LOGE("There's no session");
206                 return -1;
207         }
208
209         asp_state_set(session, ASP_SESSION_CLOESD, ASP_SESSION_CLOSE_LOCAL_CLOSE, NULL);
210         asp_session_destroy(session->session_mac, session->session_id);
211         __ASP_LOG_FUNC_EXIT__;
212         return res;
213 }
214
215 int asp_session_receive_request_session(void *data)
216 {
217         __ASP_LOG_FUNC_ENTER__;
218         asp_s *asp = NULL;
219         asp_session_s *session = NULL;
220         asp_event_data *event = data;
221         unsigned char session_mac[MAC_LEN] = {0,};
222         int res = 0;
223
224         asp = asp_get_manager();
225         if (asp == NULL) {
226                 ASP_LOGE("memory allocation for session is failed");
227                 return -1;
228         }
229
230         macaddr_atoe(event->session_request.session_mac, session_mac);
231         session = asp_session_create(session_mac, event->session_request.session_id,
232                         event->session_request.advertisement_id);
233         if (session == NULL) {
234                 ASP_LOGE("memory allocation for session is failed");
235                 return -1;
236         }
237
238         /* make local p2p address as service mac */
239         memcpy(session->service_mac, asp->p2p_local_address, MAC_LEN);
240
241         asp_manager_gdbus_notify_session_request(event->session_request.advertisement_id,
242                         event->session_request.session_id, event->session_request.session_mac,
243                         event->session_request.device_name, event->session_request.session_info,
244                         event->session_request.get_pin, event->session_request.pin);
245
246         asp_state_set(session, ASP_SESSION_REQUESTED, 0, "PDRequest");
247
248         __ASP_LOG_FUNC_EXIT__;
249         return res;
250 }
251
252 /* SessionConfigRequest event is triggered by the ASP on a Service Seeker */
253 int asp_session_receive_session_config_request(void *data)
254 {
255         __ASP_LOG_FUNC_ENTER__;
256         asp_s *asp = NULL;
257         asp_session_s *session = NULL;
258         asp_event_data *event = data;
259         int res = 0;
260
261         asp = asp_get_manager();
262         if (asp == NULL) {
263                 ASP_LOGE("memory allocation for session is failed");
264                 return -1;
265         }
266
267         /* session config request only received on seeker */
268         session = asp_session_get_session(asp->p2p_local_address, event->session_config_request.session_id);
269         if (session == NULL) {
270                 ASP_LOGE("There's no session");
271                 return -1;
272         }
273
274         asp_manager_gdbus_notify_session_config_request(
275                         event->session_config_request.session_id,
276                         event->session_config_request.get_pin,
277                         event->session_config_request.pin);
278
279         __ASP_LOG_FUNC_EXIT__;
280         return res;
281 }
282
283 int asp_session_receive_connect_status(void *data)
284 {
285         __ASP_LOG_FUNC_ENTER__;
286         asp_session_s *session = NULL;
287         asp_event_data *event = data;
288         unsigned char session_mac[MAC_LEN] = {0,};
289         int res = 0;
290
291         macaddr_atoe(event->session_request.session_mac, session_mac);
292         session = asp_session_get_session(session_mac,
293                         event->connect_status.session_id);
294         if (session == NULL) {
295                 ASP_LOGE("There's no session");
296                 return -1;
297         }
298
299         /* set session status */
300         asp_state_set_sub(session, event->connect_status.status, event->connect_status.deferred);
301
302         switch (event->connect_status.status) {
303         case ASP_SESSION_ROLE_REJECTED:
304         case ASP_SESSION_NOMORE_CONNECT:
305         case ASP_SESSION_REQUEST_FAILED:
306         case ASP_GROUP_FORMATION_FAILED:
307                 asp_state_set(session, ASP_SESSION_CLOESD, ASP_SESSION_CLOSE_LOCAL_CLOSE, NULL);
308                 asp_session_destroy(session->session_mac, session->session_id);
309         }
310
311         __ASP_LOG_FUNC_EXIT__;
312         return res;
313 }