]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/hooks/ports/wireless-ap
Fix hook settings writing and checking
[people/stevee/network.git] / src / hooks / ports / wireless-ap
index c6d91da042a75b78e06c4132e17abe72a338947d..3ff80d06136050d15cd71de1dc50c61d4f5cb470 100644 (file)
 
 . /usr/lib/network/header-port
 
-HOOK_SETTINGS="HOOK ADDRESS BROADCAST_SSID CHANNEL COUNTRY_CODE MODE PHY SSID"
+HOOK_SETTINGS="HOOK ADDRESS BROADCAST_SSID CHANNEL MODE PHY SSID"
 HOOK_SETTINGS="${HOOK_SETTINGS} ENCRYPTION KEY"
 
 ADDRESS=$(mac_generate)
 BROADCAST_SSID=on
 CHANNEL=1
-COUNTRY_CODE="US"
 ENCRYPTION=""
 KEY=""
 MODE="g"
 SSID=
 
-function hook_check() {
+function hook_check_settings() {
        assert isset ADDRESS
        assert ismac ADDRESS
        assert isset BROADCAST_SSID
        assert isbool BROADCAST_SSID
        assert isset CHANNEL
-       assert isset COUNTRY_CODE
        assert isset MODE
        assert isoneof MODE a b g n
        assert isset PHY
@@ -55,7 +53,7 @@ function hook_check() {
        fi
 }
 
-function hook_create() {
+function hook_new() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --broadcast-ssid=*)
@@ -64,9 +62,6 @@ function hook_create() {
                        --channel=*)
                                CHANNEL=$(cli_get_val ${1})
                                ;;
-                       --country-code=*)
-                               COUNTRY_CODE=$(cli_get_val ${1})
-                               ;;
                        --encryption=*)
                                ENCRYPTION=$(cli_get_val ${1})
                                ;;
@@ -99,7 +94,7 @@ function hook_create() {
        local port=$(port_find_free ${PORT_PATTERN_ACCESSPOINT})
        assert isset port
 
-       config_write $(port_file ${port}) ${HOOK_SETTINGS}
+       port_settings_write "${port}" ${HOOK_SETTINGS}
 
        exit ${EXIT_OK}
 }
@@ -110,7 +105,7 @@ function hook_edit() {
 
        assert isset port
 
-       config_read $(port_file ${port})
+       port_settings_read "${port}" ${HOOK_SETTINGS}
 
        while [ $# -gt 0 ]; do
                case "${1}" in
@@ -120,9 +115,6 @@ function hook_edit() {
                        --channel=*)
                                CHANNEL=$(cli_get_val ${1})
                                ;;
-                       --country-code=*)
-                               COUNTRY_CODE=$(cli_get_val ${1})
-                               ;;
                        --encryption=*)
                                ENCRYPTION=$(cli_get_val ${1})
                                ;;
@@ -142,16 +134,18 @@ function hook_edit() {
                shift
        done
 
-       config_write $(port_file ${port}) ${HOOK_SETTINGS}
+       port_settings_write "${port}" ${HOOK_SETTINGS}
 
        exit ${EXIT_OK} 
 }
 
-function hook_up() {
-       local port=${1}
+function hook_create() {
+       local port="${1}"
        assert isset port
 
-       config_read $(port_file ${port})
+       device_exists "${port}" && exit ${EXIT_OK}
+
+       port_settings_read "${port}" ${HOOK_SETTINGS}
 
        # Check if the PHY is present.
        local phy=$(phy_get ${PHY})
@@ -160,60 +154,67 @@ function hook_up() {
                exit ${EXIT_ERROR}
        fi
 
-       # Create the wireless device, if it does not exist, yet.
-       if ! device_exists ${port}; then
-               wireless_create ${port} --phy="${phy}" --type="ap" \
-                       --address="${ADDRESS}"
-       fi
-
-       # Start the hostapd service.
-       hostapd_start ${port}
-       local ret=$?
-
-       if [ ${ret} -ne ${EXIT_OK} ]; then
-               log ERROR "Could not start hostapd on port '${port}': ${ret}"
-               exit ${EXIT_ERROR}
-       fi
+       # Create the wireless device
+       wireless_create "${port}" \
+               --phy="${phy}" \
+               --type="ap" \
+               --address="${ADDRESS}"
 
        exit ${EXIT_OK}
 }
 
-function hook_down() {
-       local port=${1}
+function hook_remove() {
+       local port="${1}"
        assert isset port
 
-       # Stop the hostapd daemon.
-       hostapd_stop ${port}
-
-       # Remove the device if it is still present.
-       if device_exists ${port}; then
-               wireless_remove ${port}
+       # Remove the device if present
+       if device_exists "${port}"; then
+               wireless_remove "${port}"
        fi
 
        exit ${EXIT_OK}
 }
 
-function hook_hotplug() {
-       local port=${1}
+function hook_up() {
+       local port="${1}"
        assert isset port
 
-       local phy=${2}
-       assert isset phy
+       # The port must already exist before
+       # hostapd is started. Otherwise it will
+       # fail horribly over and over again.
+       assert device_exists "${port}"
 
-       assert port_exists ${port}
+       hostapd_start "${port}"
+}
 
-       # Read configuration of port.
-       config_read $(port_file ${port})
+function hook_down() {
+       local port="${1}"
+       assert isset port
 
-       # Get the address of the phy.
-       local phy_address=$(phy_get_address ${phy})
+       hostapd_stop "${port}"
+}
 
-       # Check if the phy is the same we have
-       # read from the configuration file.
-       if [ "${PHY}" = "${phy_address}" ]; then
-               wireless_create ${port} --phy="${phy_address}" --type="ap" \
-                       --address="${ADDRESS}"
-       fi
+function hook_hotplug() {
+       local port="${1}"
+       assert isset port
 
-       exit ${EXIT_OK}
+       case "$(hotplug_action)" in
+               add)
+                       # Create the port when the phy is plugged in
+                       if hotplug_event_port_uses_phy "${port}"; then
+                               hook_create "${port}"
+                       fi
+                       ;;
+
+               remove)
+                       # Stop hostapd
+                       if hotplug_event_port_is_interface "${port}"; then
+                               hostapd_stop "${port}"
+
+                               exit ${EXIT_OK}
+                       fi
+                       ;;
+       esac
+
+       exit ${EXIT_NOT_HANDLED}
 }