Added default sim's IMSI fetch logic. 68/139668/3
authorNishant Chaprana <n.chaprana@samsung.com>
Thu, 20 Jul 2017 05:12:45 +0000 (10:42 +0530)
committerNishant Chaprana <n.chaprana@samsung.com>
Thu, 20 Jul 2017 05:31:15 +0000 (11:01 +0530)
Change-Id: I9040fb2bec433e828c4289e3ce7841a901989dc8
Signed-off-by: Nishant Chaprana <n.chaprana@samsung.com>
packaging/stc-manager.spec
src/monitor/include/stc-default-connection.h
src/monitor/stc-default-connection.c
src/monitor/stc-monitor.c

index e82aec1..4c005ca 100644 (file)
@@ -1,6 +1,6 @@
 Name:       stc-manager
 Summary:    STC(Smart Traffic Control) manager
-Version:    0.0.23
+Version:    0.0.24
 Release:    0
 Group:      Network & Connectivity/Other
 License:    Apache-2.0
index ec5b2dc..861e58d 100755 (executable)
 #include "stc-manager-gdbus.h"
 #include "stc-manager-util.h"
 
+#define IMSI_LENGTH 16
+
 /**
  * @brief default connection information will be fetched from net-config
  */
 typedef struct {
-       gchar *path; /* to identify each connection uniquely */
+       /* to identify each connection uniquely */
+       gchar *path;
 
        /* profile info */
        stc_iface_type_e type;
        gchar *ifname;
-       gboolean roaming; /* cellular profile only else it is always false */
+
+       /* cellular profile only else it is always false */
+       gboolean roaming;
+
+       /* only present when default profile is cellular */
+       char imsi[IMSI_LENGTH];
+
+       /* hardware network protocol type */
+       stc_hw_net_protocol_type_e hw_net_protocol_type;
 } default_connection_s;
 
 stc_error_e stc_default_connection_monitor_init(stc_s *stc);
 stc_error_e stc_default_connection_monitor_deinit(stc_s *stc);
 
-stc_iface_type_e stc_default_connection_get_type(void);
 gchar *stc_default_connection_get_ifname(void);
-gboolean stc_default_connection_get_roaming(void);
 default_connection_s *stc_get_default_connection(void);
 
 #endif /* __STC_DEFAULT_CONNECTION_H__ */
index ad9b74f..8b32a3e 100755 (executable)
  * limitations under the License.
  */
 
+#include <vconf/vconf.h>
+
 #include "stc-monitor.h"
 #include "stc-default-connection.h"
 
+/* connman service dbus details */
 #define CONNMAN_SERVICE                          "net.connman"
 #define CONNMAN_PATH                             "/net/connman"
 
 
 #define CONNMAN_SIGNAL_PROPERTY_CHANGED          "PropertyChanged"
 
+/* telephony service dbus details */
+#define TELEPHONY_SERVICE                        "org.tizen.telephony"
+#define TELEPHONY_DEFAULT_PATH                   "/org/tizen/telephony"
+
+#define TELEPHONY_SERVICE_MANAGER                TELEPHONY_SERVICE".Manager"
+#define TELEPHONY_SIM_INTERFACE                  TELEPHONY_SERVICE".Sim"
+
+#define TELEPHONY_GET_MODEMS                     "GetModems"
+#define TELEPHONY_GET_IMSI                       "GetIMSI"
+
+#define SIM_SLOT_SINGLE 1
+
+#define VCONF_TELEPHONY_DEFAULT_DATA_SERVICE     "db/telephony/dualsim/default_data_service"
+
 default_connection_s g_default_connection;
 guint g_default_connection_sub_id = 0;
 
