1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* Licensed under the Flora License, Version 1.1 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <tzplatform_config.h>
#include "ttd-config.h"
#include "ttd-cmd.h"
#include "ttd-log.h"
#define CONF_FILE_NAME "ttd.conf"
#define CONF_PATH tzplatform_mkpath3(TZ_SYS_ETC, "ttd", "ttd.conf")
static GKeyFile *gkf = NULL;
int ttd_config_init(void)
{
GKeyFile *tmp_gkf = NULL;
tmp_gkf = g_key_file_new();
retv_if(!tmp_gkf, -1);
if (!g_key_file_load_from_file(tmp_gkf, CONF_PATH, G_KEY_FILE_NONE, NULL)) {
_E("Failed to load conf file");
g_key_file_free(tmp_gkf);
return -1;
}
gkf = tmp_gkf;
return 0;
}
void ttd_config_fini(void)
{
if (gkf)
g_key_file_free(gkf);
gkf = NULL;
}
int ttd_config_read_integer(const char *group, const char *key, int *value)
{
GError *error = NULL;
int ret = 0;
retv_if(!group, -1);
retv_if(!key, -1);
retv_if(!value, -1);
if (!gkf) {
if (ttd_config_init() < 0) {
_E("Failed to initialize configure");
return -1;
}
}
ret = g_key_file_get_integer(gkf, group, key, &error);
if (error) {
_E("Failed to get integer[%s]", error->message);
g_error_free(error);
return -1;
}
*value = ret;
return 0;
}
int ttd_config_read_boolean(const char *group, const char *key, bool *value)
{
GError *error = NULL;
gboolean ret = FALSE;
retv_if(!group, -1);
retv_if(!key, -1);
retv_if(!value, -1);
if (!gkf) {
if (ttd_config_init() < 0) {
_E("Failed to initialize configure");
return -1;
}
}
ret = g_key_file_get_boolean(gkf, group, key, &error);
if (error) {
_E("Failed to get boolean[%s]", error->message);
g_error_free(error);
return -1;
}
*value = ret;
return 0;
}
int ttd_config_read_string(const char *group, const char *key, char **value)
{
GError *error = NULL;
char *ret = NULL;
retv_if(!group, -1);
retv_if(!key, -1);
retv_if(!value, -1);
if (!gkf) {
if (ttd_config_init() < 0) {
_E("Failed to initialize configure");
return -1;
}
}
ret = g_key_file_get_string(gkf, group, key, &error);
if (error) {
_E("Failed to get string[%s]", error->message);
g_error_free(error);
return -1;
}
*value = ret;
return 0;
}
int ttd_config_write_integer(const char *group, const char *key, int value)
{
retv_if(!group, -1);
retv_if(!key, -1);
if (!gkf) {
if (ttd_config_init() < 0) {
_E("Failed to initialize configure");
return -1;
}
}
g_key_file_set_integer(gkf, group, key, value);
return 0;
}
int ttd_config_write_boolean(const char *group, const char *key, bool value)
{
retv_if(!group, -1);
retv_if(!key, -1);
if (!gkf) {
if (ttd_config_init() < 0) {
_E("Failed to initialize configure");
return -1;
}
}
g_key_file_set_boolean(gkf, group, key, value);
return 0;
}
int ttd_config_write_string(const char *group, const char *key, const char *value)
{
retv_if(!group, -1);
retv_if(!key, -1);
retv_if(!value, -1);
if (!gkf) {
if (ttd_config_init() < 0) {
_E("Failed to initialize configure");
return -1;
}
}
g_key_file_set_string(gkf, group, key, value);
return 0;
}
int ttd_config_remove_group(const char *group)
{
gboolean ret = FALSE;
GError *error = NULL;
retv_if(!group, -1);
if (!gkf) {
if (ttd_config_init() < 0) {
_E("Failed to initialize configure");
return -1;
}
}
ret = g_key_file_remove_group(gkf, group, &error);
if (!ret) {
_E("Failed to remove group[%s]--caused by[%s]", group, error->message);
g_error_free(error);
return -1;
}
return 0;
}
int ttd_config_remove_key(const char *group, const char *key)
{
gboolean ret = FALSE;
GError *error = NULL;
retv_if(!group, -1);
retv_if(!key, -1);
if (!gkf) {
if (ttd_config_init() < 0) {
_E("Failed to initialize configure");
return -1;
}
}
ret = g_key_file_remove_key(gkf, group, key, &error);
if (!ret) {
_E("Failed to remove group[%s]--caused by[%s]", group, error->message);
g_error_free(error);
return -1;
}
return 0;
}
int ttd_config_write(void *cmd_data)
{
ttd_cmd_data *c_data = cmd_data;
void *data = NULL;
unsigned int data_len = 0;
int ret = 0;
retv_if(!cmd_data, -1);
ret = ttd_cmd_get_data(c_data, &data, &data_len);
retv_if(ret < 0, -1);
switch (ttd_cmd_get_command(cmd_data))
{
case TTD_CMD_CONFIG_SET_SERVER_URL:
ret = ttd_config_write_string("url", "server", (char *)data);
if (ret < 0) {
_E("Failed to set server url[%s]", (char *)data);
return -1;
}
break;
case TTD_CMD_CONFIG_SET_APP_ID:
ret = ttd_config_write_string("worker", "appid", (char *)data);
if (ret < 0) {
_E("Failed to set app id[%s]", (char *)data);
return -1;
}
break;
#if 0
case TTD_CMD_CONFIG_SET_XXXX_XXXX:
int val = __convert_string_to_int((char *)c_data->data);
ret = ttd_config_write_integer("", "", val);
if (ret < 0) {
_E("Failed to set XXX XXX[%d]", val);
return -1;
}
break;
case TTD_CMD_CONFIG_SET_XXXX_XXX:
bool val = __convert_string_to_bool((char *)c_data->data);
ret = ttd_config_write_boolean("", "", val);
if (ret < 0) {
_E("Failed to set XXX XXX[%d]", val);
return -1;
}
break;
#endif
default:
_E("Strange action");
return -1;
break;
}
return 0;
}
#if 0
static int __convert_string_to_int(const char *str);
static bool __convert_string_to_bool(const char *str);
static int __convert_string_to_int(const char *str)
{
if (!str) {
_E("String is NULL!!!!!!!");
return -1;
}
return atoi(str);
}
static bool __convert_string_to_bool(const char *str)
{
if (!str) {
_E("String is NULL!!!!!!!");
return false;
}
if ((!strncmp(str, "false", strlen("false"))) || (!strncmp(str, "FALSE", strlen("FALSE"))))
return false;
else if ((!strncmp(str, "true", strlen("true")))
|| (!strncmp(str, "TRUE", strlen("TRUE"))))
return true;
else {
_E("Strange data!!!!!!");
return false;
}
}
#endif
|