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 *****************************************************************************/
33 /*****************************************************************************
34 * Application Service Platform manager headers
35 *****************************************************************************/
36 #include "asp-manager.h"
37 #include "asp-service.h"
38 #include "asp-session.h"
40 #include "asp-manager-gdbus.h"
41 #include "asp-manager-util.h"
43 /*****************************************************************************
45 *****************************************************************************/
47 /*****************************************************************************
49 *****************************************************************************/
50 static asp_s *g_asp = NULL;
52 /*****************************************************************************
53 * Local Functions Definition
54 *****************************************************************************/
56 static void __session_request_cb(gint32 error_code, guint8 *session_mac,
57 guint32 session_id, guint32 adv_id, gchar *device_name,
58 size_t name_length, guint8 *session_info, size_t info_length,
59 gboolean get_pin, guint32 pin, gpointer user_data)
61 __ASP_LOG_FUNC_ENTER__;
63 gchar *session_info_str = NULL;
64 gchar session_mac_str[MACSTR_LEN + 1] = {0,};
65 gchar pin_str[MACSTR_LEN + 1] = {0,};
67 if (info_length != 0) {
68 session_info_str = g_try_malloc0(info_length + 1);
70 memcpy(session_info_str, session_info, info_length);
73 g_snprintf(session_mac_str, MACSTR_LEN + 1, MACSTR, MAC2STR(session_mac));
74 g_snprintf(pin_str, PINSTR_LEN + 1, "%u", pin);
75 asp_manager_gdbus_notify_session_request(error_code, session_mac_str,
76 session_id, adv_id, device_name, session_info_str, get_pin, pin_str);
78 __ASP_LOG_FUNC_EXIT__;
82 static void __session_config_request_cb(gint32 error_code, guint8 *session_mac,
83 guint32 session_id, gboolean get_pin, guint32 pin, gpointer user_data)
85 __ASP_LOG_FUNC_ENTER__;
87 gchar session_mac_str[MACSTR_LEN + 1] = {0,};
88 gchar pin_str[MACSTR_LEN + 1] = {0,};
90 g_snprintf(session_mac_str, MACSTR_LEN + 1, MACSTR, MAC2STR(session_mac));
91 g_snprintf(pin_str, PINSTR_LEN + 1, "%u", pin);
93 asp_manager_gdbus_notify_session_config_request(session_mac_str, session_id,
96 __ASP_LOG_FUNC_EXIT__;
100 static void __connect_status_cb(gint32 error_code, const guint8 *session_mac,
101 guint32 session_id, asp_session_connect_status_e status,
102 guint8 *deferred_resp, size_t resp_length, gpointer user_data)
104 __ASP_LOG_FUNC_ENTER__;
106 gchar session_mac_str[MACSTR_LEN + 1] = {0,};
107 g_snprintf(session_mac_str, MACSTR_LEN + 1, MACSTR, MAC2STR(session_mac));
110 asp_manager_gdbus_notify_connect_status(session_mac_str, session_id,
111 status, (gchar *)deferred_resp);
113 __ASP_LOG_FUNC_EXIT__;
117 static void __session_status_cb(gint32 error_code, const guint8 *session_mac,
118 guint32 session_id, asp_session_status_e state,
119 asp_session_closed_state_e status, const gchar *requested_info,
122 __ASP_LOG_FUNC_ENTER__;
124 gchar session_mac_str[MACSTR_LEN + 1] = {0,};
125 g_snprintf(session_mac_str, MACSTR_LEN + 1, MACSTR, MAC2STR(session_mac));
127 asp_manager_gdbus_notify_session_status(session_mac_str,
128 session_id, state, status, requested_info);
130 __ASP_LOG_FUNC_EXIT__;
134 static void __port_status_cb(gint32 error_code, const guint8 *session_mac,
135 guint32 session_id, const gchar *ip, guint16 port, guint8 protocol,
136 asp_session_port_status_e status, gpointer user_data)
138 __ASP_LOG_FUNC_ENTER__;
140 gchar session_mac_str[MACSTR_LEN + 1] = {0,};
141 g_snprintf(session_mac_str, MACSTR_LEN + 1, MACSTR, MAC2STR(session_mac));
143 asp_manager_gdbus_notify_port_status(session_mac_str, session_id,
144 ip, port, protocol, status);
146 __ASP_LOG_FUNC_EXIT__;
151 asp_s *asp_get_manager()
156 static void __asp_manager_deinit()
158 __ASP_LOG_FUNC_ENTER__;
162 ASP_LOGD("Memory for manager structure is not allocated");
166 if (!asp_session_deinitialize())
167 ASP_LOGE("Failed to deinitialize session");
171 res = asp_service_deinit();
173 ASP_LOGE("Failed to deinitialize service");
175 asp_manager_gdbus_deinit((gpointer)g_asp);
179 __ASP_LOG_FUNC_EXIT__;
182 static asp_s *__asp_manager_init()
184 __ASP_LOG_FUNC_ENTER__;
188 asp = (asp_s*) g_try_malloc0(sizeof(asp_s));
190 ASP_LOGE("Failed to allocate memory for manager structure");
193 ASP_LOGD("ASP: manager alloc");
197 /* TODO : Handle tech failure */
199 res = asp_service_init();
201 ASP_LOGE("Failed to initialize service");
206 if (!asp_session_initialize()) {
207 ASP_LOGE("Failed to initialize session");
212 asp_session_set_session_request_cb(__session_request_cb, NULL);
213 asp_session_set_session_config_request_cb(__session_config_request_cb, NULL);
214 asp_session_set_connect_status_cb(__connect_status_cb, NULL);
215 asp_session_set_session_status_cb(__session_status_cb, NULL);
216 asp_session_set_port_status_cb(__port_status_cb, NULL);
218 asp_manager_gdbus_init((gpointer)asp);
220 ASP_LOGD("asp manager initialized");
221 __ASP_LOG_FUNC_EXIT__;
225 gint32 asp_manager_load()
227 __ASP_LOG_FUNC_ENTER__;
229 g_asp = __asp_manager_init();
231 ASP_LOGE("Failed to initialize asp-manager");
235 __ASP_LOG_FUNC_EXIT__;
239 void asp_manager_unload()
241 __ASP_LOG_FUNC_ENTER__;
242 __asp_manager_deinit();
243 __ASP_LOG_FUNC_EXIT__;