Add some chains to separate monitoring and restriction
[platform/core/connectivity/stc-manager.git] / src / stc-manager-util.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
17 #include "stc-manager-util.h"
18
19 static int g_debuglog = -1;
20
21 static void __sync_file_to_disk(const char *path)
22 {
23         FILE *fp = NULL;
24         fp = fopen(path, "a+");
25         if (fp) {
26                 fflush(fp);
27                 fsync(fp->_fileno);
28                 fclose(fp);
29                 STC_LOGD("Sync the file to disk");
30         }
31 }
32
33 static GKeyFile *__load_key_file(const char *path)
34 {
35         GKeyFile *keyfile = NULL;
36         GError *error = NULL;
37
38         STC_LOGD("Loading [%s]", path);
39
40         keyfile = g_key_file_new();
41
42         if (!g_key_file_load_from_file(keyfile, path, 0, &error)) {
43                 STC_LOGD("Unable to load [%s] : %s", path, error->message); //LCOV_EXCL_LINE
44                 g_clear_error(&error); //LCOV_EXCL_LINE
45                 g_key_file_free(keyfile); //LCOV_EXCL_LINE
46                 keyfile = NULL; //LCOV_EXCL_LINE
47         }
48
49         return keyfile;
50 }
51
52 static int __save_key_file(GKeyFile *keyfile, char *path)
53 {
54         gchar *data = NULL;
55         gsize length = 0;
56         GError *error = NULL;
57         int ret = 0;
58
59         data = g_key_file_to_data(keyfile, &length, NULL);
60
61         if (!g_file_set_contents(path, data, length, &error)) {
62                 STC_LOGD("Failed to save information : %s", error->message); //LCOV_EXCL_LINE
63                 g_error_free(error); //LCOV_EXCL_LINE
64                 ret = -EIO; //LCOV_EXCL_LINE
65         }
66
67         __sync_file_to_disk(path);
68
69         g_free(data);
70         return ret;
71 }
72
73 //LCOV_EXCL_START
74 gboolean stc_util_get_config_bool(char *key)
75 {
76         char path[MAX_PATH_LENGTH];
77         GKeyFile *keyfile;
78         gboolean value;
79
80         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
81
82         keyfile = __load_key_file(path);
83         if (!keyfile)
84                 keyfile = g_key_file_new();
85
86         value = g_key_file_get_boolean(keyfile, path, key, NULL);
87         g_key_file_free(keyfile);
88
89         return value;
90 }
91
92 gchar * stc_util_get_config_str(char *key)
93 {
94         char path[MAX_PATH_LENGTH];
95         GKeyFile *keyfile;
96         gchar *value;
97
98         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
99
100         keyfile = __load_key_file(path);
101         if (!keyfile)
102                 keyfile = g_key_file_new();
103
104         value = g_key_file_get_string(keyfile, path, key, NULL);
105         g_key_file_free(keyfile);
106
107         return value;
108 }
109 //LCOV_EXCL_STOP
110
111 int stc_util_get_config_int(char *key)
112 {
113         char path[MAX_PATH_LENGTH];
114         GKeyFile *keyfile;
115         gint value;
116
117         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
118
119         keyfile = __load_key_file(path);
120         if (!keyfile)
121                 keyfile = g_key_file_new(); //LCOV_EXCL_LINE
122
123
124         value = g_key_file_get_integer(keyfile, path, key, NULL);
125         g_key_file_free(keyfile);
126
127         return value;
128 }
129
130 //LCOV_EXCL_START
131 API void stc_util_set_debuglog(int debuglog)
132 {
133         g_debuglog = debuglog;
134 }
135 //LCOV_EXCL_STOP
136
137 API int stc_util_get_debuglog(void)
138 {
139         if (g_debuglog == -1)
140                 g_debuglog = stc_util_get_config_int(INFO_DEBUGLOG);
141
142         return g_debuglog;
143 }
144
145 void stc_util_initialize_config(void)
146 {
147         char path[MAX_PATH_LENGTH];
148         GKeyFile *keyfile;
149
150         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
151
152         keyfile = __load_key_file(path);
153         if (!keyfile) {
154                 keyfile = g_key_file_new(); //LCOV_EXCL_LINE
155                 g_key_file_set_integer(keyfile, path, INFO_DEBUGLOG, 0);
156         }
157
158         __save_key_file(keyfile, path);
159 }