2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "table-statistics.h"
19 #include "stc-statistics.h"
20 #include "stc-manager-gdbus.h"
22 #define STATISTICS_DBUS_ERROR_NAME "net.stc.statistics.Error.Failed"
24 #define STC_STATISTICS_DBUS_REPLY_ERROR(invocation, err_num) \
25 g_dbus_method_invocation_return_dbus_error((invocation), \
26 STATISTICS_DBUS_ERROR_NAME, \
27 stc_err_strs[-(err_num)])
29 static const gchar *stc_err_strs[] = {
42 void __stc_statistics_print_app_info(const table_statistics_info *info)
47 STC_LOGD("========== App data (Statistics) ==========");
48 STC_LOGD("app_id : [%s]", info->app_id ? info->app_id : "NULL");
49 STC_LOGD("ifname : [%s]", info->ifname ? info->ifname : "NULL");
50 STC_LOGD("Sent : [%lld] bytes", info->cnt.out_bytes);
51 STC_LOGD("Received : [%lld] bytes", info->cnt.in_bytes);
52 STC_LOGD("===========================================");
55 void __stc_extract_select_rule(const char *key, GVariant *value,
58 __STC_LOG_FUNC_ENTER__;
60 table_statistics_select_rule *rule =
61 (table_statistics_select_rule *) user_data;
63 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
64 return; //LCOV_EXCL_LINE
67 if (!g_strcmp0(key, "from")) {
68 rule->from = g_variant_get_uint64(value);
69 STC_LOGD("from: [%lu]", rule->from);
71 } else if (!g_strcmp0(key, "to")) {
72 rule->to = g_variant_get_uint64(value);
73 STC_LOGD("to: [%lu]", rule->to);
75 } else if (!g_strcmp0(key, "iftype")) {
76 rule->iftype = g_variant_get_uint16(value);
77 STC_LOGD("iftype: [%u]", (unsigned int) rule->iftype);
80 STC_LOGD("Unknown select rule"); //LCOV_EXCL_LINE
83 __STC_LOG_FUNC_EXIT__;
86 void __stc_extract_reset_rule(const char *key, GVariant *value,
89 __STC_LOG_FUNC_ENTER__;
91 table_statistics_reset_rule *rule =
92 (table_statistics_reset_rule *) user_data;
94 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
95 return; //LCOV_EXCL_LINE
98 if (!g_strcmp0(key, "app_id")) {
100 rule->app_id = g_variant_dup_string(value, &len);
101 STC_LOGD("app_id: [%s]", rule->app_id);
103 } else if (!g_strcmp0(key, "subscriber_id")) {
105 rule->subscriber_id = g_variant_dup_string(value, &len);
106 STC_LOGD("subscriber_id: [%s]", rule->subscriber_id);
108 } else if (!g_strcmp0(key, "iftype")) {
109 rule->iftype = g_variant_get_uint16(value);
110 STC_LOGD("iftype: [%u]", (unsigned int) rule->iftype);
112 } else if (!g_strcmp0(key, "from")) {
113 if (!(rule->interval)) {
114 rule->interval = MALLOC0(stc_db_tm_interval_s, 1); //LCOV_EXCL_LINE
115 if (!(rule->interval)) { //LCOV_EXCL_LINE
116 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
117 return; //LCOV_EXCL_LINE
121 rule->interval->from = g_variant_get_uint64(value);
122 STC_LOGD("from: [%lu]", rule->interval->from);
124 } else if (!g_strcmp0(key, "to")) {
125 if (!(rule->interval)) {
126 rule->interval = MALLOC0(stc_db_tm_interval_s, 1); //LCOV_EXCL_LINE
127 if (!(rule->interval)) { //LCOV_EXCL_LINE
128 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
129 return; //LCOV_EXCL_LINE
133 rule->interval->to = g_variant_get_uint64(value);
134 STC_LOGD("to: [%lu]", rule->interval->to);
137 STC_LOGD("Unknown reset rule"); //LCOV_EXCL_LINE
140 __STC_LOG_FUNC_EXIT__;
143 void __stc_statistics_app_info_builder_add(GVariantBuilder *builder,
144 const table_statistics_info *info)
146 __STC_LOG_FUNC_ENTER__;
148 if (!builder || !info) {
149 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
150 return; //LCOV_EXCL_LINE
154 g_variant_builder_add(builder, "{sv}", "app_id",
155 g_variant_new_string(info->app_id));
158 g_variant_builder_add(builder, "{sv}", "ifname",
159 g_variant_new_string(info->ifname));
161 g_variant_builder_add(builder, "{sv}", "subscriber_id",
162 g_variant_new_string(info->subscriber_id));
164 g_variant_builder_add(builder, "{sv}", "iftype",
165 g_variant_new_uint16(info->iftype));
167 if (info->interval != NULL) {
168 g_variant_builder_add(builder, "{sv}", "interval_to",
169 g_variant_new_uint64(info->interval->to));
171 g_variant_builder_add(builder, "{sv}", "interval_from",
172 g_variant_new_uint64(info->interval->from));
175 g_variant_builder_add(builder, "{sv}", "cnt_out_bytes",
176 g_variant_new_int64(info->cnt.out_bytes));
178 g_variant_builder_add(builder, "{sv}", "cnt_in_bytes",
179 g_variant_new_int64(info->cnt.in_bytes));
181 g_variant_builder_add(builder, "{sv}", "roaming",
182 g_variant_new_uint16(info->roaming));
184 g_variant_builder_add(builder, "{sv}", "hw_net_protocol_type",
185 g_variant_new_uint16(info->hw_net_protocol_type));
187 g_variant_builder_add(builder, "{sv}", "ground",
188 g_variant_new_uint16(info->ground));
190 __STC_LOG_FUNC_EXIT__;
193 stc_cb_ret_e __table_statistics_foreach_app_cb(const table_statistics_info *info,
196 __STC_LOG_FUNC_ENTER__;
197 GVariantBuilder *builder = (GVariantBuilder *)user_data;
198 GVariantBuilder sub_builder;
200 if (!info || !builder) {
201 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
202 return STC_CANCEL; //LCOV_EXCL_LINE
205 g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}"));
206 __stc_statistics_app_info_builder_add(&sub_builder, info);
207 __stc_statistics_print_app_info(info);
208 g_variant_builder_add_value(builder,
209 g_variant_builder_end(&sub_builder));
211 __STC_LOG_FUNC_EXIT__;
215 stc_cb_ret_e __table_statistics_per_app_cb(const table_statistics_info *info,
218 __STC_LOG_FUNC_ENTER__;
219 GVariantBuilder *builder = (GVariantBuilder *)user_data;
220 GVariantBuilder sub_builder;
222 if (!info || !builder) {
223 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
224 return STC_CANCEL; //LCOV_EXCL_LINE
227 g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}"));
228 __stc_statistics_app_info_builder_add(&sub_builder, info);
229 __stc_statistics_print_app_info(info);
230 g_variant_builder_add_value(builder,
231 g_variant_builder_end(&sub_builder));
233 __STC_LOG_FUNC_EXIT__;
237 gboolean handle_statistics_get_all(StcStatistics *object,
238 GDBusMethodInvocation *invocation,
239 GVariant *select_rule,
242 __STC_LOG_FUNC_ENTER__;
243 table_statistics_select_rule rule = {0, };
244 const time_t cur_time = time(0);
245 const time_t epoch = 0;
246 GVariantBuilder *builder = NULL;
247 GVariant *return_parameters = NULL;
250 stc_set_keep_alive(TRUE);
252 /* Total statistics since epoch */
255 rule.iftype = STC_IFACE_ALL;
257 if (select_rule != NULL) {
258 DEBUG_GDBUS_VARIANT("Selection rule: ", select_rule);
259 GVariantIter *iter = NULL;
261 g_variant_get(select_rule, "a{sv}", &iter);
263 stc_manager_gdbus_dict_foreach(iter,
264 __stc_extract_select_rule,
266 g_variant_iter_free(iter);
269 STC_LOGD("No selection rule, using default selection rule."); //LCOV_EXCL_LINE
272 builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
274 ret = table_statistics_foreach_app(&rule,
275 __table_statistics_foreach_app_cb,
277 if (ret < STC_ERROR_NONE) {
278 g_variant_builder_unref(builder); //LCOV_EXCL_LINE
279 STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
280 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
281 return TRUE; //LCOV_EXCL_LINE
284 return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder);
285 g_variant_builder_unref(builder);
287 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
288 STC_DBUS_REPLY(invocation, return_parameters);
290 __STC_LOG_FUNC_EXIT__;
294 gboolean handle_statistics_init(StcStatistics *object,
295 GDBusMethodInvocation *invocation)
297 __STC_LOG_FUNC_ENTER__;
299 stc_set_keep_alive(TRUE);
301 STC_LOGI("stc statistics initialized");
302 stc_statistics_complete_init(object, invocation);
304 __STC_LOG_FUNC_EXIT__;
308 gboolean handle_statistics_get(StcStatistics *object,
309 GDBusMethodInvocation *invocation,
311 GVariant *select_rule,
314 __STC_LOG_FUNC_ENTER__;
315 table_statistics_select_rule rule = {0, };
316 const time_t cur_time = time(0);
317 const time_t epoch = 0;
318 GVariantBuilder *builder = NULL;
319 GVariant *return_parameters = NULL;
322 stc_set_keep_alive(TRUE);
324 /* Total statistics since epoch */
327 rule.iftype = STC_IFACE_ALL;
329 if (select_rule != NULL) {
330 DEBUG_GDBUS_VARIANT("Selection rule: ", select_rule);
331 GVariantIter *iter = NULL;
333 g_variant_get(select_rule, "a{sv}", &iter);
335 stc_manager_gdbus_dict_foreach(iter,
336 __stc_extract_select_rule,
338 g_variant_iter_free(iter);
341 STC_LOGD("No selection rule, using default selection rule."); //LCOV_EXCL_LINE
344 builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
346 ret = table_statistics_per_app(app_id, &rule,
347 __table_statistics_per_app_cb,
349 if (ret < STC_ERROR_NONE) {
350 g_variant_builder_unref(builder); //LCOV_EXCL_LINE
351 STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
352 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
353 return TRUE; //LCOV_EXCL_LINE
356 return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder);
357 g_variant_builder_unref(builder);
359 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
360 STC_DBUS_REPLY(invocation, return_parameters);
362 __STC_LOG_FUNC_EXIT__;
366 gboolean handle_statistics_reset(StcStatistics *object,
367 GDBusMethodInvocation *invocation,
368 GVariant *reset_rule,
371 __STC_LOG_FUNC_ENTER__;
372 table_statistics_reset_rule rule = {0, };
373 GVariant *return_parameters = NULL;
376 stc_set_keep_alive(TRUE);
378 if (reset_rule != NULL) {
379 DEBUG_GDBUS_VARIANT("Selection rule: ", reset_rule);
380 GVariantIter *iter = NULL;
382 g_variant_get(reset_rule, "a{sv}", &iter);
384 stc_manager_gdbus_dict_foreach(iter,
385 __stc_extract_reset_rule,
387 g_variant_iter_free(iter);
390 STC_LOGD("No selection rule, using default selection rule."); //LCOV_EXCL_LINE
393 ret = table_statistics_reset(&rule);
394 if (ret < STC_ERROR_NONE) {
395 STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
396 goto handle_error; //LCOV_EXCL_LINE
399 return_parameters = g_variant_new("(i)", STC_ERROR_NONE);
401 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
402 STC_DBUS_REPLY(invocation, return_parameters);
406 FREE(rule.subscriber_id);
408 __STC_LOG_FUNC_EXIT__;