Support asynchronous plugin initializer
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / src / main.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Contact: Suresh Kumar N (suresh.n@samsung.com)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <glib.h>
24
25 #include <zblib.h>
26 #include <zblib_service.h>
27 #include <zblib_plugin.h>
28
29 #include <zigbee_service_interface.h>
30 #include <zigbee_service.h>
31
32 #define NOTUSED(var) (var = var)
33
34 /**< ZigBee Service */
35 ZigBeeService *zigbee_service;
36
37 static gboolean _request_terminate(gpointer user_data)
38 {
39         NOTUSED(user_data);
40
41         zblib_service_exit(zigbee_service);
42
43         return G_SOURCE_REMOVE;
44 }
45
46 int main(int arg, char **argv)
47 {
48         ZigBeeService *service = NULL;
49         int ret_code = 0;
50         gboolean ret;
51
52 #if !GLIB_CHECK_VERSION(2, 35, 0)
53         g_type_init();
54 #endif
55 #if !GLIB_CHECK_VERSION(2, 31, 0)
56         g_thread_init(NULL);
57 #endif
58         NOTUSED(arg);
59         NOTUSED(argv);
60
61         /* Create ZigBee service */
62         zigbee_service = service = zblib_service_new();
63
64         /* Initialize ZigBee service interface layer */
65         ret = zigbee_service_interface_init(service);
66         if (G_UNLIKELY(FALSE == ret)) {
67                 Z_LOGE("zigbee_service_interface_init failed!");
68                 goto END;
69         }
70
71         /* Initialize ZigBee service */
72         ret = zigbee_service_init(service);
73         if (G_UNLIKELY(FALSE == ret)) {
74                 Z_LOGE("zigbee_service_init failed!");
75                 /* Minimum mainloop time is required to notify the result */
76                 g_timeout_add(100, _request_terminate, NULL);
77         }
78
79         /* Run ZigBee service */
80         Z_LOGI("service mainloop start");
81         ret = zblib_service_run(service);
82         if (G_UNLIKELY(FALSE == ret)) {
83                 Z_LOGE("Run service failed!");
84                 ret_code = EXIT_FAILURE;
85         }
86
87 END:
88         /* De-initialize ZigBee service */
89         zigbee_service_deinit(service);
90
91         /* De-initialize ZigBee service interface layer */
92         zigbee_service_interface_deinit(service);
93
94         /* Free ZigBee service */
95         zblib_service_free(service);
96         zigbee_service = NULL;
97
98         return ret_code;
99 }