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 ifname = ? " \
30 " AND subscriber_id = ? AND roaming = ?"
32 /* SELECT statements */
33 #define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \
34 " iftype, rstn_type, roaming, ifname, subscriber_id, " \
35 " data_warn_limit, monthly_limit, weekly_limit, daily_limit, month_start_date, " \
36 " restriction_id FROM restrictions"
38 #define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, data_limit, " \
39 " iftype, rstn_type, roaming, ifname, subscriber_id, " \
40 " data_warn_limit, monthly_limit, weekly_limit, daily_limit, month_start_date, " \
42 " FROM restrictions INDEXED BY restrictions_index " \
45 #define SELECT_RESTRICTION_TYPE "SELECT rstn_type " \
46 " FROM restrictions INDEXED BY restrictions_index " \
47 " WHERE binpath = ? AND iftype = ?"
49 #define SELECT_RESTRICTION_TYPE_SUBSCRIBER_ID "SELECT rstn_type " \
50 " FROM restrictions INDEXED BY restrictions_index " \
51 " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?"
53 #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \
54 " WHERE binpath = ? AND iftype = ? AND subscriber_id = ? AND " \
55 " roaming = ? AND ifname = ?"
57 /* UPDATE statement */
58 #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \
59 " SET binpath = ?, data_limit = ?, iftype = ?, rstn_type = ?, " \
60 " roaming = ?, ifname = ?, subscriber_id = ?, data_warn_limit = ?, " \
61 " monthly_limit = ?, weekly_limit = ?, daily_limit = ? " \
62 " WHERE restriction_id = ?"
64 /* INSERT statement */
65 #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \
66 " (binpath, data_limit, iftype, rstn_type, " \
67 " roaming, ifname, subscriber_id, data_warn_limit, " \
68 " monthly_limit, weekly_limit, daily_limit, month_start_date) " \
69 " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
71 static void __finalize_delete(void);
73 #define PREPARE_DELETE(stm, query) do { \
74 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
75 if (rc != SQLITE_OK) { \
77 __finalize_delete(); \
78 STC_LOGE("Failed to prepare \"%s\"query" \
84 static void __finalize_select(void);
86 #define PREPARE_SELECT(stm, query) do { \
87 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
88 if (rc != SQLITE_OK) { \
90 __finalize_select(); \
91 STC_LOGE("Failed to prepare \"%s\"query" \
97 static void __finalize_update(void);
99 #define PREPARE_UPDATE(stm, query) do { \
100 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
101 if (rc != SQLITE_OK) { \
103 __finalize_update(); \
104 STC_LOGE("Failed to prepare \"%s\"query" \
110 static void __finalize_insert(void);
112 #define PREPARE_INSERT(stm, query) do { \
113 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
114 if (rc != SQLITE_OK) { \
116 __finalize_insert(); \
117 STC_LOGE("Failed to prepare \"%s\"query" \
123 #define FINALIZE(stm) do { \
125 sqlite3_finalize(stm); \
130 /* DELETE statements */
131 static sqlite3_stmt *delete_restrictions;
133 /* SELECT statements */
134 static sqlite3_stmt *select_restriction;
135 static sqlite3_stmt *select_restriction_per_app;
136 static sqlite3_stmt *select_restriction_type;
137 static sqlite3_stmt *select_restriction_type_subscriber_id;
138 static sqlite3_stmt *select_restriction_id;
140 /* REPLACE statements */
141 static sqlite3_stmt *update_net_restrictions;
143 /* INSERT statements */
144 static sqlite3_stmt *insert_net_restrictions;
146 static int __prepare_delete(sqlite3 *db)
148 __STC_LOG_FUNC_ENTER__;
150 static int initialized;
153 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
154 return SQLITE_OK; //LCOV_EXCL_LINE
157 PREPARE_DELETE(delete_restrictions, DELETE_RESTRICTIONS);
160 __STC_LOG_FUNC_EXIT__;
164 static void __finalize_delete(void)
166 __STC_LOG_FUNC_ENTER__;
168 FINALIZE(delete_restrictions);
170 __STC_LOG_FUNC_EXIT__;
173 static int __prepare_select(sqlite3 *db)
175 __STC_LOG_FUNC_ENTER__;
177 static int initialized;
180 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
181 return SQLITE_OK; //LCOV_EXCL_LINE
184 PREPARE_SELECT(select_restriction, SELECT_RESTRICTIONS);
185 PREPARE_SELECT(select_restriction_per_app, SELECT_RESTRICTIONS_PER_APP);
186 PREPARE_SELECT(select_restriction_type, SELECT_RESTRICTION_TYPE);
187 PREPARE_SELECT(select_restriction_type_subscriber_id, SELECT_RESTRICTION_TYPE_SUBSCRIBER_ID);
188 PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
191 __STC_LOG_FUNC_EXIT__;
195 static void __finalize_select(void)
197 __STC_LOG_FUNC_ENTER__;
199 FINALIZE(select_restriction);
200 FINALIZE(select_restriction_per_app);
201 FINALIZE(select_restriction_type);
202 FINALIZE(select_restriction_type_subscriber_id);
203 FINALIZE(select_restriction_id);
205 __STC_LOG_FUNC_EXIT__;
208 static int __prepare_replace(sqlite3 *db)
210 __STC_LOG_FUNC_ENTER__;
212 static int initialized;
215 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
216 return SQLITE_OK; //LCOV_EXCL_LINE
219 PREPARE_UPDATE(update_net_restrictions, UPDATE_NET_RESTRICTIONS);
222 __STC_LOG_FUNC_EXIT__;
226 static void __finalize_update(void)
228 __STC_LOG_FUNC_ENTER__;
230 FINALIZE(update_net_restrictions);
232 __STC_LOG_FUNC_EXIT__;
235 static int __prepare_insert(sqlite3 *db)
237 __STC_LOG_FUNC_ENTER__;
239 static int initialized;
242 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
243 return SQLITE_OK; //LCOV_EXCL_LINE
246 PREPARE_UPDATE(insert_net_restrictions, INSERT_NET_RESTRICTIONS);
249 __STC_LOG_FUNC_EXIT__;
253 static void __finalize_insert(void)
255 __STC_LOG_FUNC_ENTER__;
257 FINALIZE(insert_net_restrictions);
259 __STC_LOG_FUNC_EXIT__;
262 stc_error_e table_restrictions_per_app(const gchar* app_id,
263 const table_restrictions_info_cb restriction_cb,
266 __STC_LOG_FUNC_ENTER__;
267 table_restrictions_info data;
269 stc_error_e error_code = STC_ERROR_NONE;
270 sqlite3_stmt *stmt = select_restriction_per_app;
273 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
274 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
277 DB_ACTION(sqlite3_bind_text(stmt, 1, app_id, -1,
279 data.app_id = (char *)app_id;
282 rc = sqlite3_step(stmt);
284 memset(&data, 0, sizeof(data));
290 data.app_id = (char *)sqlite3_column_text(stmt, 0);
291 data.data_limit = sqlite3_column_int64(stmt, 1);
292 data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
294 (stc_rstn_type_e)sqlite3_column_int(stmt, 3);
295 data.roaming = sqlite3_column_int(stmt, 4);
296 data.ifname = (char *)sqlite3_column_text(stmt, 5);
297 data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
298 data.data_warn_limit = sqlite3_column_int64(stmt, 7);
299 data.monthly_limit = sqlite3_column_int64(stmt, 8);
300 data.weekly_limit = sqlite3_column_int64(stmt, 9);
301 data.daily_limit = sqlite3_column_int64(stmt, 10);
302 data.restriction_id = sqlite3_column_int64(stmt, 11);
304 if (restriction_cb(&data, user_data) == STC_CANCEL)
305 rc = SQLITE_DONE; //LCOV_EXCL_LINE
309 STC_LOGE("Failed to enumerate restrictions: %s\n", //LCOV_EXCL_LINE
310 sqlite3_errmsg(stc_db_get_database()));
312 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
313 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
315 } while (rc == SQLITE_ROW);
319 __STC_LOG_FUNC_EXIT__;
323 stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restriction_cb,
326 __STC_LOG_FUNC_ENTER__;
327 table_restrictions_info data;
329 stc_error_e error_code = STC_ERROR_NONE;
330 sqlite3_stmt *stmt = select_restriction;
333 rc = sqlite3_step(stmt);
335 memset(&data, 0, sizeof(data));
341 data.app_id = (char *)sqlite3_column_text(stmt, 0);
342 data.data_limit = sqlite3_column_int64(stmt, 1);
343 data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
345 (stc_rstn_type_e)sqlite3_column_int(stmt, 3);
346 data.roaming = sqlite3_column_int(stmt, 4);
347 data.ifname = (char *)sqlite3_column_text(stmt, 5);
348 data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
349 data.data_warn_limit = sqlite3_column_int64(stmt, 7);
350 data.monthly_limit = sqlite3_column_int64(stmt, 8);
351 data.weekly_limit = sqlite3_column_int64(stmt, 9);
352 data.daily_limit = sqlite3_column_int64(stmt, 10);
353 data.month_start_date = sqlite3_column_int(stmt, 11);
354 data.restriction_id = sqlite3_column_int64(stmt, 12);
356 if (restriction_cb(&data, user_data) == STC_CANCEL)
357 rc = SQLITE_DONE; //LCOV_EXCL_LINE
361 STC_LOGE("Failed to enumerate restrictions: %s\n", //LCOV_EXCL_LINE
362 sqlite3_errmsg(stc_db_get_database()));
364 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
365 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
367 } while (rc == SQLITE_ROW);
370 __STC_LOG_FUNC_EXIT__;
374 stc_error_e table_restrictions_get_restriction_type_subscriber_id(const char *app_id,
375 stc_iface_type_e iftype,
376 const char *subscriber_id,
377 stc_rstn_type_e *type)
379 __STC_LOG_FUNC_ENTER__;
380 int error_code = STC_ERROR_NONE;
382 bool state_subscriber_id = 0;
385 STC_LOGE("Please provide valid argument!"); //LCOV_EXCL_LINE
386 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
387 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
390 *type = STC_RSTN_TYPE_UNKNOWN;
391 sqlite3_reset(select_restriction_type_subscriber_id);
392 sqlite3_reset(select_restriction_type);
394 if (subscriber_id == NULL) {
395 state_subscriber_id = 0;
396 DB_ACTION(sqlite3_bind_text(select_restriction_type, 1,
397 app_id ? app_id : "", -1,
399 DB_ACTION(sqlite3_bind_int(select_restriction_type, 2,
401 ret = sqlite3_step(select_restriction_type);
403 state_subscriber_id = 1;
404 DB_ACTION(sqlite3_bind_text(select_restriction_type_subscriber_id, 1,
405 app_id ? app_id : "", -1,
407 DB_ACTION(sqlite3_bind_int(select_restriction_type_subscriber_id, 2,
409 DB_ACTION(sqlite3_bind_text(select_restriction_type_subscriber_id, 3,
410 subscriber_id, -1, SQLITE_STATIC));
411 ret = sqlite3_step(select_restriction_type_subscriber_id);
418 if (state_subscriber_id)
419 *type = (stc_rstn_type_e)sqlite3_column_int(select_restriction_type_subscriber_id, 0);
421 *type = (stc_rstn_type_e)sqlite3_column_int(select_restriction_type, 0);
425 STC_LOGE("Can't perform sql query: %s\n", //LCOV_EXCL_LINE
426 sqlite3_errmsg(stc_db_get_database()));
427 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
431 sqlite3_reset(select_restriction_type);
432 sqlite3_reset(select_restriction_type_subscriber_id);
436 stc_error_e table_restrictions_get_restriction_type(const char *app_id,
437 stc_iface_type_e iftype,
438 stc_rstn_type_e *type)
440 __STC_LOG_FUNC_ENTER__;
441 __STC_LOG_FUNC_EXIT__;
442 return table_restrictions_get_restriction_type_subscriber_id(app_id, iftype,
446 stc_error_e table_restrictions_delete(const char *app_id,
447 const stc_iface_type_e iftype,
449 const char *subscriber_id,
450 const stc_roaming_type_e roaming)
452 stc_error_e error_code = STC_ERROR_NONE;
453 sqlite3_stmt *stmt = delete_restrictions;
455 STC_LOGD("app_id[%s] iftype[%d] ifname[%s] subscriber_id[%s] roaming[%d]",
456 app_id, iftype, ifname, subscriber_id, roaming);
458 DB_ACTION(sqlite3_bind_text(stmt, 1, app_id ? app_id : "",
459 -1, SQLITE_TRANSIENT));
460 DB_ACTION(sqlite3_bind_int(stmt, 2, iftype));
461 DB_ACTION(sqlite3_bind_text(stmt, 3, ifname ? ifname : "",
462 -1, SQLITE_TRANSIENT));
463 DB_ACTION(sqlite3_bind_text(stmt, 4, subscriber_id ? subscriber_id : "",
464 -1, SQLITE_TRANSIENT));
465 DB_ACTION(sqlite3_bind_int(stmt, 5, roaming));
467 if (sqlite3_step(stmt) != SQLITE_DONE) {
468 STC_LOGE("Failed to remove restrictions by network interface %s\n", //LCOV_EXCL_LINE
469 sqlite3_errmsg(stc_db_get_database()));
470 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
471 goto handle_error; //LCOV_EXCL_LINE
474 STC_LOGD("Restriction deleted for app_id [%s]", app_id);
482 stc_error_e __get_restriction_id(table_restrictions_info *info)
484 __STC_LOG_FUNC_ENTER__;
486 stc_error_e error_code = STC_ERROR_NONE;
487 sqlite3_stmt *stmt = select_restriction_id;
489 DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
490 -1, SQLITE_TRANSIENT));
491 DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype));
492 DB_ACTION(sqlite3_bind_text(stmt, 3, info->subscriber_id ? info->subscriber_id : "",
493 -1, SQLITE_TRANSIENT));
494 DB_ACTION(sqlite3_bind_int(stmt, 4, info->roaming));
495 DB_ACTION(sqlite3_bind_text(stmt, 5, info->ifname ? info->ifname : "",
496 -1, SQLITE_TRANSIENT));
498 rc = sqlite3_step(stmt);
504 info->restriction_id = sqlite3_column_int64(stmt, 0);
505 STC_LOGD("restriction id [%llu]", info->restriction_id);
509 STC_LOGE("Failed to get restriction id: %s\n", //LCOV_EXCL_LINE
510 sqlite3_errmsg(stc_db_get_database()));
515 __STC_LOG_FUNC_EXIT__;
519 stc_error_e table_restrictions_update(table_restrictions_info *info)
521 stc_error_e error_code = STC_ERROR_NONE;
522 sqlite3_stmt *stmt = insert_net_restrictions;
525 error_code = STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
526 goto handle_error; //LCOV_EXCL_LINE
529 __get_restriction_id(info);
530 if (info->restriction_id)
531 stmt = update_net_restrictions;
533 DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
534 -1, SQLITE_TRANSIENT));
535 DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_limit));
536 DB_ACTION(sqlite3_bind_int(stmt, 3, info->iftype));
537 DB_ACTION(sqlite3_bind_int(stmt, 4, info->rstn_type));
538 DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming));
539 DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "",
540 -1, SQLITE_TRANSIENT));
541 DB_ACTION(sqlite3_bind_text(stmt, 7, info->subscriber_id ? info->subscriber_id : "",
542 -1, SQLITE_TRANSIENT));
543 DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit));
544 DB_ACTION(sqlite3_bind_int64(stmt, 9, info->monthly_limit));
545 DB_ACTION(sqlite3_bind_int64(stmt, 10, info->weekly_limit));
546 DB_ACTION(sqlite3_bind_int64(stmt, 11, info->daily_limit));
548 if (info->restriction_id)
549 DB_ACTION(sqlite3_bind_int64(stmt, 12, info->restriction_id));
551 DB_ACTION(sqlite3_bind_int64(stmt, 12, info->month_start_date));
553 if (sqlite3_step(stmt) != SQLITE_DONE) {
554 STC_LOGE("Failed to set network restriction: %s\n", //LCOV_EXCL_LINE
555 sqlite3_errmsg(stc_db_get_database()));
556 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
557 goto handle_error; //LCOV_EXCL_LINE
560 if (info->restriction_id) {
561 STC_LOGD("Restriction updated app_id [%s]", info->app_id);
563 STC_LOGD("Restriction inserted app_id [%s]", info->app_id);
564 __get_restriction_id(info);
572 stc_error_e table_restrictions_prepare(sqlite3 *db)
574 __STC_LOG_FUNC_ENTER__;
576 stc_error_e error_code = STC_ERROR_NONE;
579 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
580 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
583 DB_ACTION(__prepare_delete(db));
584 DB_ACTION(__prepare_select(db));
585 DB_ACTION(__prepare_replace(db));
586 DB_ACTION(__prepare_insert(db));
590 __STC_LOG_FUNC_EXIT__;
594 void table_restrictions_finalize(void)
596 __STC_LOG_FUNC_ENTER__;
601 __STC_LOG_FUNC_EXIT__;