-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuplink.sh
107 lines (87 loc) · 3.15 KB
/
uplink.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
#################################
## LTE Uplink for WATNE ##
#################################
# Self-healing LTE connection for when you need it most.
# Messed up so bad you can't connect to your drone anymore?
# If it has enough power to last until reboot time you get another shot!
# Just make sure you don't set the daily reboot to a time where it's likely to be in the air.
# WATNE's ArduPilot runs directly off the Linux stack and a reboot would cause the intricate flying machine to fall to the ground with all the elegance of a very expensive toaster oven.
# 300s = 5mins
# 3600s = 1hr
# You figure out the rest.
LOG_FILE="/home/pi/uplink.log"
PING_TARGET="dataplicity.com"
WWAN_INTERFACE="wwan0"
PING_INTERVAL=300 # 5 minutes
RETRY_INTERVAL=60
RETRY_ON_FAILURE=true
MAX_RETRIES=3
APN="hologram"
# Schedule daily reboot at 01:00
sudo shutdown -r 01:00
function log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> "$LOG_FILE"
}
function check_connection() {
local packet_loss=$(ping -I "$WWAN_INTERFACE" -c 3 -W 3 "$PING_TARGET" | grep -oP '\d+(?=% packet loss)' || echo "Error")
if [[ "$packet_loss" == "Error" || "$packet_loss" -ge 100 ]]; then
log "Connection check failed. Packet loss: ${packet_loss:-Unknown}%"
return 1
else
log "Received response from ping. Packet loss: $packet_loss%"
return 0
fi
}
function reconnect_wwan() {
log "Attempting to reconnect $WWAN_INTERFACE..."
# Step 1: Disable WWAN interface
sudo ip link set "$WWAN_INTERFACE" down
# Step 2: Set raw_ip mode
echo 'Y' | sudo tee "/sys/class/net/$WWAN_INTERFACE/qmi/raw_ip"
# Step 3: Enable WWAN interface
sudo ip link set "$WWAN_INTERFACE" up
# Step 4: Start network with qmicli
sudo qmicli -p -d /dev/cdc-wdm0 --device-open-net='net-raw-ip|net-no-qos-header' --wds-start-network="apn='$APN',ip-type=4" --client-no-release-cid
# Step 5: Run udhcpc and log relevant messages
udhcpc_output=$(sudo udhcpc -q -f -i "$WWAN_INTERFACE" 2>&1)
if [[ $? -eq 0 ]]; then
log "DHCP lease obtained successfully."
else
log "DHCP configuration failure: $udhcpc_output"
fi
}
function handle_retries() {
local retries=$1
if [[ "$retries" -ge "$MAX_RETRIES" ]]; then
log "Exceeded maximum retries. Rebooting device..."
sudo reboot
else
log "Retrying connection. Attempt $((retries + 1)) of $MAX_RETRIES..."
reconnect_wwan
sleep "$RETRY_INTERVAL"
fi
}
# Log first attempt since boot
log "First attempt since boot."
# Main loop
while true; do
if check_connection; then
sleep "$PING_INTERVAL"
else
log "Connection lost. Reconnecting..."
# Kill qmicli and udhcpc processes
sudo pkill -f "qmicli|udhcpc"
# Retry connection steps every RETRY_INTERVAL seconds
if "$RETRY_ON_FAILURE"; then
retry_counter=0
while ! check_connection; do
handle_retries "$retry_counter"
((retry_counter++))
done
else
reconnect_wwan
sleep "$RETRY_INTERVAL"
fi
fi
done