From ed6856c490c56a5987b790b091afac053a172778 Mon Sep 17 00:00:00 2001 From: Tobias Mueller Date: Tue, 17 Oct 2023 14:48:47 +0200 Subject: [PATCH] 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. --- Makefile | 2 +- files/etc/gl-switch.d/sim.sh | 13 +++- files/usr/bin/blue-merle-switch-stage1 | 66 +++++++++++++++++++ ...-merle-switch => blue-merle-switch-stage2} | 63 +----------------- 4 files changed, 78 insertions(+), 66 deletions(-) create mode 100644 files/usr/bin/blue-merle-switch-stage1 rename files/usr/bin/{blue-merle-switch => blue-merle-switch-stage2} (50%) mode change 100755 => 100644 diff --git a/Makefile b/Makefile index 913d463..122655d 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/files/etc/gl-switch.d/sim.sh b/files/etc/gl-switch.d/sim.sh index 97324d8..bc8b1e2 100644 --- a/files/etc/gl-switch.d/sim.sh +++ b/files/etc/gl-switch.d/sim.sh @@ -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 diff --git a/files/usr/bin/blue-merle-switch-stage1 b/files/usr/bin/blue-merle-switch-stage1 new file mode 100644 index 0000000..7977848 --- /dev/null +++ b/files/usr/bin/blue-merle-switch-stage1 @@ -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 diff --git a/files/usr/bin/blue-merle-switch b/files/usr/bin/blue-merle-switch-stage2 old mode 100755 new mode 100644 similarity index 50% rename from files/usr/bin/blue-merle-switch rename to files/usr/bin/blue-merle-switch-stage2 index 58af8fd..0fb0144 --- a/files/usr/bin/blue-merle-switch +++ b/files/usr/bin/blue-merle-switch-stage2 @@ -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