Refactoring structures for monitoring and restrictions
[platform/core/connectivity/stc-manager.git] / src / monitor / stc-default-connection.c
old mode 100755 (executable)
new mode 100644 (file)
index e524970..8c4b1ef
  */
 
 #include <vconf/vconf.h>
+#include <openssl/sha.h>
 
 #include "stc-monitor.h"
+#include "stc-firewall.h"
 #include "stc-manager-gdbus.h"
 #include "stc-default-connection.h"
 
@@ -58,29 +60,44 @@ static int __telephony_get_current_sim(void)
        int current_sim = 0;
 
        if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT_COUNT, &sim_slot_count) != 0) {
-               STC_LOGD("failed to get sim slot count");
-               return -1;
+               STC_LOGD("failed to get sim slot count"); //LCOV_EXCL_LINE
+               return -1; //LCOV_EXCL_LINE
        }
 
        if (sim_slot_count == SIM_SLOT_SINGLE) {
-              STC_LOGD("It's single sim model");
-              return current_sim;
+              STC_LOGD("It's single sim model"); //LCOV_EXCL_LINE
+              return current_sim; //LCOV_EXCL_LINE
        }
 
        if (vconf_get_int(VCONF_TELEPHONY_DEFAULT_DATA_SERVICE, &current_sim) != 0) {
-               STC_LOGD("failed to get default data service = %d\n",
+               STC_LOGD("failed to get default data service = %d\n", //LCOV_EXCL_LINE
                         current_sim);
-               return -1;
+               return -1; //LCOV_EXCL_LINE
        }
 
        return current_sim;
 }
 
-static void __telephony_get_modem_imsi(GDBusConnection *connection,
+static void __make_imsi_to_subscriber_id(char *imsi)
+{
+       int i = 0;
+       SHA256_CTX ctx;
+       unsigned char md[SHA256_DIGEST_LENGTH];
+
+       SHA256_Init(&ctx);
+       SHA256_Update(&ctx, imsi, strlen(imsi));
+       SHA256_Final(md, &ctx);
+
+       for (i = 0; i < SHA256_DIGEST_LENGTH; ++i)
+               snprintf(g_default_connection.subscriber_id + (i * 2), 3, "%02x", md[i]);
+}
+
+static void __telephony_get_modem_subscriber_id(GDBusConnection *connection,
                                       const char *default_modem_name)
 {
        GVariant *message = NULL;
        char tel_path[MAX_PATH_LENGTH];
+       char imsi[IMSI_LENGTH];
        const char *plmn = NULL;
        int plmn_len = 0;
        const char *msin = NULL;
@@ -95,29 +112,32 @@ static void __telephony_get_modem_imsi(GDBusConnection *connection,
                                              TELEPHONY_GET_IMSI,
                                              NULL);
        if (message == NULL) {
-               STC_LOGE("Failed to get services informations");
-               goto done;
+               STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE
+               goto done; //LCOV_EXCL_LINE
        }
 
        DEBUG_PARAMS(message);
        DEBUG_PARAM_TYPE(message);
        g_variant_get(message, "(&s&s)", &plmn, &msin);
-       plmn_len = strlen(plmn);
-       msin_len = strlen(msin);
+       if (plmn)
+               plmn_len = strlen(plmn);
+       if (msin)
+               msin_len = strlen(msin);
 
        if (msin_len + plmn_len >= IMSI_LENGTH) {
-               STC_LOGD("Incorrect length of mobile subscriber identifier + net id");
-               goto done;
+               STC_LOGD("Incorrect length of mobile subscriber identifier + net id"); //LCOV_EXCL_LINE
+               goto done; //LCOV_EXCL_LINE
        }
 
-       snprintf(g_default_connection.imsi, IMSI_LENGTH, "%s%s", plmn, msin);
+       snprintf(imsi, IMSI_LENGTH, "%s%s", plmn, msin);
+       __make_imsi_to_subscriber_id(imsi);
 
 done:
        g_variant_unref(message);
        return;
 }
 
-static void __telephony_update_default_modem_imsi(GDBusConnection *connection)
+static void __telephony_update_default_modem_subscriber_id(GDBusConnection *connection)
 {
        GVariant *message = NULL;
        GVariantIter *iter = NULL;
@@ -126,8 +146,8 @@ static void __telephony_update_default_modem_imsi(GDBusConnection *connection)
        int current_sim = __telephony_get_current_sim();
 
        if (current_sim < 0) {
-               STC_LOGI("Sim not found");
-               return;
+               STC_LOGI("Sim not found"); //LCOV_EXCL_LINE
+               return; //LCOV_EXCL_LINE
        }
 
        message = stc_manager_gdbus_call_sync(connection,
@@ -137,8 +157,8 @@ static void __telephony_update_default_modem_imsi(GDBusConnection *connection)
                                              TELEPHONY_GET_MODEMS,
                                              NULL);
        if (message == NULL) {
-               STC_LOGE("Failed to get services informations");
-               return;
+               STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE
+               return; //LCOV_EXCL_LINE
        }
 
        g_variant_get(message, "(as)", &iter);
@@ -150,10 +170,10 @@ static void __telephony_update_default_modem_imsi(GDBusConnection *connection)
                        FREE(modem_name);
                        break;
                }
-               current_sim--;
+               current_sim--; //LCOV_EXCL_LINE
        }
 
