Fix a memory leak
[platform/core/connectivity/stc-manager.git] / plugin / tether / stc-plugin-tether.c
old mode 100644 (file)
new mode 100755 (executable)
index 629abbf..f1278f9
@@ -31,13 +31,14 @@ static GDBusConnection *connection = NULL;
 static GCancellable *cancellable = NULL;
 static int g_mobileap_signal_sub_id = 0;
 
-static stc_error_e add_station_monitor(gchar *pkg_id, gchar *app_id, const char *ip)
+static stc_error_e add_station_monitor(gchar *pkg_id, gchar *app_id,
+                        const char *mac)
 {
        int ret;
        stc_app_key_s app_key;
        stc_app_value_s app_value;
 
-       if (pkg_id == NULL || app_id == NULL || ip == NULL) {
+       if (pkg_id == NULL || app_id == NULL || mac == NULL) {
                STC_LOGE("invalid station station info");
                return STC_ERROR_INVALID_PARAMETER;
        }
@@ -48,7 +49,7 @@ static stc_error_e add_station_monitor(gchar *pkg_id, gchar *app_id, const char
        app_key.app_id = g_strconcat(app_id, STC_TETHERING_APP_SUFFIX, NULL);
        app_value.type = STC_APP_TYPE_TETHERING;
        app_value.processes = NULL;
-       g_strlcpy(app_value.ipaddr, ip, IPV4_IPADDRESS_LEN);
+       g_strlcpy(app_value.mac, mac, STATION_MAC_STR_LEN);
 
        ret = stc_monitor_application_add(app_key, app_value);
        FREE(app_key.pkg_id);
@@ -83,6 +84,14 @@ static int _compare_sta_by_mac_func(gconstpointer a,
        return g_ascii_strcasecmp(si->mac, (const char *)b);
 }
 
+static int _compare_sta_by_classid_func(gconstpointer a,
+               gconstpointer b)
+{
+       tether_sta_info_s *si = (tether_sta_info_s *)a;
+       int *classid = (int *)b;
+       return si->classid - *classid;
+}
+
 static int _get_station_info(gconstpointer data, GCompareFunc func,
                tether_sta_info_s **si)
 {
@@ -142,7 +151,7 @@ static void _add_station_info(tether_sta_info_s *info)
                if (!g_strcmp0(tmp->name, info->name) && !g_strcmp0(tmp->ip, info->ip))
                        return;
 
-               //Remove the station if dhcp info changed.
+               /* Remove the station if dhcp info changed. */
                _remove_station_info(info->mac, _compare_sta_by_mac_func);
        }
 
@@ -151,7 +160,7 @@ static void _add_station_info(tether_sta_info_s *info)
 
        /* add tethering client for monitoring data usage */
        info->station_id = g_strdup_printf("%s_%s", info->mac, info->name);
-       add_station_monitor(info->mac, info->station_id, info->ip);
+       add_station_monitor(info->mac, info->station_id, info->mac);
 }
 
 static void _mobileap_signal_cb(GDBusConnection *conn,
@@ -172,18 +181,18 @@ static void _mobileap_signal_cb(GDBusConnection *conn,
 
        STC_LOGI("%s interface(%s)", sig, interface);
 
-       sta = (tether_sta_info_s *)g_malloc0(sizeof(tether_sta_info_s));
-       if (sta == NULL) {
-               STC_LOGE("g_malloc0 failed");
-               return;
-       }
-
        g_variant_get(param, "(susssu)", &state, &type, &ip, &mac, &hostname, &tm);
        STC_LOGI("%s: ip(%s) mac(%s) name(%s) tm(%d)", state, ip, mac, hostname, tm);
 
        if (!g_strcmp0(state, "DhcpConnected")) {
-               g_strlcpy(sta->ip, ip, STATION_STR_INFO_LEN);
-               g_strlcpy(sta->mac, mac, STATION_STR_INFO_LEN);
+               sta = (tether_sta_info_s *)g_malloc0(sizeof(tether_sta_info_s));
+               if (sta == NULL) {
+                       STC_LOGE("g_malloc0 failed");
+                       return;
+               }
+
+               g_strlcpy(sta->ip, ip, INET_ADDRSTRLEN);
+               g_strlcpy(sta->mac, mac, STATION_MAC_STR_LEN);
                g_strlcpy(sta->name, hostname, STATION_STR_HOSTNAME_LEN);
                _add_station_info(sta);
        } else if (!g_strcmp0(state, "DhcpLeaseDeleted")) {
@@ -196,8 +205,59 @@ static void _mobileap_signal_cb(GDBusConnection *conn,
        g_free(hostname);
 }
 
-stc_error_e tether_plugin_status_changed(void)
+stc_error_e tether_plugin_get_station_ip(const char *mac, char *ip)
 {
+       tether_sta_info_s *tmp = NULL;
+
+       if (mac == NULL || ip == NULL)
+               return STC_ERROR_FAIL;
+
+       if (_get_station_info((gconstpointer)mac,
+                               _compare_sta_by_mac_func, &tmp) != 0) {
+               STC_LOGE("mac(%s) not found", mac);
+               return STC_ERROR_FAIL;
+       }
+
+       g_strlcpy(ip, tmp->ip, INET_ADDRSTRLEN);
+       return STC_ERROR_NONE;
+}
+
+stc_error_e tether_plugin_get_station_by_classid(const int classid, char *mac)
+{
+       tether_sta_info_s *tmp = NULL;
+       int classid_value = classid;
+
+       if (mac == NULL)
+               return STC_ERROR_FAIL;
+
+       if (_get_station_info((gconstpointer)&classid_value,
+                               _compare_sta_by_classid_func, &tmp) != 0) {
+               STC_LOGE("classid(%s) not found", classid);
+               return STC_ERROR_FAIL;
+       }
+
+       g_strlcpy(mac, tmp->mac, STATION_MAC_STR_LEN);
+       return STC_ERROR_NONE;
+}
+
+stc_error_e tether_plugin_set_station_classid(const char *mac, int classid)
+{
+       tether_sta_info_s *tmp = NULL;
+
+       if (mac == NULL) {
+               STC_LOGE("invalid param");
+               return STC_ERROR_FAIL;
+       }
+
+       if (_get_station_info((gconstpointer)mac,
+                               _compare_sta_by_mac_func, &tmp) != 0) {
+               STC_LOGE("mac(%s) not found", mac);
+               return STC_ERROR_FAIL;
+       }
+
+       if (tmp)
+               tmp->classid = classid;
+
        return STC_ERROR_NONE;
 }
 
@@ -244,5 +304,7 @@ void tether_plugin_deinit(void)
 API stc_plugin_tether_s tether_plugin = {
        .init = tether_plugin_init,
        .deinit = tether_plugin_deinit,
-       .status_changed = tether_plugin_status_changed
+       .get_station_ip = tether_plugin_get_station_ip,
+       .get_station_by_classid = tether_plugin_get_station_by_classid,
+       .set_station_classid = tether_plugin_set_station_classid
 };