switch: split the switch action into separate stages

When toggling the switch, a lock is held for a relatively long time,
preventing another toggling of the switch to be noticed. With this
change, I hope we can first shutdown the modem, wait for a toggle, and
then continue.
We're losing the abort function but I currently don't know how we would
be able to keep that functionality given that the toggle is queued and
we don't get the notification.
This commit is contained in:
Tobias Mueller 2023-10-17 14:48:47 +02:00
parent eab0633ad9
commit ed6856c490
4 changed files with 78 additions and 66 deletions

View File

@ -31,7 +31,7 @@ define Package/blue-merle/install
$(INSTALL_BIN) ./files/etc/init.d/* $(1)/etc/init.d/
$(INSTALL_BIN) ./files/etc/gl-switch.d/* $(1)/etc/gl-switch.d/
$(INSTALL_BIN) ./files/lib/blue-merle/mac-wipe.sh $(1)/lib/blue-merle/mac-wipe.sh
$(INSTALL_BIN) ./files/usr/bin/blue-merle $(1)/usr/bin/blue-merle
$(INSTALL_BIN) ./files/usr/bin/* $(1)/usr/bin/
$(INSTALL_BIN) ./files/usr/libexec/blue-merle $(1)/usr/libexec/blue-merle
$(INSTALL_BIN) ./files/lib/blue-merle/imei_generate.py $(1)/lib/blue-merle/imei_generate.py
endef

View File

@ -10,14 +10,21 @@ logger -p notice -t blue-merle-toggle "Called... ${action}"
if [ "$action" = "on" ];then
mcu_send_message "Blue Merle ${action}"
echo "on" > /tmp/sim_change_switch
ubus call file exec '{ "command": "flock", "params": ["-n", "/tmp/blue-merle-switch.lock", "timeout", "90", "/usr/bin/blue-merle-switch"] }'
flock -n /tmp/blue-merle-switch.lock logger -p notice -t blue-merle-toggle "Running Stage 1" || logger -p notice -t blue-merle-toggle "Lockfile busy" &
flock -n /tmp/blue-merle-switch.lock timeout 90 /usr/bin/blue-merle-switch-stage1
elif [ "$action" = "off" ];then
mcu_send_message "Blue Merle ${action}"
# We check for any previous run and eventually execute the second stage. We could check for the age of this marker and only activate the second stage is the marker is young enough.
if [ -f /tmp/blue-merle-stage1 ]; then
flock -n /tmp/blue-merle-switch.lock || logger -p notice -t blue-merle-toggle "Lockfile busy" &
flock -n /tmp/blue-merle-switch.lock timeout 90 /usr/bin/blue-merle-switch-stage2
else
logger -p notice -t blue-merle-toggle "No Stage 1; Toggling Off"
fi
echo "off" > /tmp/sim_change_switch
else
echo "off" > /tmp/sim_change_switch
fi
logger -p notice -t blue-merle-toggle "Finished Switch"
logger -p notice -t blue-merle-toggle "Finished Switch $action"
sleep 1

View File

@ -0,0 +1,66 @@
#!/bin/sh
. /lib/blue-merle/functions.sh
. /lib/functions/gl_util.sh
if [ ! -f "/tmp/sim_change_start" ]; then
echo 0 > /tmp/sim_change_start
fi
if [ ! -f "/tmp/sim_change_switch" ]; then
sim_switch off
fi
now=$(date +%s)
sim_change_last=`cat /tmp/sim_change_start`
sim_change_diff=$((now-sim_change_last))
if [[ "$sim_change_diff" -lt 60 ]]; then
mcu_send_message "Please wait >1min between two SIM swaps. ($sim_change_diff s)"
exit 1
fi
echo "$now" > /tmp/sim_change_start
mcu_send_message "Starting SIM swap."
sleep 3
## We're disabling this abort functionality for the moment because the switch keeps being blocked and we cannot notice the pulled switch. We could abort by default and require another toggle to continue.
#i=5
#until [[ $i -lt 0 ]]
#do
# mcu_send_message "Pull switch to abort ($i). "
# i=$((i-1))
# sleep 1
#
# CHECK_ABORT
#done
#
#mcu_send_message "Continuing ..."
#sleep 1
mcu_send_message "Disabling the ME from transmitting and receiving RF signals."
sleep 3
old_imei=$(READ_IMEI)
old_imsi=$(READ_IMSI)
#CHECK_ABORT
answer=1
while [[ "$answer" -eq 1 ]]; do
gl_modem AT AT+CFUN=4 | grep -q OK
if [[ $? -eq 1 ]]; then
mcu_send_message "Disabling failed. Trying again."
CHECK_ABORT
else
answer=0
mcu_send_message "Disabled."
sleep 2
fi
done
mcu_send_message "Replace the SIM card. Then pull the switch."
echo done > /tmp/blue-merle-stage1

View File

@ -3,68 +3,7 @@
. /lib/blue-merle/functions.sh
. /lib/functions/gl_util.sh
if [ ! -f "/tmp/sim_change_start" ]; then
echo 0 > /tmp/sim_change_start
fi
if [ ! -f "/tmp/sim_change_switch" ]; then
sim_switch off
fi
now=$(date +%s)
sim_change_last=`cat /tmp/sim_change_start`
sim_change_diff=$((now-sim_change_last))
if [[ "$sim_change_diff" -lt 60 ]]; then
mcu_send_message "Please wait >1min between two SIM swaps. ($sim_change_diff s)"
exit 1
fi
echo "$now" > /tmp/sim_change_start
mcu_send_message "Starting SIM swap."
sleep 3
i=5
until [[ $i -lt 0 ]]
do
mcu_send_message "Pull switch to abort ($i). "
i=$((i-1))
sleep 1
CHECK_ABORT
done
mcu_send_message "Continuing ..."
sleep 1
mcu_send_message "Disabling the ME from transmitting and receiving RF signals."
sleep 3
old_imei=$(READ_IMEI)
old_imsi=$(READ_IMSI)
CHECK_ABORT
answer=1
while [[ "$answer" -eq 1 ]]; do
gl_modem AT AT+CFUN=4 | grep -q OK
if [[ $? -eq 1 ]]; then
mcu_send_message "Disabling failed. Trying again."
CHECK_ABORT
else
answer=0
mcu_send_message "Disabled."
sleep 2
fi
done
mcu_send_message "Replace the SIM card. Then pull the switch."
while [[ `cat /tmp/sim_change_switch` = "on" ]]; do
mcu_send_message "Replace the SIM card. Then pull the switch."
sleep 3
done
rm -f /tmp/blue-merle-stage1
mcu_send_message "Switch pulled. Continuing..."
sleep 1