-       __telephony_get_modem_imsi(connection, default_modem_name);
+       __telephony_get_modem_subscriber_id(connection, default_modem_name);
 
        FREE(default_modem_name);
        g_variant_iter_free(iter);
@@ -169,16 +189,28 @@ static void __print_default_connection_info(void)
        STC_LOGI("ifname  [%s]", g_default_connection.ifname);
        STC_LOGI("roaming [%u]", g_default_connection.roaming ? TRUE : FALSE);
        if (g_default_connection.type == STC_IFACE_DATACALL)
-               STC_LOGI("imsi    [%s]", g_default_connection.imsi);
+               STC_LOGI("sub_id  [%s]", g_default_connection.subscriber_id);
        STC_LOGI("==================================================");
 }
 
+static void __print_tether_connection_info(void)
+{
+       STC_LOGI("============= tethering connection info ============");
+       STC_LOGI("mode    [%u]", g_default_connection.tether_state ? TRUE : FALSE);
+       STC_LOGI("type    [%d]", g_default_connection.tether_iface.type);
+       STC_LOGI("ifname  [%s]", g_default_connection.tether_iface.ifname);
+       STC_LOGI("====================================================");
+}
+
 static void __reset_default_connection_data(void)
 {
        FREE(g_default_connection.path);
        FREE(g_default_connection.ifname);
+       FREE(g_default_connection.tether_iface.ifname);
        g_default_connection.type = STC_IFACE_UNKNOWN;
        g_default_connection.roaming = FALSE;
+       g_default_connection.tether_iface.type = STC_IFACE_UNKNOWN;
+       g_default_connection.tether_state = FALSE;
 }
 
 static gboolean __is_cellular_internet_profile(const char *profile)
@@ -203,7 +235,7 @@ static gboolean __is_cellular_profile(const char *profile)
        if (profile == NULL)
                return FALSE;
 
-       return g_str_has_prefix(profile,
+       return g_str_has_prefix(profile, //LCOV_EXCL_LINE
                                CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX);
 }
 
@@ -212,7 +244,8 @@ static gboolean __is_wifi_profile(const char *profile)
        if (profile == NULL)
                return FALSE;
 
-       return g_str_has_prefix(profile, CONNMAN_WIFI_SERVICE_PROFILE_PREFIX);
+       return g_str_has_prefix(profile, //LCOV_EXCL_LINE
+                               CONNMAN_WIFI_SERVICE_PROFILE_PREFIX);
 }
 
 static gboolean __is_ethernet_profile(const char *profile)
@@ -220,7 +253,7 @@ static gboolean __is_ethernet_profile(const char *profile)
        if (profile == NULL)
                return FALSE;
 
-       return g_str_has_prefix(profile,
+       return g_str_has_prefix(profile, //LCOV_EXCL_LINE
                                CONNMAN_ETHERNET_SERVICE_PROFILE_PREFIX);
 }
 
@@ -229,7 +262,7 @@ static gboolean __is_bluetooth_profile(const char *profile)
        if (profile == NULL)
                return FALSE;
 
-       return g_str_has_prefix(profile,
+       return g_str_has_prefix(profile, //LCOV_EXCL_LINE
                                CONNMAN_BLUETOOTH_SERVICE_PROFILE_PREFIX);
 }
 
