Replace HAL interface with hal-api-wifi 08/252108/6 accepted/tizen/unified/20210210.130355 submit/tizen/20210209.010000
authorJaehyun Kim <jeik01.kim@samsung.com>
Fri, 22 Jan 2021 12:24:50 +0000 (21:24 +0900)
committerJaehyun Kim <jeik01.kim@samsung.com>
Wed, 3 Feb 2021 06:24:33 +0000 (15:24 +0900)
Change-Id: I4339754ddca51ecb08cb8f32818ada9a26b1da46
Signed-off-by: Jaehyun Kim <jeik01.kim@samsung.com>
CMakeLists.txt
include/util.h
include/wifi-firmware.h
include/wifi.h
packaging/net-config.spec
resources/usr/lib/systemd/system/net-config.service
resources/usr/lib/systemd/system/net-config_tv.service
src/main.c
src/utils/util.c
src/wifi-firmware.c
src/wifi.c

index 8a81aac..50a546f 100755 (executable)
@@ -90,6 +90,7 @@ PKG_CHECK_MODULES(pkgs REQUIRED
        libtzplatform-config
        libnl-3.0
        libnl-genl-3.0
+       hal-api-wifi
        ${P2P_REQUIRED_PKGS}
        ${WEARABLE_REQUIRED_PKGS}
        )
index 9f185bd..436f49c 100755 (executable)
@@ -100,8 +100,10 @@ void netconfig_set_vconf_str(const char * key, const char * value, gboolean log)
 int netconfig_vconf_get_int(const char * key, int *value);
 int netconfig_vconf_get_bool(const char * key, int *value);
 char* netconfig_get_env(const char *key);
+void netconfig_set_mac_address_to_vconf(const char *def_mac);
 void netconfig_set_mac_address_from_file(void);
 char *netconfig_get_mac_address_from_file(const char *ifname);
+char *netconfig_get_default_ifname_from_file(void);
 int netconfig_freq_to_channel(int freq);
 int netconfig_get_operating_class(int freq);
 void __netconfig_pop_wifi_connected_poppup(const char *ssid);
index bef9542..01eaacb 100755 (executable)
@@ -33,7 +33,7 @@ enum netconfig_wifi_firmware {
        NETCONFIG_WIFI_P2P              = 0x02,
        NETCONFIG_WIFI_SOFTAP   = 0x03,
 };
-
+int netconfig_wifi_firmware_get_mac(const char *interface_name, char **mac);
 int netconfig_wifi_firmware(enum netconfig_wifi_firmware type,
        const char *interface_name, gboolean enable);
 
