Revert "Official Hot Fix for QuecManager 2.3.0"

This reverts commit 6bd2c7ea52.
This commit is contained in:
Cameron Thompson
2025-08-27 23:40:17 -04:00
parent 1773301af8
commit 20c2f37452
38 changed files with 203 additions and 4134 deletions

View File

@@ -2,40 +2,11 @@
# 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>"
@@ -49,14 +20,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"
log_at_queue_client "debug" "urldecode: input='$encoded'"
logger -t at_queue -p daemon.debug "urldecode: input='$encoded'"
# Handle %2B -> + and %22 -> " conversions
local decoded="${encoded//%2B/+}"
@@ -64,23 +35,10 @@ urldecode() {
# Then handle other encoded characters
decoded=$(printf '%b' "${decoded//%/\\x}")
log_at_queue_client "debug" "urldecode: output='$decoded'"
logger -t at_queue -p daemon.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"
@@ -114,19 +72,19 @@ get_command_id() {
# Normalize AT command
normalize_at_command() {
local cmd="$1"
log_at_queue_client "debug" "normalize: input='$cmd'"
logger -t at_queue -p daemon.debug "normalize: input='$cmd'"
# URL decode the command
cmd=$(urldecode "$cmd")
log_at_queue_client "debug" "normalize: after urldecode='$cmd'"
logger -t at_queue -p daemon.debug "normalize: after urldecode='$cmd'"
# Remove any carriage returns or newlines
cmd=$(echo "$cmd" | tr -d '\r\n')
log_at_queue_client "debug" "normalize: after cleanup='$cmd'"
logger -t at_queue -p daemon.debug "normalize: after cleanup='$cmd'"
# Trim leading/trailing whitespace while preserving quotes
cmd=$(echo "$cmd" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
log_at_queue_client "debug" "normalize: final output='$cmd'"
logger -t at_queue -p daemon.debug "normalize: final output='$cmd'"
echo "$cmd"
}
@@ -143,7 +101,7 @@ submit_command() {
# Submit using appropriate method
if [ "${SCRIPT_NAME}" != "" ]; then
# CGI mode - direct execution like the original working version
# CGI mode - direct execution
local escaped_cmd=$(echo "$cmd" | sed 's/"/\\"/g')
QUERY_STRING="action=enqueue&command=${escaped_cmd}&priority=$priority" "$QUEUE_MANAGER"
else
@@ -160,7 +118,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
log_at_queue_client "error" "Empty result file for command ID: $cmd_id"
logger -t at_queue -p daemon.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,9 +1,8 @@
#!/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
printf "Content-type: application/json\r\n"
printf "\r\n"
echo "Content-type: application/json"
echo ""
# Define paths and constants to match queue system
QUEUE_DIR="/tmp/at_queue"
@@ -14,11 +13,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 '
@@ -37,46 +36,39 @@ escape_json() {
# Acquire token directly (avoid CGI overhead)
acquire_token() {
priority="${1:-10}"
max_attempts=10
attempt=0
log_message "debug" "Acquiring token"
local priority="${1:-10}"
local max_attempts=10
local attempt=0
while [ $attempt -lt $max_attempts ]; do
# Check if token file exists
if [ -f "$TOKEN_FILE" ]; then
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}"
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)
# 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
printf "{\"id\":\"$LOCK_ID\",\"priority\":$priority,\"timestamp\":$(date +%s)}" >"$TOKEN_FILE" 2>/dev/null
echo "{\"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
holder=$(cat "$TOKEN_FILE" 2>/dev/null | jsonfilter -e '@.id' 2>/dev/null)
local holder=$(cat "$TOKEN_FILE" 2>/dev/null | jsonfilter -e '@.id' 2>/dev/null)
if [ "$holder" = "$LOCK_ID" ]; then
return 0
fi
@@ -87,16 +79,13 @@ 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
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}"
local current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
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
@@ -104,21 +93,18 @@ release_token() {
# Direct AT command execution with minimal overhead
execute_at_command() {
CMD="$1"
local CMD="$1"
sms_tool at "$CMD" -t 3 2>/dev/null
}
# Batch process all commands with a single token
process_all_commands() {
commands="$1"
priority="${2:-10}"
first=1
log_message "info" "Before acquire_token check"
acquire_token "$priority"
trying=$?
log_message "debug" "trying: ${trying}"
local commands="$1"
local priority="${2:-10}"
local first=1
# Acquire a single token for all commands
if [ $trying -ne 0 ]; then
if ! acquire_token "$priority"; then
log_message "error" "Failed to acquire token for batch processing"
# Return all failed responses
printf '['
@@ -129,7 +115,7 @@ process_all_commands() {
ESCAPED_CMD=$(escape_json "$cmd")
printf '{"command":"%s","response":"Failed to acquire token","status":"error"}' "${ESCAPED_CMD}"
done
printf ']\r\n'
printf ']\n'
return 1
fi
@@ -138,9 +124,10 @@ process_all_commands() {
for cmd in $commands; do
[ $first -eq 0 ] && printf ','
first=0
OUTPUT=$(execute_at_command "$cmd")
CMD_STATUS=$?
log_message "debug" "CMD: ${cmd}, OUTPUT: ${OUTPUT}, CMD_STAT: ${CMD_STATUS}"
local CMD_STATUS=$?
ESCAPED_CMD=$(escape_json "$cmd")
ESCAPED_OUTPUT=$(escape_json "$OUTPUT")
@@ -153,7 +140,8 @@ process_all_commands() {
"${ESCAPED_CMD}"
fi
done
printf ']\r\n'
printf ']\n'
# Release token after all commands are done
release_token
return 0
@@ -196,14 +184,15 @@ if echo "$COMMANDS" | grep -qi "AT+QSCAN"; then
PRIORITY=1
fi
# (
# sleep 60
# kill -TERM $$
# ) &
# TIMEOUT_PID=$!
# Process commands with timeout protection
(
sleep 60
kill -TERM $$ 2>/dev/null
) &
TIMEOUT_PID=$!
process_all_commands "$COMMANDS" "$PRIORITY"
# kill $TIMEOUT_PID 2>/dev/null
release_token
process_all_commands "$COMMANDS" "$PRIORITY"
# Clean up
kill $TIMEOUT_PID 2>/dev/null
release_token