+static int __telephony_get_current_sim(void)
+{
+       int sim_slot_count = 0;
+       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;
+       }
+
+       if (sim_slot_count == SIM_SLOT_SINGLE) {
+              STC_LOGD("It's single sim model");
+              return current_sim;
+       }
+
+       if (vconf_get_int(VCONF_TELEPHONY_DEFAULT_DATA_SERVICE, &current_sim) != 0) {
+               STC_LOGD("failed to get default data service = %d\n",
+                        current_sim);
+               return -1;
+       }
+
+       return current_sim;
+}
+
+static void __telephony_get_modem_imsi(GDBusConnection *connection,
+                                      const char *default_modem_name)
+{
+       GVariant *message = NULL;
+       char tel_path[MAX_PATH_LENGTH];
+       const char *plmn = NULL;
+       int plmn_len = 0;
+       const char *msin = NULL;
+       int msin_len = 0;
+
+       snprintf(tel_path, sizeof(tel_path), "%s/%s", TELEPHONY_DEFAULT_PATH,
+                default_modem_name);
+       message = stc_manager_gdbus_call_sync(connection,
+                                             TELEPHONY_SERVICE,
+                                             tel_path,
+                                             TELEPHONY_SIM_INTERFACE,
+                                             TELEPHONY_GET_IMSI,
+                                             NULL);
+       if (message == NULL) {
+               STC_LOGE("Failed to get services informations");
+               goto done;
+       }
+
+       DEBUG_PARAMS(message);
+       DEBUG_PARAM_TYPE(message);
+       g_variant_get(message, "(&s&s)", &plmn, &msin);
+       plmn_len = strlen(plmn);
+       msin_len = strlen(msin);
+
+       if (msin_len + plmn_len >= IMSI_LENGTH) {
+               STC_LOGD("Incorrect length of mobile subscriber identifier + net id");
+               goto done;
+       }
+
+       snprintf(g_default_connection.imsi, IMSI_LENGTH, "%s%s", plmn, msin);
+
+done:
+       g_variant_unref(message);
+       return;
+}
+
+static void __telephony_update_default_modem_imsi(GDBusConnection *connection)
+{
+       GVariant *message = NULL;
+       GVariantIter *iter = NULL;
+       gchar *default_modem_name = NULL;
+       gchar *modem_name = NULL;
+       int current_sim = __telephony_get_current_sim();
+
+       if (current_sim < 0) {
+               STC_LOGI("Sim not found");
+               return;
+       }
+
+       message = stc_manager_gdbus_call_sync(connection,
+                                             TELEPHONY_SERVICE,
+                                             TELEPHONY_DEFAULT_PATH,
+                                             TELEPHONY_SERVICE_MANAGER,
+                                             TELEPHONY_GET_MODEMS,
+                                             NULL);
+       if (message == NULL) {
+               STC_LOGE("Failed to get services informations");
+               return;
+       }
+
+       g_variant_get(message, "(as)", &iter);
+       DEBUG_PARAMS(message);
+       DEBUG_PARAM_TYPE(message);
+       while (g_variant_iter_loop(iter, "s", &modem_name)) {
+               if (current_sim == 0) {
+                       default_modem_name = g_strdup(modem_name);
+                       FREE(modem_name);
+                       break;
+               }
+               current_sim--;
+       }
+
+       __telephony_get_modem_imsi(connection, default_modem_name);
+
+       FREE(default_modem_name);
+       g_variant_iter_free(iter);
+       g_variant_unref(message);
+       return;
+}
+
 static void __print_default_connection_info(void)
 {
        STC_LOGI("============= default connection info ============");
@@ -41,6 +167,8 @@ static void __print_default_connection_info(void)
        STC_LOGI("type    [%d]", g_default_connection.type);
        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("==================================================");
 }
 
@@ -247,16 +375,18 @@ static stc_error_e __get_default_profile(GDBusConnection *connection)
        g_variant_iter_free(iter);
        g_variant_unref(message);
 
-       if (__is_cellular_profile(g_default_connection.path))
+       if (__is_cellular_profile(g_default_connection.path)) {
                g_default_connection.type = STC_IFACE_DATACALL;
-       else if (__is_wifi_profile(g_default_connection.path))
+               __telephony_update_default_modem_imsi(connection);
+       } else if (__is_wifi_profile(g_default_connection.path)) {
                g_default_connection.type = STC_IFACE_WIFI;
-       else if (__is_ethernet_profile(g_default_connection.path))
+       } else if (__is_ethernet_profile(g_default_connection.path)) {
                g_default_connection.type = STC_IFACE_WIRED;
-       else if (__is_bluetooth_profile(g_default_connection.path))
+       } else if (__is_bluetooth_profile(g_default_connection.path)) {
                g_default_connection.type = STC_IFACE_BLUETOOTH;
-       else
+       } else {
                g_default_connection.type = STC_IFACE_UNKNOWN;
+       }
 
        __get_default_connection_info(connection, g_default_connection.path);
 
@@ -357,21 +487,11 @@ stc_error_e stc_default_connection_monitor_deinit(stc_s *stc)
        return STC_ERROR_NONE;
 }
 
-stc_iface_type_e stc_default_connection_get_type(void)
-{
-       return g_default_connection.type;
-}
-
 gchar *stc_default_connection_get_ifname(void)
 {
        return g_strdup(g_default_connection.ifname);
 }
 
