From 7803699e667d654bc9979b8f83a59fcdc9a2eee0 Mon Sep 17 00:00:00 2001 From: HgO Date: Wed, 23 Aug 2023 10:24:43 +0200 Subject: [PATCH] display unused wifi devices --- scripts/_common.sh | 22 +++++++++++++++++++++- scripts/config | 7 ++++--- scripts/install | 2 +- scripts/restore | 6 +++++- scripts/upgrade | 6 +++++- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index a39ce75..b6e0acb 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -14,7 +14,27 @@ free_firmware_packages="firmware-ath9k-htc" function iw_devices() { - echo -n $(/sbin/iw dev | grep Interface | grep -v 'mon\.' | grep -v hotspot | awk '{ print $NF }') | tr ' ' '|' + /sbin/iw dev | grep Interface | grep -v 'mon\.' | grep -v hotspot | awk '{ print $NF }' +} + +function used_iw_devices() +{ + local app_shortname="${app%%__*}" + local hotspot_apps=$(yunohost app list --output-as json | jq -r .apps[].id | grep -F $app_shortname) + # Remove this app from hotspot apps list + local other_hotspot_apps=$(grep -F -x -v $app <<< ${hotspot_apps}) + for hotspot_app in ${other_hotspot_apps}; do + hotspot_wifi_device=$(ynh_app_setting_get --app=$hotspot_app --key=wifi_device) + if [[ -n ${hotspot_wifi_device} ]]; then + echo "${hotspot_wifi_device}" + fi + done +} + +function unused_iw_devices() +{ + # Only prints devices that are not in the list of used devices + iw_devices | grep -F -v -f <(used_iw_devices) } function check_armbian_nonfree_conflict() diff --git a/scripts/config b/scripts/config index b0013ee..7ad0dc5 100644 --- a/scripts/config +++ b/scripts/config @@ -27,7 +27,7 @@ final_path=$(ynh_app_setting_get $app final_path) #================================================= get__no_antenna() { - if [[ $(iw_devices) == "" ]] + if [[ $(unused_iw_devices) == "" ]] then echo "value: true" else @@ -76,14 +76,15 @@ EOF } get__wifi_device() { - if [[ $(iw_devices) == "" ]] + local unused_wifi_devices=$(unused_iw_devices) + if [[ -z ${unused_wifi_devices} ]] then echo "choices: []" else cat << EOF choices: EOF - for device in $(iw_devices | sed "s/|/ /g") + for device in $unused_wifi_devices do echo " $device: $device" done diff --git a/scripts/install b/scripts/install index d89e519..8643f91 100644 --- a/scripts/install +++ b/scripts/install @@ -138,7 +138,7 @@ ip4_nat_prefix="10.${ip4_nat_prefix_index}.242" ip4_dns="${ip4_nat_prefix}.1" hot_reload_usb_wifi_cards -wifi_device=$(iw_devices | awk -F\| '{ print $1 }') +wifi_device=$(unused_iw_devices | head -n 1) wifi_secure=1 wifi_channel=6 diff --git a/scripts/restore b/scripts/restore index a892475..0f9f615 100644 --- a/scripts/restore +++ b/scripts/restore @@ -29,6 +29,7 @@ app=$YNH_APP_INSTANCE_NAME firmware_nonfree=$(ynh_app_setting_get --app=$app --key=firmware_nonfree) service_name=$(ynh_app_setting_get --app=$app --key=service_name) +wifi_device=$(ynh_app_setting_get --app=$app --key=wifi_device) #================================================= # CHECK IF THE APP CAN BE RESTORED @@ -106,7 +107,10 @@ yunohost service add $service_name --description "Creates a Wi-Fi access point" ynh_script_progression --message="Starting a systemd service..." hot_reload_usb_wifi_cards -wifi_device=$(iw_devices | awk -F\| '{ print $1 }') +if [[ -z $wifi_device ]] || ! grep -q -F $wifi_device <(unused_iw_devices); then + wifi_device=$(unused_iw_devices | head -n 1) + ynh_app_setting_set --app=$app --key=wifi_device --value="${wifi_device}" +fi if [[ -z $wifi_device ]]; then ynh_app_setting_set --app=$app --key=service_enabled --value=0 diff --git a/scripts/upgrade b/scripts/upgrade index 47c6ebe..bfad83b 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -19,6 +19,7 @@ app=$YNH_APP_INSTANCE_NAME firmware_nonfree=$(ynh_app_setting_get --app=$app --key=firmware_nonfree) service_name=$(ynh_app_setting_get --app=$app --key=service_name) +wifi_device=$(ynh_app_setting_get --app=$app --key=wifi_device) wifi_ssid=$(ynh_app_setting_get --app=$app --key=wifi_ssid) wifi_secure=$(ynh_app_setting_get --app=$app --key=wifi_secure) wifi_passphrase=$(ynh_app_setting_get --app=$app --key=wifi_passphrase) @@ -201,7 +202,10 @@ ynh_install_app_dependencies $pkg_dependencies ynh_script_progression --message="Copying configuration..." hot_reload_usb_wifi_cards -wifi_device=$(iw_devices | awk -F\| '{ print $1 }') +if [[ -z $wifi_device ]] || ! grep -q -F $wifi_device <(unused_iw_devices); then + wifi_device=$(unused_iw_devices | head -n 1) + ynh_app_setting_set --app=$app --key=wifi_device --value="${wifi_device}" +fi mkdir -pm 0755 /etc/hostapd/$app/ chown root: /etc/hostapd/$app/