@@ -279,14 +312,14 @@ static void __get_default_connection_info(GDBusConnection *connection,
                                              CONNMAN_SERVICE_INTERFACE,
                                              "GetProperties", NULL);
        if (message == NULL) {
-               STC_LOGE("Failed to get services informations");
-               goto done;
+               STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE
+               goto done; //LCOV_EXCL_LINE
        }
 
        g_variant_get(message, "(a{sv})", &iter);
        if (iter == NULL) {
-               STC_LOGE("Profile %s doesn't exist", object_path);
-               goto done;
+               STC_LOGE("Profile %s doesn't exist", object_path); //LCOV_EXCL_LINE
+               goto done; //LCOV_EXCL_LINE
        }
 
        while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
@@ -297,7 +330,7 @@ static void __get_default_connection_info(GDBusConnection *connection,
 
                        g_variant_get(variant, "a{sv}", &iter1);
                        if (iter1 == NULL)
-                               continue;
+                               continue; //LCOV_EXCL_LINE
 
                        while (g_variant_iter_loop(iter1, "{sv}", &key1,
                                                   &variant1)) {
@@ -346,14 +379,14 @@ static stc_error_e __get_default_profile(GDBusConnection *connection)
                                              CONNMAN_MANAGER_INTERFACE,
                                              "GetServices", NULL);
        if (message == NULL) {
-               STC_LOGE("Failed to get profiles");
-               return STC_ERROR_FAIL;
+               STC_LOGE("Failed to get profiles"); //LCOV_EXCL_LINE
+               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
        }
 
        g_variant_get(message, "(a(oa{sv}))", &iter);
        while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
                if (object_path == NULL)
-                       continue;
+                       continue; //LCOV_EXCL_LINE
 
                if (__is_cellular_profile(object_path) &&
                    !__is_cellular_internet_profile(object_path))
@@ -377,23 +410,24 @@ static stc_error_e __get_default_profile(GDBusConnection *connection)
        g_variant_unref(message);
 
        if (__is_cellular_profile(g_default_connection.path)) {
-               g_default_connection.type = STC_IFACE_DATACALL;
-               __telephony_update_default_modem_imsi(connection);
+               g_default_connection.type = STC_IFACE_DATACALL; //LCOV_EXCL_LINE
+               __telephony_update_default_modem_subscriber_id(connection); //LCOV_EXCL_LINE
        } else if (__is_wifi_profile(g_default_connection.path)) {
-               g_default_connection.type = STC_IFACE_WIFI;
+               g_default_connection.type = STC_IFACE_WIFI; //LCOV_EXCL_LINE
        } else if (__is_ethernet_profile(g_default_connection.path)) {
-               g_default_connection.type = STC_IFACE_WIRED;
+               g_default_connection.type = STC_IFACE_WIRED; //LCOV_EXCL_LINE
        } else if (__is_bluetooth_profile(g_default_connection.path)) {
-               g_default_connection.type = STC_IFACE_BLUETOOTH;
+               g_default_connection.type = STC_IFACE_BLUETOOTH; //LCOV_EXCL_LINE
        } else {
-               g_default_connection.type = STC_IFACE_UNKNOWN;
+               g_default_connection.type = STC_IFACE_UNKNOWN; //LCOV_EXCL_LINE
        }
 
        __get_default_connection_info(connection, g_default_connection.path);
 
        __print_default_connection_info();
 
-       stc_monitor_update_rstn_by_default_connection(&g_default_connection);
+       stc_monitor_update_by_default_connection(&g_default_connection);
+       stc_firewall_update();
 
        return STC_ERROR_NONE;
 }
@@ -431,11 +465,12 @@ static void _service_signal_cb(GDBusConnection *conn,
                        }
                } else {
                        if (g_strcmp0(g_default_connection.path, path) == 0) {
-                               __reset_default_connection_data();
-                               __get_default_profile(stc->connection);
+                               __reset_default_connection_data(); //LCOV_EXCL_LINE
+                               __get_default_profile(stc->connection); //LCOV_EXCL_LINE
                        }
                }
        } else if (g_strcmp0(sigvalue, "Roaming") == 0) {
+               //LCOV_EXCL_START
                if (g_strcmp0(g_default_connection.path, path) == 0) {
                        gboolean roaming = 0;
 
@@ -445,6 +480,7 @@ static void _service_signal_cb(GDBusConnection *conn,
                                g_default_connection.roaming = roaming;
                        }
                }
+               //LCOV_EXCL_STOP
        } else {
                ;//Do nothing
        }
