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
|
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
###
|
|
|
|
### Basic WIFI properties used across the tool.
|
|
|
|
###
|
|
|
|
|
|
|
|
CFG_ROOT="/storage/.cache"
|
|
|
|
WIFI_CFG="${CFG_ROOT}/connman/wifi.config"
|
|
|
|
HOSTAPD_CFG="${CFG_ROOT}/.hostapd.conf"
|
|
|
|
WIFI_TYPE=$(get_setting network.adhoc.enabled)
|
|
|
|
NETWORK_ADDRESS="192.168.80"
|
|
|
|
ADHOC_CLIENT_ID=$(get_setting wifi.adhoc.id)
|
|
|
|
WIFI_DEV="wlan0"
|
|
|
|
DEFAULT_CHAN="6"
|
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
|
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
if [ ! "${WIFI_TYPE}" = "0" ]
|
|
|
|
then
|
|
|
|
SSID="${SSID}_np"
|
|
|
|
fi
|
|
|
|
|
2022-05-05 19:37:52 +00:00
|
|
|
if [ ! -z "${3}" ]
|
|
|
|
then
|
|
|
|
PSK="${3}"
|
|
|
|
else
|
|
|
|
PSK="$(get_setting wifi.key)" 2>/dev/null
|
|
|
|
fi
|
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
if [ ! -d "" ]
|
|
|
|
then
|
|
|
|
mkdir -p "${CFG_ROOT}/connman"
|
|
|
|
fi
|
|
|
|
|
|
|
|
ADHOC_CHAN=$(get_setting wifi.adhoc.channel)
|
|
|
|
if [ -z "${ADHOC_CHAN}" ]
|
|
|
|
then
|
|
|
|
ADHOC_CHAN="${DEFAULT_CHAN}"
|
|
|
|
set_setting wifi.adhoc.channel ${DEFAULT_CHAN}
|
|
|
|
fi
|
|
|
|
|
2023-08-14 22:05:56 +00:00
|
|
|
###
|
2023-08-15 22:10:48 +00:00
|
|
|
### Functions...
|
2023-08-14 22:05:56 +00:00
|
|
|
###
|
|
|
|
|
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() {
|
2023-06-28 11:41:10 +00:00
|
|
|
ENABLED=$(get_setting system.power.wifi)
|
2022-06-09 12:53:47 +00:00
|
|
|
if [ "${ENABLED}" = "1" ]
|
|
|
|
then
|
2023-01-20 02:30:33 +00:00
|
|
|
log $0 "Enabling WIFI power saving."
|
2023-08-15 22:10:48 +00:00
|
|
|
iw ${WIFI_DEV} set power_save on 2>/dev/null
|
2022-06-09 12:53:47 +00:00
|
|
|
else
|
2023-01-20 02:30:33 +00:00
|
|
|
log $0 "Disabling WIFI power saving."
|
2023-08-15 22:10:48 +00:00
|
|
|
iw ${WIFI_DEV} set power_save off 2>/dev/null
|
2022-06-09 12:53:47 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
wifi_service() {
|
|
|
|
for SERVICE in wpa_supplicant connman-vpn connman
|
|
|
|
do
|
|
|
|
systemctl ${1} ${SERVICE}
|
|
|
|
done
|
2022-08-25 11:10:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 17:14:15 +00:00
|
|
|
connect_wifi() {
|
2023-08-15 22:10:48 +00:00
|
|
|
systemctl restart connman 2>/dev/null
|
|
|
|
connmanctl enable wifi 2>/dev/null
|
2023-01-20 17:14:15 +00:00
|
|
|
set_powersave 2>/dev/null
|
|
|
|
}
|
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
create_adhoc() {
|
|
|
|
wifi_service stop
|
2023-08-14 22:05:56 +00:00
|
|
|
rfkill unblock wifi
|
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
cat <<EOF >${HOSTAPD_CFG}
|
|
|
|
interface=${WIFI_DEV}
|
|
|
|
auth_algs=1
|
|
|
|
channel=${ADHOC_CHAN}
|
|
|
|
wpa=2
|
|
|
|
wpa_key_mgmt=WPA-PSK
|
|
|
|
wpa_pairwise=CCMP
|
|
|
|
rsn_pairwise=CCMP
|
|
|
|
wpa_passphrase=${PSK}
|
|
|
|
ssid=${SSID}
|
|
|
|
EOF
|
2023-08-14 22:05:56 +00:00
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
sysctl -w net.ipv4.ip_forward=1
|
2023-08-14 22:05:56 +00:00
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
### Flush any existing IP
|
|
|
|
ip addr flush dev ${WIFI_DEV}
|
2023-08-14 22:05:56 +00:00
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
### Set our IP
|
|
|
|
ip addr add ${NETWORK_ADDRESS}.${ADHOC_CLIENT_ID}/24 dev ${WIFI_DEV} >/dev/null 2>&1
|
2023-08-14 22:05:56 +00:00
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
systemctl start hostapd
|
2023-08-14 22:05:56 +00:00
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
exit 0
|
2023-08-14 22:05:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 22:10:48 +00:00
|
|
|
destroy_adhoc() {
|
|
|
|
ip addr flush dev ${WIFI_DEV}
|
|
|
|
ip link set ${WIFI_DEV} down
|
|
|
|
systemctl stop hostapd
|
2023-08-14 22:05:56 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 17:14:15 +00:00
|
|
|
set_profile() {
|
2023-08-15 22:10:48 +00:00
|
|
|
wifi_service stop
|
2023-08-14 22:05:56 +00:00
|
|
|
cat > "${WIFI_CFG}" <<EOF
|
2023-08-15 22:10:48 +00:00
|
|
|
[Settings]
|
|
|
|
AutoConnect = true
|
|
|
|
|
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}
|
2022-02-06 16:39:01 +00:00
|
|
|
EOF
|
2023-08-15 22:10:48 +00:00
|
|
|
|
|
|
|
if [ "${WIFI_TYPE}" = "1" ]
|
|
|
|
then
|
|
|
|
cat >> "${WIFI_CFG}" <<EOF
|
|
|
|
Security = wpa
|
|
|
|
IPv4 = ${NETWORK_ADDRESS}.${ADHOC_CLIENT_ID}/255.255.255.0/255.255.255.254
|
|
|
|
IPv6 = off
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm -rf ${CFG_ROOT}/connman/wifi_*
|
|
|
|
wifi_service restart
|
2022-02-06 16:39:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 17:14:15 +00:00
|
|
|
case "${1}" in
|
|
|
|
enable)
|
2023-08-15 22:10:48 +00:00
|
|
|
set_profile >/dev/null 2>&1
|
|
|
|
if [ "${WIFI_TYPE}" = "1" ] && \
|
|
|
|
[ "${ADHOC_CLIENT_ID}" = "1" ]
|
|
|
|
then
|
|
|
|
create_adhoc >/dev/null 2>&1
|
|
|
|
else
|
|
|
|
destroy_adhoc >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
connect_wifi >/dev/null 2>&1
|
2023-01-20 17:14:15 +00:00
|
|
|
set_setting wifi.enabled 1
|
|
|
|
;;
|
|
|
|
disable)
|
2023-08-15 22:10:48 +00:00
|
|
|
if [ "${WIFI_TYPE}" = "1" ]
|
|
|
|
then
|
|
|
|
destroy_adhoc >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
connmanctl disable wifi >/dev/null 2>&1
|
|
|
|
rfkill block wifi >/dev/null 2>&1
|
|
|
|
rm -f "${WIFI_CFG}" 2>/dev/null
|
2023-01-20 17:14:15 +00:00
|
|
|
set_setting wifi.enabled 0
|
|
|
|
;;
|
|
|
|
reconnect)
|
|
|
|
/usr/bin/wifictl disable
|
|
|
|
/usr/bin/wifictl enable
|
|
|
|
;;
|
|
|
|
list)
|
|
|
|
list_wifi | awk '{sub(/\S+=/,"",$0);print}'
|
|
|
|
;;
|
2023-08-15 22:10:48 +00:00
|
|
|
channels)
|
|
|
|
iw list | awk '/[0-9] MHz \[/ && ! /disabled|radar|no IR/ { gsub(/^.*\[/,""); gsub(/\].*$/,""); print}'
|
|
|
|
;;
|
2023-01-20 17:14:15 +00:00
|
|
|
scan)
|
|
|
|
connmanctl scan wifi 2>/dev/null
|
|
|
|
;;
|
|
|
|
scanlist)
|
|
|
|
set_wifi scan 2>/dev/null
|
|
|
|
list_wifi | awk '{sub(/\S+=/,"",$0);print}'
|
|
|
|
;;
|
|
|
|
service)
|
|
|
|
get_wifi_service "${SSID}"
|
|
|
|
;;
|
|
|
|
setpowersave)
|
|
|
|
set_powersave
|
|
|
|
;;
|
|
|
|
setprofile)
|
|
|
|
set_profile
|
|
|
|
;;
|
|
|
|
esac
|