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.
23 * @desc Network statistics reset implementation. This function's clearing
26 * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
34 #include "data_usage.h"
35 #include "datausage-reset.h"
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 ?"
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)
51 static sqlite3_stmt *reset_stms[4];
53 #define PREPARE(stm, query) do { \
54 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
55 if (rc != SQLITE_OK) { \
57 finalize_datausage_reset(); \
58 _E("Failed to prepare statement for\"%s\"query" \
64 static int init_datausage_reset(sqlite3 *db)
67 static int initialized;
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);
81 #define FINALIZE(stm) do { \
83 sqlite3_finalize(stm); \
89 void finalize_datausage_reset(void)
92 for (i = 0; i < sizeof(reset_stms) / sizeof(*reset_stms); i++)
93 FINALIZE(reset_stms[i]);
96 API resourced_ret_c reset_data_usage(const data_usage_reset_rule *rule)
99 resourced_ret_c result = RESOURCED_ERROR_NONE;
100 int pos = 1;/* running through positions where to
101 bind parameters in the query */
103 if (!rule || !rule->interval)
104 return RESOURCED_ERROR_INVALID_PARAMETER;
106 libresourced_db_initialize_once();
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;
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)];
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;
124 if (rule->iftype != RESOURCED_IFACE_LAST_ELEM &&
125 sqlite3_bind_int(stm, pos++, rule->iftype) != SQLITE_OK) {
126 result = RESOURCED_ERROR_DB_FAILED;
130 if (sqlite3_bind_int64(stm, pos++, rule->interval->from) != SQLITE_OK) {
131 result = RESOURCED_ERROR_DB_FAILED;
134 if (sqlite3_bind_int64(stm, pos++, rule->interval->to) != SQLITE_OK) {
135 result = RESOURCED_ERROR_DB_FAILED;
139 if (sqlite3_step(stm) != SQLITE_DONE) {
140 _D("Failed to drop collected statistics.");
141 result = RESOURCED_ERROR_DB_FAILED;