@@ -458,8 +494,80 @@ done:
        return;
 }
 
+static void __vconf_key_callback(keynode_t *node, void *user_data)
+{
+       int vconf_key;
+
+       if (node == NULL) {
+               STC_LOGE("Invalid parameter");
+               return;
+       }
+
+       if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
+               STC_LOGE("Invalid vconf key type");
+               return;
+       }
+
+       vconf_key = vconf_keynode_get_int(node);
+
+       /* Check the tethering type */
+       switch (vconf_key) {
+       case VCONFKEY_MOBILE_HOTSPOT_MODE_USB:
+               STC_LOGI("Hotspot mode USB type !");
+               g_default_connection.tether_state = TRUE;
+               g_default_connection.tether_iface.ifname = g_strdup(TETHERING_USB_IF);
+               g_default_connection.tether_iface.type = STC_IFACE_USB;
+               break;
+       case VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI:
+               STC_LOGI("Hotspot mode Wi-Fi type !");
+               g_default_connection.tether_state = TRUE;
+               g_default_connection.tether_iface.ifname = g_strdup(TETHERING_WIFI_IF);
+               g_default_connection.tether_iface.type = STC_IFACE_WIFI;
+               break;
+       case VCONFKEY_MOBILE_HOTSPOT_MODE_BT:
+               STC_LOGI("Hotspot mode Bluetooth type !");
+               g_default_connection.tether_state = TRUE;
+               g_default_connection.tether_iface.ifname = g_strdup(TETHERING_BT_IF);
+               g_default_connection.tether_iface.type = STC_IFACE_BLUETOOTH;
+               break;
+       case VCONFKEY_MOBILE_HOTSPOT_MODE_P2P:
+               STC_LOGI("Hotspot mode P2P type !");
+               g_default_connection.tether_state = TRUE;
+               g_default_connection.tether_iface.ifname = g_strdup(TETHERING_P2P_IF);
+               g_default_connection.tether_iface.type = STC_IFACE_P2P;
+               break;
+       case VCONFKEY_MOBILE_HOTSPOT_MODE_NONE:
+               STC_LOGI("Hotspot mode none");
+               g_default_connection.tether_state = FALSE;
+               break;
+       default:
+               STC_LOGE("Unknown Hotspot mode type !");
+               break;
+       }
+
+       /* add monitoring for tethering if active found */
+       if (g_default_connection.tether_state == TRUE && g_default_connection.tether_iface.ifname) {
+               __print_tether_connection_info();
+               stc_monitor_update_by_default_connection(&g_default_connection);
+               stc_firewall_update();
+               STC_LOGI("Data monitoring started for tethering iface !");
+               return;
+       }
+
+       /* remove monitoring for tethering if in-active found */
+       if (g_default_connection.tether_state == FALSE && g_default_connection.tether_iface.ifname) {
+               stc_monitor_update_by_default_connection(&g_default_connection);
+               g_free(g_default_connection.tether_iface.ifname);
+               g_default_connection.tether_iface.ifname = NULL;
+               g_default_connection.tether_iface.type = STC_IFACE_UNKNOWN;
+               STC_LOGI("Data monitoring stopped for tethering iface !");
+               return;
+       }
+}
+
 stc_error_e stc_default_connection_monitor_init(stc_s *stc)
 {
+       int ret;
        ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data");
 
        __get_default_profile(stc->connection);
@@ -473,6 +581,10 @@ stc_error_e stc_default_connection_monitor_init(stc_s *stc)
                                                   _service_signal_cb,
                                                   NULL, NULL);
 
+       ret = vconf_notify_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE, __vconf_key_callback, NULL);
+       if (ret < 0)
+               STC_LOGE("vconf_notify_key_changed failed: %d", ret);
+
        STC_LOGI("Successfully subscribed connman [%s] signal", CONNMAN_SIGNAL_PROPERTY_CHANGED);
        return STC_ERROR_NONE;
 }