Added data limits monitoring for daily, weekly and monthly
[platform/core/connectivity/stc-manager.git] / src / stc-restriction.c
old mode 100755 (executable)
new mode 100644 (file)
index e8a8a7d..2f44459
@@ -34,7 +34,9 @@ static const gchar *stc_err_strs[] = {
        "OUT_OF_MEMORY",
        "INVALID_PARAMETER",
        "NO_DATA",
+       "ALREADY_DATA",
        "UNINITIALIZED",
+       "PERMISSION_DENIED",
        "NOTIMPL"
 };
 
@@ -43,15 +45,17 @@ void __initialize_rstn_rule(table_restrictions_info *rule)
        rule->app_id = NULL;
        rule->ifname = NULL;
        rule->iftype = STC_IFACE_ALL;
-       rule->rst_state = STC_RESTRICTION_REMOVED;
-       rule->data_limit = 0;
-       rule->data_warn_limit = 0;
+       rule->rstn_type = STC_RSTN_TYPE_UNKNOWN;
+       rule->data_limit = -1;
+       rule->data_warn_limit = -1;
+       rule->monthly_limit = -1;
+       rule->weekly_limit = -1;
+       rule->daily_limit = -1;
        rule->roaming = STC_ROAMING_DISABLE;
        rule->subscriber_id = NULL;
 }
 
