Fixed AT Terminal state sessions and shedule cell locking bugs.
This commit is contained in:
@@ -1,42 +1,135 @@
|
||||
#!/bin/sh
|
||||
# CGI header
|
||||
|
||||
# Set content-type for JSON response
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Queue file
|
||||
# Define file paths and configuration
|
||||
QUEUE_FILE="/tmp/at_pipe.txt"
|
||||
RESULT_FILE="/tmp/at_results.json"
|
||||
LOG_FILE="/var/log/at_commands.log"
|
||||
|
||||
# Create queue file if it doesn't exist
|
||||
touch "${QUEUE_FILE}"
|
||||
LOCK_KEYWORD="FETCH_DATA_LOCK"
|
||||
CELL_SCAN_KEYWORD="CELL_SCAN"
|
||||
MAX_WAIT=6 # Maximum seconds to wait for lock
|
||||
COMMAND_TIMEOUT=4 # Timeout for individual AT commands
|
||||
|
||||
# Function to log messages
|
||||
log_message() {
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "${LOG_FILE}"
|
||||
logger -t at_commands "$1"
|
||||
}
|
||||
|
||||
# Function to generate random ID
|
||||
generate_random_id() {
|
||||
# Combine multiple sources of randomness
|
||||
local timestamp=$(date +%s%N)
|
||||
local random1=$(head -c 4 /dev/urandom | xxd -p)
|
||||
local random2=$(echo $$ $RANDOM | md5sum | head -c 8)
|
||||
echo "${timestamp}-${random1}-${random2}"
|
||||
# Function to output error in JSON format
|
||||
output_error() {
|
||||
printf '{"status":"error","message":"%s","timestamp":"%s"}\n' "$1" "$(date '+%H:%M:%S')"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Function to escape special characters for JSON
|
||||
# Function to wait for high-priority operations
|
||||
wait_for_high_priority() {
|
||||
while grep -q "\"command\":\"$CELL_SCAN_KEYWORD\"" "$QUEUE_FILE" || \
|
||||
grep -q "\"priority\":\"high\"" "$QUEUE_FILE"; do
|
||||
log_message "Waiting for high-priority operation to complete"
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
# Function to clean and add lock with simplified timeout logic
|
||||
add_clean_lock() {
|
||||
local TIMESTAMP=$(date +%s)
|
||||
local WAIT_START=$(date +%s)
|
||||
|
||||
# First, wait for any high-priority operations
|
||||
wait_for_high_priority
|
||||
|
||||
while true; do
|
||||
local CURRENT_TIME=$(date +%s)
|
||||
|
||||
# After MAX_WAIT seconds, forcibly remove any existing lock
|
||||
if [ $((CURRENT_TIME - WAIT_START)) -ge $MAX_WAIT ]; then
|
||||
sed -i "/${LOCK_KEYWORD}/d" "$QUEUE_FILE"
|
||||
log_message "Removed existing lock after $MAX_WAIT seconds timeout"
|
||||
fi
|
||||
|
||||
# Add our lock entry with low priority
|
||||
printf '{"id":"%s","timestamp":"%s","command":"%s","status":"lock","pid":"%s","start_time":"%s","priority":"low"}\n' \
|
||||
"${LOCK_KEYWORD}" \
|
||||
"$(date '+%H:%M:%S')" \
|
||||
"${LOCK_KEYWORD}" \
|
||||
"$$" \
|
||||
"$TIMESTAMP" >> "$QUEUE_FILE"
|
||||
|
||||
# Verify our lock was written
|
||||
if grep -q "\"pid\":\"$$\".*\"start_time\":\"$TIMESTAMP\"" "$QUEUE_FILE"; then
|
||||
log_message "Lock created by PID $$ at $TIMESTAMP"
|
||||
trap 'remove_lock; exit' INT TERM EXIT
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [ $((CURRENT_TIME - WAIT_START)) -lt $MAX_WAIT ]; then
|
||||
sleep 1
|
||||
else
|
||||
log_message "Failed to acquire lock after $MAX_WAIT seconds"
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Function to remove lock
|
||||
remove_lock() {
|
||||
sed -i "/\"pid\":\"$$\"/d" "$QUEUE_FILE"
|
||||
log_message "Lock removed by PID $$"
|
||||
}
|
||||
|
||||
# Function to escape JSON
|
||||
escape_json() {
|
||||
echo "$1" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g'
|
||||
printf '%s' "$1" | awk '
|
||||
BEGIN { RS="\n"; ORS="\\n" }
|
||||
{
|
||||
gsub(/\\/, "\\\\")
|
||||
gsub(/"/, "\\\"")
|
||||
gsub(/\r/, "")
|
||||
gsub(/\t/, "\\t")
|
||||
gsub(/\f/, "\\f")
|
||||
gsub(/\b/, "\\b")
|
||||
print
|
||||
}
|
||||
' | sed 's/\\n$//'
|
||||
}
|
||||
|
||||
# Function to decode URL
|
||||
decode_url() {
|
||||
local encoded="$1"
|
||||
# First handle percent-encoded characters
|
||||
printf '%b' "${encoded}" | sed -e 's/%\([0-9A-Fa-f][0-9A-Fa-f]\)/\\x\1/g' | xargs -0 echo -e |
|
||||
# Then handle plus signs separately (preserve them for AT commands)
|
||||
sed 's/[+]/%2B/g' | sed 's/%2B/+/g'
|
||||
# Simplified AT command execution with basic response validation
|
||||
execute_at_command() {
|
||||
local CMD="$1"
|
||||
local RETRY_COUNT=0
|
||||
local MAX_RETRIES=3
|
||||
local OUTPUT=""
|
||||
|
||||
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
|
||||
# Execute command with -D parameter to include OK/ERROR responses
|
||||
OUTPUT=$(timeout $COMMAND_TIMEOUT sms_tool at "$CMD" -D 2>&1)
|
||||
local EXIT_CODE=$?
|
||||
|
||||
if [ $EXIT_CODE -eq 0 ]; then
|
||||
# Check if response contains "CME" for execution failure
|
||||
if echo "$OUTPUT" | grep -q "CME"; then
|
||||
echo "$OUTPUT"
|
||||
return 2 # Command execution failed
|
||||
# Check if response contains OK (simple grep)
|
||||
elif echo "$OUTPUT" | grep -q "OK"; then
|
||||
echo "$OUTPUT"
|
||||
return 0
|
||||
else
|
||||
# Any other response is considered unsupported
|
||||
echo "$OUTPUT"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
RETRY_COUNT=$((RETRY_COUNT + 1))
|
||||
[ $RETRY_COUNT -lt $MAX_RETRIES ] && sleep 1
|
||||
done
|
||||
|
||||
log_message "Command failed after $MAX_RETRIES attempts: $CMD"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Get command from query string
|
||||
@@ -44,31 +137,44 @@ QUERY_STRING="${QUERY_STRING:-}"
|
||||
RAW_COMMAND=$(echo "${QUERY_STRING}" | sed 's/^command=//')
|
||||
|
||||
if [ -n "${RAW_COMMAND}" ]; then
|
||||
# Decode URL-encoded command with fixed plus sign handling
|
||||
AT_COMMAND=$(decode_url "${RAW_COMMAND}")
|
||||
# Decode URL-encoded command
|
||||
AT_COMMAND=$(printf '%b' "${RAW_COMMAND}" | sed -e 's/%\([0-9A-Fa-f][0-9A-Fa-f]\)/\\x\1/g' | xargs -0 echo -e)
|
||||
|
||||
# Generate unique random ID
|
||||
CMD_ID=$(generate_random_id)
|
||||
# Set timeout for the entire script
|
||||
( sleep 60; kill -TERM $$ 2>/dev/null ) &
|
||||
TIMEOUT_PID=$!
|
||||
|
||||
# Create timestamp
|
||||
TIMESTAMP=$(date '+%H:%M:%S')
|
||||
if ! add_clean_lock; then
|
||||
kill $TIMEOUT_PID 2>/dev/null
|
||||
output_error "Failed to acquire lock for command processing"
|
||||
fi
|
||||
|
||||
# Escape command for JSON
|
||||
# Execute command and capture result
|
||||
RESULT=$(execute_at_command "${AT_COMMAND}")
|
||||
EXIT_CODE=$?
|
||||
|
||||
# Clean up
|
||||
remove_lock
|
||||
kill $TIMEOUT_PID 2>/dev/null
|
||||
|
||||
# Escape command and result for JSON
|
||||
ESCAPED_COMMAND=$(escape_json "${AT_COMMAND}")
|
||||
ESCAPED_RESULT=$(escape_json "${RESULT}")
|
||||
|
||||
# Create JSON entry for queue (all in one line)
|
||||
QUEUE_ENTRY=$(printf '{"id":"%s","timestamp":"%s","command":"%s","status":"pending"}\n' \
|
||||
"${CMD_ID}" "${TIMESTAMP}" "${ESCAPED_COMMAND}")
|
||||
|
||||
# Add to queue file
|
||||
echo "${QUEUE_ENTRY}" >> "${QUEUE_FILE}"
|
||||
log_message "Queued command: ${AT_COMMAND} with ID: ${CMD_ID}"
|
||||
|
||||
# Return immediate response
|
||||
printf '{"status":"queued","message":"Command has been queued","command":"%s","id":"%s","queued_at":"%s"}\n' \
|
||||
"${ESCAPED_COMMAND}" "${CMD_ID}" "${TIMESTAMP}"
|
||||
# Return response based on simplified exit codes
|
||||
if [ $EXIT_CODE -eq 0 ]; then
|
||||
# Command succeeded with OK response
|
||||
printf '{"status":"success","command":"%s","response":"%s","timestamp":"%s"}\n' \
|
||||
"${ESCAPED_COMMAND}" "${ESCAPED_RESULT}" "$(date '+%H:%M:%S')"
|
||||
elif [ $EXIT_CODE -eq 2 ]; then
|
||||
# Command contains CME - execution failed
|
||||
printf '{"status":"error","command":"%s","message":"Command execution failed","response":"%s","timestamp":"%s"}\n' \
|
||||
"${ESCAPED_COMMAND}" "${ESCAPED_RESULT}" "$(date '+%H:%M:%S')"
|
||||
else
|
||||
# Any other response is considered unsupported
|
||||
printf '{"status":"error","command":"%s","message":"Unsupported command","response":"%s","timestamp":"%s"}\n' \
|
||||
"${ESCAPED_COMMAND}" "${ESCAPED_RESULT}" "$(date '+%H:%M:%S')"
|
||||
fi
|
||||
else
|
||||
# Return error response
|
||||
printf '{"status":"error","message":"No command provided","timestamp":"%s"}\n' "$(date '+%H:%M:%S')"
|
||||
exit 1
|
||||
output_error "No command provided"
|
||||
fi
|
||||
@@ -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 ""
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Set content-type for JSON response
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Configuration
|
||||
QUEUE_FILE="/tmp/at_pipe.txt"
|
||||
CELL_SCAN_KEYWORD="CELL_SCAN"
|
||||
MAX_SCAN_TIME=180 # 3 minutes maximum scan time
|
||||
LOCK_WAIT_TIME=6 # Maximum seconds to wait for lock
|
||||
|
||||
# Function to output error in JSON format
|
||||
output_error() {
|
||||
printf '{"status":"error","error":"%s","output":""}\n' "$1"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Function to create and verify queue entry
|
||||
create_queue_entry() {
|
||||
local TIMESTAMP=$(date +%s)
|
||||
local entry=$(printf '{"id":"%s","timestamp":"%s","command":"%s","status":"scanning","pid":"%s","start_time":"%s","priority":"high"}\n' \
|
||||
"${CELL_SCAN_KEYWORD}" \
|
||||
"$(date '+%H:%M:%S')" \
|
||||
"${CELL_SCAN_KEYWORD}" \
|
||||
"$$" \
|
||||
"$TIMESTAMP")
|
||||
|
||||
echo "$entry" >> "$QUEUE_FILE"
|
||||
|
||||
# Verify our entry was written
|
||||
if grep -q "\"pid\":\"$$\".*\"start_time\":\"$TIMESTAMP\"" "$QUEUE_FILE"; then
|
||||
logger -t cell_scan "Queue entry created successfully"
|
||||
return 0
|
||||
else
|
||||
logger -t cell_scan "Failed to create queue entry"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Remove our entry from the queue
|
||||
remove_queue_entry() {
|
||||
sed -i "/\"pid\":\"$$\"/d" "$QUEUE_FILE"
|
||||
logger -t cell_scan "Removed entry for PID $$"
|
||||
}
|
||||
|
||||
# Escape special characters for JSON string
|
||||
escape_json() {
|
||||
printf '%s' "$1" | awk '
|
||||
BEGIN { RS="\n"; ORS="\\n" }
|
||||
{
|
||||
gsub(/\\/, "\\\\")
|
||||
gsub(/"/, "\\\"")
|
||||
gsub(/\r/, "")
|
||||
print
|
||||
}' | sed 's/\\n$//'
|
||||
}
|
||||
|
||||
# Execute cell scan with proper timeout handling
|
||||
execute_cell_scan() {
|
||||
local tmp_output=$(mktemp)
|
||||
local scan_pid
|
||||
|
||||
# Start scan in background
|
||||
(sms_tool at "AT+QSCAN=3,1" -t $MAX_SCAN_TIME > "$tmp_output" 2>/dev/null) &
|
||||
scan_pid=$!
|
||||
logger -t cell_scan "Started QSCAN with PID: $scan_pid"
|
||||
|
||||
# Wait for scan to complete or timeout
|
||||
local wait_time=0
|
||||
while [ $wait_time -lt $MAX_SCAN_TIME ]; do
|
||||
if ! kill -0 $scan_pid 2>/dev/null; then
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
wait_time=$((wait_time + 1))
|
||||
done
|
||||
|
||||
# Check if we need to kill the scan
|
||||
if [ $wait_time -ge $MAX_SCAN_TIME ]; then
|
||||
kill $scan_pid 2>/dev/null
|
||||
wait $scan_pid 2>/dev/null
|
||||
logger -t cell_scan "Scan timed out after $MAX_SCAN_TIME seconds"
|
||||
output_error "Scan timed out"
|
||||
fi
|
||||
|
||||
logger -t cell_scan "Scan completed in $wait_time seconds"
|
||||
|
||||
# Process and output results
|
||||
if [ -s "$tmp_output" ]; then
|
||||
local escaped_output=$(escape_json "$(cat "$tmp_output")")
|
||||
printf '{"status":"success","output":"%s"}\n' "$escaped_output"
|
||||
else
|
||||
output_error "No scan results"
|
||||
fi
|
||||
|
||||
rm -f "$tmp_output"
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
# Set global timeout
|
||||
( sleep $MAX_SCAN_TIME; kill -TERM $$ 2>/dev/null ) &
|
||||
TIMEOUT_PID=$!
|
||||
|
||||
# Ensure queue file exists
|
||||
touch "$QUEUE_FILE"
|
||||
|
||||
# Create queue entry
|
||||
if ! create_queue_entry; then
|
||||
output_error "Failed to create queue entry"
|
||||
fi
|
||||
|
||||
# Register cleanup handler
|
||||
trap 'remove_queue_entry; kill $TIMEOUT_PID 2>/dev/null; exit' INT TERM EXIT
|
||||
|
||||
# Execute scan and output results
|
||||
execute_cell_scan
|
||||
|
||||
# Cleanup
|
||||
kill $TIMEOUT_PID 2>/dev/null
|
||||
remove_queue_entry
|
||||
}
|
||||
|
||||
# Start main execution
|
||||
main
|
||||
@@ -7,6 +7,7 @@ echo ""
|
||||
# Define file paths and configuration
|
||||
QUEUE_FILE="/tmp/at_pipe.txt"
|
||||
LOCK_KEYWORD="FETCH_DATA_LOCK"
|
||||
CELL_SCAN_KEYWORD="CELL_SCAN" # Added cell scan keyword
|
||||
MAX_WAIT=6 # Maximum seconds to wait for lock
|
||||
|
||||
# Function to output error in JSON format
|
||||
@@ -15,11 +16,23 @@ output_error() {
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Function to wait for high-priority operations
|
||||
wait_for_high_priority() {
|
||||
while grep -q "\"command\":\"$CELL_SCAN_KEYWORD\"" "$QUEUE_FILE" || \
|
||||
grep -q "\"priority\":\"high\"" "$QUEUE_FILE"; do
|
||||
logger -t at_commands "Waiting for high-priority operation to complete"
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
# Function to clean and add lock with simplified timeout logic
|
||||
add_clean_lock() {
|
||||
local TIMESTAMP=$(date +%s)
|
||||
local WAIT_START=$(date +%s)
|
||||
|
||||
# First, wait for any high-priority operations
|
||||
wait_for_high_priority
|
||||
|
||||
while true; do
|
||||
local CURRENT_TIME=$(date +%s)
|
||||
|
||||
@@ -30,8 +43,8 @@ add_clean_lock() {
|
||||
logger -t at_commands "Removed existing lock after $MAX_WAIT seconds timeout"
|
||||
fi
|
||||
|
||||
# Add our lock entry
|
||||
printf '{"id":"%s","timestamp":"%s","command":"%s","status":"lock","pid":"%s","start_time":"%s"}\n' \
|
||||
# Add our lock entry with low priority
|
||||
printf '{"id":"%s","timestamp":"%s","command":"%s","status":"lock","pid":"%s","start_time":"%s","priority":"low"}\n' \
|
||||
"${LOCK_KEYWORD}" \
|
||||
"$(date '+%H:%M:%S')" \
|
||||
"${LOCK_KEYWORD}" \
|
||||
|
||||
@@ -42,16 +42,33 @@ fi
|
||||
# Fix the spacing in the cron line to ensure exactly 5 fields
|
||||
CRON_LINE="0 0 * * * $SCRIPT_PATH"
|
||||
|
||||
# Install crontab if not already present
|
||||
# Install crontab while preserving other entries
|
||||
if ! crontab -l | grep -Fq "$SCRIPT_PATH"; then
|
||||
# Get existing crontab - ensuring clean formatting
|
||||
(crontab -l 2>/dev/null | grep -v "$SCRIPT_PATH" || true; echo "$CRON_LINE") | crontab -
|
||||
if [ $? -eq 0 ]; then
|
||||
# Create temporary file
|
||||
TEMP_CRON=$(mktemp)
|
||||
|
||||
# Get existing crontab
|
||||
crontab -l 2>/dev/null > "$TEMP_CRON" || true
|
||||
|
||||
# Remove any old instances of this script
|
||||
if [ -s "$TEMP_CRON" ]; then
|
||||
sed -i "\#$SCRIPT_PATH#d" "$TEMP_CRON"
|
||||
fi
|
||||
|
||||
# Add new cron line
|
||||
echo "$CRON_LINE" >> "$TEMP_CRON"
|
||||
|
||||
# Install new crontab
|
||||
if crontab "$TEMP_CRON"; then
|
||||
logger -t log_cleanup "Successfully installed crontab job"
|
||||
else
|
||||
logger -t log_cleanup "Failed to install crontab job"
|
||||
rm -f "$TEMP_CRON"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Clean up
|
||||
rm -f "$TEMP_CRON"
|
||||
fi
|
||||
|
||||
# Clean log files
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Configuration
|
||||
LOGDIR="/www/signal_graphs"
|
||||
MAX_ENTRIES=10
|
||||
INTERVAL=60
|
||||
QUEUE_FILE="/tmp/at_pipe.txt"
|
||||
FETCH_LOCK_KEYWORD="FETCH_LOCK"
|
||||
CELL_SCAN_KEYWORD="CELL_SCAN" # Added to check for cell scan
|
||||
PAUSE_FILE="/tmp/signal_logging.pause"
|
||||
|
||||
# Ensure the directory exists
|
||||
@@ -13,7 +13,7 @@ mkdir -p "$LOGDIR"
|
||||
|
||||
# Check for stale entries and clean them
|
||||
check_and_clean_stale() {
|
||||
local command_type="$1" # Either "FETCH_LOCK" or "AT_COMMAND"
|
||||
local command_type="$1"
|
||||
local wait_count=0
|
||||
|
||||
while [ $wait_count -lt 6 ]; do
|
||||
@@ -33,13 +33,30 @@ check_and_clean_stale() {
|
||||
return 0
|
||||
}
|
||||
|
||||
# Simplified lock handling
|
||||
# Wait for high-priority operations
|
||||
wait_for_high_priority() {
|
||||
while grep -q "\"priority\":\"high\"" "$QUEUE_FILE"; do
|
||||
logger -t signal_metrics "Waiting for high-priority operation to complete"
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
# Simplified lock handling with priority awareness
|
||||
handle_lock() {
|
||||
# First check and clean any FETCH_LOCK entries
|
||||
# Wait for any high-priority operations first
|
||||
wait_for_high_priority
|
||||
|
||||
# Check and clean any FETCH_LOCK entries
|
||||
check_and_clean_stale "FETCH_LOCK"
|
||||
|
||||
# Add our own entry
|
||||
printf '{"command":"AT_COMMAND","pid":"%s","timestamp":"%s"}\n' \
|
||||
# Check for cell scan operations
|
||||
while grep -q "\"command\":\"$CELL_SCAN_KEYWORD\"" "$QUEUE_FILE"; do
|
||||
logger -t signal_metrics "Waiting for cell scan to complete"
|
||||
sleep 1
|
||||
done
|
||||
|
||||
# Add our low-priority entry
|
||||
printf '{"command":"AT_COMMAND","pid":"%s","timestamp":"%s","priority":"low"}\n' \
|
||||
"$$" \
|
||||
"$(date '+%H:%M:%S')" >>"$QUEUE_FILE"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user