index 84d030f..75145ae 100755 (executable)
@@ -34,6 +34,8 @@ extern "C" {
 #define WIFI_CERT_STORAGEDIR   "/var/lib/wifi/cert"
 #define CONNMAN_STORAGEDIR             "/var/lib/connman"
 
+void wifi_set_default_mac(char *def_mac);
+int wifi_check_systemd_service(void);
 void __netconfig_wifi_connect_reply(GObject *source_object, GAsyncResult *res,
                gpointer user_data);
 
index 5e2ae91..3b966e2 100755 (executable)
@@ -17,6 +17,7 @@ BuildRequires:        pkgconfig(libtzplatform-config)
 BuildRequires: pkgconfig(key-manager)
 BuildRequires: pkgconfig(libnl-3.0)
 BuildRequires: pkgconfig(libnl-genl-3.0)
+BuildRequires:  pkgconfig(hal-api-wifi)
 Requires:              vconf
 Requires:              connman
 Requires:              systemd
index 20b2458..13e018e 100755 (executable)
@@ -10,7 +10,7 @@ Group=network_fw
 SmackProcessLabel=System
 ExecStart=/usr/bin/net-config
 Restart=on-failure
-Capabilities=cap_dac_override,cap_net_admin,cap_net_raw=i
+Capabilities=cap_dac_override,cap_net_admin,cap_net_raw,cap_sys_module=i
 SecureBits=keep-caps
 
 [Install]
index 2f2f4f4..37b5bb4 100755 (executable)
@@ -8,7 +8,7 @@ Group=network_fw
 BusName=net.netconfig
 RemainAfterExit=yes
 ExecStart=/usr/bin/net-config
-Capabilities=cap_dac_override,cap_net_admin,cap_net_raw=i
+Capabilities=cap_dac_override,cap_net_admin,cap_net_raw,cap_sys_module=i
 SecureBits=keep-caps
 
 [Install]
index cd6aa31..c4b7565 100755 (executable)
@@ -42,6 +42,7 @@
 #include "network-statistics.h"
 #include "network-dump.h"
 #include "ethernet.h"
+#include "wifi-firmware.h"
 
 static GMainLoop *main_loop = NULL;
 
@@ -86,6 +87,8 @@ int main(int argc, char *argv[])
 {
        int ret;
        int check_ethernet_monitor_timer = 0;
+       char *ifname = NULL;
+       char *wifi_def_mac = NULL;
 
        umask(0077);
 
@@ -109,6 +112,18 @@ int main(int argc, char *argv[])
        g_type_init();
 #endif
 
+       /* Backward compatibility:
+        * Execute only when the old style hal architecture is not running
+        */
+       if (!wifi_check_systemd_service()) {
+               ifname = netconfig_get_default_ifname_from_file();
+
+               if (ifname && netconfig_wifi_firmware_get_mac(ifname, &wifi_def_mac) == 0)
+                       wifi_set_default_mac(wifi_def_mac);
+
+               g_free(ifname);
+       }
+
        main_loop = g_main_loop_new(NULL, FALSE);
        if (main_loop == NULL) {
                ERR("Couldn't create GMainLoop\n");
@@ -139,7 +154,10 @@ int main(int argc, char *argv[])
        check_dump_state_and_start();
 #endif
 
-       if (TIZEN_WLAN_BOARD_SPRD)
+       /* Backward compatibility:
+        * Execute only when the old style hal architecture is running
+        */
+       if (TIZEN_WLAN_BOARD_SPRD && wifi_check_systemd_service())
                wifi_firmware_download();
 
        /*In case no emulator, set the ETH0 Mac address*/
index a034074..bf7c4bc 100755 (executable)
@@ -54,6 +54,8 @@
 #define TELEPHONY_PLUGIN_FILEPATH      "/usr/lib/net-config-plugin-telephony.so"
 #define STC_PLUGIN_FILEPATH            "/usr/lib/net-config-plugin-stc.so"
 #define BATTERY_PLUGIN_FILEPATH            "/usr/lib/net-config-plugin-battery.so"
+#define CONNMAN_MAINFILE                       "/etc/connman/main.conf"
+#define CONNMAN_WIFI_DEF_IFNAME                "DefaultWifiInterface"
 
 static gboolean netconfig_device_picker_test = FALSE;
 static int mdnsd_ref_count = 0;
@@ -1388,6 +1390,19 @@ char* netconfig_get_env(const char *key)
        return value;
 }
 
+void netconfig_set_mac_address_to_vconf(const char *def_mac)
+{
+       int mac_len = 0;
+
+       mac_len = strlen(def_mac);
+       if (mac_len < 17) {
+               ERR("def_mac is empty");
+               return;
+       }
+
+       netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, def_mac, TRUE);
+}
+
 void netconfig_set_mac_address_from_file(void)
 {
        FILE *file = NULL;
@@ -1462,6 +1477,39 @@ char *netconfig_get_mac_address_from_file(const char *ifname)
        return g_strdup(mac_str);
 }
 
+char *netconfig_get_default_ifname_from_file(void)
+{
+       GKeyFile *keyfile = NULL;
+
+       keyfile = netconfig_keyfile_load(CONNMAN_MAINFILE);
+       if (keyfile == NULL) {
+               ERR("keyfile[%s] is NULL", CONNMAN_MAINFILE);
+               return NULL;
+       }
+
+       char *str = g_key_file_get_string(keyfile, "General",
+                       CONNMAN_WIFI_DEF_IFNAME, NULL);
+       g_key_file_free(keyfile);
+
+       if (!str)
+               return NULL;
+
+       if (*str == '\0') {
+               g_free(str);
+               return NULL;
+       }
+
+       g_strchomp(str);
+
+       if (strlen(str) >= IFNAMSIZ) {
+               g_free(str);
+               return NULL;
+       }
+
+       DBG("ifname[%s]", str);
+       return str;
+}
+
 int netconfig_freq_to_channel(int freq)
 {
        if (freq < 2412 || freq > 5825 ||
index 628f1b2..3fec54a 100755 (executable)
@@ -22,6 +22,7 @@
 #include <vconf-keys.h>
 #include <stdio.h>
 #include <fcntl.h>
+#include <hal-wifi.h>
 
 #include "log.h"
 #include "util.h"
@@ -32,8 +33,6 @@
 #include "wifi-firmware.h"
 #include "network-statistics.h"
 
-#define WLAN_DRIVER_SCRIPT                     "/usr/bin/wlan.sh"
-
 #define QUAD_CPUS_COUNT                        4
 #define TEMP_BUFFER_LEN                        100
 #define WIFI_MAC_ADD_PATH              "/sys/class/net/wlan0/address"
@@ -123,13 +122,18 @@ static gboolean __netconfig_remove_wifi_driver_from_list(
 static int __netconfig_sta_firmware_start(const char *interface_name)
 {
        int rv = 0;
-       const char *path = WLAN_DRIVER_SCRIPT;
-       char *const args[] = { "/usr/bin/wlan.sh", "start", (char *)interface_name, NULL };
-       char *const envs[] = { NULL };
 
-       rv = netconfig_execute_file(path, args, envs);
-       if (rv < 0)
+       rv = hal_wifi_get_backend();
+       if (rv < 0) {
+               DBG("hal_wifi_get_backend() failed, ret: %d", rv);
                return -EIO;
+       }
+
+       rv = hal_wifi_sta_start(interface_name);
+       if (rv < 0) {
+               DBG("hal_wifi_sta_start() failed, ret: %d", rv);
+               return -EIO;
+       }
 
        rv = netconfig_interface_up(interface_name);
        if (rv != TRUE)
@@ -142,17 +146,23 @@ static int __netconfig_sta_firmware_start(const char *interface_name)
 static int __netconfig_sta_firmware_stop(const char *interface_name)
 {
        int rv = 0;
-       const char *path = WLAN_DRIVER_SCRIPT;
-       char *const args[] = { "/usr/bin/wlan.sh", "stop", (char *)interface_name, NULL };
-       char *const envs[] = { NULL };
 
        rv = netconfig_interface_down(interface_name);
        if (rv != TRUE)
                return -EIO;
 
-       rv = netconfig_execute_file(path, args, envs);
-       if (rv < 0)
+       rv = hal_wifi_get_backend();
+       if (rv < 0) {
+               DBG("hal_wifi_get_backend() failed, ret: %d", rv);
                return -EIO;
+       }
+
+       rv = hal_wifi_stop(interface_name);
+       if (rv < 0) {
+               DBG("hal_wifi_stop() failed, ret: %d", rv);
+               netconfig_interface_up(interface_name);
+               return -EIO;
+       }
 
        DBG("Successfully removed wireless device driver");
        return 0;
@@ -164,13 +174,18 @@ static int __netconfig_p2p_firmware_start(const char *interface_name)
                return -ENODEV;
 
        int rv = 0;
-       const char *path = WLAN_DRIVER_SCRIPT;
-       char *const args[] = { "/usr/bin/wlan.sh", "p2p", (char *)interface_name, NULL };
-       char *const envs[] = { NULL };
 
-       rv = netconfig_execute_file(path, args, envs);
-       if (rv < 0)
+       rv = hal_wifi_get_backend();
+       if (rv < 0) {
+               DBG("hal_wifi_get_backend() failed, ret: %d", rv);
                return -EIO;
+       }
+
+       rv = hal_wifi_p2p_start(interface_name);
+       if (rv < 0) {
+               DBG("hal_wifi_p2p_start() failed, ret: %d", rv);
+               return -EIO;
+       }
 
 #if defined TIZEN_WLAN_USE_P2P_INTERFACE
        rv = netconfig_interface_up(interface_name);
@@ -188,17 +203,23 @@ static int __netconfig_p2p_firmware_stop(const char *interface_name)
                return -ENODEV;
 
        int rv = 0;
-       const char *path = WLAN_DRIVER_SCRIPT;
-       char *const args[] = { "/usr/bin/wlan.sh", "stop", (char *)interface_name, NULL };
-       char *const envs[] = { NULL };
 
        rv = netconfig_interface_down(interface_name);
        if (rv != TRUE)
                return -EIO;
 
-       rv = netconfig_execute_file(path, args, envs);
-       if (rv < 0)
+       rv = hal_wifi_get_backend();
+       if (rv < 0) {
+               DBG("hal_wifi_get_backend() failed, ret: %d", rv);
                return -EIO;
+       }
+
+       rv = hal_wifi_stop(interface_name);
+       if (rv < 0) {
+               DBG("hal_wifi_stop() failed, ret: %d", rv);
+               netconfig_interface_up(interface_name);
+               return -EIO;
+       }
 
        DBG("Successfully removed p2p device driver");
        return 0;
@@ -211,13 +232,18 @@ static int __netconfig_softap_firmware_start(const char *interface_name)
                return -ENODEV;
 
        int rv = 0;
-       const char *path = WLAN_DRIVER_SCRIPT;
-       char *const args[] = { "/usr/bin/wlan.sh", "softap", (char *)interface_name, NULL };
-       char *const envs[] = { NULL };
 
-       rv = netconfig_execute_file(path, args, envs);
-       if (rv < 0)
+       rv = hal_wifi_get_backend();
+       if (rv < 0) {
+               DBG("hal_wifi_get_backend() failed, ret: %d", rv);
                return -EIO;
+       }
+
+       rv = hal_wifi_softap_start(interface_name);
+       if (rv < 0) {
+               DBG("hal_wifi_softap_start() failed, ret: %d", rv);
+               return -EIO;
+       }
 
        if (netconfig_interface_up(interface_name) == FALSE)
                return -EIO;
@@ -233,17 +259,23 @@ static int __netconfig_softap_firmware_stop(const char *interface_name)
                return -ENODEV;
 
        int rv = 0;
-       const char *path = WLAN_DRIVER_SCRIPT;
-       char *const args[] = { "/usr/bin/wlan.sh", "stop", (char *)interface_name, NULL };
-       char *const envs[] = { NULL };
 
        rv = netconfig_interface_down(interface_name);
        if (rv != TRUE)
                return -EIO;
 
-       rv = netconfig_execute_file(path, args, envs);
-       if (rv < 0)
+       rv = hal_wifi_get_backend();
+       if (rv < 0) {
+               DBG("hal_wifi_get_backend() failed, ret: %d", rv);
                return -EIO;
+       }
+
+       rv = hal_wifi_stop(interface_name);
+       if (rv < 0) {
+               DBG("hal_wifi_stop() failed, ret: %d", rv);
+               netconfig_interface_up(interface_name);
+               return -EIO;
+       }
 
        DBG("Successfully removed softap device driver");
        return 0;
@@ -359,6 +391,26 @@ static int __netconfig_set_rps_cpus(const char *interface_name)
        return 0;
 }
 
+int netconfig_wifi_firmware_get_mac(const char *interface_name, char **mac)
+{
+       int rv = 0;
+
+       rv = hal_wifi_get_backend();
+       if (rv < 0) {
+               DBG("hal_wifi_get_backend() failed, ret: %d", rv);
+               return -EIO;
+       }
+
+       rv = hal_wifi_get_mac(interface_name, mac);
+       if (rv < 0) {
+               DBG("hal_wifi_get_mac() failed, ret: %d", rv);
+               return -EIO;
+       }
+
+       DBG("Successfully loaded default wifi mac address: %s", *mac);
+       return 0;
+}
+
 int netconfig_wifi_firmware(enum netconfig_wifi_firmware type,
                const char *interface_name, gboolean enable)
 {
index 60f8092..5f966b7 100755 (executable)
 #include "wifi-extension.h"
 #include "wifi-dpp.h"
 
-#define SPRD_CP2_FIRMWARE_PATH "/usr/bin/cp2-downloader"
+#define SPRD_CP2_FIRMWARE_PATH "/hal/bin/cp2-downloader"
 #define SPRD_CP2_FIRMWARE_STATE_PATH "/tmp/.wifi-firmware-loaded"
+#define WIFI_SYSTEMD_SERVICE_PATH "/usr/lib/systemd/system/wifi-ready.service"
 static int is_wifi_firmware_downloaded = FALSE;
 
 static Wifi *wifi_object = NULL;
 static NetConnmanAgent *connman_agent_object = NULL;
 static WifiFirmware *wififirmware_object = NULL;
+static char *wifi_def_mac = NULL;
 
 Wifi *get_wifi_object(void){
        return wifi_object;
@@ -74,11 +76,33 @@ static void _set_wifi_mac_address(void)
        gchar *mac_addr = NULL;
 
        mac_addr = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
-       if (mac_addr != NULL) {
-               if (strlen(mac_addr) == 0)
+       if (mac_addr == NULL || strlen(mac_addr) == 0) {
+               if (wifi_def_mac)
+                       netconfig_set_mac_address_to_vconf(wifi_def_mac);
+               else
                        netconfig_set_mac_address_from_file();
-               free(mac_addr);
        }
+
+       g_free(mac_addr);
+}
+
+void wifi_set_default_mac(char *def_mac)
+{
+       if (!def_mac)
+               return;
+
+       if (wifi_def_mac)
+               g_free(wifi_def_mac);
+
+       wifi_def_mac = def_mac;
+}
+
+int wifi_check_systemd_service(void)
+{
+       if (access(WIFI_SYSTEMD_SERVICE_PATH, F_OK ) != -1 )
+           return 1;
+       else
+           return 0;
 }
 
 void __netconfig_wifi_connect_reply(GObject *source_object, GAsyncResult *res,
@@ -390,4 +414,6 @@ void wifi_object_deinit(void)
 
        wifi_power_deinitialize();
        wifi_state_deinitialize();
+
+       g_free(wifi_def_mac);
 }