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-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_ENABLE) {
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 gboolean handle_restriction_set(StcRestriction *object,
263 GDBusMethodInvocation *invocation,
264 GVariant *parameters,
267 __STC_LOG_FUNC_ENTER__;
268 GVariantIter *iter = NULL;
269 table_restrictions_info rule;
270 stc_error_e ret = STC_ERROR_NONE;
272 memset(&rule, 0, sizeof(table_restrictions_info));
273 __initialize_rstn_rule(&rule);
275 g_variant_get(parameters, "a{sv}", &iter);
277 stc_manager_gdbus_dict_foreach(iter,
278 __stc_extract_restriction_rule,
280 g_variant_iter_free(iter);
283 if (__validate_rstn_rule(&rule) == FALSE) {
284 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
285 STC_ERROR_INVALID_PARAMETER);
286 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
290 ret = table_restrictions_update(&rule);
291 if (ret != STC_ERROR_NONE) {
292 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
293 STC_ERROR_DB_FAILED);
294 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
298 stc_monitor_rstn_add(&rule);
299 STC_DBUS_REPLY_ERROR_NONE(invocation);
300 __STC_LOG_FUNC_EXIT__;
304 gboolean handle_restriction_unset(StcRestriction *object,
305 GDBusMethodInvocation *invocation,
306 GVariant *parameters,
309 __STC_LOG_FUNC_ENTER__;
310 GVariantIter *iter = NULL;
311 table_restrictions_info rule;
312 stc_error_e ret = STC_ERROR_NONE;
314 memset(&rule, 0, sizeof(table_restrictions_info));
315 __initialize_rstn_rule(&rule);
317 g_variant_get(parameters, "a{sv}", &iter);
319 stc_manager_gdbus_dict_foreach(iter,
320 __stc_extract_restriction_rule,
322 g_variant_iter_free(iter);
325 if (__validate_rstn_rule(&rule) == FALSE) {
326 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
327 STC_ERROR_INVALID_PARAMETER);
328 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
332 ret = table_restrictions_delete(rule.app_id, rule.iftype, rule.ifname,
333 rule.subscriber_id, rule.roaming);
334 if (ret != STC_ERROR_NONE) {
335 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
336 STC_ERROR_DB_FAILED);
337 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
341 stc_monitor_rstn_remove(&rule);
342 STC_DBUS_REPLY_ERROR_NONE(invocation);
343 __STC_LOG_FUNC_EXIT__;
347 gboolean handle_restriction_get(StcRestriction *object,
348 GDBusMethodInvocation *invocation,
352 __STC_LOG_FUNC_ENTER__;
353 GVariantBuilder *builder = NULL;
354 GVariant *return_parameters = NULL;
357 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
359 ret = table_restrictions_per_app(app_id,
360 __table_restrictions_per_app_cb,
362 if (ret < STC_ERROR_NONE) {
363 g_variant_builder_unref(builder); //LCOV_EXCL_LINE
364 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
365 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
366 return TRUE; //LCOV_EXCL_LINE
369 return_parameters = g_variant_new("(ia{sv})", STC_ERROR_NONE, builder);
370 g_variant_builder_unref(builder);
372 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
373 STC_DBUS_REPLY(invocation, return_parameters);
374 __STC_LOG_FUNC_EXIT__;
378 gboolean handle_restriction_get_all(StcRestriction *object,
379 GDBusMethodInvocation *invocation,
382 __STC_LOG_FUNC_ENTER__;
383 GVariantBuilder *builder = NULL;
384 GVariant *return_parameters = NULL;
387 builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
389 ret = table_restrictions_foreach(__table_restrictions_foreach_app_cb,
391 if (ret < STC_ERROR_NONE) {
392 g_variant_builder_unref(builder); //LCOV_EXCL_LINE
393 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
394 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
395 return TRUE; //LCOV_EXCL_LINE
398 return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder);
399 g_variant_builder_unref(builder);
401 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
402 STC_DBUS_REPLY(invocation, return_parameters);
403 __STC_LOG_FUNC_EXIT__;
407 gboolean handle_restriction_get_type(StcRestriction *object,
408 GDBusMethodInvocation *invocation,
413 __STC_LOG_FUNC_ENTER__;
414 GVariant *return_parameters = NULL;
415 stc_rstn_type_e type = STC_RSTN_TYPE_UNKNOWN;
418 ret = table_restrictions_get_restriction_type(app_id, iftype, &type);
419 if (ret < STC_ERROR_NONE) {
420 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
421 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
425 return_parameters = g_variant_new("(ii)", STC_ERROR_NONE, type);
426 STC_DBUS_REPLY(invocation, return_parameters);
427 __STC_LOG_FUNC_EXIT__;