Skip to content

Commit

Permalink
Merge Autohotspot setup into main install script (MiczFlor#2086)
Browse files Browse the repository at this point in the history
* merge autohotspot setup into main install script

* added autohotspot config step

* fix intendation

* fix tests

* add "iw" to needed packages.
Update docker packages

* refactored into sample confs.
disable autohotspot service if not selected.
remove old docs

* use crontab command.
reactivate disable powermanagment.
update comments.
remove obsolete files and calls.

* hamonized function calls

* only copy original file if not already present.

even with multiple invocation we want to keep the first original file.
Delete autohotspot service in helperscript

* update CODEOWNERS

* remove changes for autohotspot if not selected

* ignore output on service check

* fixes for "clear autohotspot configurations"

* added test for autohotsppt installation

* fix intendation

* check for activated wifi device

* moved autohotspot installation to separate script

moved autohotspot.sh to sampleconfig

* fix remove crontab entry

* unified: dont remove autohotspot configuration

* fix refactored device / ssid check logic

on boot or after unblock of wifi the device can give additional errorcodes (-11)
this caused incorrectly a hotspot start

* fix conf file copy handling on multiple invocation

* update docs

* refactored duplications

* fix for deactivated wifi on startup

also check for hard blocked wifi

* add --force-hotspot option for easier testing

* update script with changes from original source

* extend service handling durin installation

* some clean up

* fix var checks

* update docs

* update docs
  • Loading branch information
AlvinSchiller authored Nov 4, 2023
1 parent 7c06608 commit c568882
Show file tree
Hide file tree
Showing 26 changed files with 599 additions and 2,575 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/components/controls/buttons_usb_encoder/ @jeripeierSBB
/components/synchronisation/sync-shared @AlvinSchiller
/scripts/installscripts/buster-install-default.sh @jeripeierSBB
/scripts/installscripts/buster-install-default-with-autohotspot.sh @jeripeierSBB
/scripts/helperscripts/setup_autohotspot.sh @Groovylein
3 changes: 2 additions & 1 deletion ci/Dockerfile.debian
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get update \
&& apt-get -y upgrade \
&& apt-get -y install \
alsa-utils \
build-essential \
git \
iw \
locales \
sudo \
systemd \
wireless-tools \
wget \
wpasupplicant \
# install internally used packages here to speed up GitHub Action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ After=multi-user.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/autohotspot
ExecStart=%AUTOHOTSPOT_SCRIPT%
[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
219 changes: 219 additions & 0 deletions misc/sampleconfigs/autohotspot.sh.stretch-default2-Hotspot.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
#!/bin/bash
#version 0.962-N/HS

#You may share this script on the condition all references to RaspberryConnect.com
#must be included in copies or derivatives of this script.

#A script to switch between a wifi network and a non internet routed Hotspot
#Works at startup or with a seperate timer or manually without a reboot
#Other setup required find out more at
#http://www.raspberryconnect.com

#Additions where made for the Phoniebox project
#https://github.com/MiczFlor/RPi-Jukebox-RFID

if [ $# -gt 0 ] ; then
if [ $# -eq 1 ] && [ "$1" == "--force-hotspot" ]; then
FORCE_HOTSPOT=1
else
echo "ignoring unrecognized parameter: $*"
fi
fi

NO_SSID='NoSSid'
ssidChk="$NO_SSID"

wifidev="wlan0" #device name to use. Default is wlan0.
#use the command: iw dev ,to see wifi interface name

IFSdef=$IFS
cnt=0
#These four lines capture the wifi networks the RPi is setup to use
wpassid=$(awk '/ssid="/{ print $0 }' /etc/wpa_supplicant/wpa_supplicant.conf | awk -F'ssid=' '{ print $2 }' | sed 's/\r//g'| awk 'BEGIN{ORS=","} {print}' | sed 's/\"/''/g' | sed 's/,$//')
IFS=","
ssids=($wpassid)
IFS=$IFSdef #reset back to defaults


#Note:If you only want to check for certain SSIDs
#Remove the # in in front of ssids=('mySSID1'.... below and put a # infront of all four lines above
# separated by a space, eg ('mySSID1' 'mySSID2')
#ssids=('mySSID1' 'mySSID2' 'mySSID3')

#Enter the Routers Mac Addresses for hidden SSIDs, seperated by spaces ie
#( '11:22:33:44:55:66' 'aa:bb:cc:dd:ee:ff' )
mac=()

ssidsmac=("${ssids[@]}" "${mac[@]}") #combines ssid and MAC for checking

CreateAdHocNetwork()
{
echo "Creating Hotspot"
ip link set dev "$wifidev" down
ip a add %AUTOHOTSPOT_IP%/24 brd + dev "$wifidev"
ip link set dev "$wifidev" up
dhcpcd -k "$wifidev" >/dev/null 2>&1
systemctl start dnsmasq
systemctl start hostapd
}

KillHotspot()
{
echo "Shutting Down Hotspot"
ip link set dev "$wifidev" down
systemctl stop hostapd
systemctl stop dnsmasq
ip addr flush dev "$wifidev"
ip link set dev "$wifidev" up
dhcpcd -n "$wifidev" >/dev/null 2>&1
}

CheckWifiUp()
{
echo "Checking WiFi connection ok"
sleep 20 #give time for connection to be completed to router
if ! wpa_cli -i "$wifidev" status | grep 'ip_address' >/dev/null 2>&1
then #Failed to connect to wifi (check your wifi settings, password etc)
echo 'Wifi failed to connect, falling back to Hotspot.'
wpa_cli terminate "$wifidev" >/dev/null 2>&1
CreateAdHocNetwork
fi
}

InitWPA() {
wpa_supplicant -B -i "$wifidev" -c /etc/wpa_supplicant/wpa_supplicant.conf >/dev/null 2>&1
}

CheckServices()
{
#After some system updates hostapd gets masked using Raspbian Buster, and above. This checks and fixes
#the issue and also checks dnsmasq is ok so the hotspot can be generated.
#Check Hostapd is unmasked and disabled
if (systemctl -all list-unit-files hostapd.service | grep "hostapd.service masked") >/dev/null 2>&1 ;then
systemctl unmask hostapd.service >/dev/null 2>&1
fi
if (systemctl -all list-unit-files hostapd.service | grep "hostapd.service enabled") >/dev/null 2>&1 ;then
systemctl disable hostapd.service >/dev/null 2>&1
systemctl stop hostapd >/dev/null 2>&1
fi
#Check dnsmasq is disabled
if (systemctl -all list-unit-files dnsmasq.service | grep "dnsmasq.service masked") >/dev/null 2>&1 ;then
systemctl unmask dnsmasq >/dev/null 2>&1
fi
if (systemctl -all list-unit-files dnsmasq.service | grep "dnsmasq.service enabled") >/dev/null 2>&1 ;then
systemctl disable dnsmasq >/dev/null 2>&1
systemctl stop dnsmasq >/dev/null 2>&1
fi
}

CheckDevice()
{
local j=0
while [ true ] #wait for wifi if busy, usb wifi is slower.
do
echo "Device availability check: try $j"
if [ $j -ge 5 ]; then
#if no wifi device,ie usb wifi removed, activate wifi so when it is
#reconnected wifi to a router will be available
echo "No wifi device connected"
InitWPA
exit 1
elif (iw dev "$wifidev" info 2>&1 >/dev/null) ; then
echo "wifi device available"
if (rfkill list wifi -rno HARD,SOFT | grep -i "unblocked.*unblocked") >/dev/null 2>&1 ; then
local wifidev_up=$(ip link show "$wifidev" up)
if [ -z "$wifidev_up" ]; then
echo "wifi is down. setting up"
ip link set dev "$wifidev" up
sleep 2
fi
return
else
echo "wifi is deactivated"
exit 0
fi
else
j=$((j + 1))
sleep 2
fi
done
}

FindSSID()
{
if [ -n "$FORCE_HOTSPOT" ]; then return; fi

#Check to see what SSID's and MAC addresses are in range
local i=0; j=0
while [ $i -eq 0 ]
do
scanreply=$(iw dev "$wifidev" scan ap-force 2>&1)
ssidreply=$(echo "$scanreply" | egrep "^BSS|SSID:")
echo "SSID availability check: try $j"
if [ $j -ge 5 ]; then
ssidreply=""
i=1
elif [ -z "$ssidreply" ] ; then
echo "Error scan SSID's: $scanreply"
j=$((j + 1))
sleep 2
else
echo "SSID's in range:"
echo "$ssidreply"
i=1
fi
done

for ssid in "${ssidsmac[@]}"
do
if (echo "$ssidreply" | grep "$ssid") >/dev/null 2>&1
then
#Valid SSid found, passing to script
echo "Valid SSID Detected, assesing Wifi status"
ssidChk=$ssid
return 0
fi
done
}

CheckSSID()
{
#Create Hotspot or connect to valid wifi networks
if [ "$ssidChk" != "$NO_SSID" ]
then
if systemctl status hostapd | grep "(running)" >/dev/null 2>&1
then #hotspot running and ssid in range
KillHotspot
echo "Hotspot Deactivated, Bringing Wifi Up"
InitWPA
CheckWifiUp
elif { wpa_cli -i "$wifidev" status | grep 'ip_address'; } >/dev/null 2>&1
then #Already connected
echo "Wifi already connected to a network"
else #ssid exists and no hotspot running connect to wifi network
echo "Connecting to the WiFi Network"
InitWPA
CheckWifiUp
fi
else #ssid or MAC address not in range
if systemctl status hostapd | grep "(running)" >/dev/null 2>&1
then
echo "Hostspot already active"
elif { wpa_cli status | grep "$wifidev"; } >/dev/null 2>&1
then
echo "Cleaning wifi files and Activating Hotspot"
wpa_cli terminate >/dev/null 2>&1
ip addr flush "$wifidev"
ip link set dev "$wifidev" down
rm -r /var/run/wpa_supplicant >/dev/null 2>&1
CreateAdHocNetwork
else #"No SSID, activating Hotspot"
CreateAdHocNetwork
fi
fi
}

CheckServices
CheckDevice
FindSSID
CheckSSID
Loading

0 comments on commit c568882

Please sign in to comment.