Merge "Added support to set and get IP configuration details." into tizen
[platform/core/connectivity/net-config.git] / src / wifi.c
index 32f3eae..d47a352 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Network Configuration Module
  *
- * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <vconf.h>
 #include <vconf-keys.h>
 #include "util.h"
 #include "netdbus.h"
 #include "neterror.h"
-#include "netconfig.h"
+#include "wifi-eap.h"
+#include "wifi-wps.h"
+#include "wifi-bssid-scan.h"
+#include "wifi-netlink-scan.h"
 #include "wifi-power.h"
 #include "wifi-state.h"
-#include "wifi-ssid-scan.h"
-#include "wifi-eap.h"
+#include "wifi-agent.h"
+#include "wifi-firmware.h"
 #include "wifi-passpoint.h"
 #include "wifi-eap-config.h"
 #include "wifi-background-scan.h"
-#include "wifi-agent.h"
-#include "wifi-firmware.h"
-
-#include "netconfig-iface-wifi-glue.h"
-
-
-#define PROP_DEFAULT           FALSE
-#define PROP_DEFAULT_STR       NULL
+#include "ip-conflict-detect.h"
+#include "wifi-config.h"
+#include "wifi-tdls.h"
+#include "wifi-key-encryption.h"
+#include "wifi-extension.h"
 
-enum {
-       PROP_O,
-       PROP_WIFI_CONN,
-       PROP_WIFI_PATH,
-};
+#define SPRD_CP2_FIRMWARE_PATH "/usr/bin/cp2-downloader"
+static int is_wifi_firmware_downloaded = FALSE;
 
-enum {
-       SIG_WIFI_DRIVER,
-       SIG_LAST
-};
-
-struct NetconfigWifiClass {
-       GObjectClass parent;
-
-       /* method and signals */
-       void (*driver_loaded) (NetconfigWifi *wifi, gchar *mac);
-};
-
-struct NetconfigWifi {
-       GObject parent;
-
-       /* member variable */
-       DBusGConnection *conn;
-       gchar *path;
-};
-
-static guint32 signals[SIG_LAST] = { 0, };
-
-G_DEFINE_TYPE(NetconfigWifi, netconfig_wifi, G_TYPE_OBJECT);
+static Wifi *wifi_object = NULL;
+static NetConnmanAgent *connman_agent_object = NULL;
+static WifiFirmware *wififirmware_object = NULL;
 
+Wifi *get_wifi_object(void){
+       return wifi_object;
+}
 
-static void __netconfig_wifi_gobject_get_property(GObject *object, guint prop_id,
-               GValue *value, GParamSpec *pspec)
+static gboolean handle_check_black_list(Wifi *wifi, GDBusMethodInvocation *context,
+               const gchar *name, const gchar *security_type, const gchar *eap)
 {
-       return;
+       ERR("Name (%s)", name);
+       INFO("disable to check");
+       wifi_complete_check_black_list(wifi, context, TRUE);
+       return TRUE;
 }
 
