Official Hot Fix for QuecManager 2.3.0

This commit is contained in:
Russel Yasol
2025-08-27 11:31:00 +08:00
parent 029d657d08
commit 6bd2c7ea52
38 changed files with 4133 additions and 202 deletions

View File

@@ -2,11 +2,40 @@
# AT Queue Client for OpenWRT
# Located in /www/cgi-bin/services/at_queue_client
# Load centralized logging
. /www/cgi-bin/services/quecmanager_logger.sh
AUTH_FILE="/tmp/auth_success"
QUEUE_DIR="/tmp/at_queue"
RESULTS_DIR="$QUEUE_DIR/results"
QUEUE_MANAGER="/www/cgi-bin/services/at_queue_manager.sh"
POLL_INTERVAL=0.01
SCRIPT_NAME_LOG="at_queue_client"
# Logging function - uses both centralized and system logging
log_at_queue_client() {
local level="$1"
local message="$2"
# Use centralized logging
case "$level" in
"error")
qm_log_error "service" "$SCRIPT_NAME_LOG" "$message"
;;
"warn")
qm_log_warn "service" "$SCRIPT_NAME_LOG" "$message"
;;
"debug")
qm_log_debug "service" "$SCRIPT_NAME_LOG" "$message"
;;
*)
qm_log_info "service" "$SCRIPT_NAME_LOG" "$message"
;;
esac
# Also maintain system logging for compatibility
logger -t at_queue -p "daemon.$level" "$message"
}
usage() {
echo "Usage: $0 [options] <AT command>"
@@ -20,14 +49,14 @@ usage() {
# Output JSON response
output_json() {
local content="$1"
local headers="${2:-1}" # Default to showing headers
local headers="${2:-1}" # Default to showing headers
echo "$content"
}
# URL decode function
urldecode() {
local encoded="$1"
logger -t at_queue -p daemon.debug "urldecode: input='$encoded'"
log_at_queue_client "debug" "urldecode: input='$encoded'"
# Handle %2B -> + and %22 -> " conversions
local decoded="${encoded//%2B/+}"
@@ -35,10 +64,23 @@ urldecode() {
# Then handle other encoded characters
decoded=$(printf '%b' "${decoded//%/\\x}")
logger -t at_queue -p daemon.debug "urldecode: output='$decoded'"
log_at_queue_client "debug" "urldecode: output='$decoded'"
echo "$decoded"
}
# URL encode function (simplified for AT commands)
urlencode() {
local string="$1"
# Simple encoding for common AT command characters
string="${string// /%20}"
string="${string//+/%2B}"
string="${string//\"/%22}"
string="${string//=/%3D}"
string="${string//&/%26}"
string="${string//?/%3F}"
echo "$string"
}
# Extract command ID from response with improved error handling
get_command_id() {
local response="$1"
@@ -72,19 +114,19 @@ get_command_id() {
# Normalize AT command
normalize_at_command() {
local cmd="$1"
logger -t at_queue -p daemon.debug "normalize: input='$cmd'"
log_at_queue_client "debug" "normalize: input='$cmd'"
# URL decode the command
cmd=$(urldecode "$cmd")
logger -t at_queue -p daemon.debug "normalize: after urldecode='$cmd'"
log_at_queue_client "debug" "normalize: after urldecode='$cmd'"
# Remove any carriage returns or newlines
cmd=$(echo "$cmd" | tr -d '\r\n')
logger -t at_queue -p daemon.debug "normalize: after cleanup='$cmd'"
log_at_queue_client "debug" "normalize: after cleanup='$cmd'"
# Trim leading/trailing whitespace while preserving quotes
cmd=$(echo "$cmd" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
logger -t at_queue -p daemon.debug "normalize: final output='$cmd'"
log_at_queue_client "debug" "normalize: final output='$cmd'"
echo "$cmd"
}
@@ -101,7 +143,7 @@ submit_command() {
# Submit using appropriate method
if [ "${SCRIPT_NAME}" != "" ]; then
# CGI mode - direct execution
# CGI mode - direct execution like the original working version
local escaped_cmd=$(echo "$cmd" | sed 's/"/\\"/g')
QUERY_STRING="action=enqueue&command=${escaped_cmd}&priority=$priority" "$QUEUE_MANAGER"
else
@@ -118,7 +160,7 @@ check_result() {
if [ -f "$RESULTS_DIR/$cmd_id.json" ]; then
local result_content=$(cat "$RESULTS_DIR/$cmd_id.json")
if [ -z "$result_content" ]; then
logger -t at_queue -p daemon.error "Empty result file for command ID: $cmd_id"
log_at_queue_client "error" "Empty result file for command ID: $cmd_id"
local error_json="{\"error\":\"Empty result file\",\"command_id\":\"$cmd_id\"}"
output_json "$error_json" "$show_headers"
return 1

View File

@@ -1,8 +1,9 @@
#!/bin/sh
# On SDXPINN and (assumed) SDXLEMUR with OpenWRT Overlay, the environment NEEDS to be /bin/sh,
# whereas QTI environment on SDXLEMUR uses /bin/bash. This assumption requires verification.
# Set content-type for JSON response
echo "Content-type: application/json"
echo ""
printf "Content-type: application/json\r\n"
printf "\r\n"
# Define paths and constants to match queue system
QUEUE_DIR="/tmp/at_queue"
@@ -13,11 +14,11 @@ TOKEN_FILE="$QUEUE_DIR/token"
# Logging function (minimized)
log_message() {
# Only log errors and critical info
if [ "$1" = "error" ] || [ "$1" = "crit" ]; then
if [ "$1" = "error" ] || [ "$1" = "crit" ]; then
logger -t at_queue -p "daemon.$1" "$2"
fi
fi
}
mkdir -m755 -p ${QUEUE_DIR}
# Enhanced JSON string escaping function
escape_json() {
printf '%s' "$1" | awk '
@@ -36,39 +37,46 @@ escape_json() {
# Acquire token directly (avoid CGI overhead)
acquire_token() {
local priority="${1:-10}"
local max_attempts=10
local attempt=0
priority="${1:-10}"
max_attempts=10
attempt=0
log_message "debug" "Acquiring token"
while [ $attempt -lt $max_attempts ]; do
# Check if token file exists
if [ -f "$TOKEN_FILE" ]; then
local current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
local current_priority=$(cat "$TOKEN_FILE" | jsonfilter -e '@.priority' 2>/dev/null)
local timestamp=$(cat "$TOKEN_FILE" | jsonfilter -e '@.timestamp' 2>/dev/null)
local current_time=$(date +%s)
current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
current_priority=$(cat "$TOKEN_FILE" | jsonfilter -e '@.priority' 2>/dev/null)
timestamp=$(cat "$TOKEN_FILE" | jsonfilter -e '@.timestamp' 2>/dev/null)
current_time=$(date +%s)
log_message "info" "current_holder: ${current_holder}"
log_message "info" "current_priority: ${current_priority}"
log_message "info" "timestamp: ${timestamp}"
log_message "info" "current_time: ${current_time}"
# Check for expired token (> 30 seconds old)
if [ $((current_time - timestamp)) -gt 30 ] || [ -z "$current_holder" ]; then
# Remove expired token
log_message "debug" "Removing token, cur time minus timestamp gt 30 or current-holder not set"
rm -f "$TOKEN_FILE" 2>/dev/null
elif [ $priority -lt $current_priority ]; then
# Preempt lower priority token
log_message "debug" "Current priority lower priority than other task"
rm -f "$TOKEN_FILE" 2>/dev/null
else
# Try again
sleep 0.1
attempt=$((attempt + 1))
log_message "debug" "Trying again $attempt"
continue
fi
else
log_message "debug" "No token file"
fi
# Try to create token file
echo "{\"id\":\"$LOCK_ID\",\"priority\":$priority,\"timestamp\":$(date +%s)}" >"$TOKEN_FILE" 2>/dev/null
printf "{\"id\":\"$LOCK_ID\",\"priority\":$priority,\"timestamp\":$(date +%s)}" >"$TOKEN_FILE" 2>/dev/null
chmod 644 "$TOKEN_FILE" 2>/dev/null
# Verify we got the token
local holder=$(cat "$TOKEN_FILE" 2>/dev/null | jsonfilter -e '@.id' 2>/dev/null)
holder=$(cat "$TOKEN_FILE" 2>/dev/null | jsonfilter -e '@.id' 2>/dev/null)
if [ "$holder" = "$LOCK_ID" ]; then
return 0
fi
@@ -79,13 +87,16 @@ acquire_token() {
return 1
}
# Release token directly
release_token() {
log_message "debug" "Release Token"
# Only remove if it's our token
if [ -f "$TOKEN_FILE" ]; then
local current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
log_message "debug" "Has Token file"
current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
log_message "debug" "Release Token, Current Holder: ${current_holder}"
if [ "$current_holder" = "$LOCK_ID" ]; then
log_message "debug" "Release Token, Current Holder: ${current_holder}, removing token"
rm -f "$TOKEN_FILE" 2>/dev/null
fi
fi
@@ -93,18 +104,21 @@ release_token() {
# Direct AT command execution with minimal overhead
execute_at_command() {
local CMD="$1"
CMD="$1"
sms_tool at "$CMD" -t 3 2>/dev/null
}
# Batch process all commands with a single token
process_all_commands() {
local commands="$1"
local priority="${2:-10}"
local first=1
commands="$1"
priority="${2:-10}"
first=1
log_message "info" "Before acquire_token check"
acquire_token "$priority"
trying=$?
log_message "debug" "trying: ${trying}"
# Acquire a single token for all commands
if ! acquire_token "$priority"; then
if [ $trying -ne 0 ]; then
log_message "error" "Failed to acquire token for batch processing"
# Return all failed responses
printf '['
@@ -115,7 +129,7 @@ process_all_commands() {
ESCAPED_CMD=$(escape_json "$cmd")
printf '{"command":"%s","response":"Failed to acquire token","status":"error"}' "${ESCAPED_CMD}"
done
printf ']\n'
printf ']\r\n'
return 1
fi
@@ -124,10 +138,9 @@ process_all_commands() {
for cmd in $commands; do
[ $first -eq 0 ] && printf ','
first=0
OUTPUT=$(execute_at_command "$cmd")
local CMD_STATUS=$?
CMD_STATUS=$?
log_message "debug" "CMD: ${cmd}, OUTPUT: ${OUTPUT}, CMD_STAT: ${CMD_STATUS}"
ESCAPED_CMD=$(escape_json "$cmd")
ESCAPED_OUTPUT=$(escape_json "$OUTPUT")
@@ -140,8 +153,7 @@ process_all_commands() {
"${ESCAPED_CMD}"
fi
done
printf ']\n'
printf ']\r\n'
# Release token after all commands are done
release_token
return 0
@@ -184,15 +196,14 @@ if echo "$COMMANDS" | grep -qi "AT+QSCAN"; then
PRIORITY=1
fi
# Process commands with timeout protection
(
sleep 60
kill -TERM $$ 2>/dev/null
) &
TIMEOUT_PID=$!
# (
# sleep 60
# kill -TERM $$
# ) &
# TIMEOUT_PID=$!
process_all_commands "$COMMANDS" "$PRIORITY"
process_all_commands "$COMMANDS" "$PRIORITY"
# kill $TIMEOUT_PID 2>/dev/null
release_token
# Clean up
kill $TIMEOUT_PID 2>/dev/null
release_token