-gboolean __validate_rstn_rule(table_restrictions_info *rule,
-                             enum traffic_restriction_type rst_type)
+gboolean __validate_rstn_rule(table_restrictions_info *rule)
 {
        __STC_LOG_FUNC_ENTER__;
 
@@ -60,11 +64,6 @@ gboolean __validate_rstn_rule(table_restrictions_info *rule,
                return FALSE; //LCOV_EXCL_LINE
        }
 
-       if (rst_type <= RST_UNDEFINDED || rst_type >= RST_MAX_VALUE) {
-               __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
-               return FALSE; //LCOV_EXCL_LINE
-       }
-
        if (rule->iftype <= STC_IFACE_UNKNOWN ||
            rule->iftype >= STC_IFACE_LAST_ELEM) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
@@ -109,8 +108,8 @@ void __stc_restriction_app_info_builder_add(GVariantBuilder *builder,
        g_variant_builder_add(builder, "{sv}", "iftype",
                              g_variant_new_uint16(info->iftype));
 
-       g_variant_builder_add(builder, "{sv}", "rst_state",
-                             g_variant_new_uint16(info->rst_state));
+       g_variant_builder_add(builder, "{sv}", "rstn_type",
+                             g_variant_new_uint16(info->rstn_type));
 
        g_variant_builder_add(builder, "{sv}", "data_limit",
                              g_variant_new_int64(info->data_limit));
@@ -118,6 +117,18 @@ void __stc_restriction_app_info_builder_add(GVariantBuilder *builder,
        g_variant_builder_add(builder, "{sv}", "data_warn_limit",
                              g_variant_new_int64(info->data_warn_limit));
 
+       g_variant_builder_add(builder, "{sv}", "month_start_date",
+                             g_variant_new_int32(info->month_start_date));
+
+       g_variant_builder_add(builder, "{sv}", "monthly_limit",
+                             g_variant_new_int64(info->monthly_limit));
+
+       g_variant_builder_add(builder, "{sv}", "weekly_limit",
+                             g_variant_new_int64(info->weekly_limit));
+
+       g_variant_builder_add(builder, "{sv}", "daily_limit",
+                             g_variant_new_int64(info->daily_limit));
+
        g_variant_builder_add(builder, "{sv}", "roaming",
                              g_variant_new_uint16(info->roaming));
 
@@ -169,8 +180,6 @@ stc_cb_ret_e __table_restrictions_per_app_cb(const table_restrictions_info *info
 static void __stc_extract_restriction_rule(const char *key, GVariant *value,
                                           void *user_data)
 {
-       __STC_LOG_FUNC_ENTER__;
-
        table_restrictions_info *rule =
                (table_restrictions_info *) user_data;
        if (rule == NULL) {
@@ -182,7 +191,7 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value,
                guint str_length;
                const gchar *str = g_variant_get_string(value, &str_length);
                rule->app_id = g_strdup(str);
-               STC_LOGD("app_id: [%s]", (unsigned int) rule->app_id);
+               STC_LOGD("app_id: [%s]", rule->app_id);
 
        } else if (!g_strcmp0(key, "ifname")) {
                guint str_length;
@@ -202,6 +211,22 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value,
                rule->data_warn_limit = g_variant_get_int64(value);
                STC_LOGD("data_warn_limit: [%lld]", rule->data_warn_limit);
 
+       } else if (!g_strcmp0(key, "month_start_date")) {
+               rule->month_start_date = g_variant_get_int32(value);
+               STC_LOGD("month_start_date: [%d]", rule->month_start_date);
+
+       } else if (!g_strcmp0(key, "monthly_limit")) {
+               rule->monthly_limit = g_variant_get_int64(value);
+               STC_LOGD("monthly_limit: [%lld]", rule->monthly_limit);
+
+       } else if (!g_strcmp0(key, "weekly_limit")) {
+               rule->weekly_limit = g_variant_get_int64(value);
+               STC_LOGD("weekly_limit: [%lld]", rule->weekly_limit);
+
+       } else if (!g_strcmp0(key, "daily_limit")) {
+               rule->daily_limit = g_variant_get_int64(value);
+               STC_LOGD("daily_limit: [%lld]", rule->daily_limit);
+
        } else if (!g_strcmp0(key, "roaming")) {
                rule->roaming = g_variant_get_uint16(value);
                STC_LOGD("roaming: [%u]", rule->roaming);
@@ -212,11 +237,13 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value,
                rule->subscriber_id = g_strdup(str);
                STC_LOGD("subscriber_id: [%s]", rule->subscriber_id);
 
+       } else if (!g_strcmp0(key, "rstn_type")) {
+               rule->rstn_type = g_variant_get_uint16(value);
+               STC_LOGD("type: [%u]", (unsigned int) rule->rstn_type);
+
        } else {
                STC_LOGD("Unknown select rule"); //LCOV_EXCL_LINE
        }
-
-       __STC_LOG_FUNC_EXIT__;
 }
 
 gboolean handle_restriction_set(StcRestriction *object,
@@ -239,47 +266,7 @@ gboolean handle_restriction_set(StcRestriction *object,
                g_variant_iter_free(iter);
        }
 
-       rule.rst_state = STC_RESTRICTION_REMOVED;
-
-       if (__validate_rstn_rule(&rule, RST_SET) == FALSE) {
-               STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
-                                                STC_ERROR_INVALID_PARAMETER);
-               __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
-               return TRUE;
-       }
-
-       table_restrictions_update(&rule);
-       /* update restriction rule in runtime structure */
-       stc_monitor_rstns_tree_add(&rule);
-
-       STC_DBUS_REPLY_ERROR_NONE(invocation);
-       __STC_LOG_FUNC_EXIT__;
-       return TRUE;
-}
-
-gboolean handle_restriction_exclude(StcRestriction *object,
-                                   GDBusMethodInvocation *invocation,
-                                   GVariant *parameters,
-                                   void *user_data)
-{
-       __STC_LOG_FUNC_ENTER__;
-       GVariantIter *iter = NULL;
-       table_restrictions_info rule;
-
-       memset(&rule, 0, sizeof(table_restrictions_info));
-       __initialize_rstn_rule(&rule);
-
-       g_variant_get(parameters, "a{sv}", &iter);
-       if (iter != NULL) {
-               stc_manager_gdbus_dict_foreach(iter,
-                                              __stc_extract_restriction_rule,
-                                              &rule);
-               g_variant_iter_free(iter);
-       }
-
-       rule.rst_state = STC_RESTRICTION_EXCLUDED;
-
-       if (__validate_rstn_rule(&rule, RST_EXCLUDE) == FALSE) {
+       if (__validate_rstn_rule(&rule) == FALSE) {
                STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
                                                 STC_ERROR_INVALID_PARAMETER);
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
@@ -315,7 +302,7 @@ gboolean handle_restriction_unset(StcRestriction *object,
                g_variant_iter_free(iter);
        }
 
-       if (__validate_rstn_rule(&rule, RST_UNSET) == FALSE) {
+       if (__validate_rstn_rule(&rule) == FALSE) {
                STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
                                                 STC_ERROR_INVALID_PARAMETER);
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
@@ -391,7 +378,7 @@ gboolean handle_restriction_get_all(StcRestriction *object,
        return TRUE;
 }
 
-gboolean handle_restriction_get_state(StcRestriction *object,
+gboolean handle_restriction_get_type(StcRestriction *object,
                                      GDBusMethodInvocation *invocation,
                                      const gchar *app_id,
                                      int iftype,
@@ -399,17 +386,17 @@ gboolean handle_restriction_get_state(StcRestriction *object,
 {
        __STC_LOG_FUNC_ENTER__;
        GVariant *return_parameters = NULL;
-       stc_restriction_state_e state = STC_RESTRICTION_UNKNOWN;
+       stc_rstn_type_e type = STC_RSTN_TYPE_UNKNOWN;
        stc_error_e ret;
 
-       ret = table_restrictions_get_restriction_state(app_id, iftype, &state);
+       ret = table_restrictions_get_restriction_type(app_id, iftype, &type);
        if (ret < STC_ERROR_NONE) {
                STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                return TRUE;
        }
 
-       return_parameters = g_variant_new("(ii)", STC_ERROR_NONE, state);
+       return_parameters = g_variant_new("(ii)", STC_ERROR_NONE, type);
        STC_DBUS_REPLY(invocation, return_parameters);
        __STC_LOG_FUNC_EXIT__;
        return TRUE;