-static void __netconfig_wifi_gobject_set_property(GObject *object, guint prop_id,
-               const GValue *value, GParamSpec *pspec)
+static void _set_wifi_mac_address(void)
 {
-       NetconfigWifi *wifi = NETCONFIG_WIFI(object);
-
-       switch (prop_id) {
-       case PROP_WIFI_CONN:
-       {
-               wifi->conn = g_value_get_boxed(value);
-               INFO("wifi(%p) set conn(%p)", wifi, wifi->conn);
-               break;
-       }
-
-       case PROP_WIFI_PATH:
-       {
-               if (wifi->path)
-                       g_free(wifi->path);
-
-               wifi->path = g_value_dup_string(value);
-               INFO("wifi(%p) path(%s)", wifi, wifi->path);
-
-               break;
-       }
+       gchar *mac_addr = NULL;
 
-       default:
-               G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+       mac_addr = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
+       if (mac_addr != NULL) {
+               if (strlen(mac_addr) == 0)
+                       netconfig_set_mac_address_from_file();
+               free(mac_addr);
        }
 }
 
-static void netconfig_wifi_init(NetconfigWifi *wifi)
+void __netconfig_wifi_connect_reply(GObject *source_object, GAsyncResult *res,
+               gpointer user_data)
 {
-       DBG("wifi initialize");
-
-       wifi->conn = NULL;
-       wifi->path = g_strdup(PROP_DEFAULT_STR);
+       GDBusConnection *conn = NULL;
+       GError *error = NULL;
+
+       DBG("WiFi Connection Reply");
+
+       conn = G_DBUS_CONNECTION(source_object);
+       g_dbus_connection_call_finish(conn, res, &error);
+       if (error != NULL) {
+               ERR("WiFi Connection Error [%s]", error->message);
+               /* No need to emit WiFiConnectFail signal if Connection is
+                * in progress */
+               if (error->code == G_IO_ERROR_TIMED_OUT) {
+                       g_error_free(error);
+                       DBG("WiFi Connection in Progress");
+                       netconfig_gdbus_pending_call_unref();
+                       return;
+               }
+               g_error_free(error);
+               if (netconfig_dbus_emit_signal(NULL, NETCONFIG_WIFI_PATH,
+                               NETCONFIG_WIFI_INTERFACE, "WiFiConnectFail",
+                               NULL) == FALSE)
+                       ERR("Failed to emit WiFiConnectFail signal");
+               else
+                       DBG("Successfully sent WiFiConnectFail signal");
+       } else
+               DBG("WiFi Connection has been initiated successfully");
+
+       netconfig_gdbus_pending_call_unref();
+       return;
 }
 
-static void netconfig_wifi_class_init(NetconfigWifiClass *klass)
+int wifi_firmware_download(void)
 {
-       GObjectClass *object_class = G_OBJECT_CLASS(klass);
-
-       DBG("class initialize");
-
-       object_class->get_property = __netconfig_wifi_gobject_get_property;
-       object_class->set_property = __netconfig_wifi_gobject_set_property;
-
-       /* DBus register */
-       dbus_g_object_type_install_info(NETCONFIG_TYPE_WIFI,
-                       &dbus_glib_netconfig_iface_wifi_object_info);
-
-       /* property */
-       g_object_class_install_property(object_class, PROP_WIFI_CONN,
-                       g_param_spec_boxed("conn", "CONNECTION", "DBus connection",
-                               DBUS_TYPE_G_CONNECTION,
-                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
-
-       g_object_class_install_property(object_class, PROP_WIFI_PATH,
-                       g_param_spec_string("path", "PATH", "Object Path",
-                               PROP_DEFAULT_STR,
-                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
-
-       /* signal */
-       signals[SIG_WIFI_DRIVER] = g_signal_new("driver-loaded",
-                       G_OBJECT_CLASS_TYPE(klass),
-                       G_SIGNAL_RUN_LAST,
-                       G_STRUCT_OFFSET(NetconfigWifiClass,
-                               driver_loaded),
-                       NULL, NULL,
-                       g_cclosure_marshal_VOID__STRING,
-                       G_TYPE_NONE, 1, G_TYPE_STRING);
+       int rv = 0;
+       const char *path = SPRD_CP2_FIRMWARE_PATH;
+       char *const args[] = { SPRD_CP2_FIRMWARE_PATH, NULL };
+       char *const envs[] = { NULL };
+
+       if (!is_wifi_firmware_downloaded) {
+               rv = netconfig_execute_file(path, args, envs);
+               if (rv < 0) {
+                       DBG("wifi firmware download fails");
+                       return -EIO;
+               }
+               is_wifi_firmware_downloaded = TRUE;
+               DBG("wifi firmware download successes");
+       }
+
+       return 0;
 }
 
-gpointer netconfig_wifi_create_and_init(DBusGConnection *conn)
+void wifi_object_create_and_init(void)
 {
-       GObject *object;
+       DBG("Create wifi object.");
+       GDBusInterfaceSkeleton *interface_wifi = NULL;
+       GDBusInterfaceSkeleton *interface_connman_agent = NULL;
+       GDBusInterfaceSkeleton *interface_wifi_firmware = NULL;
+       GDBusConnection *connection = NULL;
+       GDBusObjectManagerServer *server = netdbus_get_wifi_manager();
+       if (server == NULL)
+               return;
+
+       connection = netdbus_get_connection();
+       g_dbus_object_manager_server_set_connection(server, connection);
+
+       /*Interface netconfig.wifi*/
+       wifi_object = wifi_skeleton_new();
+       interface_wifi = G_DBUS_INTERFACE_SKELETON(wifi_object);
+
+       /* WIFI power */
+       g_signal_connect(wifi_object, "handle-load-driver",
+                       G_CALLBACK(handle_load_driver), NULL);
+       g_signal_connect(wifi_object, "handle-remove-driver",
+                       G_CALLBACK(handle_remove_driver), NULL);
+       g_signal_connect(wifi_object, "handle-load-p2p-driver",
+                               G_CALLBACK(handle_load_p2p_driver), NULL);
+       g_signal_connect(wifi_object, "handle-remove-p2p-driver",
+                       G_CALLBACK(handle_remove_p2p_driver), NULL);
+
+       /* WIFI state */
+       g_signal_connect(wifi_object, "handle-get-wifi-state",
+                       G_CALLBACK(handle_get_wifi_state), NULL);
+
+       /* WIFI scan */
+       g_signal_connect(wifi_object, "handle-request-bssid-scan",
+                       G_CALLBACK(handle_request_bssid_scan), NULL);
+       g_signal_connect(wifi_object, "handle-get-bssid-list",
+                       G_CALLBACK(handle_get_bssid_list), NULL);
+       g_signal_connect(wifi_object, "handle-netlink-scan",
+                       G_CALLBACK(handle_netlink_scan), NULL);
+
+       /* WPS Connect */
+       g_signal_connect(wifi_object, "handle-request-wps-connect",
+                       G_CALLBACK(handle_request_wps_connect), NULL);
+       g_signal_connect(wifi_object, "handle-request-wps-cancel",
+                       G_CALLBACK(handle_request_wps_cancel), NULL);
+
+       /* WIFI direct */
+       g_signal_connect(wifi_object, "handle-launch-direct",
+                       G_CALLBACK(handle_launch_direct), NULL);
+
+       /* EAP config */
+       g_signal_connect(wifi_object, "handle-create-eap-config",
+                       G_CALLBACK(handle_create_eap_config), NULL);
+       g_signal_connect(wifi_object, "handle-delete-eap-config",
+                       G_CALLBACK(handle_delete_eap_config), NULL);
+
+       /* VSIE methods */
+       g_signal_connect(wifi_object, "handle-add-vsie",
+                       G_CALLBACK(handle_add_vsie), NULL);
+       g_signal_connect(wifi_object, "handle-get-vsie",
+                       G_CALLBACK(handle_get_vsie), NULL);
+       g_signal_connect(wifi_object, "handle-remove-vsie",
+                       G_CALLBACK(handle_remove_vsie), NULL);
+
+       /* IP conflict methods */
+       g_signal_connect(wifi_object, "handle-ip-conflict-set-enable",
+                       G_CALLBACK(handle_ip_conflict_set_enable), NULL);
+       g_signal_connect(wifi_object, "handle-is-ip-conflict-detect-enabled",
+                       G_CALLBACK(handle_is_ip_conflict_detect_enabled), NULL);
+       g_signal_connect(wifi_object, "handle-set-ip-conflict-period",
+                       G_CALLBACK(handle_set_ip_conflict_period), NULL);
+       g_signal_connect(wifi_object, "handle-get-ip-conflict-state",
+                       G_CALLBACK(handle_get_ip_conflict_state), NULL);
+       g_signal_connect(wifi_object, "handle-get-ip-conflict-period",
+                       G_CALLBACK(handle_get_ip_conflict_period), NULL);
+
+       /* WIFI configuration */
+       g_signal_connect(wifi_object, "handle-save-configuration",
+                       G_CALLBACK(handle_save_configuration), NULL);
+       g_signal_connect(wifi_object, "handle-remove-configuration",
+                       G_CALLBACK(handle_remove_configuration), NULL);
+       g_signal_connect(wifi_object, "handle-get-config-ids",
+                       G_CALLBACK(handle_get_config_ids), NULL);
+       g_signal_connect(wifi_object, "handle-load-configuration",
+                       G_CALLBACK(handle_load_configuration), NULL);
+       g_signal_connect(wifi_object, "handle-set-config-field",
+                       G_CALLBACK(handle_set_config_field), NULL);
+       g_signal_connect(wifi_object, "handle-get-config-passphrase",
+                       G_CALLBACK(handle_get_config_passphrase), NULL);
+
+       /* WIFI EAP configuration */
+       g_signal_connect(wifi_object, "handle-save-eap-configuration",
+                       G_CALLBACK(handle_save_eap_configuration), NULL);
+       g_signal_connect(wifi_object, "handle-load-eap-configuration",
+                       G_CALLBACK(handle_load_eap_configuration), NULL);
+
+       /* BG scan mode */
+       g_signal_connect(wifi_object, "handle-set-bgscan",
+                       G_CALLBACK(handle_set_bgscan), NULL);
+       g_signal_connect(wifi_object, "handle-resume-bgscan",
+                       G_CALLBACK(handle_resume_bgscan), NULL);
+       g_signal_connect(wifi_object, "handle-pause-bgscan",
+                       G_CALLBACK(handle_pause_bgscan), NULL);
+       g_signal_connect(wifi_object, "handle-reset-bgscan-interval",
+                       G_CALLBACK(handle_reset_bgscan_interval), NULL);
+
+       /* Auto Scan Mode */
+       g_signal_connect(wifi_object, "handle-get-autoscan",
+                       G_CALLBACK(handle_get_autoscan), NULL);
+       g_signal_connect(wifi_object, "handle-get-autoscanmode",
+                       G_CALLBACK(handle_get_autoscanmode), NULL);
+
+       /* Extension API methods */
+       g_signal_connect(wifi_object, "handle-flush-bss",
+                       G_CALLBACK(handle_flush_bss), NULL);
+
+       /* Passpoint */
+       g_signal_connect(wifi_object, "handle-set-passpoint",
+                               G_CALLBACK(handle_set_passpoint), NULL);
+       g_signal_connect(wifi_object, "handle-get-passpoint",
+                                       G_CALLBACK(handle_get_passpoint), NULL);
+
+       /* EAP authentication */
+       g_signal_connect(wifi_object, "handle-get-aka-auth",
+                               G_CALLBACK(handle_get_aka_auth), NULL);
+       g_signal_connect(wifi_object, "handle-get-sim-auth",
+                               G_CALLBACK(handle_get_sim_auth), NULL);
+       g_signal_connect(wifi_object, "handle-get-sim-imsi",
+                               G_CALLBACK(handle_get_sim_imsi), NULL);
+       g_signal_connect(wifi_object, "handle-req-aka-auth",
+                       G_CALLBACK(handle_req_aka_auth), NULL);
+       g_signal_connect(wifi_object, "handle-req-sim-auth",
+                       G_CALLBACK(handle_req_sim_auth), NULL);
+
+       /* WIFI MDM blacklist */
+       g_signal_connect(wifi_object, "handle-check-black-list",
+                       G_CALLBACK(handle_check_black_list), NULL);
+
+       /* TDLS methods */
+       g_signal_connect(wifi_object, "handle-tdls-disconnect",
+                       G_CALLBACK(handle_tdls_disconnect), NULL);
+       g_signal_connect(wifi_object, "handle-tdls-connected-peer",
+                       G_CALLBACK(handle_tdls_connected_peer), NULL);
+       g_signal_connect(wifi_object, "handle-tdls-discover",
+                       G_CALLBACK(handle_tdls_discover), NULL);
+       g_signal_connect(wifi_object, "handle-tdls-connect",
+                       G_CALLBACK(handle_tdls_connect), NULL);
+       g_signal_connect(wifi_object, "handle-tdls-channel-switch",
+                       G_CALLBACK(handle_tdls_channel_switch), NULL);
+       g_signal_connect(wifi_object, "handle-tdls-cancel-channel-switch",
+                       G_CALLBACK(handle_tdls_cancel_channel_switch), NULL);
+
+       /* Passphrase Encryption */
+       g_signal_connect(wifi_object, "handle-encrypt-passphrase",
+                       G_CALLBACK(handle_encrypt_passphrase), NULL);
+       g_signal_connect(wifi_object, "handle-decrypt-passphrase",
+                       G_CALLBACK(handle_decrypt_passphrase), NULL);
+
+       if (!g_dbus_interface_skeleton_export(interface_wifi, connection,
+                       NETCONFIG_WIFI_PATH, NULL)) {
+               ERR("Export WIFI_PATH for wifi failed");
+       }
+
+       /* Interface connman.Agent */
+       connman_agent_object = net_connman_agent_skeleton_new();
+
+       interface_connman_agent = G_DBUS_INTERFACE_SKELETON(connman_agent_object);
+       g_signal_connect(connman_agent_object, "handle-report-error",
+                       G_CALLBACK(handle_report_error), NULL);
+       g_signal_connect(connman_agent_object, "handle-request-browser",
+                       G_CALLBACK(handle_request_browser), NULL);
+       g_signal_connect(connman_agent_object, "handle-request-input",
+                       G_CALLBACK(handle_request_input), NULL);
+       g_signal_connect(connman_agent_object, "handle-set-field",
+                       G_CALLBACK(handle_set_field), NULL);
+
+       if (!g_dbus_interface_skeleton_export(interface_connman_agent, connection,
+                       NETCONFIG_WIFI_PATH, NULL)) {
+               ERR("Export WIFI_PATH for agent failed");
+       }
+
+       /*Interface netconfig.wifi.Firmware*/
+       wififirmware_object = wifi_firmware_skeleton_new();
 
-       g_return_val_if_fail(conn != NULL, NULL);
+       interface_wifi_firmware = G_DBUS_INTERFACE_SKELETON(wififirmware_object);
+       g_signal_connect(wififirmware_object, "handle-start",
+                       G_CALLBACK(handle_start), NULL);
+       g_signal_connect(wififirmware_object, "handle-stop",
+                               G_CALLBACK(handle_stop), NULL);
 
-       object = g_object_new(NETCONFIG_TYPE_WIFI, "conn", conn, "path",
-                       NETCONFIG_WIFI_PATH, NULL);
+       if (!g_dbus_interface_skeleton_export(interface_wifi_firmware, connection,
+                       NETCONFIG_WIFI_PATH, NULL)) {
+               ERR("Export WIFI_PATH for firmware failed");
+       }
 
-       INFO("create wifi(%p)", object);
+       _set_wifi_mac_address();
 
-       dbus_g_connection_register_g_object(conn, NETCONFIG_WIFI_PATH, object);
+       wifi_power_initialize();
 
-       INFO("wifi(%p) register DBus path(%s)", object, NETCONFIG_WIFI_PATH);
+       return;
+}
 
-       netconfig_wifi_power_configuration();
-       netconfig_wifi_init_bgscan();
+void wifi_object_deinit(void)
+{
+       g_object_unref(wifi_object);
+       g_object_unref(connman_agent_object);
+       g_object_unref(wififirmware_object);
 
-       return object;
+       wifi_power_deinitialize();
 }