2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /*****************************************************************************
19 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
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"
45 #include "../generated-code.h"
47 /*****************************************************************************
49 *****************************************************************************/
51 /* TODO : define session data structure */
53 /*****************************************************************************
55 *****************************************************************************/
57 GList *g_sessions = NULL;
59 /*****************************************************************************
60 * Local Functions Definition
61 *****************************************************************************/
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)
66 __ASP_LOG_FUNC_ENTER__;
67 asp_session_s *session = NULL;
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");
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;
80 g_sessions = g_list_append(g_sessions, session);
82 __ASP_LOG_FUNC_EXIT__;
86 /* Deallocate memory for session */
87 int asp_session_destroy(unsigned char *session_mac, int session_id)
89 __ASP_LOG_FUNC_ENTER__;
90 asp_session_s *session = NULL;
93 session = asp_session_get_session(session_mac, session_id);
94 if (session == NULL) {
95 ASP_LOGE("There's no session");
99 g_sessions = g_list_remove(g_sessions, session);
102 __ASP_LOG_FUNC_EXIT__;
106 int asp_session_connect_session(asp_connect_session_params *params, asp_session_s **session)
108 __ASP_LOG_FUNC_ENTER__;
110 asp_session_s * local_session = NULL;
114 asp = asp_get_manager();
116 ASP_LOGE("memory allocation for session is failed");
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;
128 /* TODO : request connection on proper technology */
130 memcpy(params->session_mac, local_session->session_mac, MAC_LEN);
131 res = asp_tech_connect_session(tech, params);
133 ASP_LOGE("request connect is failed");
134 asp_session_destroy(local_session->session_mac, local_session->session_id);
137 *session = local_session;
138 asp_state_set(local_session, ASP_SESSION_INITIATED, 0, NULL);
140 __ASP_LOG_FUNC_EXIT__;
144 int asp_session_confirm_session(unsigned char *session_mac, int session_id, int confirm, const char *pin)
146 __ASP_LOG_FUNC_ENTER__;
147 asp_session_s *session = NULL;
150 session = asp_session_get_session(session_mac, session_id);
151 if (session == NULL) {
152 ASP_LOGE("There's no session");
155 res = asp_tech_confirm_session(ASP_TECH_P2P, session_mac,
156 session_id, confirm, pin);
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);
164 __ASP_LOG_FUNC_EXIT__;
168 asp_session_s *asp_session_get_session(unsigned char *session_mac, int session_id)
170 __ASP_LOG_FUNC_ENTER__;
172 asp_session_s *session = NULL;
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)) {
185 __ASP_LOG_FUNC_EXIT__;
189 void asp_session_set_session_ready(unsigned char *session_mac, int session_id)
191 __ASP_LOG_FUNC_ENTER__;
193 __ASP_LOG_FUNC_EXIT__;
197 int asp_session_close_session(unsigned char *session_mac, int session_id)
199 __ASP_LOG_FUNC_ENTER__;
200 asp_session_s *session = NULL;
203 session = asp_session_get_session(session_mac, session_id);
204 if (session == NULL) {
205 ASP_LOGE("There's no session");
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__;
215 int asp_session_receive_request_session(void *data)
217 __ASP_LOG_FUNC_ENTER__;
219 asp_session_s *session = NULL;
220 asp_event_data *event = data;
221 unsigned char session_mac[MAC_LEN] = {0,};
224 asp = asp_get_manager();
226 ASP_LOGE("memory allocation for session is failed");
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");
238 /* make local p2p address as service mac */
239 memcpy(session->service_mac, asp->p2p_local_address, MAC_LEN);
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);
246 asp_state_set(session, ASP_SESSION_REQUESTED, 0, "PDRequest");
248 __ASP_LOG_FUNC_EXIT__;
252 /* SessionConfigRequest event is triggered by the ASP on a Service Seeker */
253 int asp_session_receive_session_config_request(void *data)
255 __ASP_LOG_FUNC_ENTER__;
257 asp_session_s *session = NULL;
258 asp_event_data *event = data;
261 asp = asp_get_manager();
263 ASP_LOGE("memory allocation for session is failed");
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");
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);
279 __ASP_LOG_FUNC_EXIT__;
283 int asp_session_receive_connect_status(void *data)
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,};
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");
299 /* set session status */
300 asp_state_set_sub(session, event->connect_status.status, event->connect_status.deferred);
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);
311 __ASP_LOG_FUNC_EXIT__;