Refactoring structures for monitoring and restrictions
[platform/core/connectivity/stc-manager.git] / src / database / tables / table-restrictions.c
old mode 100755 (executable)
new mode 100644 (file)
index 2d64f9c..929449d
 
 /* DELETE statements */
 #define DELETE_RESTRICTIONS "DELETE FROM restrictions " \
-       " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?"
+       " WHERE binpath = ? AND iftype = ? AND ifname = ? " \
+       " AND subscriber_id = ? AND roaming = ?"
 
 /* SELECT statements */
 #define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \
-       " iftype, rst_type, roaming, ifname, subscriber_id, " \
-       " data_warn_limit, restriction_id FROM restrictions"
+       " iftype, rstn_type, roaming, ifname, subscriber_id, " \
+       " data_warn_limit, monthly_limit, weekly_limit, daily_limit, month_start_date, " \
+       " restriction_id FROM restrictions"
 
 #define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, data_limit, " \
-       " iftype, rst_type, roaming, ifname, subscriber_id, " \
-       " data_warn_limit, restriction_id " \
+       " iftype, rstn_type, roaming, ifname, subscriber_id, " \
+       " data_warn_limit, monthly_limit, weekly_limit, daily_limit, month_start_date, " \
+       " restriction_id " \
        " FROM restrictions INDEXED BY restrictions_index " \
        " WHERE binpath = ?"
 
-#define SELECT_RESTRICTION_STATE "SELECT rst_type " \
+#define SELECT_RESTRICTION_TYPE "SELECT rstn_type " \
        " FROM restrictions INDEXED BY restrictions_index " \
        " WHERE binpath = ? AND iftype = ?"
 
-#define SELECT_RESTRICTION_STATE_SUBSCRIBER_ID "SELECT rst_type " \
+#define SELECT_RESTRICTION_TYPE_SUBSCRIBER_ID "SELECT rstn_type " \
        " FROM restrictions INDEXED BY restrictions_index " \
        " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?"
 
 #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \
        " WHERE binpath = ? AND iftype = ? AND subscriber_id = ? AND " \
-       " rst_type = ? AND roaming = ? AND ifname = ?"
+       " roaming = ? AND ifname = ?"
 
 /* UPDATE statement */
 #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \
-       " SET binpath = ?, data_limit = ?, iftype = ?, rst_type = ?, " \
-       " roaming = ?, ifname = ?, subscriber_id = ?, data_warn_limit = ? " \
+       " SET binpath = ?, data_limit = ?, iftype = ?, rstn_type = ?, " \
+       " roaming = ?, ifname = ?, subscriber_id = ?, data_warn_limit = ?, " \
+       " monthly_limit = ?, weekly_limit = ?, daily_limit = ? " \
        " WHERE restriction_id = ?"
 
 /* INSERT statement */
 #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \
-       " (binpath, data_limit, iftype, rst_type, " \
-       " roaming, ifname, subscriber_id, data_warn_limit) " \
-       " VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
+       " (binpath, data_limit, iftype, rstn_type, " \
+       " roaming, ifname, subscriber_id, data_warn_limit, " \
+       " monthly_limit, weekly_limit, daily_limit, month_start_date) " \
+       " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
 
 static void __finalize_delete(void);
 
@@ -128,8 +133,8 @@ static sqlite3_stmt *delete_restrictions;
 /* SELECT statements */
 static sqlite3_stmt *select_restriction;
 static sqlite3_stmt *select_restriction_per_app;
-static sqlite3_stmt *select_restriction_state;
-static sqlite3_stmt *select_restriction_state_subscriber_id;
+static sqlite3_stmt *select_restriction_type;
+static sqlite3_stmt *select_restriction_type_subscriber_id;
 static sqlite3_stmt *select_restriction_id;
 
 /* REPLACE statements */
@@ -178,8 +183,8 @@ static int __prepare_select(sqlite3 *db)
 
        PREPARE_SELECT(select_restriction, SELECT_RESTRICTIONS);
        PREPARE_SELECT(select_restriction_per_app, SELECT_RESTRICTIONS_PER_APP);
