Revert: [TNEXT-136] Check model name for wlan driver
[platform/core/connectivity/net-config.git] / src / utils / util.c
index 8dc93c2..6efd972 100755 (executable)
  *
  */
 
-#if defined TIZEN_WEARABLE
-#include <aul.h>
-#endif
-#include <app.h>
+#include <dlfcn.h>
 #include <errno.h>
 #include <vconf.h>
 #include <stdio.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <vconf-keys.h>
-#include <syspopup_caller.h>
-#include <bundle.h>
-#include <bundle_internal.h>
-#include <eventsystem.h>
+#include <tzplatform_config.h>
+#include <system_info.h>
 
 #include "log.h"
 #include "util.h"
 #include "neterror.h"
 #include "wifi-state.h"
+#include "netdbus.h"
 
-#define WC_POPUP_EXTRA_DATA_KEY        "http://tizen.org/appcontrol/data/connection_type"
-#define MAC_INFO_FILEPATH              "/opt/etc/.mac.info"
+#define DBUS_SERVICE_DBUS              "org.freedesktop.DBus"
+#define DBUS_INTERFACE_DBUS            "org.freedesktop.DBus"
+#define MAC_INFO_FILEPATH              tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
+#define MAC_ADDRESS_FILEPATH   "/sys/class/net/wlan0/address"
 #define MAC_ADDRESS_MAX_LEN            18
+#define HEADED_PLUGIN_FILEPATH         "/usr/lib/net-config-plugin-headed.so"
 
 static gboolean netconfig_device_picker_test = FALSE;
+static int mdnsd_ref_count = 0;
+typedef struct {
+       char *conn_name;
+       int conn_id;
+} dnssd_conn_destroy_data;
+
+static gboolean netconfig_plugin_headed_enabled = FALSE;
+static void *handle_headed;
+static struct netconfig_headed_plugin_t *headed_plugin;
 
 GKeyFile *netconfig_keyfile_load(const char *pathname)
 {
@@ -64,6 +72,7 @@ GKeyFile *netconfig_keyfile_load(const char *pathname)
                keyfile = NULL;
        }
 
+       DBG("loaded keyfile %s", pathname);
        return keyfile;
 }
 
@@ -82,6 +91,7 @@ void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
 
        if (directory == NULL || (*directory) == '\0') {
                g_free(directory);
+               ERR("directory is NULL");
                return;
        }
 
@@ -89,6 +99,7 @@ void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
                if (g_mkdir_with_parents(directory,
                                S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
                        g_free(directory);
+                       ERR("failed to make directory");
                        return;
                }
        }
@@ -96,11 +107,12 @@ void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
 
        keydata = g_key_file_to_data(keyfile, &size, &error);
        if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
-               DBG("Unable to save %s, error %s", pathname, error->message);
+               ERR("Unable to save %s, error %s", pathname, error->message);
                g_error_free(error);
        }
 
        chmod(pathname, S_IRUSR | S_IWUSR);
+       DBG("Successfully saved keyfile %s", pathname);
 
        g_free(keydata);
 }
@@ -188,39 +200,13 @@ static gboolean __netconfig_test_device_picker()
 
 static void __netconfig_pop_device_picker(void)
 {
-#if defined TIZEN_WEARABLE
-       int ret = 0;
-       app_control_h   control = NULL;
-
-       ret = app_control_create(&control);
-       if (APP_CONTROL_ERROR_NONE != ret) {
-               DBG("failed to create app control");
-               return ;
-       }
-
-       app_control_add_extra_data(control, "viewtype", "scanlist");
-
-       app_control_set_app_id(control, "org.tizen.wifi");
-       ret = app_control_send_launch_request(control, NULL, NULL);
-       if (APP_CONTROL_ERROR_NONE == ret)
-               DBG("Launch request sent successfully");
-
-       app_control_destroy(control);
-#else
-       bundle *b = NULL;
-       int wifi_ug_state = 0;
-
-       vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
-       if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
+       if (!netconfig_plugin_headed_enabled)
                return;
 
-       b = bundle_create();
-
-       DBG("Launch Wi-Fi device picker");
-       syspopup_launch("wifi-qs", b);
+       if (!headed_plugin)
+               return;
 
-       bundle_free(b);
-#endif
+       headed_plugin->pop_device_picker();
 }
 
 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
