Merge "Return errors to caller" into tizen_5.5
[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 typedef struct {
20         int state;
21         char *key;
22 } log_info_s;
23
24 log_info_s g_log_info[] = {
25         {-1, INFO_DEBUG_LOG},
26         {-1, INFO_STAT_LOG},
27         {-1, INFO_RSTN_LOG},
28         {-1, INFO_FW_LOG},
29         {-1, INFO_PCAP_LOG},
30 };
31
32 static void __sync_file_to_disk(const char *path)
33 {
34         FILE *fp = NULL;
35         fp = fopen(path, "a+");
36         if (fp) {
37                 fflush(fp);
38                 fsync(fp->_fileno);
39                 fclose(fp);
40                 STC_LOGD("Sync the file to disk");
41         }
42 }
43
44 static GKeyFile *__load_key_file(const char *path)
45 {
46         GKeyFile *keyfile = NULL;
47         GError *error = NULL;
48
49         STC_LOGD("Loading [%s]", path);
50
51         keyfile = g_key_file_new();
52
53         if (!g_key_file_load_from_file(keyfile, path, 0, &error)) {
54                 STC_LOGD("Unable to load [%s] : %s", path, error->message); //LCOV_EXCL_LINE
55                 g_clear_error(&error); //LCOV_EXCL_LINE
56                 g_key_file_free(keyfile); //LCOV_EXCL_LINE
57                 keyfile = NULL; //LCOV_EXCL_LINE
58         }
59
60         return keyfile;
61 }
62
63 static int __save_key_file(GKeyFile *keyfile, char *path)
64 {
65         gchar *data = NULL;
66         gsize length = 0;
67         GError *error = NULL;
68         int ret = 0;
69
70         data = g_key_file_to_data(keyfile, &length, NULL);
71
72         if (!g_file_set_contents(path, data, length, &error)) {
73                 STC_LOGD("Failed to save information : %s", error->message); //LCOV_EXCL_LINE
74                 g_error_free(error); //LCOV_EXCL_LINE
75                 ret = -EIO; //LCOV_EXCL_LINE
76         }
77
78         __sync_file_to_disk(path);
79
80         g_free(data);
81         return ret;
82 }
83
84 //LCOV_EXCL_START
85 gboolean stc_util_get_config_bool(char *key)
86 {
87         char path[MAX_PATH_LENGTH];
88         GKeyFile *keyfile;
89         gboolean value;
90
91         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
92
93         keyfile = __load_key_file(path);
94         if (!keyfile)
95                 keyfile = g_key_file_new();
96
97         value = g_key_file_get_boolean(keyfile, path, key, NULL);
98         g_key_file_free(keyfile);
99
100         return value;
101 }
102
103 gchar * stc_util_get_config_str(char *key)
104 {
105         char path[MAX_PATH_LENGTH];
106         GKeyFile *keyfile;
107         gchar *value;
108
109         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
110
111         keyfile = __load_key_file(path);
112         if (!keyfile)
113                 keyfile = g_key_file_new();
114
115         value = g_key_file_get_string(keyfile, path, key, NULL);
116         g_key_file_free(keyfile);
117
118         return value;
119 }
120 //LCOV_EXCL_STOP
121
122 int stc_util_get_config_int(char *key)
123 {
124         char path[MAX_PATH_LENGTH];
125         GKeyFile *keyfile;
126         gint value;
127
128         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
129
130         keyfile = __load_key_file(path);
131         if (!keyfile)
132                 keyfile = g_key_file_new(); //LCOV_EXCL_LINE
133
134
135         value = g_key_file_get_integer(keyfile, path, key, NULL);
136         g_key_file_free(keyfile);
137
138         return value;
139 }
140
141 //LCOV_EXCL_START
142 API void stc_util_update_log_state(void)
143 {
144         int i;
145
146         for (i = DEBUG_LOG_INFO; i < MAX_LOG_INFO; ++i)
147                 g_log_info[i].state = stc_util_get_config_int(g_log_info[i].key);
148
149         STC_LOGD("log info [%d:%d:%d:%d:%d]",
150                 g_log_info[DEBUG_LOG_INFO].state, g_log_info[STAT_LOG_INFO].state,
151                 g_log_info[RSTN_LOG_INFO].state, g_log_info[FW_LOG_INFO].state,
152                 g_log_info[PCAP_LOG_INFO].state);
153 }
154
155 API void stc_util_set_log_state(log_info_e info, int state)
156 {
157         g_log_info[info].state = state;
158 }
159 //LCOV_EXCL_STOP
160
161 API int stc_util_get_log_state(log_info_e info)
162 {
163         if (g_log_info[info].state == -1)
164                 g_log_info[info].state = stc_util_get_config_int(g_log_info[info].key);
165
166         return g_log_info[info].state;
167 }
168
169 void stc_util_initialize_config(void)
170 {
171         char path[MAX_PATH_LENGTH];
172         GKeyFile *keyfile;
173         int i;
174
175         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
176
177         keyfile = __load_key_file(path);
178         if (!keyfile) {
179                 keyfile = g_key_file_new(); //LCOV_EXCL_LINE
180                 for (i = DEBUG_LOG_INFO; i < MAX_LOG_INFO; ++i)
181                         g_key_file_set_integer(keyfile, path, g_log_info[i].key, 0);
182         }
183
184         __save_key_file(keyfile, path);
185         g_key_file_free(keyfile);
186 }