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-restrictions.h"
19 #include "stc-restriction.h"
20 #include "stc-manager-gdbus.h"
21 #include "stc-manager-plugin-monitor.h"
23 #define RESTRICTION_DBUS_ERROR_NAME "net.stc.restriction.Error.Failed"
25 #define STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, err_num) \
26 g_dbus_method_invocation_return_dbus_error((invocation), \
27 RESTRICTION_DBUS_ERROR_NAME, \
28 stc_err_strs[-(err_num)])
30 static const gchar *stc_err_strs[] = {
43 void __initialize_rstn_rule(table_restrictions_info *rule)
47 rule->iftype = STC_IFACE_ALL;
50 rule->subscriber_id = NULL;
51 rule->roaming = STC_ROAMING_DISABLE;
53 rule->rstn_type = STC_RSTN_TYPE_UNKNOWN;
54 rule->data_limit = -1;
55 rule->data_warn_limit = -1;
57 rule->month_start_date = 1;
58 rule->monthly_limit = -1;
59 rule->weekly_limit = -1;
60 rule->daily_limit = -1;
65 gboolean __validate_rstn_rule(table_restrictions_info *rule)
67 __STC_LOG_FUNC_ENTER__;
70 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
71 return FALSE; //LCOV_EXCL_LINE
74 if (rule->iftype <= STC_IFACE_UNKNOWN ||
75 rule->iftype >= STC_IFACE_LAST_ELEM) {
76 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
77 return FALSE; //LCOV_EXCL_LINE
80 if (rule->roaming > STC_ROAMING_DISABLE) {
81 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
82 return FALSE; //LCOV_EXCL_LINE
85 if (rule->subscriber_id == NULL) {
86 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
87 return FALSE; //LCOV_EXCL_LINE
90 if (rule->app_id == NULL) {
91 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
92 return FALSE; //LCOV_EXCL_LINE
95 __STC_LOG_FUNC_EXIT__;
99 void __stc_restriction_app_info_builder_add(GVariantBuilder *builder,
100 const table_restrictions_info *info)
102 __STC_LOG_FUNC_ENTER__;
104 if (!builder || !info) {
105 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
106 return; //LCOV_EXCL_LINE
109 g_variant_builder_add(builder, "{sv}", "app_id",
110 g_variant_new_string(info->app_id));
112 g_variant_builder_add(builder, "{sv}", "ifname",
113 g_variant_new_string(info->ifname));
115 g_variant_builder_add(builder, "{sv}", "iftype",
116 g_variant_new_uint16(info->iftype));
118 g_variant_builder_add(builder, "{sv}", "rstn_type",
119 g_variant_new_uint16(info->rstn_type));
121 g_variant_builder_add(builder, "{sv}", "data_limit",
122 g_variant_new_int64(info->data_limit));
124 g_variant_builder_add(builder, "{sv}", "data_warn_limit",
125 g_variant_new_int64(info->data_warn_limit));
127 g_variant_builder_add(builder, "{sv}", "month_start_date",
128 g_variant_new_int32(info->month_start_date));
130 g_variant_builder_add(builder, "{sv}", "monthly_limit",
131 g_variant_new_int64(info->monthly_limit));
133 g_variant_builder_add(builder, "{sv}", "weekly_limit",
134 g_variant_new_int64(info->weekly_limit));
136 g_variant_builder_add(builder, "{sv}", "daily_limit",
137 g_variant_new_int64(info->daily_limit));
139 g_variant_builder_add(builder, "{sv}", "roaming",
140 g_variant_new_uint16(info->roaming));
142 g_variant_builder_add(builder, "{sv}", "subscriber_id",
143 g_variant_new_string(info->subscriber_id));
145 __STC_LOG_FUNC_EXIT__;
148 stc_cb_ret_e __table_restrictions_foreach_app_cb(const table_restrictions_info *info,
151 __STC_LOG_FUNC_ENTER__;
152 GVariantBuilder *builder = (GVariantBuilder *)user_data;
153 GVariantBuilder sub_builder;
155 if (!info || !builder) {
156 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
157 return STC_CANCEL; //LCOV_EXCL_LINE
160 g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}"));
161 __stc_restriction_app_info_builder_add(&sub_builder, info);
163 g_variant_builder_add_value(builder,
164 g_variant_builder_end(&sub_builder));
166 __STC_LOG_FUNC_EXIT__;
170 stc_cb_ret_e __table_restrictions_per_app_cb(const table_restrictions_info *info,
173 __STC_LOG_FUNC_ENTER__;
174 GVariantBuilder *builder = (GVariantBuilder *)user_data;
176 if (!info || !builder) {
177 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
178 return STC_CANCEL; //LCOV_EXCL_LINE
181 __stc_restriction_app_info_builder_add(builder, info);
183 __STC_LOG_FUNC_EXIT__;
187 static void __stc_extract_restriction_rule(const char *key, GVariant *value,
190 table_restrictions_info *rule =
191 (table_restrictions_info *) user_data;
193 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
194 return; //LCOV_EXCL_LINE
197 if (!g_strcmp0(key, "app_id")) {
199 const gchar *str = g_variant_get_string(value, &str_length);
200 rule->app_id = g_strdup(str);
201 STC_LOGD("app_id: [%s]", rule->app_id);
203 } else if (!g_strcmp0(key, "ifname")) {
205 const gchar *str = g_variant_get_string(value, &str_length);
206 rule->ifname = g_strdup(str);
207 STC_LOGD("ifname: [%s]", rule->ifname);
209 } else if (!g_strcmp0(key, "iftype")) {
210 rule->iftype = g_variant_get_uint16(value);
211 STC_LOGD("iftype: [%u]", (unsigned int) rule->iftype);
213 } else if (!g_strcmp0(key, "data_limit")) {
214 rule->data_limit = g_variant_get_int64(value);
215 STC_LOGD("data_limit: [%lld]", rule->data_limit);
217 } else if (!g_strcmp0(key, "data_warn_limit")) {
218 rule->data_warn_limit = g_variant_get_int64(value);
219 STC_LOGD("data_warn_limit: [%lld]", rule->data_warn_limit);
221 } else if (!g_strcmp0(key, "month_start_date")) {
222 rule->month_start_date = g_variant_get_int32(value);
223 STC_LOGD("month_start_date: [%d]", rule->month_start_date);
225 } else if (!g_strcmp0(key, "monthly_limit")) {
226 rule->monthly_limit = g_variant_get_int64(value);
227 STC_LOGD("monthly_limit: [%lld]", rule->monthly_limit);
229 } else if (!g_strcmp0(key, "weekly_limit")) {
230 rule->weekly_limit = g_variant_get_int64(value);
231 STC_LOGD("weekly_limit: [%lld]", rule->weekly_limit);
233 } else if (!g_strcmp0(key, "daily_limit")) {
234 rule->daily_limit = g_variant_get_int64(value);
235 STC_LOGD("daily_limit: [%lld]", rule->daily_limit);
237 } else if (!g_strcmp0(key, "roaming")) {
238 rule->roaming = g_variant_get_uint16(value);
239 STC_LOGD("roaming: [%u]", rule->roaming);
241 } else if (!g_strcmp0(key, "subscriber_id")) {
243 const gchar *str = g_variant_get_string(value, &str_length);
244 rule->subscriber_id = g_strdup(str);
245 STC_LOGD("subscriber_id: [%s]", rule->subscriber_id);
247 } else if (!g_strcmp0(key, "rstn_type")) {
248 rule->rstn_type = g_variant_get_uint16(value);
249 STC_LOGD("type: [%u]", (unsigned int) rule->rstn_type);
251 } else if (!g_strcmp0(key, "mac")) {
253 const gchar *str = g_variant_get_string(value, &str_length);
254 rule->mac = g_strdup(str);
255 STC_LOGD("mac: [%s]", rule->mac);
258 STC_LOGD("Unknown select rule"); //LCOV_EXCL_LINE
262 static void __stc_free_restriction_rule_members(table_restrictions_info *rule)
266 FREE(rule->subscriber_id);
270 gboolean handle_restriction_set(StcRestriction *object,
271 GDBusMethodInvocation *invocation,
272 GVariant *parameters,
275 __STC_LOG_FUNC_ENTER__;
276 GVariantIter *iter = NULL;
277 table_restrictions_info rule;
278 stc_error_e ret = STC_ERROR_NONE;
280 stc_set_keep_alive(TRUE);
282 memset(&rule, 0, sizeof(table_restrictions_info));
283 __initialize_rstn_rule(&rule);
285 g_variant_get(parameters, "a{sv}", &iter);
287 stc_manager_gdbus_dict_foreach(iter,
288 __stc_extract_restriction_rule,
290 g_variant_iter_free(iter);
293 if (__validate_rstn_rule(&rule) == FALSE) {
294 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
295 STC_ERROR_INVALID_PARAMETER);
296 __stc_free_restriction_rule_members(&rule);
297 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
301 ret = table_restrictions_update(&rule);
302 if (ret != STC_ERROR_NONE) {
303 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
304 STC_ERROR_DB_FAILED);
305 __stc_free_restriction_rule_members(&rule);
306 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
310 stc_plugin_monitor_add_rstn(&rule);
311 STC_DBUS_REPLY_ERROR_NONE(invocation);
312 __stc_free_restriction_rule_members(&rule);
313 __STC_LOG_FUNC_EXIT__;
317 gboolean handle_restriction_unset(StcRestriction *object,
318 GDBusMethodInvocation *invocation,
319 GVariant *parameters,
322 __STC_LOG_FUNC_ENTER__;
323 GVariantIter *iter = NULL;
324 table_restrictions_info rule;
325 stc_error_e ret = STC_ERROR_NONE;
327 stc_set_keep_alive(TRUE);
329 memset(&rule, 0, sizeof(table_restrictions_info));
330 __initialize_rstn_rule(&rule);
332 g_variant_get(parameters, "a{sv}", &iter);
334 stc_manager_gdbus_dict_foreach(iter,
335 __stc_extract_restriction_rule,
337 g_variant_iter_free(iter);
340 if (__validate_rstn_rule(&rule) == FALSE) {
341 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
342 STC_ERROR_INVALID_PARAMETER);
343 __stc_free_restriction_rule_members(&rule);
344 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
348 ret = table_restrictions_delete(rule.app_id, rule.iftype, rule.ifname,
349 rule.subscriber_id, rule.roaming);
350 if (ret != STC_ERROR_NONE) {
351 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
352 STC_ERROR_DB_FAILED);
353 __stc_free_restriction_rule_members(&rule);
354 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
358 stc_plugin_monitor_remove_rstn(&rule);
359 STC_DBUS_REPLY_ERROR_NONE(invocation);
360 __stc_free_restriction_rule_members(&rule);
361 __STC_LOG_FUNC_EXIT__;
365 gboolean handle_restriction_set_list(StcRestriction *object,
366 GDBusMethodInvocation *invocation,
367 GVariant *parameters,
370 __STC_LOG_FUNC_ENTER__;
371 GVariantIter *iter = NULL;
372 GVariantIter *iter_row = NULL;
373 stc_error_e ret = STC_ERROR_NONE;
375 stc_set_keep_alive(TRUE);
377 g_variant_get(parameters, "aa{sv}", &iter);
378 while (g_variant_iter_next(iter, "a{sv}", &iter_row)) {
379 table_restrictions_info rule;
381 memset(&rule, 0, sizeof(table_restrictions_info));
382 __initialize_rstn_rule(&rule);
384 stc_manager_gdbus_dict_foreach(iter_row,
385 __stc_extract_restriction_rule, &rule);
387 if (__validate_rstn_rule(&rule)) {
388 ret = table_restrictions_update(&rule);
389 if (ret != STC_ERROR_NONE) {
390 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
391 g_variant_iter_free(iter_row);
392 g_variant_iter_free(iter);
393 __stc_free_restriction_rule_members(&rule);
394 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
398 ret = stc_plugin_monitor_add_rstn(&rule);
399 if (ret != STC_ERROR_NONE) {
400 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
401 g_variant_iter_free(iter_row);
402 g_variant_iter_free(iter);
403 __stc_free_restriction_rule_members(&rule);
404 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
409 __stc_free_restriction_rule_members(&rule);
410 g_variant_iter_free(iter_row);
412 g_variant_iter_free(iter);
414 STC_DBUS_REPLY_ERROR_NONE(invocation);
415 __STC_LOG_FUNC_EXIT__;
419 gboolean handle_restriction_unset_list(StcRestriction *object,
420 GDBusMethodInvocation *invocation,
421 GVariant *parameters,
424 __STC_LOG_FUNC_ENTER__;
425 GVariantIter *iter = NULL;
426 GVariantIter *iter_row = NULL;
427 stc_error_e ret = STC_ERROR_NONE;
429 stc_set_keep_alive(TRUE);
431 g_variant_get(parameters, "aa{sv}", &iter);
432 while (g_variant_iter_next(iter, "a{sv}", &iter_row)) {
433 table_restrictions_info rule;
435 memset(&rule, 0, sizeof(table_restrictions_info));
436 __initialize_rstn_rule(&rule);
438 stc_manager_gdbus_dict_foreach(iter_row,
439 __stc_extract_restriction_rule, &rule);
441 if (__validate_rstn_rule(&rule)) {
442 ret = table_restrictions_delete(rule.app_id, rule.iftype,
443 rule.ifname, rule.subscriber_id, rule.roaming);
444 if (ret != STC_ERROR_NONE) {
445 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
446 g_variant_iter_free(iter_row);
447 g_variant_iter_free(iter);
448 __stc_free_restriction_rule_members(&rule);
449 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
453 ret = stc_plugin_monitor_remove_rstn(&rule);
454 if (ret != STC_ERROR_NONE) {
455 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
456 g_variant_iter_free(iter_row);
457 g_variant_iter_free(iter);
458 __stc_free_restriction_rule_members(&rule);
459 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
464 __stc_free_restriction_rule_members(&rule);
465 g_variant_iter_free(iter_row);
467 g_variant_iter_free(iter);
469 STC_DBUS_REPLY_ERROR_NONE(invocation);
470 __STC_LOG_FUNC_EXIT__;
474 gboolean handle_restriction_get(StcRestriction *object,
475 GDBusMethodInvocation *invocation,
479 __STC_LOG_FUNC_ENTER__;
480 GVariantBuilder *builder = NULL;
481 GVariant *return_parameters = NULL;
484 stc_set_keep_alive(TRUE);
486 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
488 ret = table_restrictions_per_app(app_id,
489 __table_restrictions_per_app_cb,
491 if (ret < STC_ERROR_NONE) {
492 g_variant_builder_unref(builder); //LCOV_EXCL_LINE
493 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
494 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
495 return TRUE; //LCOV_EXCL_LINE
498 return_parameters = g_variant_new("(ia{sv})", STC_ERROR_NONE, builder);
499 g_variant_builder_unref(builder);
501 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
502 STC_DBUS_REPLY(invocation, return_parameters);
503 __STC_LOG_FUNC_EXIT__;
507 gboolean handle_restriction_get_all(StcRestriction *object,
508 GDBusMethodInvocation *invocation,
511 __STC_LOG_FUNC_ENTER__;
512 GVariantBuilder *builder = NULL;
513 GVariant *return_parameters = NULL;
516 stc_set_keep_alive(TRUE);
518 builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
520 ret = table_restrictions_foreach(__table_restrictions_foreach_app_cb,
522 if (ret < STC_ERROR_NONE) {
523 g_variant_builder_unref(builder); //LCOV_EXCL_LINE
524 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
525 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
526 return TRUE; //LCOV_EXCL_LINE
529 return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder);
530 g_variant_builder_unref(builder);
532 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
533 STC_DBUS_REPLY(invocation, return_parameters);
534 __STC_LOG_FUNC_EXIT__;
538 gboolean handle_restriction_get_type(StcRestriction *object,
539 GDBusMethodInvocation *invocation,
544 __STC_LOG_FUNC_ENTER__;
545 GVariant *return_parameters = NULL;
546 stc_rstn_type_e type = STC_RSTN_TYPE_UNKNOWN;
549 stc_set_keep_alive(TRUE);
551 ret = table_restrictions_get_restriction_type(app_id, iftype, &type);
552 if (ret < STC_ERROR_NONE) {
553 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
554 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
558 return_parameters = g_variant_new("(ii)", STC_ERROR_NONE, type);
559 STC_DBUS_REPLY(invocation, return_parameters);
560 __STC_LOG_FUNC_EXIT__;