@@ -269,18 +255,11 @@ void netconfig_wifi_device_picker_service_start(void)
        else
                return;
 
-#if defined TIZEN_WEARABLE
-       if (aul_app_is_running("org.tizen.wifi") > 0) {
-               DBG("wifi app is running");
-               return;
-       }
-#else
        int wifi_ug_state;
 
-       vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
+       netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
        if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
                return;
-#endif
 
        DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
        netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
@@ -308,7 +287,7 @@ gboolean netconfig_is_wifi_direct_on(void)
 #if defined TIZEN_P2P_ENABLE
        int wifi_direct_state = 0;
 
-       vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
+       netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
 
        DBG("Wi-Fi direct mode %d", wifi_direct_state);
        return (wifi_direct_state != 0) ? TRUE : FALSE;
@@ -322,12 +301,15 @@ gboolean netconfig_is_wifi_tethering_on(void)
 #if defined TIZEN_TETHERING_ENABLE
        int wifi_tethering_state = 0;
 
-       vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
-
+       netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
        DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
-       if (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
+       if ((wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
+               || (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP)) {
+               DBG("Mobile AP is on");
                return TRUE;
+       }
 #endif
+       DBG("Mobile AP is off");
        return FALSE;
 }
 
@@ -356,7 +338,7 @@ gboolean netconfig_interface_up(const char *ifname)
 
        close(fd);
 
-       DBG("Successfully activated wireless interface");
+       DBG("Successfully activated wireless interface %s", ifname);
        return TRUE;
 }
 
@@ -385,7 +367,7 @@ gboolean netconfig_interface_down(const char *ifname)
 
        close(fd);
 
-       DBG("Successfully de-activated wireless interface");
+       DBG("Successfully de-activated wireless interface %s", ifname);
        return TRUE;
 }
 
@@ -763,6 +745,7 @@ gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
                netconfig_error_wifi_direct_failed(context);
                return FALSE;
        }
+
        wifi_complete_launch_direct(wifi, context);
        return TRUE;
 #else
@@ -771,171 +754,146 @@ gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
 #endif
 }
 
-gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
+int execute_mdnsd_script(char* op)
 {
-       int ret = 0;
-       bundle *b;
-       static gboolean is_found_noti_exists = FALSE;
-       static gboolean is_portal_noti_exists = FALSE;
-
-       if (noti == NULL) {
-               ERR("Invalid notification");
-               return FALSE;
-       }
-
-       if (g_strcmp0(noti, NETCONFIG_DEL_FOUND_AP_NOTI) == 0) {
-               if (is_found_noti_exists == FALSE)
-                       return TRUE;
+       const char *path = "/usr/bin/mdnsresponder-server.sh";
+       char *const args[] = { "mdnsresponder-server.sh", op, NULL };
+       char *const envs[] = { NULL };
 
-               is_found_noti_exists = FALSE;
-       } else if (g_strcmp0(noti, NETCONFIG_ADD_FOUND_AP_NOTI) == 0) {
-               if (is_found_noti_exists == TRUE)
-                       return TRUE;
+       return netconfig_execute_file(path, args, envs);
+}
 
-               is_found_noti_exists = TRUE;
-       } else if (g_strcmp0(noti, NETCONFIG_ADD_PORTAL_NOTI) == 0) {
-               if (is_portal_noti_exists == TRUE)
-                       return TRUE;
+static void __dnssd_conn_destroyed_cb(GDBusConnection *conn,
+               const gchar *Name, const gchar *path, const gchar *interface,
+               const gchar *sig, GVariant *param, gpointer user_data)
+{
+       gchar *name = NULL;
+       gchar *old = NULL;
+       gchar *new = NULL;
+       dnssd_conn_destroy_data *data = user_data;
+       GDBusConnection *connection = NULL;
+       connection = netdbus_get_connection();
+
+       if (param == NULL)
+               return;
 
-               is_portal_noti_exists = TRUE;
-       } else if (g_strcmp0(noti, NETCONFIG_DEL_PORTAL_NOTI) == 0) {
-               if (is_portal_noti_exists == FALSE)
-                       return TRUE;
+       g_variant_get(param, "(sss)", &name, &old, &new);
 
-               is_portal_noti_exists = FALSE;
+       if (g_strcmp0(name, data->conn_name) == 0 && *new == '\0') {
+               DBG("Connection %s Destroyed: name %s id %d", data->conn_name, name,
+                       data->conn_id);
+               mdnsd_ref_count--;
+               g_dbus_connection_signal_unsubscribe(connection, data->conn_id);
+               if (mdnsd_ref_count == 0) {
+                       if (execute_mdnsd_script("stop") < 0)
+                               ERR("Failed to stop mdnsresponder daemon");
+               }
        }
+       g_free(name);
+       g_free(old);
+       g_free(new);
+       g_free(data->conn_name);
+       g_free(data);
+       return;
+}
 
-       b = bundle_create();
-       bundle_add(b, "_SYSPOPUP_TYPE_", noti);
+static void register_dnssd_conn_destroy_signal(gchar *name)
+{
+       dnssd_conn_destroy_data *data;
+       GDBusConnection *connection = NULL;
+       connection = netdbus_get_connection();
 
-       if (ssid != NULL) {
-               DBG("ssid (%s)", ssid);
-               bundle_add(b, "_AP_NAME_", ssid);
+       if (connection == NULL) {
+               ERR("Failed to get GDbus Connection");
+               return;
        }
 
-       ret = syspopup_launch("net.netpopup", b);
+       data = g_try_malloc0(sizeof(dnssd_conn_destroy_data));
+       data->conn_name = g_strdup(name);
 
-       bundle_free(b);
+       data->conn_id = g_dbus_connection_signal_subscribe(connection,
+                                                       DBUS_SERVICE_DBUS, DBUS_INTERFACE_DBUS,
+                                                       "NameOwnerChanged", NULL, name,
+                                                       G_DBUS_SIGNAL_FLAGS_NONE, __dnssd_conn_destroyed_cb,
+                                                       data, NULL);
+       return;
+}
 
-       if (ret < 0) {
-               ERR("Unable to launch noti-popup. Err = %d", ret);
+gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
+                                                       gchar *name)
+{
+       DBG("Launch mdnsresponder daemon");
+
+       if (execute_mdnsd_script("start") < 0) {
+               ERR("Failed to launch mdnsresponder daemon");
+               netconfig_error_invalid_parameter(context);
                return FALSE;
        }
 
-       DBG("Successfully sent notification (%s)", noti);
+       mdnsd_ref_count++;
+       register_dnssd_conn_destroy_signal(name);
+       DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
+
+       network_complete_launch_mdns(object, context);
        return TRUE;
 }
 
-int netconfig_send_message_to_net_popup(const char *title,
-               const char *content, const char *type, const char *ssid)
+gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
 {
-       int ret = 0;
-       bundle *b = bundle_create();
-
-       bundle_add(b, "_SYSPOPUP_TITLE_", title);
-       bundle_add(b, "_SYSPOPUP_CONTENT_", content);
-       bundle_add(b, "_SYSPOPUP_TYPE_", type);
-       bundle_add(b, "_AP_NAME_", ssid);
-
-       ret = syspopup_launch("net.netpopup", b);
+       if (!netconfig_plugin_headed_enabled)
+               return FALSE;
 
-       bundle_free(b);
+       if (!headed_plugin)
+               return FALSE;
 
-       return ret;
+       return headed_plugin->send_notification_to_net_popup(noti, ssid);
 }
 
-void netconfig_set_system_event(const char * sys_evt, const char * evt_key, const char * evt_val)
+int netconfig_send_message_to_net_popup(const char *title,
+               const char *content, const char *type, const char *ssid)
 {
-       bundle *b = NULL;
+       if (!netconfig_plugin_headed_enabled)
+               return 0;
 
-       DBG("System event set [%s : %s : %s]", sys_evt, evt_key, evt_val);
+       if (!headed_plugin)
+               return 0;
 
-       b = bundle_create();
-       bundle_add_str(b, evt_key, evt_val);
-       eventsystem_send_system_event(sys_evt, b);
-       bundle_free(b);
+       return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
 }
 
-#if defined TIZEN_WEARABLE
-int wc_launch_syspopup(netconfig_wcpopup_type_e type)
+int netconfig_send_restriction_to_net_popup(const char *title,
+               const char *type, const char *restriction)
 {
-        int ret;
-        bundle* b;
-        char *ssid = NULL;
-
-        b = bundle_create();
-        if (!b) {
-                ERR("Failed to create bundle");
-                return -1;
-        }
-
-        switch (type) {
-        case WC_POPUP_TYPE_SESSION_OVERLAPPED:
-                bundle_add(b, "event-type", "wps-session-overlapped");
-                break;
-        case WC_POPUP_TYPE_WIFI_CONNECTED:
-                ssid = vconf_get_str(VCONFKEY_WIFI_CONNECTED_AP_NAME);
-                if (ssid == NULL) {
-                        ERR("Failed to get connected ap ssid");
-                        ssid = g_strdup(" ");
-                }
-                bundle_add(b, "event-type", "wifi-connected");
-                bundle_add(b, "ssid", ssid);
-                if (ssid)
-                        g_free(ssid);
-                break;
-        case WC_POPUP_TYPE_WIFI_RESTRICT:
-                               bundle_add(b, "event-type", "wifi-restrict");
-                               break;
-        default:
-                ERR("Popup is not supported[%d]", type);
-                bundle_free(b);
-                return -1;
-        }
+       if (!netconfig_plugin_headed_enabled)
+               return 0;
 
-        ret = syspopup_launch("wc-syspopup", b);
-        if (ret < 0)
-                ERR("Failed to launch syspopup");
+       if (!headed_plugin)
+               return 0;
 
-        bundle_free(b);
-
-        return ret;
+       return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
 }
 
-int wc_launch_popup(netconfig_wcpopup_type_e type)
+void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
 {
-       int ret;
-       app_control_h app_control = NULL;
+       if (!netconfig_plugin_headed_enabled)
+               return;
 
-       ret = app_control_create(&app_control);
-       if (ret != APP_CONTROL_ERROR_NONE) {
-               ERR("Failed to create appcontrol[%d]", ret);
-               return -1;
-       }
+       if (!headed_plugin)
+               return;
 
-       switch (type) {
-       case WC_POPUP_TYPE_CAPTIVE_PORTAL:
-               app_control_add_extra_data(app_control, WC_POPUP_EXTRA_DATA_KEY, "captive-portal");
-               break;
-       default:
-               ERR("Popup is not supported[%d]", type);
-               app_control_destroy(app_control);
-               return -1;
-       }
+       headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
+}
 
-       app_control_set_app_id(app_control, "com.samsung.weconn-popup");
-       ret = app_control_send_launch_request(app_control, NULL, NULL);
-       if (ret != APP_CONTROL_ERROR_NONE) {
-               DBG("failed appcontrol launch request [%d]", ret);
-               app_control_destroy(app_control);
-               return -1;
-       }
+void __netconfig_pop_wifi_connected_poppup(const char *ssid)
+{
+       if (!netconfig_plugin_headed_enabled)
+               return;
 
-       app_control_destroy(app_control);
+       if (!headed_plugin)
+               return;
 
-       return 0;
+       headed_plugin->pop_wifi_connected_poppup(ssid);
 }
-#endif
 
 void netconfig_set_vconf_int(const char * key, int value)
 {
@@ -959,16 +917,42 @@ void netconfig_set_vconf_str(const char * key, const char * value)
                ERR("Failed to set");
 }
 
+int netconfig_vconf_get_int(const char * key, int *value)
+{
+       int ret = 0;
+
+       ret = vconf_get_int(key, value);
+       if (ret != VCONF_OK) {
+               ERR("Failed to get vconfkey [%s] value", key);
+               return -1;
+       }
+
+       return 0;
+}
+
+int netconfig_vconf_get_bool(const char * key, int *value)
+{
+       int ret = 0;
+
+       ret = vconf_get_bool(key, value);
+       if (ret != VCONF_OK) {
+               ERR("Failed to get vconfkey [%s] value", key);
+               return -1;
+       }
+
+       return 0;
+}
+
 char* netconfig_get_env(const char *key)
 {
        FILE *fp;
        char buf[256], *entry = NULL, *value = NULL, *last;
-       int len=0;
+       int len = 0;
 
        if (!key)
                return NULL;
 
-       fp = fopen(NETCONFIG_TIZENMOBILEENV, "r");
+       fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
        if (!fp)
                return NULL;
 
@@ -978,12 +962,11 @@ char* netconfig_get_env(const char *key)
                if (entry) {
                        if (strstr(entry, key)) {
                                entry = strtok_r(NULL, "\n", &last);
-                               if(entry){
+                               if (entry) {
                                        len = strlen(entry);
                                        value = (char*)malloc(len+1);
                                        g_strlcpy(value, entry, len+1);
-                               }
-                               else{
+                               } else {
                                        value = (char*)malloc(sizeof(char));
                                        g_strlcpy(value, "\n", sizeof(char));
                                }
@@ -1006,9 +989,13 @@ void netconfig_set_mac_address_from_file(void)
        file = fopen(MAC_INFO_FILEPATH, "r");
        if (file == NULL) {
                ERR("Fail to open %s", MAC_INFO_FILEPATH);
-               return;
+               file = fopen(MAC_ADDRESS_FILEPATH, "r");
+               if (file == NULL) {
+                       ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
+                       return;
+               }
        }
-       if (fgets(mac_str, sizeof(mac_str), file) == NULL ) {
+       if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
                ERR("Fail to read mac address");
                fclose(file);
                return;
@@ -1027,3 +1014,69 @@ void netconfig_set_mac_address_from_file(void)
        g_free(mac_lower_str);
        fclose(file);
 }
+
+tizen_profile_t _get_tizen_profile()
+{
+       static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
+       if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
+               return profile;
+
+       char *profileName;
+       system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
+       switch (*profileName) {
+       case 'm':
+       case 'M':
+               profile = TIZEN_PROFILE_MOBILE;
+               break;
+       case 'w':
+       case 'W':
+               profile = TIZEN_PROFILE_WEARABLE;
+               break;
+       case 't':
+       case 'T':
+               profile = TIZEN_PROFILE_TV;
+               break;
+       case 'i':
+       case 'I':
+               profile = TIZEN_PROFILE_IVI;
+               break;
+       default: // common or unknown ==> ALL ARE COMMON.
+               profile = TIZEN_PROFILE_COMMON;
+       }
+       free(profileName);
+
+       return profile;
+}
+
+void netconfig_plugin_init()
+{
+       handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
+       if (!handle_headed) {
+               ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
+               return;
+       }
+
+       headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
+       if (!headed_plugin) {
+               ERR("Can't load symbol: %s", dlerror());
+               dlclose(handle_headed);
+               return;
+       }
+
+       netconfig_plugin_headed_enabled = TRUE;
+}
+
+void netconfig_plugin_deinit()
+{
+       if (!netconfig_plugin_headed_enabled)
+               return;
+
+       netconfig_plugin_headed_enabled = FALSE;
+       dlclose(handle_headed);
+}
+
+gboolean netconfig_get_headed_plugin_flag()
+{
+       return netconfig_plugin_headed_enabled;
+}
+