-       PREPARE_SELECT(select_restriction_state, SELECT_RESTRICTION_STATE);
-       PREPARE_SELECT(select_restriction_state_subscriber_id, SELECT_RESTRICTION_STATE_SUBSCRIBER_ID);
+       PREPARE_SELECT(select_restriction_type, SELECT_RESTRICTION_TYPE);
+       PREPARE_SELECT(select_restriction_type_subscriber_id, SELECT_RESTRICTION_TYPE_SUBSCRIBER_ID);
        PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
 
        initialized = 1;
@@ -193,8 +198,8 @@ static void __finalize_select(void)
 
        FINALIZE(select_restriction);
        FINALIZE(select_restriction_per_app);
-       FINALIZE(select_restriction_state);
-       FINALIZE(select_restriction_state_subscriber_id);
+       FINALIZE(select_restriction_type);
+       FINALIZE(select_restriction_type_subscriber_id);
        FINALIZE(select_restriction_id);
 
        __STC_LOG_FUNC_EXIT__;
@@ -285,13 +290,16 @@ stc_error_e table_restrictions_per_app(const gchar* app_id,
                        data.app_id = (char *)sqlite3_column_text(stmt, 0);
                        data.data_limit = sqlite3_column_int64(stmt, 1);
                        data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
-                       data.rst_type =
+                       data.rstn_type =
                                (stc_rstn_type_e)sqlite3_column_int(stmt, 3);
                        data.roaming = sqlite3_column_int(stmt, 4);
                        data.ifname = (char *)sqlite3_column_text(stmt, 5);
                        data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
                        data.data_warn_limit = sqlite3_column_int64(stmt, 7);
-                       data.restriction_id = sqlite3_column_int64(stmt, 8);
+                       data.monthly_limit = sqlite3_column_int64(stmt, 8);
+                       data.weekly_limit = sqlite3_column_int64(stmt, 9);
+                       data.daily_limit = sqlite3_column_int64(stmt, 10);
+                       data.restriction_id = sqlite3_column_int64(stmt, 11);
 
                        if (restriction_cb(&data, user_data) == STC_CANCEL)
                                rc = SQLITE_DONE; //LCOV_EXCL_LINE
@@ -333,13 +341,17 @@ stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restrict
                        data.app_id = (char *)sqlite3_column_text(stmt, 0);
                        data.data_limit = sqlite3_column_int64(stmt, 1);
                        data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
-                       data.rst_type =
+                       data.rstn_type =
                                (stc_rstn_type_e)sqlite3_column_int(stmt, 3);
                        data.roaming = sqlite3_column_int(stmt, 4);
                        data.ifname = (char *)sqlite3_column_text(stmt, 5);
                        data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
                        data.data_warn_limit = sqlite3_column_int64(stmt, 7);
-                       data.restriction_id = sqlite3_column_int64(stmt, 8);
+                       data.monthly_limit = sqlite3_column_int64(stmt, 8);
+                       data.weekly_limit = sqlite3_column_int64(stmt, 9);
+                       data.daily_limit = sqlite3_column_int64(stmt, 10);
+                       data.month_start_date = sqlite3_column_int(stmt, 11);
+                       data.restriction_id = sqlite3_column_int64(stmt, 12);
 
                        if (restriction_cb(&data, user_data) == STC_CANCEL)
                                rc = SQLITE_DONE; //LCOV_EXCL_LINE
@@ -359,44 +371,44 @@ stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restrict
        return error_code;
 }
 
