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_DBUS_REPLY(invocation, parameters) \
25 g_dbus_method_invocation_return_value((invocation), parameters);
27 #define STC_STATISTICS_DBUS_REPLY_ERROR(invocation, err_num) \
28 g_dbus_method_invocation_return_dbus_error((invocation), \
29 STATISTICS_DBUS_ERROR_NAME, \
30 stc_err_strs[-(err_num)])
32 static const gchar *stc_err_strs[] = {
43 void __stc_statistics_print_app_info(const table_statistics_info *info)
48 STC_LOGD("========== App data (Statistics) ==========");
49 STC_LOGD("app_id : [%s]", info->app_id ? info->app_id : "NULL");
50 STC_LOGD("ifname : [%s]", info->ifname ? info->ifname : "NULL");
51 STC_LOGD("Sent : [%lld] bytes", info->cnt.out_bytes);
52 STC_LOGD("Received : [%lld] bytes", info->cnt.in_bytes);
53 STC_LOGD("===========================================");
56 void __stc_extract_select_rule(const char *key, GVariant *value,
59 __STC_LOG_FUNC_ENTER__;
61 table_statistics_select_rule *rule =
62 (table_statistics_select_rule *) user_data;
64 __STC_LOG_FUNC_EXIT__;
68 if (!g_strcmp0(key, "version")) {
69 rule->version = g_variant_get_byte(value);
70 STC_LOGD("version: [%u]", (unsigned int) rule->version);
72 } else if (!g_strcmp0(key, "from")) {
73 rule->from = g_variant_get_uint64(value);
74 STC_LOGD("from: [%lu]", rule->from);
76 } else if (!g_strcmp0(key, "to")) {
77 rule->to = g_variant_get_uint64(value);
78 STC_LOGD("to: [%lu]", rule->to);
80 } else if (!g_strcmp0(key, "iftype")) {
81 rule->iftype = g_variant_get_uint16(value);
82 STC_LOGD("iftype: [%u]", (unsigned int) rule->iftype);
84 } else if (!g_strcmp0(key, "granularity")) {
85 rule->granularity = g_variant_get_int32(value);
86 STC_LOGD("granularity: [%d]", rule->granularity);
89 STC_LOGD("Unknown select rule");
92 __STC_LOG_FUNC_EXIT__;
95 void __stc_extract_reset_rule(const char *key, GVariant *value,
98 __STC_LOG_FUNC_ENTER__;
100 table_statistics_reset_rule *rule =
101 (table_statistics_reset_rule *) user_data;
103 __STC_LOG_FUNC_EXIT__;
107 if (!g_strcmp0(key, "version")) {
108 rule->version = g_variant_get_byte(value);
109 STC_LOGD("version: [%u]", (unsigned int) rule->version);
111 } else if (!g_strcmp0(key, "app_id")) {
113 rule->app_id = g_variant_dup_string(value, &len);
114 STC_LOGD("app_id: [%s]", rule->app_id);
116 } else if (!g_strcmp0(key, "subscriber_id")) {
118 rule->subscriber_id = g_variant_dup_string(value, &len);
119 STC_LOGD("subscriber_id: [%s]", rule->subscriber_id);
121 } else if (!g_strcmp0(key, "iftype")) {
122 rule->iftype = g_variant_get_uint16(value);
123 STC_LOGD("iftype: [%u]", (unsigned int) rule->iftype);
125 } else if (!g_strcmp0(key, "from")) {
126 if (!(rule->interval)) {
127 rule->interval = MALLOC0(stc_db_tm_interval_s, 1);
128 if (!(rule->interval)) {
129 __STC_LOG_FUNC_EXIT__;
134 rule->interval->from = g_variant_get_uint64(value);
135 STC_LOGD("from: [%lu]", rule->interval->from);
137 } else if (!g_strcmp0(key, "to")) {
138 if (!(rule->interval)) {
139 rule->interval = MALLOC0(stc_db_tm_interval_s, 1);
140 if (!(rule->interval)) {
141 __STC_LOG_FUNC_EXIT__;
146 rule->interval->to = g_variant_get_uint64(value);
147 STC_LOGD("to: [%lu]", rule->interval->to);
149 } else if (!g_strcmp0(key, "connection_state")) {
150 rule->connection_state = g_variant_get_int32(value);
151 STC_LOGD("connection_state: [%d]", rule->connection_state);
154 STC_LOGD("Unknown reset rule");
157 __STC_LOG_FUNC_EXIT__;
160 void __stc_statistics_app_info_builder_add(GVariantBuilder *builder,
161 const table_statistics_info *info)
163 __STC_LOG_FUNC_ENTER__;
165 if (!builder || !info) {
166 __STC_LOG_FUNC_EXIT__;
171 g_variant_builder_add(builder, "{sv}", "app_id",
172 g_variant_new_string(info->app_id));
175 g_variant_builder_add(builder, "{sv}", "ifname",
176 g_variant_new_string(info->ifname));
178 g_variant_builder_add(builder, "{sv}", "subscriber_id",
179 g_variant_new_string(info->subscriber_id));
181 g_variant_builder_add(builder, "{sv}", "iftype",
182 g_variant_new_uint16(info->iftype));
184 if (info->interval != NULL) {
185 g_variant_builder_add(builder, "{sv}", "interval_to",
186 g_variant_new_uint64(info->interval->to));
188 g_variant_builder_add(builder, "{sv}", "interval_from",
189 g_variant_new_uint64(info->interval->from));
192 g_variant_builder_add(builder, "{sv}", "cnt_out_bytes",
193 g_variant_new_int64(info->cnt.out_bytes));
195 g_variant_builder_add(builder, "{sv}", "cnt_in_bytes",
196 g_variant_new_int64(info->cnt.in_bytes));
198 g_variant_builder_add(builder, "{sv}", "roaming",
199 g_variant_new_uint16(info->roaming));
201 g_variant_builder_add(builder, "{sv}", "hw_net_protocol_type",
202 g_variant_new_uint16(info->hw_net_protocol_type));
204 g_variant_builder_add(builder, "{sv}", "ground",
205 g_variant_new_uint16(info->ground));
207 __STC_LOG_FUNC_EXIT__;
210 stc_cb_ret_e __table_statistics_foreach_app_cb(const table_statistics_info *info,
213 __STC_LOG_FUNC_ENTER__;
214 GVariantBuilder *builder = (GVariantBuilder *)user_data;
215 GVariantBuilder sub_builder;
217 if (!info || !builder) {
218 __STC_LOG_FUNC_EXIT__;
222 g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}"));
223 __stc_statistics_app_info_builder_add(&sub_builder, info);
224 __stc_statistics_print_app_info(info);
225 g_variant_builder_add_value(builder,
226 g_variant_builder_end(&sub_builder));
228 __STC_LOG_FUNC_EXIT__;
232 stc_cb_ret_e __table_statistics_per_app_cb(const table_statistics_info *info,
235 __STC_LOG_FUNC_ENTER__;
236 GVariantBuilder *builder = (GVariantBuilder *)user_data;
237 GVariantBuilder sub_builder;
239 if (!info || !builder) {
240 __STC_LOG_FUNC_EXIT__;
244 g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}"));
245 __stc_statistics_app_info_builder_add(&sub_builder, info);
246 __stc_statistics_print_app_info(info);
247 g_variant_builder_add_value(builder,
248 g_variant_builder_end(&sub_builder));
250 __STC_LOG_FUNC_EXIT__;
254 gboolean handle_statistics_get_all(StcStatistics *object,
255 GDBusMethodInvocation *invocation,
256 GVariant *select_rule,
259 __STC_LOG_FUNC_ENTER__;
260 table_statistics_select_rule rule = {0, };
261 const time_t cur_time = time(0);
262 const time_t epoch = 0;
263 GVariantBuilder *builder = NULL;
264 GVariant *return_parameters = NULL;
267 /* Total statistics since epoch */
270 rule.iftype = STC_IFACE_ALL;
272 if (select_rule != NULL) {
273 DEBUG_GDBUS_VARIANT("Selection rule: ", select_rule);
274 GVariantIter *iter = NULL;
276 g_variant_get(select_rule, "a{sv}", &iter);
278 stc_manager_gdbus_dict_foreach(iter,
279 __stc_extract_select_rule,
281 g_variant_iter_free(iter);
284 STC_LOGD("No selection rule, using default selection rule.");
287 builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
289 ret = table_statistics_foreach_app(&rule,
290 __table_statistics_foreach_app_cb,
292 if (ret < STC_ERROR_NONE) {
293 g_variant_builder_unref(builder);
294 STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret);
295 __STC_LOG_FUNC_EXIT__;
299 return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder);
300 g_variant_builder_unref(builder);
302 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
303 STC_DBUS_REPLY(invocation, return_parameters);
305 __STC_LOG_FUNC_EXIT__;
309 gboolean handle_statistics_init(StcStatistics *object,
310 GDBusMethodInvocation *invocation)
312 __STC_LOG_FUNC_ENTER__;
314 STC_LOGI("stc statistics initialized");
315 stc_statistics_complete_init(object, invocation);
317 __STC_LOG_FUNC_EXIT__;
321 gboolean handle_statistics_get(StcStatistics *object,
322 GDBusMethodInvocation *invocation,
324 GVariant *select_rule,
327 __STC_LOG_FUNC_ENTER__;
328 table_statistics_select_rule rule = {0, };
329 const time_t cur_time = time(0);
330 const time_t epoch = 0;
331 GVariantBuilder *builder = NULL;
332 GVariant *return_parameters = NULL;
335 /* Total statistics since epoch */
338 rule.iftype = STC_IFACE_ALL;
340 if (select_rule != NULL) {
341 DEBUG_GDBUS_VARIANT("Selection rule: ", select_rule);
342 GVariantIter *iter = NULL;
344 g_variant_get(select_rule, "a{sv}", &iter);
346 stc_manager_gdbus_dict_foreach(iter,
347 __stc_extract_select_rule,
349 g_variant_iter_free(iter);
352 STC_LOGD("No selection rule, using default selection rule.");
355 builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
357 ret = table_statistics_per_app(app_id, &rule,
358 __table_statistics_per_app_cb,
360 if (ret < STC_ERROR_NONE) {
361 g_variant_builder_unref(builder);
362 STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret);
363 __STC_LOG_FUNC_EXIT__;
367 return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder);
368 g_variant_builder_unref(builder);
370 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
371 STC_DBUS_REPLY(invocation, return_parameters);
373 __STC_LOG_FUNC_EXIT__;
377 gboolean handle_statistics_reset(StcStatistics *object,
378 GDBusMethodInvocation *invocation,
379 GVariant *reset_rule,
382 __STC_LOG_FUNC_ENTER__;
383 table_statistics_reset_rule rule = {0, };
384 GVariant *return_parameters = NULL;
387 if (reset_rule != NULL) {
388 DEBUG_GDBUS_VARIANT("Selection rule: ", reset_rule);
389 GVariantIter *iter = NULL;
391 g_variant_get(reset_rule, "a{sv}", &iter);
393 stc_manager_gdbus_dict_foreach(iter,
394 __stc_extract_reset_rule,
396 g_variant_iter_free(iter);
399 STC_LOGD("No selection rule, using default selection rule.");
402 ret = table_statistics_reset(&rule);
403 if (ret < STC_ERROR_NONE) {
404 STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret);
408 return_parameters = g_variant_new("(i)", STC_ERROR_NONE);
410 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
411 STC_DBUS_REPLY(invocation, return_parameters);
415 FREE(rule.subscriber_id);
417 __STC_LOG_FUNC_EXIT__;