2022-02-06 16:39:01 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
# Copyright (C) 2020 Fewtarius (https://github.com/fewtarius)
|
2022-08-02 23:27:17 +00:00
|
|
|
# Copyright (C) 2022 kkoshelev (https://github.com/kkoshelev)
|
2022-02-06 16:39:01 +00:00
|
|
|
|
|
|
|
. /etc/profile
|
|
|
|
|
|
|
|
COMMAND="$1"
|
2022-05-05 19:37:52 +00:00
|
|
|
|
|
|
|
### ES won't save the configuration in time
|
|
|
|
### for it to be useable by the script, so
|
|
|
|
### we have to accept the ssid and key on the
|
|
|
|
### command line too.
|
|
|
|
|
|
|
|
if [ ! -z "${2}" ]
|
|
|
|
then
|
|
|
|
SSID="${2}"
|
|
|
|
else
|
|
|
|
SSID="$(get_setting wifi.ssid)" 2>/dev/null
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -z "${3}" ]
|
|
|
|
then
|
|
|
|
PSK="${3}"
|
|
|
|
else
|
|
|
|
PSK="$(get_setting wifi.key)" 2>/dev/null
|
|
|
|
fi
|
|
|
|
|
2022-02-06 16:39:01 +00:00
|
|
|
WIFICFG="/storage/.cache/connman/wifi.config"
|
|
|
|
|
2022-08-02 23:27:17 +00:00
|
|
|
# lists all wifi services in service=ssid format
|
|
|
|
list_wifi() {
|
|
|
|
connmanctl services | cut -b 5- | awk '/\S+.+\s+wifi/ {a=$0; sub(/\s+wifi_.*$/,"", a); b=$0; sub(a, "", b); sub(/\s+/, "", b); print b "=" a}' | sort | uniq
|
|
|
|
}
|
|
|
|
|
|
|
|
# Looksup connman service name based on ssid
|
|
|
|
# $1 - SSID to lookup
|
|
|
|
get_wifi_service() {
|
|
|
|
list_wifi | awk -v ssid="${1}" '{ split($0, a, "="); if (a[2]==ssid) print a[1] }'
|
|
|
|
}
|
|
|
|
|
2022-06-09 12:53:47 +00:00
|
|
|
set_powersave() {
|
|
|
|
ENABLED=$(get_setting wifi.powersave)
|
|
|
|
if [ "${ENABLED}" = "1" ]
|
|
|
|
then
|
|
|
|
iw wlan0 set power_save on
|
|
|
|
else
|
|
|
|
iw wlan0 set power_save off
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-08-25 11:10:02 +00:00
|
|
|
get_wifi_state() {
|
|
|
|
STATE=$(connmanctl state | awk '/State/ {print $3}' 2>/dev/null)
|
|
|
|
echo ${STATE}
|
|
|
|
}
|
|
|
|
|
|
|
|
check_wifi() {
|
|
|
|
SSID_CHK=$(grep "${SSID}" ${WIFICFG} 2>/dev/null)
|
|
|
|
KEY_CHK=$(grep "${PSK}" ${WIFICFG} 2>/dev/null)
|
|
|
|
STATE=$(get_wifi_state)
|
|
|
|
if [ -n "${SSID_CHK}" ] && \
|
|
|
|
[ -n "${KEY_CHK}" ] && \
|
|
|
|
[ "${STATE}" = "online" ]
|
|
|
|
then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-02-06 16:39:01 +00:00
|
|
|
set_wifi() {
|
|
|
|
case "${1}" in
|
|
|
|
"enable")
|
2022-08-25 11:10:02 +00:00
|
|
|
check_wifi
|
2022-02-06 16:39:01 +00:00
|
|
|
set_setting wifi.enabled 1
|
|
|
|
# Create the WIFI config.
|
|
|
|
cat > "${WIFICFG}" <<EOF
|
|
|
|
[global]
|
|
|
|
Name=${OS_NAME}
|
|
|
|
|
2022-02-06 17:02:58 +00:00
|
|
|
[service_${OS_NAME}_default]
|
2022-09-20 21:16:51 +00:00
|
|
|
Type = wifi
|
|
|
|
Name = ${SSID}
|
|
|
|
Passphrase = ${PSK}
|
|
|
|
AutoConnect = true
|
2022-02-06 16:39:01 +00:00
|
|
|
EOF
|
|
|
|
|
2022-08-25 11:10:02 +00:00
|
|
|
STATE=$(get_wifi_state)
|
|
|
|
if [ ! "${STATE}" = "online" ]
|
|
|
|
then
|
|
|
|
connmanctl enable wifi 2>/dev/null
|
|
|
|
fi
|
2022-06-09 12:53:47 +00:00
|
|
|
set_powersave 2>/dev/null
|
2022-08-25 11:10:02 +00:00
|
|
|
nohup connmanctl connect $(get_wifi_service "${SSID}") 2>/dev/null &
|
2022-02-06 16:39:01 +00:00
|
|
|
;;
|
|
|
|
"disable")
|
|
|
|
connmanctl disable wifi 2>/dev/null
|
|
|
|
rm -f "${WIFICFG}" 2>/dev/null
|
|
|
|
set_setting wifi.enabled 0
|
|
|
|
;;
|
2022-08-28 23:53:29 +00:00
|
|
|
"reconnect")
|
|
|
|
/usr/bin/wifictl disable
|
|
|
|
/usr/bin/wifictl enable
|
2022-08-30 21:05:35 +00:00
|
|
|
;;
|
2022-02-06 16:39:01 +00:00
|
|
|
"list")
|
2022-08-02 23:27:17 +00:00
|
|
|
#connmanctl services | cut -b 5- | awk '/wifi/ {sub(/\s+wifi_.*$/,"",$0);print}' | sort | uniq
|
|
|
|
list_wifi | awk '{sub(/\S+=/,"",$0);print}'
|
2022-02-06 16:39:01 +00:00
|
|
|
;;
|
2022-02-06 17:02:58 +00:00
|
|
|
"scan")
|
2022-10-11 11:34:21 +00:00
|
|
|
connmanctl scan wifi 2>/dev/null
|
2022-02-06 17:02:58 +00:00
|
|
|
;;
|
|
|
|
"scanlist")
|
|
|
|
set_wifi scan 2>/dev/null
|
2022-08-02 23:27:17 +00:00
|
|
|
list_wifi | awk '{sub(/\S+=/,"",$0);print}'
|
|
|
|
;;
|
|
|
|
"service")
|
|
|
|
get_wifi_service "${SSID}"
|
2022-02-06 17:02:58 +00:00
|
|
|
;;
|
2022-06-09 12:53:47 +00:00
|
|
|
"setpowersave")
|
|
|
|
set_powersave 2>/dev/null
|
|
|
|
;;
|
2022-02-06 16:39:01 +00:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ ! -d "" ]
|
|
|
|
then
|
|
|
|
mkdir -p "/storage/.cache/connman"
|
|
|
|
fi
|
|
|
|
|
|
|
|
set_wifi ${COMMAND}
|