tizen 2.3 release
[kernel/api/system-resource.git] / src / network / reset.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  * @file reset.c
22  *
23  * @desc Network statistics reset implementation. This function's clearing
24  * datausage database.
25  *
26  * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
27  *
28  */
29
30 #include <sqlite3.h>
31 #include <string.h>
32
33 #include "database.h"
34 #include "data_usage.h"
35 #include "datausage-reset.h"
36 #include "macro.h"
37 #include "trace.h"
38
39 #define RESET_ALL "delete from statistics where time_stamp between ? and ?"
40 #define RESET_APP "delete from statistics where binpath=? and " \
41         "time_stamp between ? and ? "
42 #define RESET_IFACE "delete from statistics where iftype=? and " \
43         "time_stamp between ? and ?"
44 #define RESET_APP_IFACE "delete from statistics where binpath=? and " \
45         "iftype=? and time_stamp between ? and ?"
46
47 /* the following array is strictly ordered
48  * to find required statement the following code will be used:
49  * (app ? 1 : 0) | (iftype ? 2 : 0)
50  */
51 static sqlite3_stmt *reset_stms[4];
52
53 #define PREPARE(stm, query) do {                                \
54         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL);     \
55         if (rc != SQLITE_OK) {                                  \
56                 stm = NULL;                                     \
57                 finalize_datausage_reset();                     \
58                 _E("Failed to prepare statement for\"%s\"query" \
59                         , query);                               \
60                 return rc;                                      \
61         }                                                       \
62 } while (0)
63
64 static int init_datausage_reset(sqlite3 *db)
65 {
66         int rc;
67         static int initialized;
68
69         if (initialized)
70                 return SQLITE_OK;
71
72         PREPARE(reset_stms[0], RESET_ALL);
73         PREPARE(reset_stms[1], RESET_APP);
74         PREPARE(reset_stms[2], RESET_IFACE);
75         PREPARE(reset_stms[3], RESET_APP_IFACE);
76
77         initialized = 1;
78         return rc;
79 }
80
81 #define FINALIZE(stm) do {              \
82         if (stm) {                      \
83                 sqlite3_finalize(stm);  \
84                 stm = NULL;             \
85         }                               \
86 } while (0)
87
88
89 void finalize_datausage_reset(void)
90 {
91         int i;
92         for (i = 0; i < sizeof(reset_stms) / sizeof(*reset_stms); i++)
93                 FINALIZE(reset_stms[i]);
94 }
95
96 API resourced_ret_c reset_data_usage(const data_usage_reset_rule *rule)
97 {
98         sqlite3_stmt *stm;
99         resourced_ret_c result = RESOURCED_ERROR_NONE;
100         int pos = 1;/* running through positions where to
101                 bind parameters in the query */
102
103         if (!rule || !rule->interval)
104                 return RESOURCED_ERROR_INVALID_PARAMETER;
105
106         libresourced_db_initialize_once();
107
108         if (init_datausage_reset(resourced_get_database()) != SQLITE_OK) {
109                 _D("Failed to initialize data usage reset statements: %s\n",
110                    sqlite3_errmsg(resourced_get_database()));
111                 return RESOURCED_ERROR_DB_FAILED;
112         }
113         /* pick a statement depending on parameters.
114                 See comment for reset_stms */
115         stm = reset_stms[(rule->app_id ? 1 : 0) |
116                 (rule->iftype != RESOURCED_IFACE_LAST_ELEM ? 2 : 0)];
117
118         if (rule->app_id && sqlite3_bind_text(stm, pos++, rule->app_id, -1,
119                         SQLITE_TRANSIENT) != SQLITE_OK) {
120                 result = RESOURCED_ERROR_DB_FAILED;
121                 goto out;
122         }
123
124         if (rule->iftype != RESOURCED_IFACE_LAST_ELEM &&
125                 sqlite3_bind_int(stm, pos++, rule->iftype) != SQLITE_OK) {
126                 result = RESOURCED_ERROR_DB_FAILED;
127                 goto out;
128         }
129
130         if (sqlite3_bind_int64(stm, pos++, rule->interval->from) != SQLITE_OK) {
131                 result = RESOURCED_ERROR_DB_FAILED;
132                 goto out;
133         }
134         if (sqlite3_bind_int64(stm, pos++, rule->interval->to) != SQLITE_OK) {
135                 result = RESOURCED_ERROR_DB_FAILED;
136                 goto out;
137         }
138
139         if (sqlite3_step(stm) != SQLITE_DONE) {
140                 _D("Failed to drop collected statistics.");
141                 result = RESOURCED_ERROR_DB_FAILED;
142         }
143
144 out:
145         sqlite3_reset(stm);
146         return result;
147 }
148