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);
318 rc = sqlite3_reset(stmt);
320 error_code = STC_ERROR_DB_FAILED;
322 __STC_LOG_FUNC_EXIT__;
326 API stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restriction_cb,
329 __STC_LOG_FUNC_ENTER__;
330 table_restrictions_info data;
332 stc_error_e error_code = STC_ERROR_NONE;
333 sqlite3_stmt *stmt = select_restriction;
336 rc = sqlite3_step(stmt);
338 memset(&data, 0, sizeof(data));
344 data.app_id = (char *)sqlite3_column_text(stmt, 0);
345 data.data_limit = sqlite3_column_int64(stmt, 1);
346 data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
348 (stc_rstn_type_e)sqlite3_column_int(stmt, 3);
349 data.roaming = sqlite3_column_int(stmt, 4);
350 data.ifname = (char *)sqlite3_column_text(stmt, 5);
351 data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
352 data.data_warn_limit = sqlite3_column_int64(stmt, 7);
353 data.monthly_limit = sqlite3_column_int64(stmt, 8);
354 data.weekly_limit = sqlite3_column_int64(stmt, 9);
355 data.daily_limit = sqlite3_column_int64(stmt, 10);
356 data.month_start_date = sqlite3_column_int(stmt, 11);
357 data.restriction_id = sqlite3_column_int64(stmt, 12);
359 if (restriction_cb(&data, user_data) == STC_CANCEL)
360 rc = SQLITE_DONE; //LCOV_EXCL_LINE
364 STC_LOGE("Failed to enumerate restrictions: %s\n", //LCOV_EXCL_LINE
365 sqlite3_errmsg(stc_db_get_database()));
367 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
368 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
370 } while (rc == SQLITE_ROW);
372 rc = sqlite3_reset(stmt);
374 error_code = STC_ERROR_DB_FAILED;
376 __STC_LOG_FUNC_EXIT__;
380 stc_error_e table_restrictions_get_restriction_type_subscriber_id(const char *app_id,
381 stc_iface_type_e iftype,
382 const char *subscriber_id,
383 stc_rstn_type_e *type)
385 __STC_LOG_FUNC_ENTER__;
386 int error_code = STC_ERROR_NONE;
388 bool state_subscriber_id = 0;
391 STC_LOGE("Please provide valid argument!"); //LCOV_EXCL_LINE
392 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
393 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
396 *type = STC_RSTN_TYPE_UNKNOWN;
397 ret = sqlite3_reset(select_restriction_type_subscriber_id);
398 if (ret != SQLITE_OK) {
399 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
400 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
403 ret = sqlite3_reset(select_restriction_type);
404 if (ret != SQLITE_OK) {
405 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
406 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
409 if (subscriber_id == NULL) {
410 state_subscriber_id = 0;
411 DB_ACTION(sqlite3_bind_text(select_restriction_type, 1,
412 app_id ? app_id : "", -1,
414 DB_ACTION(sqlite3_bind_int(select_restriction_type, 2,
416 ret = sqlite3_step(select_restriction_type);
418 state_subscriber_id = 1;
419 DB_ACTION(sqlite3_bind_text(select_restriction_type_subscriber_id, 1,
420 app_id ? app_id : "", -1,
422 DB_ACTION(sqlite3_bind_int(select_restriction_type_subscriber_id, 2,
424 DB_ACTION(sqlite3_bind_text(select_restriction_type_subscriber_id, 3,
425 subscriber_id, -1, SQLITE_STATIC));
426 ret = sqlite3_step(select_restriction_type_subscriber_id);
433 if (state_subscriber_id)
434 *type = (stc_rstn_type_e)sqlite3_column_int(select_restriction_type_subscriber_id, 0);
436 *type = (stc_rstn_type_e)sqlite3_column_int(select_restriction_type, 0);
440 STC_LOGE("Can't perform sql query: %s\n", //LCOV_EXCL_LINE
441 sqlite3_errmsg(stc_db_get_database()));
442 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
446 ret = sqlite3_reset(select_restriction_type);
447 if (ret != SQLITE_OK)
448 error_code = STC_ERROR_DB_FAILED;
450 ret = sqlite3_reset(select_restriction_type_subscriber_id);
451 if (ret != SQLITE_OK)
452 error_code = STC_ERROR_DB_FAILED;
457 stc_error_e table_restrictions_get_restriction_type(const char *app_id,
458 stc_iface_type_e iftype,
459 stc_rstn_type_e *type)
461 __STC_LOG_FUNC_ENTER__;
462 __STC_LOG_FUNC_EXIT__;
463 return table_restrictions_get_restriction_type_subscriber_id(app_id, iftype,
467 stc_error_e table_restrictions_delete(const char *app_id,
468 const stc_iface_type_e iftype,
470 const char *subscriber_id,
471 const stc_roaming_type_e roaming)
473 stc_error_e error_code = STC_ERROR_NONE;
474 sqlite3_stmt *stmt = delete_restrictions;
476 STC_LOGD("app_id[%s] iftype[%d] ifname[%s] subscriber_id[%s] roaming[%d]",
477 app_id, iftype, ifname, subscriber_id, roaming);
479 DB_ACTION(sqlite3_bind_text(stmt, 1, app_id ? app_id : "",
480 -1, SQLITE_TRANSIENT));
481 DB_ACTION(sqlite3_bind_int(stmt, 2, iftype));
482 DB_ACTION(sqlite3_bind_text(stmt, 3, ifname ? ifname : "",
483 -1, SQLITE_TRANSIENT));
484 DB_ACTION(sqlite3_bind_text(stmt, 4, subscriber_id ? subscriber_id : "",
485 -1, SQLITE_TRANSIENT));
486 DB_ACTION(sqlite3_bind_int(stmt, 5, roaming));
488 if (sqlite3_step(stmt) != SQLITE_DONE) {
489 STC_LOGE("Failed to remove restrictions by network interface %s\n", //LCOV_EXCL_LINE
490 sqlite3_errmsg(stc_db_get_database()));
491 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
492 goto handle_error; //LCOV_EXCL_LINE
495 STC_LOGD("Restriction deleted for app_id [%s]", app_id);
499 if (sqlite3_reset(stmt) != SQLITE_OK)
500 error_code = STC_ERROR_DB_FAILED;
505 stc_error_e __get_restriction_id(table_restrictions_info *info)
507 __STC_LOG_FUNC_ENTER__;
509 stc_error_e error_code = STC_ERROR_NONE;
510 sqlite3_stmt *stmt = select_restriction_id;
512 DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
513 -1, SQLITE_TRANSIENT));
514 DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype));
515 DB_ACTION(sqlite3_bind_text(stmt, 3, info->subscriber_id ? info->subscriber_id : "",
516 -1, SQLITE_TRANSIENT));
517 DB_ACTION(sqlite3_bind_int(stmt, 4, info->roaming));
518 DB_ACTION(sqlite3_bind_text(stmt, 5, info->ifname ? info->ifname : "",
519 -1, SQLITE_TRANSIENT));
521 rc = sqlite3_step(stmt);
527 info->restriction_id = sqlite3_column_int64(stmt, 0);
528 STC_LOGD("restriction id [%llu]", info->restriction_id);
532 STC_LOGE("Failed to get restriction id: %s\n", //LCOV_EXCL_LINE
533 sqlite3_errmsg(stc_db_get_database()));
534 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
538 rc = sqlite3_reset(stmt);
540 error_code = STC_ERROR_DB_FAILED;
542 __STC_LOG_FUNC_EXIT__;
546 stc_error_e table_restrictions_update(table_restrictions_info *info)
548 stc_error_e error_code = STC_ERROR_NONE;
549 sqlite3_stmt *stmt = insert_net_restrictions;
552 error_code = STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
553 goto handle_error; //LCOV_EXCL_LINE
556 if (__get_restriction_id(info) != STC_ERROR_NONE) {
557 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
558 goto handle_error; //LCOV_EXCL_LINE
561 if (info->restriction_id)
562 stmt = update_net_restrictions;
564 DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
565 -1, SQLITE_TRANSIENT));
566 DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_limit));
567 DB_ACTION(sqlite3_bind_int(stmt, 3, info->iftype));
568 DB_ACTION(sqlite3_bind_int(stmt, 4, info->rstn_type));
569 DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming));
570 DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "",
571 -1, SQLITE_TRANSIENT));
572 DB_ACTION(sqlite3_bind_text(stmt, 7, info->subscriber_id ? info->subscriber_id : "",
573 -1, SQLITE_TRANSIENT));
574 DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit));
575 DB_ACTION(sqlite3_bind_int64(stmt, 9, info->monthly_limit));
576 DB_ACTION(sqlite3_bind_int64(stmt, 10, info->weekly_limit));
577 DB_ACTION(sqlite3_bind_int64(stmt, 11, info->daily_limit));
579 if (info->restriction_id)
580 DB_ACTION(sqlite3_bind_int64(stmt, 12, info->restriction_id));
582 DB_ACTION(sqlite3_bind_int64(stmt, 12, info->month_start_date));
584 if (sqlite3_step(stmt) != SQLITE_DONE) {
585 STC_LOGE("Failed to set network restriction: %s\n", //LCOV_EXCL_LINE
586 sqlite3_errmsg(stc_db_get_database()));
587 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
588 goto handle_error; //LCOV_EXCL_LINE
591 if (info->restriction_id) {
592 STC_LOGD("Restriction updated app_id [%s]", info->app_id);
594 STC_LOGD("Restriction inserted app_id [%s]", info->app_id);
595 if (__get_restriction_id(info) != STC_ERROR_NONE)
596 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
600 if (sqlite3_reset(stmt) != SQLITE_OK)
601 error_code = STC_ERROR_DB_FAILED;
606 stc_error_e table_restrictions_prepare(sqlite3 *db)
608 __STC_LOG_FUNC_ENTER__;
610 stc_error_e error_code = STC_ERROR_NONE;
613 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
614 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
617 DB_ACTION(__prepare_delete(db));
618 DB_ACTION(__prepare_select(db));
619 DB_ACTION(__prepare_replace(db));
620 DB_ACTION(__prepare_insert(db));
624 __STC_LOG_FUNC_EXIT__;
628 void table_restrictions_finalize(void)
630 __STC_LOG_FUNC_ENTER__;
635 __STC_LOG_FUNC_EXIT__;