merge controller util module from master branch
[apps/native/position-finder-server.git] / src / controller_util.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
5  *          Geunsun Lee <gs86.lee@samsung.com>
6  *          Eunyoung Lee <ey928.lee@samsung.com>
7  *          Junkyu Han <junkyu.han@samsung.com>
8  *
9  * Licensed under the Flora License, Version 1.1 (the License);
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://floralicense.org/license/
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an AS IS BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <stdlib.h>
23 #include <glib.h>
24
25 #include "log.h"
26
27 #define CONF_GROUP_DEFAULT_NAME "default"
28 #define CONF_KEY_PATH_NAME "path"
29 #define CONF_KEY_ADDRESS_NAME "address"
30
31 struct controller_util_s {
32         char *path;
33         char *address;
34 };
35
36 struct controller_util_s controller_util = { 0, };
37
38 static int _read_conf_file(void)
39 {
40         GKeyFile *gkf = NULL;
41
42         gkf = g_key_file_new();
43         retv_if(!gkf, -1);
44
45         if (!g_key_file_load_from_file(gkf, CONF_FILE, G_KEY_FILE_NONE, NULL)) {
46                 _E("could not read config file %s", CONF_FILE);
47                 return -1;
48         }
49
50         controller_util.path = g_key_file_get_string(gkf,
51                         CONF_GROUP_DEFAULT_NAME,
52                         CONF_KEY_PATH_NAME,
53                         NULL);
54         if (!controller_util.path)
55                 _E("could not get the key string");
56
57         controller_util.address = g_key_file_get_string(gkf,
58                         CONF_GROUP_DEFAULT_NAME,
59                         CONF_KEY_ADDRESS_NAME,
60                         NULL);
61         if (!controller_util.address)
62                 _E("could not get the key string");
63
64         g_key_file_free(gkf);
65
66         return 0;
67 }
68
69 int controller_util_get_path(const char **path)
70 {
71         retv_if(!path, -1);
72
73         if (!controller_util.path) {
74                 int ret = -1;
75                 ret = _read_conf_file();
76                 retv_if(-1 == ret, -1);
77         }
78
79         *path = controller_util.path;
80
81         return 0;
82 }
83
84 int controller_util_get_address(const char **address)
85 {
86         retv_if(!address, -1);
87
88         if (!controller_util.address) {
89                 int ret = -1;
90                 ret = _read_conf_file();
91                 retv_if(-1 == ret, -1);
92         }
93
94         *address = controller_util.address;
95
96         return 0;
97 }
98
99 void controller_util_free(void)
100 {
101         if (controller_util.path) {
102                 free(controller_util.path);
103                 controller_util.path = NULL;
104         }
105
106         if (controller_util.address) {
107                 free(controller_util.address);
108                 controller_util.address = NULL;
109         }
110 }