Fixed AT Terminal state sessions and shedule cell locking bugs.
This commit is contained in:
@@ -4,53 +4,123 @@
|
||||
CONFIG_FILE="/etc/cell_lock_schedule.conf"
|
||||
STATUS_FILE="/tmp/cell_lock_status"
|
||||
CELL_LOCK_SCRIPT="/usr/bin/set_cell_lock.sh"
|
||||
QUEUE_FILE="/tmp/at_pipe.txt"
|
||||
LOG_FILE="/tmp/cell_lock.log"
|
||||
|
||||
# Function to log messages
|
||||
log_message() {
|
||||
local level="$1"
|
||||
local message="$2"
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo "${timestamp} - [${level}] ${message}" >> "$LOG_FILE"
|
||||
logger -t cell_lock "${level}: ${message}"
|
||||
}
|
||||
|
||||
# Function to handle AT command queue
|
||||
handle_lock() {
|
||||
log_message "DEBUG" "Checking queue file status before lock"
|
||||
if [ ! -f "$QUEUE_FILE" ]; then
|
||||
log_message "DEBUG" "Queue file does not exist, creating it"
|
||||
touch "$QUEUE_FILE"
|
||||
fi
|
||||
|
||||
# Clean any stale entries
|
||||
if grep -q "\"command\":\"AT_COMMAND\"" "$QUEUE_FILE"; then
|
||||
local wait_count=0
|
||||
while [ $wait_count -lt 6 ]; do
|
||||
if ! grep -q "\"command\":\"AT_COMMAND\"" "$QUEUE_FILE"; then
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
wait_count=$((wait_count + 1))
|
||||
done
|
||||
[ $wait_count -eq 6 ] && sed -i "/\"command\":\"AT_COMMAND\"/d" "$QUEUE_FILE"
|
||||
fi
|
||||
|
||||
printf '{"command":"AT_COMMAND","pid":"%s","timestamp":"%s"}\n' \
|
||||
"$$" \
|
||||
"$(date '+%H:%M:%S')" >> "$QUEUE_FILE"
|
||||
}
|
||||
|
||||
# Function to execute AT command
|
||||
execute_at_command() {
|
||||
local command="$1"
|
||||
local result=""
|
||||
|
||||
log_message "DEBUG" "Executing AT command: ${command}"
|
||||
handle_lock
|
||||
|
||||
result=$(sms_tool at "$command" -t 4 2>&1)
|
||||
local status=$?
|
||||
|
||||
sed -i "/\"pid\":\"$$\"/d" "$QUEUE_FILE"
|
||||
|
||||
if [ $status -ne 0 ]; then
|
||||
log_message "ERROR" "Command failed with status $status: $command"
|
||||
log_message "ERROR" "Command output: $result"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_message "DEBUG" "Command successful. Output: $result"
|
||||
echo "$result"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to create set_cell_lock.sh script
|
||||
create_cell_lock_script() {
|
||||
# Only create the script if it doesn't exist
|
||||
if [ ! -f "$CELL_LOCK_SCRIPT" ]; then
|
||||
cat >"$CELL_LOCK_SCRIPT" <<'EOL'
|
||||
#!/bin/sh
|
||||
|
||||
ACTION=$1
|
||||
LTE_PARAMS=$2
|
||||
NR5G_PARAMS=$3
|
||||
|
||||
QUEUE_FILE="/tmp/at_pipe.txt"
|
||||
LOG_FILE="/tmp/cell_lock.log"
|
||||
|
||||
# Import common functions
|
||||
. /etc/quecmanager/imei_profile/common_functions.sh || {
|
||||
echo "Failed to import common functions"
|
||||
exit 1
|
||||
}
|
||||
|
||||
case "$ACTION" in
|
||||
enable)
|
||||
# Enable LTE lock if parameters exist
|
||||
if [ -n "$LTE_PARAMS" ]; then
|
||||
echo "AT+QNWLOCK=\"common/4g\",$LTE_PARAMS" | atinout - /dev/smd11 -
|
||||
execute_at_command "AT+QNWLOCK=\"common/4g\",$LTE_PARAMS"
|
||||
fi
|
||||
|
||||
# Enable NR5G lock if parameters exist
|
||||
if [ -n "$NR5G_PARAMS" ]; then
|
||||
echo "AT+QNWLOCK=\"common/5g\",$NR5G_PARAMS" | atinout - /dev/smd11 -
|
||||
execute_at_command "AT+QNWLOCK=\"common/5g\",$NR5G_PARAMS"
|
||||
fi
|
||||
;;
|
||||
|
||||
disable)
|
||||
# Disable LTE lock
|
||||
echo 'AT+QNWLOCK="common/4g",0' | atinout - /dev/smd11 -
|
||||
execute_at_command "AT+QNWLOCK=\"common/4g\",0"
|
||||
|
||||
# Disable NR5G lock
|
||||
echo 'AT+QNWLOCK="common/5g",0' | atinout - /dev/smd11 -
|
||||
execute_at_command "AT+QNWLOCK=\"common/5g\",0"
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "Invalid action"
|
||||
log_message "ERROR" "Invalid action: $ACTION"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Restart network registration to apply changes
|
||||
echo "AT+COPS=2" | atinout - /dev/smd11 -
|
||||
execute_at_command "AT+COPS=2"
|
||||
sleep 2
|
||||
echo "AT+COPS=0" | atinout - /dev/smd11 -
|
||||
execute_at_command "AT+COPS=0"
|
||||
exit 0
|
||||
EOL
|
||||
|
||||
# Make the script executable
|
||||
chmod +x "$CELL_LOCK_SCRIPT"
|
||||
log_message "INFO" "Created cell lock script at $CELL_LOCK_SCRIPT"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -58,6 +128,7 @@ EOL
|
||||
remove_cell_lock_script() {
|
||||
if [ -f "$CELL_LOCK_SCRIPT" ]; then
|
||||
rm "$CELL_LOCK_SCRIPT"
|
||||
log_message "INFO" "Removed cell lock script"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -76,16 +147,16 @@ save_config() {
|
||||
echo "START_TIME=$1" >"$CONFIG_FILE"
|
||||
echo "END_TIME=$2" >>"$CONFIG_FILE"
|
||||
echo "ENABLED=1" >>"$CONFIG_FILE"
|
||||
log_message "INFO" "Saved configuration - Start: $1, End: $2"
|
||||
}
|
||||
|
||||
# Function to disable scheduling
|
||||
disable_scheduling() {
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
sed -i 's/ENABLED=1/ENABLED=0/' "$CONFIG_FILE"
|
||||
log_message "INFO" "Disabled scheduling"
|
||||
fi
|
||||
# Remove any existing cron jobs
|
||||
crontab -l | grep -v "set_cell_lock.sh" | crontab -
|
||||
# Remove the set_cell_lock.sh script
|
||||
remove_cell_lock_script
|
||||
}
|
||||
|
||||
@@ -95,7 +166,7 @@ get_status() {
|
||||
ENABLED=$(grep "ENABLED=" "$CONFIG_FILE" | cut -d'=' -f2)
|
||||
START_TIME=$(grep "START_TIME=" "$CONFIG_FILE" | cut -d'=' -f2)
|
||||
END_TIME=$(grep "END_TIME=" "$CONFIG_FILE" | cut -d'=' -f2)
|
||||
|
||||
|
||||
echo "Status: 200 OK"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
@@ -110,12 +181,9 @@ get_status() {
|
||||
|
||||
# Handle POST requests
|
||||
if [ "$REQUEST_METHOD" = "POST" ]; then
|
||||
# Read POST data
|
||||
read -r POST_DATA
|
||||
|
||||
# Check if disabling is requested
|
||||
echo "$POST_DATA" | grep -q "disable=true"
|
||||
if [ $? -eq 0 ]; then
|
||||
|
||||
if echo "$POST_DATA" | grep -q "disable=true"; then
|
||||
disable_scheduling
|
||||
echo "Status: 200 OK"
|
||||
echo "Content-Type: application/json"
|
||||
@@ -123,56 +191,48 @@ if [ "$REQUEST_METHOD" = "POST" ]; then
|
||||
echo "{\"status\":\"success\",\"message\":\"Scheduling disabled\"}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Extract start and end times
|
||||
|
||||
START_TIME=$(echo "$POST_DATA" | grep -o 'start_time=[^&]*' | cut -d'=' -f2)
|
||||
END_TIME=$(echo "$POST_DATA" | grep -o 'end_time=[^&]*' | cut -d'=' -f2)
|
||||
|
||||
# Decode times
|
||||
|
||||
START_TIME=$(urldecode "$START_TIME")
|
||||
END_TIME=$(urldecode "$END_TIME")
|
||||
|
||||
# Validate times
|
||||
|
||||
if [ -z "$START_TIME" ] || [ -z "$END_TIME" ]; then
|
||||
log_message "ERROR" "Missing start or end time"
|
||||
echo "Status: 400 Bad Request"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
echo "{\"error\":\"Missing start or end time\"}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create set_cell_lock.sh script
|
||||
|
||||
create_cell_lock_script
|
||||
|
||||
# Convert times to cron format
|
||||
|
||||
CRON_START=$(convert_to_cron_time "$START_TIME")
|
||||
CRON_END=$(convert_to_cron_time "$END_TIME")
|
||||
|
||||
# Save configuration
|
||||
|
||||
save_config "$START_TIME" "$END_TIME"
|
||||
|
||||
|
||||
# Check current cell lock status and get parameters
|
||||
LTE_STATUS=$(echo 'AT+QNWLOCK="common/4g"' | atinout - /dev/smd11 -)
|
||||
NR5G_STATUS=$(echo 'AT+QNWLOCK="common/5g"' | atinout - /dev/smd11 -)
|
||||
|
||||
# Extract LTE parameters if locked
|
||||
LTE_STATUS=$(execute_at_command 'AT+QNWLOCK="common/4g"')
|
||||
NR5G_STATUS=$(execute_at_command 'AT+QNWLOCK="common/5g"')
|
||||
|
||||
LTE_PARAMS=$(echo "$LTE_STATUS" | grep -o '"common/4g",[^[:space:]]*' | cut -d',' -f2-)
|
||||
NR5G_PARAMS=$(echo "$NR5G_STATUS" | grep -o '"common/5g",[^[:space:]]*' | cut -d',' -f2-)
|
||||
|
||||
# Create temporary file for new crontab
|
||||
|
||||
TEMP_CRON=$(mktemp)
|
||||
|
||||
# Get existing crontab entries (excluding our script)
|
||||
|
||||
crontab -l 2>/dev/null | grep -v "set_cell_lock.sh" >"$TEMP_CRON"
|
||||
|
||||
# Add new entries
|
||||
|
||||
echo "$CRON_START * * * $CELL_LOCK_SCRIPT enable \"$LTE_PARAMS\" \"$NR5G_PARAMS\"" >>"$TEMP_CRON"
|
||||
echo "$CRON_END * * * $CELL_LOCK_SCRIPT disable" >>"$TEMP_CRON"
|
||||
|
||||
# Install new crontab
|
||||
|
||||
crontab "$TEMP_CRON"
|
||||
rm "$TEMP_CRON"
|
||||
|
||||
|
||||
log_message "INFO" "Scheduling enabled with start time $START_TIME and end time $END_TIME"
|
||||
|
||||
echo "Status: 200 OK"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
@@ -194,6 +254,7 @@ if [ "$REQUEST_METHOD" = "GET" ]; then
|
||||
fi
|
||||
|
||||
# If no valid request is made
|
||||
log_message "ERROR" "Invalid request received"
|
||||
echo "Status: 400 Bad Request"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
|
||||
Reference in New Issue
Block a user