4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 * @file datausage-quota.c
24 * @desc Quota logic implementation
26 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
38 #include "data_usage.h"
40 #include "datausage-quota.h"
41 #include "edbus-handler.h"
45 static resourced_ret_c send_quota_message(const char *interface,
46 const char *format_str, char *params[])
50 resourced_ret_c ret_val;
54 msg = dbus_method_sync(BUS_NAME, RESOURCED_PATH_NETWORK,
55 RESOURCED_INTERFACE_NETWORK,
60 _E("Re-try to sync DBUS message, err_count : %d", i);
61 } while (i++ < RETRY_MAX);
64 _E("Failed to sync DBUS message.");
65 return RESOURCED_ERROR_FAIL;
68 dbus_error_init(&err);
70 ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val,
74 _E("no message : [%s:%s]\n", err.name, err.message);
75 ret_val = RESOURCED_ERROR_FAIL;
78 dbus_message_unref(msg);
79 dbus_error_free(&err);
84 static resourced_ret_c send_create_quota_message(const char *app_id,
85 const data_usage_quota *quota)
88 char snd_quota[MAX_DEC_SIZE(int64_t)], rcv_quota[MAX_DEC_SIZE(int64_t)];
90 snprintf(snd_quota, sizeof(snd_quota), "%" PRId64 "", quota->snd_quota);
91 snprintf(rcv_quota, sizeof(rcv_quota), "%" PRId64 "", quota->rcv_quota);
93 serialize_params(params, ARRAY_SIZE(params), app_id, quota->time_period,
94 snd_quota, rcv_quota, quota->snd_warning_threshold,
95 quota->rcv_warning_threshold, quota->quota_type, quota->iftype,
96 *quota->start_time, quota->roaming_type);
97 return send_quota_message(RESOURCED_NETWORK_CREATE_QUOTA, "sdttdddddd",
101 static resourced_ret_c send_remove_quota_message(const char *app_id,
102 const resourced_iface_type iftype,
103 const resourced_roaming_type roaming_type)
107 serialize_params(params, ARRAY_SIZE(params), app_id, iftype,
109 return send_quota_message(RESOURCED_NETWORK_REMOVE_QUOTA, "sdd",
113 API resourced_ret_c remove_datausage_quota(
114 const struct datausage_quota_reset_rule *rule)
116 if (!rule || !rule->app_id)
117 return RESOURCED_ERROR_INVALID_PARAMETER;
119 if (rule->iftype <= RESOURCED_IFACE_UNKNOWN ||
120 rule->iftype >= RESOURCED_IFACE_LAST_ELEM)
121 return RESOURCED_ERROR_INVALID_PARAMETER;
123 if (rule->roaming < RESOURCED_ROAMING_UNKNOWN ||
124 rule->roaming >= RESOURCED_ROAMING_LAST_ELEM)
125 return RESOURCED_ERROR_INVALID_PARAMETER;
127 return send_remove_quota_message(rule->app_id, rule->iftype,
131 API resourced_ret_c remove_datausage_quota_by_iftype(
132 const char *app_id, const resourced_iface_type iftype)
134 struct datausage_quota_reset_rule rule = {
137 .roaming = RESOURCED_ROAMING_UNKNOWN,
140 return remove_datausage_quota(&rule);
143 static int _is_valid_datausage_quota_params(const char *app_id,
144 const data_usage_quota *quota)
147 _SE("Empty appid! Please provide valid appid.");
152 _E("Empty quota! Please provide valid quota.");
156 if (quota->iftype >= RESOURCED_IFACE_LAST_ELEM) {
157 _E("Not valid value for iftype! See resourced_iface_type!");
164 static time_t _get_datausage_start_time(const time_t *quota_start_time)
166 return quota_start_time ? *quota_start_time : time(0);
169 API resourced_ret_c set_datausage_quota(const char *app_id,
170 const data_usage_quota *quota)
172 /* support old behaviour undefined iftype mean all iftype */
173 time_t start_time = 0;
174 data_usage_quota quota_to_send;
176 if (!_is_valid_datausage_quota_params(app_id, quota))
177 return RESOURCED_ERROR_INVALID_PARAMETER;
179 quota_to_send = *quota;
180 start_time = _get_datausage_start_time(quota->start_time);
181 quota_to_send.start_time = &start_time;
183 _SD("quota for app %s set", app_id);
184 _SD("===============================");
185 _SD("quota.start_time = %s", ctime(quota->start_time));
186 _SD("quota.time_period = %d", quota->time_period);
187 _SD("quota.snd_quota = %lld", quota->snd_quota);
188 _SD("quota.rcv_quota = %lld", quota->rcv_quota);
189 _SD("quota.quota_type = %d", quota->quota_type);
190 _SD("quota.iftype = %d", quota->iftype);
191 _SD("===============================");
193 return send_create_quota_message(app_id, "a_to_send);