-stc_error_e table_restrictions_get_restriction_state_subscriber_id(const char *app_id,
+stc_error_e table_restrictions_get_restriction_type_subscriber_id(const char *app_id,
                                                          stc_iface_type_e iftype,
                                                          const char *subscriber_id,
-                                                         stc_restriction_state_e *state)
+                                                         stc_rstn_type_e *type)
 {
        __STC_LOG_FUNC_ENTER__;
        int error_code = STC_ERROR_NONE;
        int ret;
        bool state_subscriber_id = 0;
 
-       if (state == NULL) {
+       if (type == NULL) {
                STC_LOGE("Please provide valid argument!"); //LCOV_EXCL_LINE
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
 
-       *state = STC_RESTRICTION_UNKNOWN;
-       sqlite3_reset(select_restriction_state_subscriber_id);
-       sqlite3_reset(select_restriction_state);
+       *type = STC_RSTN_TYPE_UNKNOWN;
+       sqlite3_reset(select_restriction_type_subscriber_id);
+       sqlite3_reset(select_restriction_type);
 
        if (subscriber_id == NULL) {
                state_subscriber_id = 0;
-               DB_ACTION(sqlite3_bind_text(select_restriction_state, 1,
+               DB_ACTION(sqlite3_bind_text(select_restriction_type, 1,
                                            app_id ? app_id : "", -1,
                                            SQLITE_STATIC));
-               DB_ACTION(sqlite3_bind_int(select_restriction_state, 2,
+               DB_ACTION(sqlite3_bind_int(select_restriction_type, 2,
                                           iftype));
-               ret = sqlite3_step(select_restriction_state);
+               ret = sqlite3_step(select_restriction_type);
        } else {
                state_subscriber_id = 1;
-               DB_ACTION(sqlite3_bind_text(select_restriction_state_subscriber_id, 1,
+               DB_ACTION(sqlite3_bind_text(select_restriction_type_subscriber_id, 1,
                                            app_id ? app_id : "", -1,
                                            SQLITE_STATIC));
-               DB_ACTION(sqlite3_bind_int(select_restriction_state_subscriber_id, 2,
+               DB_ACTION(sqlite3_bind_int(select_restriction_type_subscriber_id, 2,
                                           iftype));
-               DB_ACTION(sqlite3_bind_text(select_restriction_state_subscriber_id, 3,
+               DB_ACTION(sqlite3_bind_text(select_restriction_type_subscriber_id, 3,
                                            subscriber_id, -1, SQLITE_STATIC));
-               ret = sqlite3_step(select_restriction_state_subscriber_id);
+               ret = sqlite3_step(select_restriction_type_subscriber_id);
        }
 
        switch (ret) {
@@ -404,49 +416,53 @@ stc_error_e table_restrictions_get_restriction_state_subscriber_id(const char *a
                break;
        case SQLITE_ROW:
                if (state_subscriber_id)
-                       *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state_subscriber_id, 0);
+                       *type = (stc_rstn_type_e)sqlite3_column_int(select_restriction_type_subscriber_id, 0);
                else
-                       *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state, 0);
+                       *type = (stc_rstn_type_e)sqlite3_column_int(select_restriction_type, 0);
                break;
        case SQLITE_ERROR:
        default:
-               STC_LOGE("Can't perform sql query: %s \n%s", //LCOV_EXCL_LINE
-                        SELECT_RESTRICTION_STATE,
+               STC_LOGE("Can't perform sql query: %s\n", //LCOV_EXCL_LINE
                         sqlite3_errmsg(stc_db_get_database()));
                error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
        }
 
 handle_error:
-       sqlite3_reset(select_restriction_state);
-       sqlite3_reset(select_restriction_state_subscriber_id);
+       sqlite3_reset(select_restriction_type);
+       sqlite3_reset(select_restriction_type_subscriber_id);
        return error_code;
 }
 
-stc_error_e table_restrictions_get_restriction_state(const char *app_id,
+stc_error_e table_restrictions_get_restriction_type(const char *app_id,
                                                     stc_iface_type_e iftype,
-                                                    stc_restriction_state_e *state)
+                                                    stc_rstn_type_e *type)
 {
        __STC_LOG_FUNC_ENTER__;
        __STC_LOG_FUNC_EXIT__;
-       return table_restrictions_get_restriction_state_subscriber_id(app_id, iftype,
-                                                            NULL, state);
+       return table_restrictions_get_restriction_type_subscriber_id(app_id, iftype,
+                                                            NULL, type);
 }
 
 stc_error_e table_restrictions_delete(const char *app_id,
                                      const stc_iface_type_e iftype,
-                                     const char *subscriber_id)
+                                     const char *ifname,
+                                     const char *subscriber_id,
+                                     const stc_roaming_type_e roaming)
 {
        stc_error_e error_code = STC_ERROR_NONE;
        sqlite3_stmt *stmt = delete_restrictions;
 
-       STC_LOGD("app_id [%s], iftype [%d], subscriber_id [%s]",
-                app_id, iftype, subscriber_id);
+       STC_LOGD("app_id[%s] iftype[%d] ifname[%s] subscriber_id[%s] roaming[%d]",
+                app_id, iftype, ifname, subscriber_id, roaming);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, app_id ? app_id : "",
                                    -1, SQLITE_TRANSIENT));
        DB_ACTION(sqlite3_bind_int(stmt, 2, iftype));
-       DB_ACTION(sqlite3_bind_text(stmt, 3, subscriber_id ? subscriber_id : "", -1,
-                                   SQLITE_TRANSIENT));
+       DB_ACTION(sqlite3_bind_text(stmt, 3, ifname ? ifname : "",
+                                   -1, SQLITE_TRANSIENT));
+       DB_ACTION(sqlite3_bind_text(stmt, 4, subscriber_id ? subscriber_id : "",
+                                       -1, SQLITE_TRANSIENT));
+       DB_ACTION(sqlite3_bind_int(stmt, 5, roaming));
 
        if (sqlite3_step(stmt) != SQLITE_DONE) {
                STC_LOGE("Failed to remove restrictions by network interface %s\n", //LCOV_EXCL_LINE
@@ -475,9 +491,8 @@ stc_error_e __get_restriction_id(table_restrictions_info *info)
        DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype));
        DB_ACTION(sqlite3_bind_text(stmt, 3, info->subscriber_id ? info->subscriber_id : "",
                                    -1, SQLITE_TRANSIENT));
-       DB_ACTION(sqlite3_bind_int(stmt, 4, info->rst_type));
-       DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming));
-       DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "",
+       DB_ACTION(sqlite3_bind_int(stmt, 4, info->roaming));
+       DB_ACTION(sqlite3_bind_text(stmt, 5, info->ifname ? info->ifname : "",
                                    -1, SQLITE_TRANSIENT));
 
        rc = sqlite3_step(stmt);
@@ -519,16 +534,21 @@ stc_error_e table_restrictions_update(table_restrictions_info *info)
                                    -1, SQLITE_TRANSIENT));
        DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_limit));
        DB_ACTION(sqlite3_bind_int(stmt, 3, info->iftype));
-       DB_ACTION(sqlite3_bind_int(stmt, 4, info->rst_type));
+       DB_ACTION(sqlite3_bind_int(stmt, 4, info->rstn_type));
        DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming));
        DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "",
                                    -1, SQLITE_TRANSIENT));
        DB_ACTION(sqlite3_bind_text(stmt, 7, info->subscriber_id ? info->subscriber_id : "",
                                    -1, SQLITE_TRANSIENT));
        DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit));
+       DB_ACTION(sqlite3_bind_int64(stmt, 9, info->monthly_limit));
+       DB_ACTION(sqlite3_bind_int64(stmt, 10, info->weekly_limit));
+       DB_ACTION(sqlite3_bind_int64(stmt, 11, info->daily_limit));
 
        if (info->restriction_id)
-               DB_ACTION(sqlite3_bind_int64(stmt, 9, info->restriction_id));
+               DB_ACTION(sqlite3_bind_int64(stmt, 12, info->restriction_id));
+       else
+               DB_ACTION(sqlite3_bind_int64(stmt, 12, info->month_start_date));
 
        if (sqlite3_step(stmt) != SQLITE_DONE) {
                STC_LOGE("Failed to set network restriction: %s\n", //LCOV_EXCL_LINE