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 subscriber_id = ?"
31 /* SELECT statements */
32 #define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \
33 " iftype, rstn_type, roaming, ifname, subscriber_id, " \
34 " data_warn_limit, restriction_id FROM restrictions"
36 #define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, data_limit, " \
37 " iftype, rstn_type, roaming, ifname, subscriber_id, " \
38 " data_warn_limit, restriction_id " \
39 " FROM restrictions INDEXED BY restrictions_index " \
42 #define SELECT_RESTRICTION_TYPE "SELECT rstn_type " \
43 " FROM restrictions INDEXED BY restrictions_index " \
44 " WHERE binpath = ? AND iftype = ?"
46 #define SELECT_RESTRICTION_TYPE_SUBSCRIBER_ID "SELECT rstn_type " \
47 " FROM restrictions INDEXED BY restrictions_index " \
48 " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?"
50 #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \
51 " WHERE binpath = ? AND iftype = ? AND subscriber_id = ? AND " \
52 " roaming = ? AND ifname = ?"
54 /* UPDATE statement */
55 #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \
56 " SET binpath = ?, data_limit = ?, iftype = ?, rstn_type = ?, " \
57 " roaming = ?, ifname = ?, subscriber_id = ?, data_warn_limit = ? " \
58 " WHERE restriction_id = ?"
60 /* INSERT statement */
61 #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \
62 " (binpath, data_limit, iftype, rstn_type, " \
63 " roaming, ifname, subscriber_id, data_warn_limit) " \
64 " VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
66 static void __finalize_delete(void);
68 #define PREPARE_DELETE(stm, query) do { \
69 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
70 if (rc != SQLITE_OK) { \
72 __finalize_delete(); \
73 STC_LOGE("Failed to prepare \"%s\"query" \
79 static void __finalize_select(void);
81 #define PREPARE_SELECT(stm, query) do { \
82 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
83 if (rc != SQLITE_OK) { \
85 __finalize_select(); \
86 STC_LOGE("Failed to prepare \"%s\"query" \
92 static void __finalize_update(void);
94 #define PREPARE_UPDATE(stm, query) do { \
95 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
96 if (rc != SQLITE_OK) { \
98 __finalize_update(); \
99 STC_LOGE("Failed to prepare \"%s\"query" \
105 static void __finalize_insert(void);
107 #define PREPARE_INSERT(stm, query) do { \
108 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
109 if (rc != SQLITE_OK) { \
111 __finalize_insert(); \
112 STC_LOGE("Failed to prepare \"%s\"query" \
118 #define FINALIZE(stm) do { \
120 sqlite3_finalize(stm); \
125 /* DELETE statements */
126 static sqlite3_stmt *delete_restrictions;
128 /* SELECT statements */
129 static sqlite3_stmt *select_restriction;
130 static sqlite3_stmt *select_restriction_per_app;
131 static sqlite3_stmt *select_restriction_type;
132 static sqlite3_stmt *select_restriction_type_subscriber_id;
133 static sqlite3_stmt *select_restriction_id;
135 /* REPLACE statements */
136 static sqlite3_stmt *update_net_restrictions;
138 /* INSERT statements */
139 static sqlite3_stmt *insert_net_restrictions;
141 static int __prepare_delete(sqlite3 *db)
143 __STC_LOG_FUNC_ENTER__;
145 static int initialized;
148 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
149 return SQLITE_OK; //LCOV_EXCL_LINE
152 PREPARE_DELETE(delete_restrictions, DELETE_RESTRICTIONS);
155 __STC_LOG_FUNC_EXIT__;
159 static void __finalize_delete(void)
161 __STC_LOG_FUNC_ENTER__;
163 FINALIZE(delete_restrictions);
165 __STC_LOG_FUNC_EXIT__;
168 static int __prepare_select(sqlite3 *db)
170 __STC_LOG_FUNC_ENTER__;
172 static int initialized;
175 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
176 return SQLITE_OK; //LCOV_EXCL_LINE
179 PREPARE_SELECT(select_restriction, SELECT_RESTRICTIONS);
180 PREPARE_SELECT(select_restriction_per_app, SELECT_RESTRICTIONS_PER_APP);
181 PREPARE_SELECT(select_restriction_type, SELECT_RESTRICTION_TYPE);
182 PREPARE_SELECT(select_restriction_type_subscriber_id, SELECT_RESTRICTION_TYPE_SUBSCRIBER_ID);
183 PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
186 __STC_LOG_FUNC_EXIT__;
190 static void __finalize_select(void)
192 __STC_LOG_FUNC_ENTER__;
194 FINALIZE(select_restriction);
195 FINALIZE(select_restriction_per_app);
196 FINALIZE(select_restriction_type);
197 FINALIZE(select_restriction_type_subscriber_id);
198 FINALIZE(select_restriction_id);
200 __STC_LOG_FUNC_EXIT__;
203 static int __prepare_replace(sqlite3 *db)
205 __STC_LOG_FUNC_ENTER__;
207 static int initialized;
210 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
211 return SQLITE_OK; //LCOV_EXCL_LINE
214 PREPARE_UPDATE(update_net_restrictions, UPDATE_NET_RESTRICTIONS);
217 __STC_LOG_FUNC_EXIT__;
221 static void __finalize_update(void)
223 __STC_LOG_FUNC_ENTER__;
225 FINALIZE(update_net_restrictions);
227 __STC_LOG_FUNC_EXIT__;
230 static int __prepare_insert(sqlite3 *db)
232 __STC_LOG_FUNC_ENTER__;
234 static int initialized;
237 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
238 return SQLITE_OK; //LCOV_EXCL_LINE
241 PREPARE_UPDATE(insert_net_restrictions, INSERT_NET_RESTRICTIONS);
244 __STC_LOG_FUNC_EXIT__;
248 static void __finalize_insert(void)
250 __STC_LOG_FUNC_ENTER__;
252 FINALIZE(insert_net_restrictions);
254 __STC_LOG_FUNC_EXIT__;
257 stc_error_e table_restrictions_per_app(const gchar* app_id,
258 const table_restrictions_info_cb restriction_cb,
261 __STC_LOG_FUNC_ENTER__;
262 table_restrictions_info data;
264 stc_error_e error_code = STC_ERROR_NONE;
265 sqlite3_stmt *stmt = select_restriction_per_app;
268 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
269 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
272 DB_ACTION(sqlite3_bind_text(stmt, 1, app_id, -1,
274 data.app_id = (char *)app_id;
277 rc = sqlite3_step(stmt);
279 memset(&data, 0, sizeof(data));
285 data.app_id = (char *)sqlite3_column_text(stmt, 0);
286 data.data_limit = sqlite3_column_int64(stmt, 1);
287 data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
289 (stc_rstn_type_e)sqlite3_column_int(stmt, 3);
290 data.roaming = sqlite3_column_int(stmt, 4);
291 data.ifname = (char *)sqlite3_column_text(stmt, 5);
292 data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
293 data.data_warn_limit = sqlite3_column_int64(stmt, 7);
294 data.restriction_id = sqlite3_column_int64(stmt, 8);
296 if (restriction_cb(&data, user_data) == STC_CANCEL)
297 rc = SQLITE_DONE; //LCOV_EXCL_LINE
301 STC_LOGE("Failed to enumerate restrictions: %s\n", //LCOV_EXCL_LINE
302 sqlite3_errmsg(stc_db_get_database()));
304 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
305 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
307 } while (rc == SQLITE_ROW);
311 __STC_LOG_FUNC_EXIT__;
315 stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restriction_cb,
318 __STC_LOG_FUNC_ENTER__;
319 table_restrictions_info data;
321 stc_error_e error_code = STC_ERROR_NONE;
322 sqlite3_stmt *stmt = select_restriction;
325 rc = sqlite3_step(stmt);
327 memset(&data, 0, sizeof(data));
333 data.app_id = (char *)sqlite3_column_text(stmt, 0);
334 data.data_limit = sqlite3_column_int64(stmt, 1);
335 data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
337 (stc_rstn_type_e)sqlite3_column_int(stmt, 3);
338 data.roaming = sqlite3_column_int(stmt, 4);
339 data.ifname = (char *)sqlite3_column_text(stmt, 5);
340 data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
341 data.data_warn_limit = sqlite3_column_int64(stmt, 7);
342 data.restriction_id = sqlite3_column_int64(stmt, 8);
344 if (restriction_cb(&data, user_data) == STC_CANCEL)
345 rc = SQLITE_DONE; //LCOV_EXCL_LINE
349 STC_LOGE("Failed to enumerate restrictions: %s\n", //LCOV_EXCL_LINE
350 sqlite3_errmsg(stc_db_get_database()));
352 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
353 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
355 } while (rc == SQLITE_ROW);
358 __STC_LOG_FUNC_EXIT__;
362 stc_error_e table_restrictions_get_restriction_type_subscriber_id(const char *app_id,
363 stc_iface_type_e iftype,
364 const char *subscriber_id,
365 stc_rstn_type_e *type)
367 __STC_LOG_FUNC_ENTER__;
368 int error_code = STC_ERROR_NONE;
370 bool state_subscriber_id = 0;
373 STC_LOGE("Please provide valid argument!"); //LCOV_EXCL_LINE
374 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
375 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
378 *type = STC_RSTN_TYPE_UNKNOWN;
379 sqlite3_reset(select_restriction_type_subscriber_id);
380 sqlite3_reset(select_restriction_type);
382 if (subscriber_id == NULL) {
383 state_subscriber_id = 0;
384 DB_ACTION(sqlite3_bind_text(select_restriction_type, 1,
385 app_id ? app_id : "", -1,
387 DB_ACTION(sqlite3_bind_int(select_restriction_type, 2,
389 ret = sqlite3_step(select_restriction_type);
391 state_subscriber_id = 1;
392 DB_ACTION(sqlite3_bind_text(select_restriction_type_subscriber_id, 1,
393 app_id ? app_id : "", -1,
395 DB_ACTION(sqlite3_bind_int(select_restriction_type_subscriber_id, 2,
397 DB_ACTION(sqlite3_bind_text(select_restriction_type_subscriber_id, 3,
398 subscriber_id, -1, SQLITE_STATIC));
399 ret = sqlite3_step(select_restriction_type_subscriber_id);
406 if (state_subscriber_id)
407 *type = (stc_rstn_type_e)sqlite3_column_int(select_restriction_type_subscriber_id, 0);
409 *type = (stc_rstn_type_e)sqlite3_column_int(select_restriction_type, 0);
413 STC_LOGE("Can't perform sql query: %s\n", //LCOV_EXCL_LINE
414 sqlite3_errmsg(stc_db_get_database()));
415 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
419 sqlite3_reset(select_restriction_type);
420 sqlite3_reset(select_restriction_type_subscriber_id);
424 stc_error_e table_restrictions_get_restriction_type(const char *app_id,
425 stc_iface_type_e iftype,
426 stc_rstn_type_e *type)
428 __STC_LOG_FUNC_ENTER__;
429 __STC_LOG_FUNC_EXIT__;
430 return table_restrictions_get_restriction_type_subscriber_id(app_id, iftype,
434 stc_error_e table_restrictions_delete(const char *app_id,
435 const stc_iface_type_e iftype,
436 const char *subscriber_id)
438 stc_error_e error_code = STC_ERROR_NONE;
439 sqlite3_stmt *stmt = delete_restrictions;
441 STC_LOGD("app_id [%s], iftype [%d], subscriber_id [%s]",
442 app_id, iftype, subscriber_id);
444 DB_ACTION(sqlite3_bind_text(stmt, 1, app_id ? app_id : "",
445 -1, SQLITE_TRANSIENT));
446 DB_ACTION(sqlite3_bind_int(stmt, 2, iftype));
447 DB_ACTION(sqlite3_bind_text(stmt, 3, subscriber_id ? subscriber_id : "", -1,
450 if (sqlite3_step(stmt) != SQLITE_DONE) {
451 STC_LOGE("Failed to remove restrictions by network interface %s\n", //LCOV_EXCL_LINE
452 sqlite3_errmsg(stc_db_get_database()));
453 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
454 goto handle_error; //LCOV_EXCL_LINE
457 STC_LOGD("Restriction deleted for app_id [%s]", app_id);
465 stc_error_e __get_restriction_id(table_restrictions_info *info)
467 __STC_LOG_FUNC_ENTER__;
469 stc_error_e error_code = STC_ERROR_NONE;
470 sqlite3_stmt *stmt = select_restriction_id;
472 DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
473 -1, SQLITE_TRANSIENT));
474 DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype));
475 DB_ACTION(sqlite3_bind_text(stmt, 3, info->subscriber_id ? info->subscriber_id : "",
476 -1, SQLITE_TRANSIENT));
477 DB_ACTION(sqlite3_bind_int(stmt, 4, info->roaming));
478 DB_ACTION(sqlite3_bind_text(stmt, 5, info->ifname ? info->ifname : "",
479 -1, SQLITE_TRANSIENT));
481 rc = sqlite3_step(stmt);
487 info->restriction_id = sqlite3_column_int64(stmt, 0);
488 STC_LOGD("restriction id [%llu]", info->restriction_id);
492 STC_LOGE("Failed to get restriction id: %s\n", //LCOV_EXCL_LINE
493 sqlite3_errmsg(stc_db_get_database()));
498 __STC_LOG_FUNC_EXIT__;
502 stc_error_e table_restrictions_update(table_restrictions_info *info)
504 stc_error_e error_code = STC_ERROR_NONE;
505 sqlite3_stmt *stmt = insert_net_restrictions;
508 error_code = STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
509 goto handle_error; //LCOV_EXCL_LINE
512 __get_restriction_id(info);
513 if (info->restriction_id)
514 stmt = update_net_restrictions;
516 DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
517 -1, SQLITE_TRANSIENT));
518 DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_limit));
519 DB_ACTION(sqlite3_bind_int(stmt, 3, info->iftype));
520 DB_ACTION(sqlite3_bind_int(stmt, 4, info->rstn_type));
521 DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming));
522 DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "",
523 -1, SQLITE_TRANSIENT));
524 DB_ACTION(sqlite3_bind_text(stmt, 7, info->subscriber_id ? info->subscriber_id : "",
525 -1, SQLITE_TRANSIENT));
526 DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit));
528 if (info->restriction_id)
529 DB_ACTION(sqlite3_bind_int64(stmt, 9, info->restriction_id));
531 if (sqlite3_step(stmt) != SQLITE_DONE) {
532 STC_LOGE("Failed to set network restriction: %s\n", //LCOV_EXCL_LINE
533 sqlite3_errmsg(stc_db_get_database()));
534 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
535 goto handle_error; //LCOV_EXCL_LINE
538 if (info->restriction_id) {
539 STC_LOGD("Restriction updated app_id [%s]", info->app_id);
541 STC_LOGD("Restriction inserted app_id [%s]", info->app_id);
542 __get_restriction_id(info);
550 stc_error_e table_restrictions_prepare(sqlite3 *db)
552 __STC_LOG_FUNC_ENTER__;
554 stc_error_e error_code = STC_ERROR_NONE;
557 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
558 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
561 DB_ACTION(__prepare_delete(db));
562 DB_ACTION(__prepare_select(db));
563 DB_ACTION(__prepare_replace(db));
564 DB_ACTION(__prepare_insert(db));
568 __STC_LOG_FUNC_EXIT__;
572 void table_restrictions_finalize(void)
574 __STC_LOG_FUNC_ENTER__;
579 __STC_LOG_FUNC_EXIT__;