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 /* Total statistics since epoch */
253 rule.iftype = STC_IFACE_ALL;
255 if (select_rule != NULL) {
256 DEBUG_GDBUS_VARIANT("Selection rule: ", select_rule);
257 GVariantIter *iter = NULL;
259 g_variant_get(select_rule, "a{sv}", &iter);
261 stc_manager_gdbus_dict_foreach(iter,
262 __stc_extract_select_rule,
264 g_variant_iter_free(iter);
267 STC_LOGD("No selection rule, using default selection rule."); //LCOV_EXCL_LINE
270 builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
272 ret = table_statistics_foreach_app(&rule,
273 __table_statistics_foreach_app_cb,
275 if (ret < STC_ERROR_NONE) {
276 g_variant_builder_unref(builder); //LCOV_EXCL_LINE
277 STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
278 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
279 return TRUE; //LCOV_EXCL_LINE
282 return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder);
283 g_variant_builder_unref(builder);
285 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
286 STC_DBUS_REPLY(invocation, return_parameters);
288 __STC_LOG_FUNC_EXIT__;
292 gboolean handle_statistics_init(StcStatistics *object,
293 GDBusMethodInvocation *invocation)
295 __STC_LOG_FUNC_ENTER__;
297 STC_LOGI("stc statistics initialized");
298 stc_statistics_complete_init(object, invocation);
300 __STC_LOG_FUNC_EXIT__;
304 gboolean handle_statistics_get(StcStatistics *object,
305 GDBusMethodInvocation *invocation,
307 GVariant *select_rule,
310 __STC_LOG_FUNC_ENTER__;
311 table_statistics_select_rule rule = {0, };
312 const time_t cur_time = time(0);
313 const time_t epoch = 0;
314 GVariantBuilder *builder = NULL;
315 GVariant *return_parameters = NULL;
318 /* Total statistics since epoch */
321 rule.iftype = STC_IFACE_ALL;
323 if (select_rule != NULL) {
324 DEBUG_GDBUS_VARIANT("Selection rule: ", select_rule);
325 GVariantIter *iter = NULL;
327 g_variant_get(select_rule, "a{sv}", &iter);
329 stc_manager_gdbus_dict_foreach(iter,
330 __stc_extract_select_rule,
332 g_variant_iter_free(iter);
335 STC_LOGD("No selection rule, using default selection rule."); //LCOV_EXCL_LINE
338 builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
340 ret = table_statistics_per_app(app_id, &rule,
341 __table_statistics_per_app_cb,
343 if (ret < STC_ERROR_NONE) {
344 g_variant_builder_unref(builder); //LCOV_EXCL_LINE
345 STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
346 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
347 return TRUE; //LCOV_EXCL_LINE
350 return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder);
351 g_variant_builder_unref(builder);
353 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
354 STC_DBUS_REPLY(invocation, return_parameters);
356 __STC_LOG_FUNC_EXIT__;
360 gboolean handle_statistics_reset(StcStatistics *object,
361 GDBusMethodInvocation *invocation,
362 GVariant *reset_rule,
365 __STC_LOG_FUNC_ENTER__;
366 table_statistics_reset_rule rule = {0, };
367 GVariant *return_parameters = NULL;
370 if (reset_rule != NULL) {
371 DEBUG_GDBUS_VARIANT("Selection rule: ", reset_rule);
372 GVariantIter *iter = NULL;
374 g_variant_get(reset_rule, "a{sv}", &iter);
376 stc_manager_gdbus_dict_foreach(iter,
377 __stc_extract_reset_rule,
379 g_variant_iter_free(iter);
382 STC_LOGD("No selection rule, using default selection rule."); //LCOV_EXCL_LINE
385 ret = table_statistics_reset(&rule);
386 if (ret < STC_ERROR_NONE) {
387 STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
388 goto handle_error; //LCOV_EXCL_LINE
391 return_parameters = g_variant_new("(i)", STC_ERROR_NONE);
393 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
394 STC_DBUS_REPLY(invocation, return_parameters);
398 FREE(rule.subscriber_id);
400 __STC_LOG_FUNC_EXIT__;