Revise directory structure
[platform/core/connectivity/asp-manager.git] / src / asp-manager.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 /*****************************************************************************
34  * Application Service Platform manager headers
35  *****************************************************************************/
36 #include "asp-manager.h"
37 #include "asp-service.h"
38 #include "asp-session.h"
39 #include "asp-tech.h"
40 #include "asp-manager-gdbus.h"
41 #include "asp-manager-util.h"
42
43 /*****************************************************************************
44  * Macros and Typedefs
45  *****************************************************************************/
46
47 /*****************************************************************************
48  * Global Variables
49  *****************************************************************************/
50 static asp_s *g_asp = NULL;
51
52 /*****************************************************************************
53  * Local Functions Definition
54  *****************************************************************************/
55
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)
60 {
61         __ASP_LOG_FUNC_ENTER__;
62
63         gchar *session_info_str = NULL;
64         gchar session_mac_str[MACSTR_LEN + 1] = {0,};
65         gchar pin_str[MACSTR_LEN + 1] = {0,};
66
67         if (info_length != 0) {
68                 session_info_str = g_try_malloc0(info_length + 1);
69                 if (session_info_str)
70                         memcpy(session_info_str, session_info, info_length);
71         }
72
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);
77
78         __ASP_LOG_FUNC_EXIT__;
79         return;
80 }
81
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)
84 {
85         __ASP_LOG_FUNC_ENTER__;
86
87         gchar session_mac_str[MACSTR_LEN + 1] = {0,};
88         gchar pin_str[MACSTR_LEN + 1] = {0,};
89
90         g_snprintf(session_mac_str, MACSTR_LEN + 1, MACSTR, MAC2STR(session_mac));
91         g_snprintf(pin_str, PINSTR_LEN + 1, "%u", pin);
92
93         asp_manager_gdbus_notify_session_config_request(session_mac_str, session_id,
94                         get_pin, pin_str);
95
96         __ASP_LOG_FUNC_EXIT__;
97         return;
98 }
99
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)
103 {
104         __ASP_LOG_FUNC_ENTER__;
105
106         gchar session_mac_str[MACSTR_LEN + 1] = {0,};
107         g_snprintf(session_mac_str, MACSTR_LEN + 1, MACSTR, MAC2STR(session_mac));
108
109
110         asp_manager_gdbus_notify_connect_status(session_mac_str, session_id,
111                         status, (gchar *)deferred_resp);
112
113         __ASP_LOG_FUNC_EXIT__;
114         return;
115 }
116
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,
120                 gpointer user_data)
121 {
122         __ASP_LOG_FUNC_ENTER__;
123
124         gchar session_mac_str[MACSTR_LEN + 1] = {0,};
125         g_snprintf(session_mac_str, MACSTR_LEN + 1, MACSTR, MAC2STR(session_mac));
126
127         asp_manager_gdbus_notify_session_status(session_mac_str,
128                         session_id, state, status, requested_info);
129
130         __ASP_LOG_FUNC_EXIT__;
131         return;
132 }
133
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)
137 {
138         __ASP_LOG_FUNC_ENTER__;
139
140         gchar session_mac_str[MACSTR_LEN + 1] = {0,};
141         g_snprintf(session_mac_str, MACSTR_LEN + 1, MACSTR, MAC2STR(session_mac));
142
143         asp_manager_gdbus_notify_port_status(session_mac_str, session_id,
144                         ip, port, protocol, status);
145
146         __ASP_LOG_FUNC_EXIT__;
147         return;
148 }
149
150
151 asp_s *asp_get_manager()
152 {
153         return g_asp;
154 }
155
156 static void __asp_manager_deinit()
157 {
158         __ASP_LOG_FUNC_ENTER__;
159         gint32 res = 0;
160
161         if (!g_asp) {
162                 ASP_LOGD("Memory for manager structure is not allocated");
163                 return;
164         }
165
166         if (!asp_session_deinitialize())
167                 ASP_LOGE("Failed to deinitialize session");
168
169         asp_tech_deinit();
170
171         res = asp_service_deinit();
172         if (res < 0)
173                 ASP_LOGE("Failed to deinitialize service");
174
175         asp_manager_gdbus_deinit((gpointer)g_asp);
176
177         g_free(g_asp);
178         g_asp = NULL;
179         __ASP_LOG_FUNC_EXIT__;
180 }
181
182 static asp_s *__asp_manager_init()
183 {
184         __ASP_LOG_FUNC_ENTER__;
185         asp_s *asp = NULL;
186         gint32 res = 0;
187
188         asp = (asp_s*) g_try_malloc0(sizeof(asp_s));
189         if (!asp) {
190                 ASP_LOGE("Failed to allocate memory for manager structure");
191                 return NULL;
192         }
193         ASP_LOGD("ASP: manager alloc");
194         g_asp = asp;
195
196         asp_tech_init();
197         /* TODO : Handle tech failure */
198
199         res = asp_service_init();
200         if (res < 0) {
201                 ASP_LOGE("Failed to initialize service");
202                 g_free(asp);
203                 return NULL;
204         }
205
206         if (!asp_session_initialize()) {
207                 ASP_LOGE("Failed to initialize session");
208                 g_free(asp);
209                 return NULL;
210         }
211
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);
217
218         asp_manager_gdbus_init((gpointer)asp);
219
220         ASP_LOGD("asp manager initialized");
221         __ASP_LOG_FUNC_EXIT__;
222         return asp;
223 }
224
225 gint32 asp_manager_load()
226 {
227         __ASP_LOG_FUNC_ENTER__;
228
229         g_asp = __asp_manager_init();
230         if (!g_asp) {
231                 ASP_LOGE("Failed to initialize asp-manager");
232                 return -1;
233         }
234
235         __ASP_LOG_FUNC_EXIT__;
236         return 0;
237 }
238
239 void asp_manager_unload()
240 {
241         __ASP_LOG_FUNC_ENTER__;
242         __asp_manager_deinit();
243         __ASP_LOG_FUNC_EXIT__;
244 }