Add Camera Module
[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_KEY_IMAGE_UPLOAD_NAME "image_address"
32 #define CONF_FILE_NAME "pi.conf"
33
34 struct controller_util_s {
35         char *path;
36         char *address;
37         char *image_upload;
38 };
39
40 struct controller_util_s controller_util = { 0, };
41
42 static int _read_conf_file(void)
43 {
44         GKeyFile *gkf = NULL;
45         char conf_path[PATH_MAX] = {0,};
46         char *prefix = NULL;
47
48         gkf = g_key_file_new();
49         retv_if(!gkf, -1);
50
51         prefix = app_get_resource_path();
52         retv_if(!prefix, -1);
53         snprintf(conf_path, sizeof(conf_path)-1, "%s%s", prefix, CONF_FILE_NAME);
54         free(prefix);
55         prefix = NULL;
56
57         if (!g_key_file_load_from_file(gkf, conf_path, G_KEY_FILE_NONE, NULL)) {
58                 _E("could not read config file %s", conf_path);
59                 return -1;
60         }
61
62         controller_util.path = g_key_file_get_string(gkf,
63                         CONF_GROUP_DEFAULT_NAME,
64                         CONF_KEY_PATH_NAME,
65                         NULL);
66         if (!controller_util.path)
67                 _E("could not get the key string");
68
69         controller_util.address = g_key_file_get_string(gkf,
70                         CONF_GROUP_DEFAULT_NAME,
71                         CONF_KEY_ADDRESS_NAME,
72                         NULL);
73         if (!controller_util.address)
74                 _E("could not get the key string");
75
76         controller_util.image_upload = g_key_file_get_string(gkf,
77                         CONF_GROUP_DEFAULT_NAME,
78                         CONF_KEY_IMAGE_UPLOAD_NAME,
79                         NULL);
80         if (!controller_util.image_upload)
81                 _E("could not get the key string");
82
83         g_key_file_free(gkf);
84
85         return 0;
86 }
87
88 int controller_util_get_path(const char **path)
89 {
90         retv_if(!path, -1);
91
92         if (!controller_util.path) {
93                 int ret = -1;
94                 ret = _read_conf_file();
95                 retv_if(-1 == ret, -1);
96         }
97
98         *path = controller_util.path;
99
100         return 0;
101 }
102
103 int controller_util_get_address(const char **address)
104 {
105         retv_if(!address, -1);
106
107         if (!controller_util.address) {
108                 int ret = -1;
109                 ret = _read_conf_file();
110                 retv_if(-1 == ret, -1);
111         }
112
113         *address = controller_util.address;
114
115         return 0;
116 }
117
118 int controller_util_get_image_address(const char **image_upload)
119 {
120         retv_if(!image_upload, -1);
121
122         if (!controller_util.image_upload) {
123                 int ret = -1;
124                 ret = _read_conf_file();
125                 retv_if(-1 == ret, -1);
126         }
127
128         *image_upload = controller_util.image_upload;
129
130         return 0;
131 }
132
133 void controller_util_free(void)
134 {
135         if (controller_util.path) {
136                 free(controller_util.path);
137                 controller_util.path = NULL;
138         }
139
140         if (controller_util.address) {
141                 free(controller_util.address);
142                 controller_util.address = NULL;
143         }
144 }