-gboolean stc_default_connection_get_roaming(void)
-{
-       return g_default_connection.roaming;
-}
-
 default_connection_s *stc_get_default_connection(void)
 {
        return &g_default_connection;
index 2bb1777..5ff6adb 100755 (executable)
@@ -289,6 +289,9 @@ static gboolean __add_application_monitor(gpointer key, gpointer value,
 
                if (!stc->carg) {
                        stc->carg = MALLOC0(counter_arg_s, 1);
+                       if (stc->carg == NULL)
+                               return FALSE;
+
                        stc->carg->sock = stc_monitor_get_counter_socket();
                }
 
@@ -319,6 +322,9 @@ static gboolean __remove_application_monitor(gpointer key, gpointer value,
 
                if (!stc->carg) {
                        stc->carg = MALLOC0(counter_arg_s, 1);
+                       if (stc->carg == NULL)
+                               return FALSE;
+
                        stc->carg->sock = stc_monitor_get_counter_socket();
                }
 
@@ -419,6 +425,11 @@ static void __process_restriction(enum traffic_restriction_type rst_type,
 
                        if (!stc->carg) {
                                stc->carg = MALLOC0(counter_arg_s, 1);
+                               if (stc->carg == NULL) {
+                                       g_free(default_ifname);
+                                       return;
+                               }
+
                                stc->carg->sock =
                                        stc_monitor_get_counter_socket();
                        }
@@ -452,6 +463,11 @@ static void __process_restriction(enum traffic_restriction_type rst_type,
 
                        if (!stc->carg) {
                                stc->carg = MALLOC0(counter_arg_s, 1);
+                               if (stc->carg == NULL) {
+                                       g_free(default_ifname);
+                                       return;
+                               }
+
                                stc->carg->sock =
                                        stc_monitor_get_counter_socket();
                        }
@@ -737,23 +753,26 @@ static gboolean __update_app_statistics(gpointer key, gpointer value,
        time_t *touch_time = (time_t *)data;
        stc_db_classid_iftype_key stat_key;
        stc_db_app_stats stat;
-       char *default_ifname = stc_default_connection_get_ifname();
+       default_connection_s *default_connection = stc_get_default_connection();
 
        memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
        memset(&stat, 0 , sizeof(stc_db_app_stats));
 
        stat_key.classid = app_value->classid;
-       stat_key.iftype = stc_default_connection_get_type();
+       stat_key.iftype = default_connection->type;
+
        if (STC_IFACE_DATACALL == stat_key.iftype)
-               stat_key.imsi = g_strdup("unknown");
+               stat_key.imsi = g_strdup(default_connection->imsi);
        else
                stat_key.imsi = g_strdup("noneimsi");
-       g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
+
+       g_strlcpy(stat_key.ifname, default_connection->ifname,
+                 MAX_IFACE_LENGTH);
 
        stat.app_id = g_strdup(app_key->app_id);
        stat.snd_count = app_value->counter.out_bytes;
        stat.rcv_count = app_value->counter.in_bytes;
-       stat.is_roaming = stc_default_connection_get_roaming();
+       stat.is_roaming = default_connection->roaming;
        stat.ground = STC_APP_STATE_UNKNOWN;
 
        table_statistics_insert(&stat_key, &stat, *touch_time);
@@ -763,7 +782,6 @@ static gboolean __update_app_statistics(gpointer key, gpointer value,
 
        FREE(stat.app_id);
        FREE(stat_key.imsi);
-       FREE(default_ifname);
 
        return FALSE;
 }
@@ -1020,6 +1038,9 @@ static gboolean __update_contr_cb(void *user_data)
        ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
        if (!stc->carg) {
                stc->carg = MALLOC0(counter_arg_s, 1);
+               if (stc->carg == NULL)
+                       return TRUE;  /* we need to continue the timer */
+
                stc->carg->sock = stc_monitor_get_counter_socket();
        }
 
@@ -1323,11 +1344,11 @@ static void __set_background_state(guint state)
        g_system->background_state = state;
 }
 
-static gboolean __processes_tree_foreach_background(gpointer key, gpointer value,
-                                              gpointer data)
+static gboolean __processes_tree_foreach_background(gpointer key,
+                                                   gpointer value,
+                                                   gpointer data)
 {
        stc_process_key_s *proc_key = (stc_process_key_s *)key;
-       stc_process_value_s *proc_value = (stc_process_value_s *)value;
        stc_app_key_s *app_key = (stc_app_key_s *)data;
 
        if (g_system->background_state)
@@ -1345,7 +1366,8 @@ static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
        stc_app_value_s *app_value = (stc_app_value_s *)value;
 
        if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
-               g_tree_foreach(app_value->processes, __processes_tree_foreach_background, app_key);
+               g_tree_foreach(app_value->processes,
+                              __processes_tree_foreach_background, app_key);
 
        return FALSE;
 }
@@ -1412,7 +1434,8 @@ stc_error_e stc_monitor_init(void)
                return STC_ERROR_FAIL;
        }
 
-       __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE, &g_system->background_state);
+       __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
+                       (int *)&g_system->background_state);
 
        __fill_restritions_list();