distribution/packages/jelos/sources/scripts/wifictl

207 lines
4.1 KiB
Text
Raw Normal View History

#!/bin/bash
# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2020 Fewtarius (https://github.com/fewtarius)
# Copyright (C) 2022 kkoshelev (https://github.com/kkoshelev)
. /etc/profile
###
### 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"
### 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 [ ! "${WIFI_TYPE}" = "0" ]
then
SSID="${SSID}_np"
fi
if [ ! -z "${3}" ]
then
PSK="${3}"
else
PSK="$(get_setting wifi.key)" 2>/dev/null
fi
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
###
### Functions...
2023-08-14 22:05:56 +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] }'
}
set_powersave() {
ENABLED=$(get_setting system.power.wifi)
if [ "${ENABLED}" = "1" ]
then
log $0 "Enabling WIFI power saving."
iw ${WIFI_DEV} set power_save on 2>/dev/null
else
log $0 "Disabling WIFI power saving."
iw ${WIFI_DEV} set power_save off 2>/dev/null
fi
}
wifi_service() {
for SERVICE in wpa_supplicant connman-vpn connman
do
systemctl ${1} ${SERVICE}
done
}
2023-01-20 17:14:15 +00:00
connect_wifi() {
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
}
create_adhoc() {
wifi_service stop
2023-08-14 22:05:56 +00:00
rfkill unblock wifi
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
sysctl -w net.ipv4.ip_forward=1
2023-08-14 22:05:56 +00:00
### Flush any existing IP
ip addr flush dev ${WIFI_DEV}
2023-08-14 22:05:56 +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
systemctl start hostapd
2023-08-14 22:05:56 +00:00
exit 0
2023-08-14 22:05:56 +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() {
wifi_service stop
2023-08-14 22:05:56 +00:00
cat > "${WIFI_CFG}" <<EOF
[Settings]
AutoConnect = true
2022-02-06 17:02:58 +00:00
[service_${OS_NAME}_default]
Type = wifi
Name = ${SSID}
Passphrase = ${PSK}
EOF
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
}
2023-01-20 17:14:15 +00:00
case "${1}" in
enable)
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)
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}'
;;
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