Add initial code for GTest
[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 #if (BUILD_GCOV != 0)
38 extern void __gcov_flush(void);
39 #endif
40
41 static gboolean _request_terminate(gpointer user_data)
42 {
43         NOTUSED(user_data);
44
45         zblib_service_exit(zigbee_service);
46
47         return G_SOURCE_REMOVE;
48 }
49
50 int main(int arg, char **argv)
51 {
52         ZigBeeService *service = NULL;
53         int ret_code = 0;
54         gboolean ret;
55
56 #if !GLIB_CHECK_VERSION(2, 35, 0)
57         g_type_init();
58 #endif
59 #if !GLIB_CHECK_VERSION(2, 31, 0)
60         g_thread_init(NULL);
61 #endif
62         NOTUSED(arg);
63         NOTUSED(argv);
64
65 #if (BUILD_GCOV != 0)
66         setenv("GCOV_PREFIX", "/tmp/", 1);
67 #endif
68
69         /* Create ZigBee service */
70         zigbee_service = service = zblib_service_new();
71
72         /* Initialize ZigBee service interface layer */
73         ret = zigbee_service_interface_init(service);
74         if (G_UNLIKELY(FALSE == ret)) {
75                 Z_LOGE("zigbee_service_interface_init failed!");
76                 goto END;
77         }
78
79         /* Initialize ZigBee service */
80         ret = zigbee_service_init(service);
81         if (G_UNLIKELY(FALSE == ret)) {
82                 Z_LOGE("zigbee_service_init failed!");
83                 /* Minimum mainloop time is required to notify the result */
84                 g_timeout_add(100, _request_terminate, NULL);
85         }
86
87         /* Run ZigBee service */
88         Z_LOGI("service mainloop start");
89         ret = zblib_service_run(service);
90         if (G_UNLIKELY(FALSE == ret)) {
91                 Z_LOGE("Run service failed!");
92                 ret_code = EXIT_FAILURE;
93         }
94
95 END:
96         /* De-initialize ZigBee service */
97         zigbee_service_deinit(service);
98
99         /* De-initialize ZigBee service interface layer */
100         zigbee_service_interface_deinit(service);
101
102         /* Free ZigBee service */
103         zblib_service_free(service);
104         zigbee_service = NULL;
105
106 #if (BUILD_GCOV != 0)
107         __gcov_flush();
108 #endif
109
110         return ret_code;
111 }