6dd9ad84cc4fafef339a0362510502de8962cd98
[platform/core/api/system-settings.git] / src / sst_core.c
1 /*
2  * Copyright (c) 2011-2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "sst_core.h"
17
18 #include <unistd.h>
19 #include <app_manager.h>
20 #include <package_manager.h>
21 #include <vconf.h>
22 #include "sst.h"
23 #include "sst_misc.h"
24 #include "sst_vconf.h"
25 #include "sst_interface.h"
26
27 int sst_get_value(system_settings_key_e key, sst_interface_data_type data_type, void **value)
28 {
29         int ret;
30
31         DBG("Enter");
32         RETVM_IF(key < 0 || SYSTEM_SETTINGS_KEY_MAX <= key,
33                 SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER, "Invalid Key(%d)", key);
34         RETV_IF(NULL == value, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
35
36         sst_interface *iface = NULL;
37         ret = sst_get_interface(key, &iface);
38         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
39                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
40                 return ret;
41         }
42
43         if (iface->data_type != data_type) {
44                 ERR("Invalide type for key(%d) : Expected(%d), Actual(%d)",
45                         key, iface->data_type, data_type);
46                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
47         }
48
49         sst_get_value_fn getter = iface->get_value_cb;
50         if (NULL == getter) {
51                 ERR("No getter for key(%d)", key);
52                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
53         }
54
55         ret = getter(iface, value);
56         if (SYSTEM_SETTINGS_ERROR_NONE != ret)
57                 ERR("getter(%d) Fail(%d)", key, ret);
58         return ret;
59 }
60
61 int sst_set_value(system_settings_key_e key, sst_interface_data_type data_type, void *value)
62 {
63         int ret;
64
65         DBG("Enter");
66         RETVM_IF(key < 0 || SYSTEM_SETTINGS_KEY_MAX <= key,
67                 SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER, "Invalid Key(%d)", key);
68         RETV_IF(NULL == value, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
69
70         sst_interface *iface = NULL;
71         ret = sst_get_interface(key, &iface);
72         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
73                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
74                 return ret;
75         }
76
77         if (iface->data_type != data_type) {
78                 ERR("Invalide type for key(%d) : Expected(%d), Actual(%d)",
79                         key, iface->data_type, data_type);
80                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
81         }
82
83         sst_set_value_fn setter = iface->set_value_cb;
84         if (NULL == setter) {
85                 ERR("No setter for key(%d)", key);
86                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
87         }
88
89         ret = setter(iface, value);
90         if (SYSTEM_SETTINGS_ERROR_NONE != ret)
91                 ERR("setter(%d) Fail(%d)", key, ret);
92         return ret;
93 }
94
95 int sst_list_value(system_settings_key_e key, sst_interface_data_type data_type, bool(*list_iterator_fn)(int, const char*, void *), void *user_data)
96 {
97         int ret;
98
99         DBG("Enter");
100         RETVM_IF(key < 0 || SYSTEM_SETTINGS_KEY_MAX <= key,
101                 SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER, "Invalid Key(%d)", key);
102
103         sst_interface *iface = NULL;
104         ret = sst_get_interface(key, &iface);
105         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
106                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
107                 return ret;
108         }
109
110         if (iface->data_type != data_type) {
111                 ERR("Invalide type for key(%d) : Expected(%d), Actual(%d)",
112                         key, iface->data_type, data_type);
113                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
114         }
115
116         sst_list_value_cb list_getter = iface->list_value_cb;
117         if (NULL == list_getter) {
118                 ERR("No list_getter for key(%d)", key);
119                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
120         }
121
122         ret = list_getter(key, list_iterator_fn, user_data);
123         if (SYSTEM_SETTINGS_ERROR_NONE != ret)
124                 ERR("list_getter(%d) Fail(%d)", key, ret);
125         return ret;
126 }
127
128 int sst_add_value(system_settings_key_e key, sst_interface_data_type data_type, void *value)
129 {
130         int ret;
131
132         DBG("Enter");
133         RETVM_IF(key < 0 || SYSTEM_SETTINGS_KEY_MAX <= key,
134                 SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER, "Invalid Key(%d)", key);
135
136         sst_interface *iface = NULL;
137         ret = sst_get_interface(key, &iface);
138         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
139                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
140                 return ret;
141         }
142
143         if (iface->data_type != data_type) {
144                 ERR("Invalide type for key(%d) : Expected(%d), Actual(%d)",
145                         key, iface->data_type, data_type);
146                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
147         }
148
149         sst_add_value_fn adder = iface->add_value_cb;
150         if (NULL == adder) {
151                 ERR("No adder for key(%d)", key);
152                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
153         }
154
155         ret = adder(key, value);
156         if (SYSTEM_SETTINGS_ERROR_NONE != ret)
157                 ERR("adder(%d) Fail(%d)", key, ret);
158         return ret;
159 }
160
161 int sst_del_value(system_settings_key_e key, sst_interface_data_type data_type, void *value)
162 {
163         int ret;
164
165         DBG("Enter");
166         RETVM_IF(key < 0 || SYSTEM_SETTINGS_KEY_MAX <= key,
167                 SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER, "Invalid Key(%d)", key);
168
169         sst_interface *iface = NULL;
170         ret = sst_get_interface(key, &iface);
171         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
172                 ERR("sst_get_interface(%d) Fail(%d)", key, ret);
173                 return ret;
174         }
175
176         if (iface->data_type != data_type) {
177                 ERR("Invalide type for key(%d) : Expected(%d), Actual(%d)",
178                         key, iface->data_type, data_type);
179                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
180         }
181
182         sst_del_value_fn        deleter;
183         deleter = iface->del_value_cb;
184         if (NULL == deleter) {
185                 ERR("No deleter for key(%d)", key);
186                 return SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
187         }
188
189         ret = deleter(key, value);
190         if (SYSTEM_SETTINGS_ERROR_NONE != ret)
191                 ERR("deleter(%d) Fail(%d)", key, ret);
192         return ret;
193
194 }
195
196 /**
197  * sound == false, vibration == false --> silent mode
198  * sound == true, vibration == false --> sound mode
199  * sound == false, vibration == true --> vibration mode
200  */
201 int sst_get_sound_silent_mode(sst_interface *iface, void **value)
202 {
203         bool sound_status = false;
204         if (sst_vconf_get_real_bool(iface->vconf_key, &sound_status)) {
205                 ERR("sst_vconf_get_real_bool(%s) Fail", iface->vconf_key);
206                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
207         }
208
209         bool vib_status = false;
210         const char *vibration_key = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
211         if (sst_vconf_get_real_bool(vibration_key, &vib_status)) {
212                 ERR("sst_vconf_get_real_bool(%s) Fail", vibration_key);
213                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
214         }
215
216         bool ret_val = (sound_status == false && vib_status == false);
217         *value = (void*)ret_val;
218
219         return SYSTEM_SETTINGS_ERROR_NONE;
220 }
221
222 /**
223  * sound == false, vibration == false --> silent mode
224  * sound == true, vibration == false --> sound mode
225  */
226 //Todo: It should return to the old status.
227 int sst_set_sound_silent_mode(sst_interface *iface, void *value)
228 {
229         bool *vconf_value = value;
230
231         bool vconf_sound = !(*vconf_value);
232         if (vconf_set_bool(iface->vconf_key, vconf_sound)) {
233                 ERR("vconf_set_bool(%s, %d) Fail", iface->vconf_key, vconf_sound);
234                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
235         }
236
237         bool vconf_vib = false;
238         const char *vibration_key = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
239         if (vconf_set_bool(vibration_key, vconf_vib)) {
240                 ERR("vconf_set_bool(%s) Fail", vibration_key);
241                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
242         }
243
244         return SYSTEM_SETTINGS_ERROR_NONE;
245 }
246
247 int sst_set_sound_notification(sst_interface *iface, void *value)
248 {
249         char *vconf_value = value;
250
251         bool is_valid = sst_misc_exist(vconf_value);
252         if (true == is_valid) {
253                 if (vconf_set_str(iface->vconf_key, vconf_value)) {
254                         ERR("vconf_set_str(%s, %s) Fail", iface->vconf_key, vconf_value);
255                         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
256                 }
257         } else {
258                 ERR("sst_misc_exist(%s) Fail", vconf_value);
259                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
260         }
261
262         return SYSTEM_SETTINGS_ERROR_NONE;
263 }
264
265 int sst_set_device_name(system_settings_key_e key, void *value)
266 {
267         char *vconf_value = value;
268
269         const char *vconf_key = VCONFKEY_SETAPPL_DEVICE_NAME_STR;
270         if (vconf_set_str(vconf_key, vconf_value)) {
271                 ERR("vconf_set_str(%s, %s) Fail", vconf_key, vconf_value);
272                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
273         }
274
275         return SYSTEM_SETTINGS_ERROR_NONE;
276 }
277
278 int sst_get_network_wifi_notification(sst_interface *iface, void **value)
279 {
280         int vconf_value = 0;
281         if (vconf_get_int(iface->vconf_key, &vconf_value)) {
282                 ERR("vconf_get_int(%s) Fail", iface->vconf_key);
283                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
284         }
285
286         bool result;
287         result = (vconf_value == VCONFKEY_WIFI_QS_ENABLE) ? true : false;
288
289         *value = (void*)result;
290         return SYSTEM_SETTINGS_ERROR_NONE;
291 }
292
293 //It is related with Advertisements
294 #define DEFAULT_ADS_ID "00000000-0000-0000-0000-000000000000"
295 int sst_get_ads_id(sst_interface *iface, void **value)
296 {
297         int result = 0;
298         const char *key = VCONFKEY_SETAPPL_AD_ID_OPT_OUT;
299         if (vconf_get_int(key, &result)) {
300                 ERR("vconf_get_int(%s) Fail", key);
301                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
302         }
303
304         if (result == 1) {
305                 *value = strdup(DEFAULT_ADS_ID);
306                 return SYSTEM_SETTINGS_ERROR_NONE;
307         }
308
309         char *vconf_value = NULL;
310         if (sst_vconf_get_string(iface->vconf_key, &vconf_value)) {
311                 ERR("sst_vconf_get_string(%s) Fail", iface->vconf_key);
312                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
313         }
314
315         *value = vconf_value;
316         return SYSTEM_SETTINGS_ERROR_NONE;
317 }
318
319 int sst_set_ads_id(sst_interface *iface, void *value)
320 {
321         char *vconf_value = value;
322
323         if (vconf_set_str(iface->vconf_key, vconf_value)) {
324                 ERR("vconf_set_str(%s) Fail", iface->vconf_key);
325                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
326         }
327
328         return SYSTEM_SETTINGS_ERROR_NONE;
329 }
330
331 int sst_get_uds_state(sst_interface *iface, void **value)
332 {
333         int **p_value = (int**)value;
334         int int_val;
335         char *str_val = NULL;
336
337         if (vconf_get_int(iface->vconf_key, &int_val))
338                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
339
340         if (int_val == SYSTEM_SETTINGS_UDS_ON) {
341                 const char *vconf_key = VCONFKEY_SETAPPL_UDSM_PKGID_LIST;
342                 int ret = sst_vconf_get_string(vconf_key, &str_val);
343                 if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
344                         ERR("sst_vconf_get_string(%s) Fail(%d)", vconf_key, ret);
345                         return ret;
346                 }
347
348                 if (SST_EQUAL != strcmp(str_val, "NONE")) {
349                         char *app_id = NULL;
350                         char *package_id = NULL;
351                         pid_t pid = getpid();
352
353                         int ret = app_manager_get_app_id(pid, &app_id);
354                         if (ret != APP_MANAGER_ERROR_NONE) {
355                                 SECURE_ERR("app_manager_get_app_id(%d) Fail(%d)", pid, ret);
356                                 free(str_val);
357                                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
358                         }
359
360                         ret = package_manager_get_package_id_by_app_id(app_id, &package_id);
361                         if (PACKAGE_MANAGER_ERROR_NONE != ret) {
362                                 SECURE_ERR("package_manager_get_package_id_by_app_id(%s) Fail(%d)", app_id, ret);
363                                 free(app_id);
364                                 free(str_val);
365                                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
366                         }
367                         free(app_id);
368
369                         if (package_id && strstr(str_val, package_id))
370                                 int_val = SYSTEM_SETTINGS_UDS_ON_WHITELISTED;
371                         free(package_id);
372                 }
373                 free(str_val);
374         }
375
376         **p_value = int_val;
377
378         return SYSTEM_SETTINGS_ERROR_NONE;
379 }