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 * This file implements restrictions entity handler methods.
20 * @file table-restrictions.c
24 #include "db-internal.h"
25 #include "table-restrictions.h"
27 /* DELETE statements */
28 #define DELETE_RESTRICTIONS "DELETE FROM restrictions " \
29 " WHERE binpath = ? AND iftype = ? AND imsi = ?"
31 /* SELECT statements */
32 #define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \
33 " iftype, rst_state, roaming, ifname, imsi, " \
34 " data_warn_limit, restriction_id FROM restrictions"
36 #define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, data_limit, " \
37 " iftype, rst_state, roaming, ifname, imsi, " \
38 " data_warn_limit, restriction_id " \
39 " FROM restrictions INDEXED BY restrictions_index " \
42 #define SELECT_RESTRICTION_STATE "SELECT rst_state " \
43 " FROM restrictions INDEXED BY restrictions_index " \
44 " WHERE binpath = ? AND iftype = ?"
46 #define SELECT_RESTRICTION_STATE_IMSI "SELECT rst_state " \
47 " FROM restrictions INDEXED BY restrictions_index " \
48 " WHERE binpath = ? AND iftype = ? AND imsi = ?"
50 #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \
51 " WHERE binpath = ? AND iftype = ? AND imsi = ? AND " \
52 " data_limit = ? AND data_warn_limit = ? AND " \
53 " rst_state = ? AND roaming = ? AND ifname = ?"
55 /* UPDATE statement */
56 #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \
57 " SET binpath = ?, data_limit = ?, iftype = ?, rst_state = ?, " \
58 " roaming = ?, ifname = ?, imsi = ?, data_warn_limit = ? " \
59 " WHERE restriction_id = ?"
61 /* INSERT statement */
62 #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \
63 " (binpath, data_limit, iftype, rst_state, " \
64 " roaming, ifname, imsi, data_warn_limit) " \
65 " VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
67 static void __finalize_delete(void);
69 #define PREPARE_DELETE(stm, query) do { \
70 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
71 if (rc != SQLITE_OK) { \
73 __finalize_delete(); \
74 STC_LOGE("Failed to prepare \"%s\"query" \
80 static void __finalize_select(void);
82 #define PREPARE_SELECT(stm, query) do { \
83 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
84 if (rc != SQLITE_OK) { \
86 __finalize_select(); \
87 STC_LOGE("Failed to prepare \"%s\"query" \
93 static void __finalize_update(void);
95 #define PREPARE_UPDATE(stm, query) do { \
96 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
97 if (rc != SQLITE_OK) { \
99 __finalize_update(); \
100 STC_LOGE("Failed to prepare \"%s\"query" \
106 static void __finalize_insert(void);
108 #define PREPARE_INSERT(stm, query) do { \
109 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
110 if (rc != SQLITE_OK) { \
112 __finalize_insert(); \
113 STC_LOGE("Failed to prepare \"%s\"query" \
119 #define FINALIZE(stm) do { \
121 sqlite3_finalize(stm); \
126 /* DELETE statements */
127 static sqlite3_stmt *delete_restrictions;
129 /* SELECT statements */
130 static sqlite3_stmt *select_restriction;
131 static sqlite3_stmt *select_restriction_per_app;
132 static sqlite3_stmt *select_restriction_state;
133 static sqlite3_stmt *select_restriction_state_imsi;
134 static sqlite3_stmt *select_restriction_id;
136 /* REPLACE statements */
137 static sqlite3_stmt *update_net_restrictions;
139 /* INSERT statements */
140 static sqlite3_stmt *insert_net_restrictions;
142 static int __prepare_delete(sqlite3 *db)
144 __STC_LOG_FUNC_ENTER__;
146 static int initialized;
149 __STC_LOG_FUNC_EXIT__;
153 PREPARE_DELETE(delete_restrictions, DELETE_RESTRICTIONS);
156 __STC_LOG_FUNC_EXIT__;
160 static void __finalize_delete(void)
162 __STC_LOG_FUNC_ENTER__;
164 FINALIZE(delete_restrictions);
166 __STC_LOG_FUNC_EXIT__;
169 static int __prepare_select(sqlite3 *db)
171 __STC_LOG_FUNC_ENTER__;
173 static int initialized;
176 __STC_LOG_FUNC_EXIT__;
180 PREPARE_SELECT(select_restriction, SELECT_RESTRICTIONS);
181 PREPARE_SELECT(select_restriction_per_app, SELECT_RESTRICTIONS_PER_APP);
182 PREPARE_SELECT(select_restriction_state, SELECT_RESTRICTION_STATE);
183 PREPARE_SELECT(select_restriction_state_imsi, SELECT_RESTRICTION_STATE_IMSI);
184 PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
187 __STC_LOG_FUNC_EXIT__;
191 static void __finalize_select(void)
193 __STC_LOG_FUNC_ENTER__;
195 FINALIZE(select_restriction);
196 FINALIZE(select_restriction_per_app);
197 FINALIZE(select_restriction_state);
198 FINALIZE(select_restriction_state_imsi);
199 FINALIZE(select_restriction_id);
201 __STC_LOG_FUNC_EXIT__;
204 static int __prepare_replace(sqlite3 *db)
206 __STC_LOG_FUNC_ENTER__;
208 static int initialized;
211 __STC_LOG_FUNC_EXIT__;
215 PREPARE_UPDATE(update_net_restrictions, UPDATE_NET_RESTRICTIONS);
218 __STC_LOG_FUNC_EXIT__;
222 static void __finalize_update(void)
224 __STC_LOG_FUNC_ENTER__;
226 FINALIZE(update_net_restrictions);
228 __STC_LOG_FUNC_EXIT__;
231 static int __prepare_insert(sqlite3 *db)
233 __STC_LOG_FUNC_ENTER__;
235 static int initialized;
238 __STC_LOG_FUNC_EXIT__;
242 PREPARE_UPDATE(insert_net_restrictions, INSERT_NET_RESTRICTIONS);
245 __STC_LOG_FUNC_EXIT__;
249 static void __finalize_insert(void)
251 __STC_LOG_FUNC_ENTER__;
253 FINALIZE(insert_net_restrictions);
255 __STC_LOG_FUNC_EXIT__;
258 stc_error_e table_restrictions_per_app(const gchar* app_id,
259 const table_restrictions_info_cb restriction_cb,
262 __STC_LOG_FUNC_ENTER__;
263 table_restrictions_info data;
265 stc_error_e error_code = STC_ERROR_NONE;
266 sqlite3_stmt *stmt = select_restriction_per_app;
269 __STC_LOG_FUNC_EXIT__;
270 return STC_ERROR_DB_FAILED;
273 DB_ACTION(sqlite3_bind_text(stmt, 1, app_id, -1,
275 data.app_id = (char *)app_id;
278 rc = sqlite3_step(stmt);
280 memset(&data, 0, sizeof(data));
286 data.app_id = (char *)sqlite3_column_text(stmt, 0);
287 data.data_limit = sqlite3_column_int64(stmt, 1);
288 data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
290 (stc_restriction_state_e)sqlite3_column_int(stmt, 3);
291 data.roaming = sqlite3_column_int(stmt, 4);
292 data.ifname = (char *)sqlite3_column_text(stmt, 5);
293 data.imsi = (char *)sqlite3_column_text(stmt, 6);
294 data.data_warn_limit = sqlite3_column_int64(stmt, 7);
295 data.restriction_id = sqlite3_column_int64(stmt, 8);
297 if (restriction_cb(&data, user_data) == STC_CANCEL)
302 STC_LOGE("Failed to enumerate restrictions: %s\n",
303 sqlite3_errmsg(stc_db_get_database()));
305 __STC_LOG_FUNC_EXIT__;
306 error_code = STC_ERROR_DB_FAILED;
308 } while (rc == SQLITE_ROW);
312 __STC_LOG_FUNC_EXIT__;
316 stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restriction_cb,
319 __STC_LOG_FUNC_ENTER__;
320 table_restrictions_info data;
322 stc_error_e error_code = STC_ERROR_NONE;
323 sqlite3_stmt *stmt = select_restriction;
326 rc = sqlite3_step(stmt);
328 memset(&data, 0, sizeof(data));
334 data.app_id = (char *)sqlite3_column_text(stmt, 0);
335 data.data_limit = sqlite3_column_int64(stmt, 1);
336 data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
338 (stc_restriction_state_e)sqlite3_column_int(stmt, 3);
339 data.roaming = sqlite3_column_int(stmt, 4);
340 data.ifname = (char *)sqlite3_column_text(stmt, 5);
341 data.imsi = (char *)sqlite3_column_text(stmt, 6);
342 data.data_warn_limit = sqlite3_column_int64(stmt, 7);
343 data.restriction_id = sqlite3_column_int64(stmt, 8);
345 if (restriction_cb(&data, user_data) == STC_CANCEL)
350 STC_LOGE("Failed to enumerate restrictions: %s\n",
351 sqlite3_errmsg(stc_db_get_database()));
353 __STC_LOG_FUNC_EXIT__;
354 error_code = STC_ERROR_DB_FAILED;
356 } while (rc == SQLITE_ROW);
359 __STC_LOG_FUNC_EXIT__;
363 stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id,
364 stc_iface_type_e iftype,
365 const char *imsi_hash,
366 stc_restriction_state_e *state)
368 __STC_LOG_FUNC_ENTER__;
369 int error_code = STC_ERROR_NONE;
374 STC_LOGE("Please provide valid argument!");
375 __STC_LOG_FUNC_EXIT__;
376 return STC_ERROR_INVALID_PARAMETER;
379 *state = STC_RESTRICTION_UNKNOWN;
380 sqlite3_reset(select_restriction_state_imsi);
381 sqlite3_reset(select_restriction_state);
383 if (imsi_hash == NULL) {
385 DB_ACTION(sqlite3_bind_text(select_restriction_state, 1,
386 app_id ? app_id : "", -1,
388 DB_ACTION(sqlite3_bind_int(select_restriction_state, 2,
390 ret = sqlite3_step(select_restriction_state);
393 DB_ACTION(sqlite3_bind_text(select_restriction_state_imsi, 1,
394 app_id ? app_id : "", -1,
396 DB_ACTION(sqlite3_bind_int(select_restriction_state_imsi, 2,
398 DB_ACTION(sqlite3_bind_text(select_restriction_state_imsi, 3,
399 imsi_hash, -1, SQLITE_STATIC));
400 ret = sqlite3_step(select_restriction_state_imsi);
408 *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state_imsi, 0);
410 *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state, 0);
414 STC_LOGE("Can't perform sql query: %s \n%s",
415 SELECT_RESTRICTION_STATE,
416 sqlite3_errmsg(stc_db_get_database()));
417 error_code = STC_ERROR_DB_FAILED;
421 sqlite3_reset(select_restriction_state);
422 sqlite3_reset(select_restriction_state_imsi);
426 stc_error_e table_restrictions_get_restriction_state(const char *app_id,
427 stc_iface_type_e iftype,
428 stc_restriction_state_e *state)
430 __STC_LOG_FUNC_ENTER__;
431 __STC_LOG_FUNC_EXIT__;
432 return table_restrictions_get_restriction_state_imsi(app_id, iftype,
436 stc_error_e table_restrictions_delete(const char *app_id,
437 const stc_iface_type_e iftype,
440 stc_error_e error_code = STC_ERROR_NONE;
441 sqlite3_stmt *stmt = delete_restrictions;
443 STC_LOGD("app_id [%s], iftype [%d], imsi [%s]",
444 app_id, iftype, imsi);
446 DB_ACTION(sqlite3_bind_text(stmt, 1, app_id ? app_id : "",
447 -1, SQLITE_TRANSIENT));
448 DB_ACTION(sqlite3_bind_int(stmt, 2, iftype));
449 DB_ACTION(sqlite3_bind_text(stmt, 3, imsi ? imsi : "", -1,
452 if (sqlite3_step(stmt) != SQLITE_DONE) {
453 STC_LOGE("Failed to remove restrictions by network interface %s\n",
454 sqlite3_errmsg(stc_db_get_database()));
455 error_code = STC_ERROR_DB_FAILED;
459 STC_LOGD("Restriction deleted for app_id [%s]", app_id);
467 stc_error_e __get_restriction_id(table_restrictions_info *info)
469 __STC_LOG_FUNC_ENTER__;
471 stc_error_e error_code = STC_ERROR_NONE;
472 sqlite3_stmt *stmt = select_restriction_id;
474 DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
475 -1, SQLITE_TRANSIENT));
476 DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype));
477 DB_ACTION(sqlite3_bind_text(stmt, 3, info->imsi ? info->imsi : "",
478 -1, SQLITE_TRANSIENT));
479 DB_ACTION(sqlite3_bind_int64(stmt, 4, info->data_limit));
480 DB_ACTION(sqlite3_bind_int64(stmt, 5, info->data_warn_limit));
481 DB_ACTION(sqlite3_bind_int(stmt, 6, info->rst_state));
482 DB_ACTION(sqlite3_bind_int(stmt, 7, info->roaming));
483 DB_ACTION(sqlite3_bind_text(stmt, 8, info->ifname ? info->ifname : "",
484 -1, SQLITE_TRANSIENT));
486 rc = sqlite3_step(stmt);
492 info->restriction_id = sqlite3_column_int64(stmt, 0);
493 STC_LOGD("restriction id [%llu]", info->restriction_id);
497 STC_LOGE("Failed to get restriction id: %s\n",
498 sqlite3_errmsg(stc_db_get_database()));
503 __STC_LOG_FUNC_EXIT__;
507 stc_error_e table_restrictions_update(table_restrictions_info *info)
509 stc_error_e error_code = STC_ERROR_NONE;
510 sqlite3_stmt *stmt = insert_net_restrictions;
513 error_code = STC_ERROR_INVALID_PARAMETER;
517 if (info->restriction_id)
518 stmt = update_net_restrictions;
520 DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
521 -1, SQLITE_TRANSIENT));
522 DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_limit));
523 DB_ACTION(sqlite3_bind_int(stmt, 3, info->iftype));
524 DB_ACTION(sqlite3_bind_int(stmt, 4, info->rst_state));
525 DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming));
526 DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "",
527 -1, SQLITE_TRANSIENT));
528 DB_ACTION(sqlite3_bind_text(stmt, 7, info->imsi ? info->imsi : "",
529 -1, SQLITE_TRANSIENT));
530 DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit));
532 if (info->restriction_id)
533 DB_ACTION(sqlite3_bind_int64(stmt, 11, info->restriction_id));
535 if (sqlite3_step(stmt) != SQLITE_DONE) {
536 STC_LOGE("Failed to set network restriction: %s\n",
537 sqlite3_errmsg(stc_db_get_database()));
538 error_code = STC_ERROR_DB_FAILED;
542 if (info->restriction_id) {
543 STC_LOGD("Restriction updated app_id [%s]", info->app_id);
545 STC_LOGD("Restriction inserted app_id [%s]", info->app_id);
546 __get_restriction_id(info);
554 stc_error_e table_restrictions_prepare(sqlite3 *db)
556 __STC_LOG_FUNC_ENTER__;
558 stc_error_e error_code = STC_ERROR_NONE;
561 __STC_LOG_FUNC_EXIT__;
562 return STC_ERROR_FAIL;
565 DB_ACTION(__prepare_delete(db));
566 DB_ACTION(__prepare_select(db));
567 DB_ACTION(__prepare_replace(db));
568 DB_ACTION(__prepare_insert(db));
572 __STC_LOG_FUNC_EXIT__;
576 void table_restrictions_finalize(void)
578 __STC_LOG_FUNC_ENTER__;
583 __STC_LOG_FUNC_EXIT__;