tizen 2.3 release
[kernel/api/system-resource.git] / src / network / datausage-quota.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20
21 /*
22  * @file datausage-quota.c
23  *
24  * @desc Quota logic implementation
25  *
26  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
27  *
28  */
29
30 #include <inttypes.h>
31 #include <sqlite3.h>
32 #include <string.h>
33 #include <time.h>
34 #include <vconf.h>
35
36 #include "const.h"
37 #include "const.h"
38 #include "data_usage.h"
39 #include "database.h"
40 #include "datausage-quota.h"
41 #include "edbus-handler.h"
42 #include "macro.h"
43 #include "trace.h"
44
45 static resourced_ret_c send_quota_message(const char *interface,
46         const char *format_str, char *params[])
47 {
48         DBusError err;
49         DBusMessage *msg;
50         resourced_ret_c ret_val;
51         int ret, i = 0;
52
53         do {
54                 msg = dbus_method_sync(BUS_NAME, RESOURCED_PATH_NETWORK,
55                                        RESOURCED_INTERFACE_NETWORK,
56                                        interface,
57                                        format_str, params);
58                 if (msg)
59                         break;
60                 _E("Re-try to sync DBUS message, err_count : %d", i);
61         } while (i++ < RETRY_MAX);
62
63         if (!msg) {
64                 _E("Failed to sync DBUS message.");
65                 return RESOURCED_ERROR_FAIL;
66         }
67
68         dbus_error_init(&err);
69
70         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val,
71                                     DBUS_TYPE_INVALID);
72
73         if (ret == FALSE) {
74                 _E("no message : [%s:%s]\n", err.name, err.message);
75                 ret_val = RESOURCED_ERROR_FAIL;
76         }
77
78         dbus_message_unref(msg);
79         dbus_error_free(&err);
80
81         return ret_val;
82 }
83
84 static resourced_ret_c send_create_quota_message(const char *app_id,
85         const data_usage_quota *quota)
86 {
87         char *params[10];
88         char snd_quota[MAX_DEC_SIZE(int64_t)], rcv_quota[MAX_DEC_SIZE(int64_t)];
89
90         snprintf(snd_quota, sizeof(snd_quota), "%" PRId64 "", quota->snd_quota);
91         snprintf(rcv_quota, sizeof(rcv_quota), "%" PRId64 "", quota->rcv_quota);
92
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",
98                 params);
99 }
100
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)
104 {
105         char *params[3];
106
107         serialize_params(params, ARRAY_SIZE(params), app_id, iftype,
108                 roaming_type);
109         return send_quota_message(RESOURCED_NETWORK_REMOVE_QUOTA, "sdd",
110                 params);
111 }
112
113 API resourced_ret_c remove_datausage_quota(
114         const struct datausage_quota_reset_rule *rule)
115 {
116         if (!rule || !rule->app_id)
117                 return RESOURCED_ERROR_INVALID_PARAMETER;
118
119         if (rule->iftype <= RESOURCED_IFACE_UNKNOWN ||
120             rule->iftype >= RESOURCED_IFACE_LAST_ELEM)
121                 return RESOURCED_ERROR_INVALID_PARAMETER;
122
123         if (rule->roaming < RESOURCED_ROAMING_UNKNOWN ||
124             rule->roaming >= RESOURCED_ROAMING_LAST_ELEM)
125                 return RESOURCED_ERROR_INVALID_PARAMETER;
126
127         return send_remove_quota_message(rule->app_id, rule->iftype,
128                  rule->roaming);
129 }
130
131 API resourced_ret_c remove_datausage_quota_by_iftype(
132         const char *app_id, const resourced_iface_type iftype)
133 {
134         struct datausage_quota_reset_rule rule = {
135                 .app_id = app_id,
136                 .iftype = iftype,
137                 .roaming = RESOURCED_ROAMING_UNKNOWN,
138         };
139
140         return remove_datausage_quota(&rule);
141 }
142
143 static int _is_valid_datausage_quota_params(const char *app_id,
144                                         const data_usage_quota *quota)
145 {
146         if (!app_id) {
147                 _SE("Empty appid! Please provide valid appid.");
148                 return 0;
149         }
150
151         if (!quota) {
152                 _E("Empty quota! Please provide valid quota.");
153                 return 0;
154         }
155
156         if (quota->iftype >= RESOURCED_IFACE_LAST_ELEM) {
157                 _E("Not valid value for iftype! See resourced_iface_type!");
158                 return 0;
159         }
160
161         return 1;
162 }
163
164 static time_t _get_datausage_start_time(const time_t *quota_start_time)
165 {
166         return quota_start_time ? *quota_start_time : time(0);
167 }
168
169 API resourced_ret_c set_datausage_quota(const char *app_id,
170                                         const data_usage_quota *quota)
171 {
172         /* support old behaviour undefined iftype mean all iftype */
173         time_t start_time = 0;
174         data_usage_quota quota_to_send;
175
176         if (!_is_valid_datausage_quota_params(app_id, quota))
177                 return RESOURCED_ERROR_INVALID_PARAMETER;
178
179         quota_to_send = *quota;
180         start_time = _get_datausage_start_time(quota->start_time);
181         quota_to_send.start_time = &start_time;
182
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("===============================");
192
193         return send_create_quota_message(app_id, &quota_to_send);
194 }