Add initial source files for ASP daemon
[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 asp_s *asp_get_manager()
57 {
58         return g_asp;
59 }
60
61 static void __asp_manager_deinit()
62 {
63         __ASP_LOG_FUNC_ENTER__;
64         int res = 0;
65
66         if (!g_asp) {
67                 printf("Memory for manager structure is not allocated");
68                 return;
69         }
70
71         asp_tech_deinit();
72
73         res = asp_service_deinit();
74         if (res < 0)
75                 ASP_LOGE("Failed to deinitialize service");
76
77         asp_manager_gdbus_deinit((void *)g_asp);
78
79         g_free(g_asp);
80         g_asp = NULL;
81         __ASP_LOG_FUNC_EXIT__;
82 }
83
84 static asp_s *__asp_manager_init()
85 {
86         __ASP_LOG_FUNC_ENTER__;
87         asp_s *asp = NULL;
88         int res = 0;
89
90         asp = (asp_s*) g_try_malloc0(sizeof(asp_s));
91         if (!asp) {
92                 ASP_LOGE("Failed to allocate memory for manager structure");
93                 return NULL;
94         }
95         ASP_LOGD("ASP: manager alloc");
96         g_asp = asp;
97
98         asp_tech_init();
99         /* TODO : Handle tech failure */
100
101         res = asp_service_init();
102         if (res < 0) {
103                 ASP_LOGE("Failed to initialize service");
104                 g_free(asp);
105                 return NULL;
106         }
107
108         asp_manager_gdbus_init((void *)asp);
109
110         ASP_LOGD("asp manager initialized");
111         __ASP_LOG_FUNC_EXIT__;
112         return asp;
113 }
114
115 int main(int argc, char *argv[])
116 {
117         GMainLoop *main_loop = NULL;
118         int ret = -1;
119
120         /* Initialize required subsystems */
121 #if !GLIB_CHECK_VERSION(2, 35, 0)
122         g_type_init();
123 #endif
124
125         g_asp = __asp_manager_init();
126         if (!g_asp)
127                 goto fail;
128
129         /* Crate the GLIB main loop */
130         main_loop = g_main_loop_new(NULL, FALSE);
131         g_asp->main_loop = main_loop;
132
133         /* Run the main loop */
134         g_main_loop_run(main_loop);
135
136         ret = 0;
137
138 fail:
139
140         __asp_manager_deinit();
141
142         g_main_loop_unref(main_loop);
143
144         return ret;
145 }