3bba0d5cf3e34e5ae94d18af83027d0cedbc0152
[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 #include <stdio.h>
25 #include <app_common.h>
26 #include "log.h"
27
28 #define CONF_GROUP_DEFAULT_NAME "default"
29 #define CONF_KEY_PATH_NAME "path"
30 #define CONF_KEY_ADDRESS_NAME "address"
31 #define CONF_FILE_NAME "pi.conf"
32
33 struct controller_util_s {
34         char *path;
35         char *address;
36 };
37
38 struct controller_util_s controller_util = { 0, };
39
40 static int _read_conf_file(void)
41 {
42         GKeyFile *gkf = NULL;
43         char conf_path[PATH_MAX] = {0,};
44         char *prefix = NULL;
45
46         gkf = g_key_file_new();
47         retv_if(!gkf, -1);
48
49         prefix = app_get_resource_path();
50         retv_if(!prefix, -1);
51         snprintf(conf_path, sizeof(conf_path)-1, "%s%s", prefix, CONF_FILE_NAME);
52         free(prefix);
53         prefix = NULL;
54
55         if (!g_key_file_load_from_file(gkf, conf_path, G_KEY_FILE_NONE, NULL)) {
56                 _E("could not read config file %s", conf_path);
57                 return -1;
58         }
59
60         controller_util.path = g_key_file_get_string(gkf,
61                         CONF_GROUP_DEFAULT_NAME,
62                         CONF_KEY_PATH_NAME,
63                         NULL);
64         if (!controller_util.path)
65                 _E("could not get the key string");
66
67         controller_util.address = g_key_file_get_string(gkf,
68                         CONF_GROUP_DEFAULT_NAME,
69                         CONF_KEY_ADDRESS_NAME,
70                         NULL);
71         if (!controller_util.address)
72                 _E("could not get the key string");
73
74         g_key_file_free(gkf);
75
76         return 0;
77 }
78
79 int controller_util_get_path(const char **path)
80 {
81         retv_if(!path, -1);
82
83         if (!controller_util.path) {
84                 int ret = -1;
85                 ret = _read_conf_file();
86                 retv_if(-1 == ret, -1);
87         }
88
89         *path = controller_util.path;
90
91         return 0;
92 }
93
94 int controller_util_get_address(const char **address)
95 {
96         retv_if(!address, -1);
97
98         if (!controller_util.address) {
99                 int ret = -1;
100                 ret = _read_conf_file();
101                 retv_if(-1 == ret, -1);
102         }
103
104         *address = controller_util.address;
105
106         return 0;
107 }
108
109 void controller_util_free(void)
110 {
111         if (controller_util.path) {
112                 free(controller_util.path);
113                 controller_util.path = NULL;
114         }
115
116         if (controller_util.address) {
117                 free(controller_util.address);
118                 controller_util.address = NULL;
119         }
120 }