Merge QuecManager beta into QuecManager

Sync beta and normal QuecManager versions
This commit is contained in:
Cameron Thompson
2025-07-01 19:35:56 -04:00
parent 559d260eb0
commit a3dd95ec30
171 changed files with 380 additions and 1433 deletions

View File

@@ -39,7 +39,7 @@ acquire_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
@@ -47,7 +47,7 @@ acquire_token() {
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
@@ -62,21 +62,21 @@ acquire_token() {
continue
fi
fi
# Try to create token file
echo "{\"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
local holder=$(cat "$TOKEN_FILE" 2>/dev/null | jsonfilter -e '@.id' 2>/dev/null)
if [ "$holder" = "$LOCK_ID" ]; then
return 0
fi
sleep 0.1
attempt=$((attempt + 1))
done
return 1
}
@@ -102,7 +102,7 @@ process_all_commands() {
local commands="$1"
local priority="${2:-10}"
local first=1
# Acquire a single token for all commands
if ! acquire_token "$priority"; then
log_message "error" "Failed to acquire token for batch processing"
@@ -118,19 +118,19 @@ process_all_commands() {
printf ']\n'
return 1
fi
# Process all commands with the single token
printf '['
for cmd in $commands; do
[ $first -eq 0 ] && printf ','
first=0
OUTPUT=$(execute_at_command "$cmd")
local CMD_STATUS=$?
ESCAPED_CMD=$(escape_json "$cmd")
ESCAPED_OUTPUT=$(escape_json "$OUTPUT")
if [ $CMD_STATUS -eq 0 ] && [ -n "$OUTPUT" ]; then
printf '{"command":"%s","response":"%s","status":"success"}' \
"${ESCAPED_CMD}" \
@@ -141,7 +141,7 @@ process_all_commands() {
fi
done
printf ']\n'
# Release token after all commands are done
release_token
return 0
@@ -151,9 +151,9 @@ process_all_commands() {
trap 'release_token; exit 1' INT TERM
# Command sets
COMMAND_SET_1='AT+QUIMSLOT? AT+CNUM AT+COPS? AT+CIMI AT+ICCID AT+CGSN AT+CPIN? AT+CGDCONT? AT+CREG? AT+CFUN? AT+QENG="servingcell" AT+QTEMP AT+CGCONTRDP AT+QCAINFO AT+QRSRP AT+QMAP="WWAN" AT+C5GREG=2;+C5GREG? AT+CGREG=2;+CGREG? AT+QRSRQ AT+QSINR'
COMMAND_SET_2='AT+CGDCONT? AT+CGCONTRDP AT+QNWPREFCFG="mode_pref" AT+QNWPREFCFG="nr5g_disable_mode" AT+QUIMSLOT? AT+CFUN? AT+QMBNCFG="AutoSel" AT+QMBNCFG="list"'
COMMAND_SET_3='AT+CGMI AT+CGMM AT+QGMR AT+CNUM AT+CIMI AT+ICCID AT+CGSN AT+QMAP="LANIP" AT+QMAP="WWAN" AT+QGETCAPABILITY'
COMMAND_SET_1='AT+QUIMSLOT? AT+CNUM AT+COPS? AT+CIMI AT+ICCID AT+CGSN AT+CPIN? AT+CGDCONT? AT+CREG? AT+CFUN? AT+QENG="servingcell" AT+QTEMP AT+CGCONTRDP AT+QCAINFO=1;+QCAINFO;+QCAINFO=0 AT+QRSRP AT+QMAP="WWAN" AT+C5GREG=2;+C5GREG? AT+CGREG=2;+CGREG? AT+QRSRQ AT+QSINR AT+CGCONTRDP AT+QNWCFG="lte_time_advance",1;+QNWCFG="lte_time_advance" AT+QNWCFG="nr5g_time_advance",1;+QNWCFG="nr5g_time_advance"'
COMMAND_SET_2='AT+CGDCONT? AT+CGCONTRDP AT+QNWPREFCFG="mode_pref" AT+QNWPREFCFG="nr5g_disable_mode" AT+QUIMSLOT? AT+CFUN? AT+QMBNCFG="AutoSel" AT+QMBNCFG="list" AT+QMAP="WWAN" AT+QNWCFG="lte_ambr" AT+QNWCFG="nr5g_ambr"'
COMMAND_SET_3='AT+CGMI AT+CGMM AT+QGMR AT+CNUM AT+CIMI AT+ICCID AT+CGSN AT+QMAP="LANIP" AT+QMAP="WWAN" AT+QGETCAPABILITY AT+QNWCFG="3gpp_rel"'
COMMAND_SET_4='AT+QMAP="MPDN_RULE" AT+QMAP="DHCPV4DNS" AT+QCFG="usbnet"'
COMMAND_SET_5='AT+QRSRP AT+QRSRQ AT+QSINR AT+QCAINFO AT+QSPN'
COMMAND_SET_6='AT+CEREG=2;+CEREG? AT+C5GREG=2;+C5GREG? AT+CPIN? AT+CGDCONT? AT+CGCONTRDP AT+QMAP="WWAN" AT+QRSRP AT+QTEMP AT+QNETRC?'
@@ -168,14 +168,14 @@ fi
# Select the appropriate command set
case "$COMMAND_SET" in
1) COMMANDS="$COMMAND_SET_1";;
2) COMMANDS="$COMMAND_SET_2";;
3) COMMANDS="$COMMAND_SET_3";;
4) COMMANDS="$COMMAND_SET_4";;
5) COMMANDS="$COMMAND_SET_5";;
6) COMMANDS="$COMMAND_SET_6";;
7) COMMANDS="$COMMAND_SET_7";;
8) COMMANDS="$COMMAND_SET_8";;
1) COMMANDS="$COMMAND_SET_1" ;;
2) COMMANDS="$COMMAND_SET_2" ;;
3) COMMANDS="$COMMAND_SET_3" ;;
4) COMMANDS="$COMMAND_SET_4" ;;
5) COMMANDS="$COMMAND_SET_5" ;;
6) COMMANDS="$COMMAND_SET_6" ;;
7) COMMANDS="$COMMAND_SET_7" ;;
8) COMMANDS="$COMMAND_SET_8" ;;
esac
# Set priority based on content
@@ -185,11 +185,14 @@ if echo "$COMMANDS" | grep -qi "AT+QSCAN"; then
fi
# Process commands with timeout protection
( sleep 60; kill -TERM $$ 2>/dev/null ) &
(
sleep 60
kill -TERM $$ 2>/dev/null
) &
TIMEOUT_PID=$!
process_all_commands "$COMMANDS" "$PRIORITY"
# Clean up
kill $TIMEOUT_PID 2>/dev/null
release_token
release_token

View File

@@ -1,127 +0,0 @@
#!/bin/sh
echo "Content-type: application/json"
echo ""
# Initialize error tracking
has_error=false
error_message=""
# Function to append to error message
append_error() {
if [ -z "$error_message" ]; then
error_message="$1"
else
error_message="$error_message; $1"
fi
has_error=true
}
# Function to log cleanup events
log_message() {
local level="$1"
local message="$2"
local LOG_DIR="/tmp/log/apnprofile"
local LOG_FILE="${LOG_DIR}/apnprofile.log"
# Ensure log directory exists
mkdir -p "${LOG_DIR}"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "${timestamp} - [${level}] ${message}" >> "$LOG_FILE"
logger -t apnprofile "${level}: ${message}"
}
log_message "INFO" "Starting APN Profile cleanup process"
# Stop and disable the service
if [ -f "/etc/init.d/apnprofile-service" ]; then
if /etc/init.d/apnprofile-service stop; then
log_message "INFO" "APN Profile service stopped"
else
append_error "Failed to stop APN Profile service"
log_message "ERROR" "Failed to stop APN Profile service"
fi
if /etc/init.d/apnprofile-service disable; then
log_message "INFO" "APN Profile service disabled"
else
append_error "Failed to disable APN Profile service"
log_message "ERROR" "Failed to disable APN Profile service"
fi
# Remove the init.d script
if rm -f "/etc/init.d/apnprofile-service"; then
log_message "INFO" "Removed init.d script"
else
append_error "Failed to remove init.d script"
log_message "ERROR" "Failed to remove init.d script"
fi
fi
# Remove service script
if [ -f "/www/cgi-bin/services/apnprofile.sh" ]; then
if rm -f "/www/cgi-bin/services/apnprofile.sh"; then
log_message "INFO" "Removed service script"
else
append_error "Failed to remove service script"
log_message "ERROR" "Failed to remove service script"
fi
fi
# Remove symlinks in rc.d if they exist
for link in /etc/rc.d/S??apnprofile-service /etc/rc.d/K??apnprofile-service; do
if [ -L "$link" ]; then
if rm -f "$link"; then
log_message "INFO" "Removed rc.d symlink: $link"
else
append_error "Failed to remove rc.d symlink: $link"
log_message "ERROR" "Failed to remove rc.d symlink: $link"
fi
fi
done
# Remove UCI configuration (only removes apn_profile section, leaves other sections intact)
if uci -q get quecmanager.apn_profile >/dev/null; then
if uci delete quecmanager.apn_profile && uci commit quecmanager; then
log_message "INFO" "Removed UCI configuration"
else
append_error "Failed to remove UCI configuration"
log_message "ERROR" "Failed to remove UCI configuration"
fi
fi
# Kill any remaining processes
if pkill -f "/www/cgi-bin/services/apnprofile.sh"; then
log_message "INFO" "Killed remaining APN Profile processes"
fi
# Clean up temporary files
for file in \
"/tmp/at_pipe.txt" \
"/var/run/apnprofile.pid" \
"/tmp/apn_result.txt" \
"/tmp/debug.log" \
"/tmp/inputICCID.txt" \
"/tmp/outputICCID.txt" \
"/tmp/inputAPN.txt" \
"/tmp/outputAPN.txt"
do
if [ -f "$file" ]; then
if rm -f "$file"; then
log_message "INFO" "Removed temporary file: $file"
else
append_error "Failed to remove temporary file: $file"
log_message "ERROR" "Failed to remove temporary file: $file"
fi
fi
done
log_message "INFO" "APN Profile cleanup completed"
# Return appropriate JSON response
if [ "$has_error" = true ]; then
echo "{\"status\": \"error\", \"message\": \"$error_message\"}"
else
echo "{\"status\": \"success\", \"message\": \"APN Profile service successfully removed\"}"
fi

View File

@@ -1,144 +0,0 @@
#!/bin/sh
# Set headers for JSON response
echo "Content-type: application/json"
echo ""
# Load UCI functions
. /lib/functions.sh
# Function to safely get UCI value with default
get_uci_value() {
local value
config_get value apn_profile "$1" "$2"
echo "${value:-$2}"
}
# Function to check if service is running
check_service_status() {
if [ -f "/var/run/apnprofile.pid" ]; then
pid=$(cat /var/run/apnprofile.pid)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
echo "running"
return
fi
fi
# Double check using process search
if pgrep -f "/www/cgi-bin/services/apnprofile.sh" >/dev/null; then
echo "running"
return
fi
echo "stopped"
}
# Function to get last log entry
get_last_log() {
local LOG_FILE="/tmp/log/apnprofile/apnprofile.log"
if [ -f "$LOG_FILE" ]; then
tail -n 1 "$LOG_FILE"
else
echo "No log entries found"
fi
}
# Function to check if init.d service is enabled
check_service_enabled() {
if [ -f "/etc/init.d/apnprofile-service" ]; then
if /etc/init.d/apnprofile-service enabled; then
echo "true"
return
fi
fi
echo "false"
}
# Load QuecManager configuration
config_load quecmanager
# Check if APN Profile section exists
if ! uci -q get quecmanager.apn_profile >/dev/null; then
echo '{"status": "inactive", "message": "APN Profile service is not configured"}'
exit 0
fi
# Get enabled status from UCI
enabled=$(get_uci_value "enabled" "0")
if [ "$enabled" != "1" ]; then
echo '{"status": "inactive", "message": "APN Profile service is disabled"}'
exit 0
fi
# Check if service script exists
if [ ! -f "/www/cgi-bin/services/apnprofile.sh" ]; then
echo '{"status": "error", "message": "Service script is missing"}'
exit 0
fi
# Get service status information
service_status=$(check_service_status)
service_enabled=$(check_service_enabled)
last_log=$(get_last_log)
# Fetch all configuration values from UCI
iccid_profile1=$(get_uci_value "iccid_profile1" "")
apn_profile1=$(get_uci_value "apn_profile1" "")
pdp_type1=$(get_uci_value "pdp_type1" "")
iccid_profile2=$(get_uci_value "iccid_profile2" "")
apn_profile2=$(get_uci_value "apn_profile2" "")
pdp_type2=$(get_uci_value "pdp_type2" "")
# Function to check if profile data exists
validate_profile_data() {
local iccid="$1"
local apn="$2"
local pdp="$3"
[ -n "$iccid" ] && [ -n "$apn" ] && [ -n "$pdp" ]
}
# Build JSON response
cat <<EOF
{
"status": "active",
"service": {
"status": "${service_status}",
"enabled": ${service_enabled},
"script": "$([ -f "/www/cgi-bin/services/apnprofile.sh" ] && echo "present" || echo "missing")",
"initScript": "$([ -f "/etc/init.d/apnprofile-service" ] && echo "present" || echo "missing")"
},
"profiles": {
EOF
# Add Profile 1 if it exists
if validate_profile_data "$iccid_profile1" "$apn_profile1" "$pdp_type1"; then
cat <<EOF
"profile1": {
"iccid": "${iccid_profile1}",
"apn": "${apn_profile1}",
"pdpType": "${pdp_type1}"
}
EOF
fi
# Add Profile 2 if it exists
if validate_profile_data "$iccid_profile2" "$apn_profile2" "$pdp_type2"; then
# Add comma if Profile 1 was added
[ -n "$iccid_profile1" ] && echo ","
cat <<EOF
"profile2": {
"iccid": "${iccid_profile2}",
"apn": "${apn_profile2}",
"pdpType": "${pdp_type2}"
}
EOF
fi
# Close the profiles object and add last activity
cat <<EOF
},
"lastActivity": "${last_log}"
}
EOF

View File

@@ -1,345 +0,0 @@
#!/bin/sh
# Read POST data
read -r QUERY_STRING
# Function to urldecode
urldecode() {
local value="$1"
value="${value//+/ }"
value="${value//%/\\x}"
printf '%b' "$value"
}
# Extract values from POST data
iccidProfile1=$(echo "$QUERY_STRING" | sed -n 's/.*iccidProfile1=\([^&]*\).*/\1/p' | tr -d "'")
apnProfile1=$(echo "$QUERY_STRING" | sed -n 's/.*apnProfile1=\([^&]*\).*/\1/p' | tr -d "'")
pdpType1=$(echo "$QUERY_STRING" | sed -n 's/.*pdpType1=\([^&]*\).*/\1/p' | tr -d "'")
iccidProfile2=$(echo "$QUERY_STRING" | sed -n 's/.*iccidProfile2=\([^&]*\).*/\1/p' | tr -d "'")
apnProfile2=$(echo "$QUERY_STRING" | sed -n 's/.*apnProfile2=\([^&]*\).*/\1/p' | tr -d "'")
pdpType2=$(echo "$QUERY_STRING" | sed -n 's/.*pdpType2=\([^&]*\).*/\1/p' | tr -d "'")
# URL decode the values
iccidProfile1=$(urldecode "$iccidProfile1")
apnProfile1=$(urldecode "$apnProfile1")
pdpType1=$(urldecode "$pdpType1")
iccidProfile2=$(urldecode "$iccidProfile2")
apnProfile2=$(urldecode "$apnProfile2")
pdpType2=$(urldecode "$pdpType2")
echo "Content-type: application/json"
echo ""
# Validate required first profile
if [ -z "$iccidProfile1" ] || [ -z "$apnProfile1" ] || [ -z "$pdpType1" ]; then
echo '{"status": "error", "message": "Profile 1 is required"}'
exit 1
fi
# Function to log messages
log_message() {
local level="$1"
local message="$2"
local LOG_DIR="/tmp/log/apnprofile"
local LOG_FILE="${LOG_DIR}/apnprofile.log"
mkdir -p "${LOG_DIR}"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "${timestamp} - [${level}] ${message}" >> "$LOG_FILE"
logger -t apnprofile "${level}: ${message}"
}
# Create required directories
mkdir -p /www/cgi-bin/services
mkdir -p /etc/init.d
# Function to create service script
create_service_script() {
cat > /www/cgi-bin/services/apnprofile.sh <<'EOL'
#!/bin/sh
# Load UCI functions
. /lib/functions.sh
# Define file paths
QUEUE_FILE="/tmp/at_pipe.txt"
LOG_DIR="/tmp/log/apnprofile"
LOG_FILE="${LOG_DIR}/apnprofile.log"
PID_FILE="/var/run/apnprofile.pid"
STATE_FILE="/tmp/apnprofile_state.json"
mkdir -p "${LOG_DIR}"
[ ! -f "${QUEUE_FILE}" ] && touch "${QUEUE_FILE}"
# Save PID
echo $$ > "${PID_FILE}"
# Enhanced logging function
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 apnprofile "${level}: ${message}"
}
# AT command handling with locking
handle_lock() {
local max_wait=30
local wait_count=0
while [ -f "$QUEUE_FILE" ] && grep -q "AT_COMMAND" "$QUEUE_FILE" && [ $wait_count -lt $max_wait ]; do
sleep 1
wait_count=$((wait_count + 1))
done
printf '{"command":"AT_COMMAND","pid":"%s","timestamp":"%s"}\n' "$$" "$(date '+%H:%M:%S')" >> "$QUEUE_FILE"
}
# Execute AT command with retries
execute_at_command() {
local command="$1"
local result=""
local retry_count=0
local max_retries=3
while [ $retry_count -lt $max_retries ]; do
handle_lock
result=$(sms_tool at "$command" -t 4 2>&1)
local status=$?
sed -i "/\"pid\":\"$$\"/d" "$QUEUE_FILE"
if [ $status -eq 0 ] && [ -n "$result" ]; then
echo "$result"
return 0
fi
retry_count=$((retry_count + 1))
[ $retry_count -lt $max_retries ] && sleep 2
done
return 1
}
# Get current ICCID
get_current_iccid() {
local result=$(execute_at_command "AT+ICCID")
if [ $? -eq 0 ] && echo "$result" | grep -q "+ICCID:"; then
echo "$result" | grep "+ICCID:" | cut -d' ' -f2 | tr -d '[:space:]'
return 0
fi
return 1
}
# Set APN with error handling
set_apn() {
local pdp_type="$1"
local apn="$2"
if [ -z "$pdp_type" ] || [ -z "$apn" ]; then
return 1
fi
if execute_at_command "AT+CGDCONT=1,\"$pdp_type\",\"$apn\";+COPS=2;+COPS=0"; then
return 0
fi
return 1
}
# Function to get current configuration hash
get_config_hash() {
config_load quecmanager
local hash_input=""
# Get Profile 1
config_get ICCID_PROFILE1 apn_profile iccid_profile1
config_get APN_PROFILE1 apn_profile apn_profile1
config_get PDP_TYPE1 apn_profile pdp_type1
# Get Profile 2
config_get ICCID_PROFILE2 apn_profile iccid_profile2
config_get APN_PROFILE2 apn_profile apn_profile2
config_get PDP_TYPE2 apn_profile pdp_type2
hash_input="${ICCID_PROFILE1}${APN_PROFILE1}${PDP_TYPE1}${ICCID_PROFILE2}${APN_PROFILE2}${PDP_TYPE2}"
echo "$hash_input" | md5sum | cut -d' ' -f1
}
# Function to read state file
read_state() {
if [ -f "$STATE_FILE" ]; then
cat "$STATE_FILE"
else
echo "{}"
fi
}
# Function to write state file
write_state() {
local current_iccid="$1"
local config_hash="$2"
local status="$3"
printf '{"iccid":"%s","config_hash":"%s","status":"%s","timestamp":"%s"}' \
"$current_iccid" "$config_hash" "$status" "$(date '+%Y-%m-%d %H:%M:%S')" > "$STATE_FILE"
}
# Main service loop
while true; do
# Get current state
current_state=$(read_state)
current_iccid=$(get_current_iccid)
config_hash=$(get_config_hash)
# Extract values from current state
state_iccid=$(echo "$current_state" | sed -n 's/.*"iccid":"\([^"]*\)".*/\1/p')
state_hash=$(echo "$current_state" | sed -n 's/.*"config_hash":"\([^"]*\)".*/\1/p')
needs_update=0
# Check if update is needed
if [ ! -f "$STATE_FILE" ]; then
log_message "INFO" "No state file found, will apply profile"
needs_update=1
elif [ "$current_iccid" != "$state_iccid" ]; then
log_message "INFO" "ICCID changed from $state_iccid to $current_iccid"
needs_update=1
elif [ "$config_hash" != "$state_hash" ]; then
log_message "INFO" "Configuration changed"
needs_update=1
fi
if [ $needs_update -eq 1 ] && [ -n "$current_iccid" ]; then
config_load quecmanager
# Get Profile 1
config_get ICCID_PROFILE1 apn_profile iccid_profile1
config_get APN_PROFILE1 apn_profile apn_profile1
config_get PDP_TYPE1 apn_profile pdp_type1
# Get Profile 2
config_get ICCID_PROFILE2 apn_profile iccid_profile2
config_get APN_PROFILE2 apn_profile apn_profile2
config_get PDP_TYPE2 apn_profile pdp_type2
if [ "${current_iccid}" = "${ICCID_PROFILE1}" ]; then
if set_apn "$PDP_TYPE1" "$APN_PROFILE1"; then
log_message "INFO" "Successfully applied Profile 1"
write_state "$current_iccid" "$config_hash" "success"
else
log_message "ERROR" "Failed to apply Profile 1"
write_state "$current_iccid" "$config_hash" "error"
fi
elif [ -n "$ICCID_PROFILE2" ] && [ "${current_iccid}" = "${ICCID_PROFILE2}" ]; then
if set_apn "$PDP_TYPE2" "$APN_PROFILE2"; then
log_message "INFO" "Successfully applied Profile 2"
write_state "$current_iccid" "$config_hash" "success"
else
log_message "ERROR" "Failed to apply Profile 2"
write_state "$current_iccid" "$config_hash" "error"
fi
else
log_message "INFO" "No matching ICCID profile found"
write_state "$current_iccid" "$config_hash" "no_match"
fi
fi
sleep 10
done
EOL
chmod 755 /www/cgi-bin/services/apnprofile.sh
}
# Function to create init.d script
create_init_script() {
cat > /etc/init.d/apnprofile-service <<'EOL'
#!/bin/sh /etc/rc.common
START=99
STOP=10
USE_PROCD=1
start_service() {
local enabled
# Check if service is enabled in UCI
config_load quecmanager
config_get enabled apn_profile enabled '0'
[ "$enabled" != "1" ] && return 0
procd_open_instance
procd_set_param command /www/cgi-bin/services/apnprofile.sh
procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param nice 19
procd_close_instance
}
service_triggers() {
procd_add_reload_trigger "quecmanager"
}
reload_service() {
stop
start
}
EOL
chmod 755 /etc/init.d/apnprofile-service
}
# Initialize UCI configuration
touch /etc/config/quecmanager
# Remove existing APN profile section if it exists
uci -q delete quecmanager.apn_profile
# Create new APN profile section
uci set quecmanager.apn_profile=service
uci set quecmanager.apn_profile.enabled=1
# Set Profile 1 configuration
uci set quecmanager.apn_profile.iccid_profile1="$iccidProfile1"
uci set quecmanager.apn_profile.apn_profile1="$apnProfile1"
uci set quecmanager.apn_profile.pdp_type1="$pdpType1"
# Set Profile 2 configuration if provided
if [ -n "$iccidProfile2" ]; then
uci set quecmanager.apn_profile.iccid_profile2="$iccidProfile2"
uci set quecmanager.apn_profile.apn_profile2="$apnProfile2"
uci set quecmanager.apn_profile.pdp_type2="$pdpType2"
fi
# Commit UCI changes
if ! uci commit quecmanager; then
log_message "ERROR" "Failed to save UCI configuration"
echo '{"status": "error", "message": "Failed to save UCI configuration"}'
exit 1
fi
log_message "INFO" "UCI configuration saved successfully"
# Create service script if it doesn't exist
if [ ! -f "/www/cgi-bin/services/apnprofile.sh" ]; then
create_service_script
log_message "INFO" "Created service script"
fi
# Create init.d script if it doesn't exist
if [ ! -f "/etc/init.d/apnprofile-service" ]; then
create_init_script
log_message "INFO" "Created init.d script"
fi
# Enable and start the service
/etc/init.d/apnprofile-service enable
if /etc/init.d/apnprofile-service restart; then
log_message "INFO" "Service started successfully"
echo '{"status": "success", "message": "APN profiles saved and service started"}'
else
log_message "ERROR" "Failed to start service"
echo '{"status": "error", "message": "Failed to start service"}'
fi

View File

@@ -1,127 +0,0 @@
#!/bin/sh
echo "Content-type: application/json"
echo ""
# Initialize error tracking
has_error=false
error_message=""
# Function to append to error message
append_error() {
if [ -z "$error_message" ]; then
error_message="$1"
else
error_message="$error_message; $1"
fi
has_error=true
}
# Function to log cleanup events
log_message() {
local level="$1"
local message="$2"
local LOG_DIR="/tmp/log/imeiprofile"
local LOG_FILE="${LOG_DIR}/imeiprofile.log"
# Ensure log directory exists
mkdir -p "${LOG_DIR}"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "${timestamp} - [${level}] ${message}" >> "$LOG_FILE"
logger -t imeiprofile "${level}: ${message}"
}
log_message "INFO" "Starting IMEI Profile cleanup process"
# Stop and disable the service
if [ -f "/etc/init.d/imeiprofile-service" ]; then
if /etc/init.d/imeiprofile-service stop; then
log_message "INFO" "IMEI Profile service stopped"
else
append_error "Failed to stop IMEI Profile service"
log_message "ERROR" "Failed to stop IMEI Profile service"
fi
if /etc/init.d/imeiprofile-service disable; then
log_message "INFO" "IMEI Profile service disabled"
else
append_error "Failed to disable IMEI Profile service"
log_message "ERROR" "Failed to disable IMEI Profile service"
fi
# Remove the init.d script
if rm -f "/etc/init.d/imeiprofile-service"; then
log_message "INFO" "Removed init.d script"
else
append_error "Failed to remove init.d script"
log_message "ERROR" "Failed to remove init.d script"
fi
fi
# Remove service script
if [ -f "/www/cgi-bin/services/imeiprofile.sh" ]; then
if rm -f "/www/cgi-bin/services/imeiprofile.sh"; then
log_message "INFO" "Removed service script"
else
append_error "Failed to remove service script"
log_message "ERROR" "Failed to remove service script"
fi
fi
# Remove symlinks in rc.d if they exist
for link in /etc/rc.d/S??imeiprofile-service /etc/rc.d/K??imeiprofile-service; do
if [ -L "$link" ]; then
if rm -f "$link"; then
log_message "INFO" "Removed rc.d symlink: $link"
else
append_error "Failed to remove rc.d symlink: $link"
log_message "ERROR" "Failed to remove rc.d symlink: $link"
fi
fi
done
# Remove UCI configuration
if uci -q get quecmanager.imei_profile >/dev/null; then
if uci delete quecmanager.imei_profile && uci commit quecmanager; then
log_message "INFO" "Removed UCI configuration"
else
append_error "Failed to remove UCI configuration"
log_message "ERROR" "Failed to remove UCI configuration"
fi
fi
# Kill any remaining processes
if pkill -f "/www/cgi-bin/services/imeiprofile.sh"; then
log_message "INFO" "Killed remaining IMEI Profile processes"
fi
# Clean up temporary files
for file in \
"/tmp/at_pipe.txt" \
"/var/run/imeiprofile.pid" \
"/tmp/imei_result.txt" \
"/tmp/debug.log" \
"/tmp/inputICCID.txt" \
"/tmp/outputICCID.txt" \
"/tmp/inputIMEI.txt" \
"/tmp/outputIMEI.txt"
do
if [ -f "$file" ]; then
if rm -f "$file"; then
log_message "INFO" "Removed temporary file: $file"
else
append_error "Failed to remove temporary file: $file"
log_message "ERROR" "Failed to remove temporary file: $file"
fi
fi
done
log_message "INFO" "IMEI Profile cleanup completed"
# Return appropriate JSON response
if [ "$has_error" = true ]; then
echo "{\"status\": \"error\", \"message\": \"$error_message\"}"
else
echo "{\"status\": \"success\", \"message\": \"IMEI Profile service successfully removed\"}"
fi

View File

@@ -1,138 +0,0 @@
#!/bin/sh
# Set headers for JSON response
echo "Content-type: application/json"
echo ""
# Load UCI functions
. /lib/functions.sh
# Function to safely get UCI value with default
get_uci_value() {
local value
config_get value imei_profile "$1" "$2"
echo "${value:-$2}"
}
# Function to check if service is running
check_service_status() {
if [ -f "/var/run/imeiprofile.pid" ]; then
pid=$(cat /var/run/imeiprofile.pid)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
echo "running"
return
fi
fi
# Double check using process search
if pgrep -f "/www/cgi-bin/services/imeiprofile.sh" >/dev/null; then
echo "running"
return
fi
echo "stopped"
}
# Function to get last log entry
get_last_log() {
local LOG_FILE="/tmp/log/imeiprofile/imeiprofile.log"
if [ -f "$LOG_FILE" ]; then
tail -n 1 "$LOG_FILE"
else
echo "No log entries found"
fi
}
# Function to check if init.d service is enabled
check_service_enabled() {
if [ -f "/etc/init.d/imeiprofile-service" ]; then
if /etc/init.d/imeiprofile-service enabled; then
echo "true"
return
fi
fi
echo "false"
}
# Load QuecManager configuration
config_load quecmanager
# Check if IMEI Profile section exists
if ! uci -q get quecmanager.imei_profile >/dev/null; then
echo '{"status": "inactive", "message": "IMEI Profile service is not configured"}'
exit 0
fi
# Get enabled status from UCI
enabled=$(get_uci_value "enabled" "0")
if [ "$enabled" != "1" ]; then
echo '{"status": "inactive", "message": "IMEI Profile service is disabled"}'
exit 0
fi
# Check if service script exists
if [ ! -f "/www/cgi-bin/services/imeiprofile.sh" ]; then
echo '{"status": "error", "message": "Service script is missing"}'
exit 0
fi
# Get service status information
service_status=$(check_service_status)
service_enabled=$(check_service_enabled)
last_log=$(get_last_log)
# Fetch configuration values from UCI
iccid_profile1=$(get_uci_value "iccid_profile1" "")
imei_profile1=$(get_uci_value "imei_profile1" "")
iccid_profile2=$(get_uci_value "iccid_profile2" "")
imei_profile2=$(get_uci_value "imei_profile2" "")
# Function to check if profile data exists
validate_profile_data() {
local iccid="$1"
local imei="$2"
[ -n "$iccid" ] && [ -n "$imei" ]
}
# Build JSON response
cat <<EOF
{
"status": "active",
"service": {
"status": "${service_status}",
"enabled": ${service_enabled},
"script": "$([ -f "/www/cgi-bin/services/imeiprofile.sh" ] && echo "present" || echo "missing")",
"initScript": "$([ -f "/etc/init.d/imeiprofile-service" ] && echo "present" || echo "missing")"
},
"profiles": {
EOF
# Add Profile 1 if it exists
if validate_profile_data "$iccid_profile1" "$imei_profile1"; then
cat <<EOF
"profile1": {
"iccid": "${iccid_profile1}",
"imei": "${imei_profile1}"
}
EOF
fi
# Add Profile 2 if it exists
if validate_profile_data "$iccid_profile2" "$imei_profile2"; then
# Add comma if Profile 1 was added
[ -n "$iccid_profile1" ] && echo ","
cat <<EOF
"profile2": {
"iccid": "${iccid_profile2}",
"imei": "${imei_profile2}"
}
EOF
fi
# Close the profiles object and add last activity
cat <<EOF
},
"lastActivity": "${last_log}"
}
EOF

View File

@@ -1,291 +0,0 @@
#!/bin/sh
# Read POST data
read -r QUERY_STRING
# Function to urldecode
urldecode() {
local value="$1"
value="${value//+/ }"
value="${value//%/\\x}"
printf '%b' "$value"
}
# Extract values from POST data
iccidProfile1=$(echo "$QUERY_STRING" | sed -n 's/.*iccidProfile1=\([^&]*\).*/\1/p' | tr -d "'")
imeiProfile1=$(echo "$QUERY_STRING" | sed -n 's/.*imeiProfile1=\([^&]*\).*/\1/p' | tr -d "'")
iccidProfile2=$(echo "$QUERY_STRING" | sed -n 's/.*iccidProfile2=\([^&]*\).*/\1/p' | tr -d "'")
imeiProfile2=$(echo "$QUERY_STRING" | sed -n 's/.*imeiProfile2=\([^&]*\).*/\1/p' | tr -d "'")
# URL decode the values
iccidProfile1=$(urldecode "$iccidProfile1")
imeiProfile1=$(urldecode "$imeiProfile1")
iccidProfile2=$(urldecode "$iccidProfile2")
imeiProfile2=$(urldecode "$imeiProfile2")
echo "Content-type: application/json"
echo ""
# Validate required first profile
if [ -z "$iccidProfile1" ] || [ -z "$imeiProfile1" ]; then
echo '{"status": "error", "message": "Profile 1 is required"}'
exit 1
fi
# Function to log messages
log_message() {
local level="$1"
local message="$2"
local LOG_DIR="/tmp/log/imeiprofile"
local LOG_FILE="${LOG_DIR}/imeiprofile.log"
mkdir -p "${LOG_DIR}"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "${timestamp} - [${level}] ${message}" >> "$LOG_FILE"
logger -t imeiprofile "${level}: ${message}"
}
# Create required directories
mkdir -p /www/cgi-bin/services
# Function to create service script
create_service_script() {
cat > /www/cgi-bin/services/imeiprofile.sh <<'EOL'
#!/bin/sh
# Load UCI functions
. /lib/functions.sh
# Define file paths
QUEUE_FILE="/tmp/at_pipe.txt"
LOG_DIR="/tmp/log/imeiprofile"
LOG_FILE="${LOG_DIR}/imeiprofile.log"
PID_FILE="/var/run/imeiprofile.pid"
mkdir -p "${LOG_DIR}"
[ ! -f "${QUEUE_FILE}" ] && touch "${QUEUE_FILE}"
# Save PID
echo $$ > "${PID_FILE}"
# Enhanced logging function
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 imeiprofile "${level}: ${message}"
}
# AT command handling with locking
handle_lock() {
local max_wait=30
local wait_count=0
while [ -f "$QUEUE_FILE" ] && grep -q "AT_COMMAND" "$QUEUE_FILE" && [ $wait_count -lt $max_wait ]; do
sleep 1
wait_count=$((wait_count + 1))
done
printf '{"command":"AT_COMMAND","pid":"%s","timestamp":"%s"}\n' "$$" "$(date '+%H:%M:%S')" >> "$QUEUE_FILE"
}
# Execute AT command with retries
execute_at_command() {
local command="$1"
local result=""
local retry_count=0
local max_retries=3
while [ $retry_count -lt $max_retries ]; do
handle_lock
result=$(sms_tool at "$command" -t 4 2>&1)
local status=$?
sed -i "/\"pid\":\"$$\"/d" "$QUEUE_FILE"
if [ $status -eq 0 ] && [ -n "$result" ]; then
echo "$result"
return 0
fi
retry_count=$((retry_count + 1))
[ $retry_count -lt $max_retries ] && sleep 2
done
return 1
}
# Get current ICCID
get_current_iccid() {
local result=$(execute_at_command "AT+ICCID")
if [ $? -eq 0 ] && echo "$result" | grep -q "+ICCID:"; then
echo "$result" | grep "+ICCID:" | cut -d' ' -f2 | tr -d '[:space:]'
return 0
fi
return 1
}
# Get current IMEI
get_current_imei() {
local result=$(execute_at_command "AT+CGSN")
if [ $? -eq 0 ]; then
echo "$result" | grep -v "AT+CGSN" | grep -v "OK" | tr -d '\r\n[:space:]'
return 0
fi
return 1
}
# Set IMEI
set_imei() {
local imei="$1"
if execute_at_command "AT+EGMR=1,7,\"$imei\";+QPOWD=1"; then
return 0
fi
return 1
}
# Function to safely get UCI value with default
get_uci_value() {
local value
config_get value imei_profile "$1" "$2"
echo "${value:-$2}"
}
# Main service loop
while true; do
# Load current configuration
config_load quecmanager
# Get Profile 1
iccid_profile1=$(get_uci_value "iccid_profile1")
imei_profile1=$(get_uci_value "imei_profile1")
# Get Profile 2
iccid_profile2=$(get_uci_value "iccid_profile2")
imei_profile2=$(get_uci_value "imei_profile2")
# Get current ICCID and IMEI
current_iccid=$(get_current_iccid)
current_imei=$(get_current_imei)
if [ $? -eq 0 ] && [ -n "$current_iccid" ] && [ -n "$current_imei" ]; then
if [ "${current_iccid}" = "${iccid_profile1}" ]; then
if [ "${current_imei}" != "${imei_profile1}" ]; then
if set_imei "${imei_profile1}"; then
log_message "INFO" "Successfully applied Profile 1 IMEI"
else
log_message "ERROR" "Failed to apply Profile 1 IMEI"
fi
fi
elif [ -n "$iccid_profile2" ] && [ "${current_iccid}" = "${iccid_profile2}" ]; then
if [ "${current_imei}" != "${imei_profile2}" ]; then
if set_imei "${imei_profile2}"; then
log_message "INFO" "Successfully applied Profile 2 IMEI"
else
log_message "ERROR" "Failed to apply Profile 2 IMEI"
fi
fi
else
log_message "INFO" "No matching ICCID profile found"
fi
else
log_message "ERROR" "Failed to get current ICCID or IMEI"
fi
sleep 30
done
EOL
chmod 755 /www/cgi-bin/services/imeiprofile.sh
}
# Function to create init.d script
create_init_script() {
cat > /etc/init.d/imeiprofile-service <<'EOL'
#!/bin/sh /etc/rc.common
START=99
STOP=10
USE_PROCD=1
start_service() {
local enabled
# Check if service is enabled in UCI
config_load quecmanager
config_get enabled imei_profile enabled '0'
[ "$enabled" != "1" ] && return 0
procd_open_instance
procd_set_param command /www/cgi-bin/services/imeiprofile.sh
procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param nice 19
procd_close_instance
}
service_triggers() {
procd_add_reload_trigger "quecmanager"
}
reload_service() {
stop
start
}
EOL
chmod 755 /etc/init.d/imeiprofile-service
}
# Initialize UCI configuration
touch /etc/config/quecmanager
# Remove existing IMEI profile section if it exists
uci -q delete quecmanager.imei_profile
# Create new IMEI profile section
uci set quecmanager.imei_profile=service
uci set quecmanager.imei_profile.enabled=1
# Set Profile 1 configuration
uci set quecmanager.imei_profile.iccid_profile1="$iccidProfile1"
uci set quecmanager.imei_profile.imei_profile1="$imeiProfile1"
# Set Profile 2 configuration if provided
if [ -n "$iccidProfile2" ]; then
uci set quecmanager.imei_profile.iccid_profile2="$iccidProfile2"
uci set quecmanager.imei_profile.imei_profile2="$imeiProfile2"
fi
# Commit UCI changes
if ! uci commit quecmanager; then
log_message "ERROR" "Failed to save UCI configuration"
echo '{"status": "error", "message": "Failed to save UCI configuration"}'
exit 1
fi
log_message "INFO" "UCI configuration saved successfully"
# Create service script if it doesn't exist
if [ ! -f "/www/cgi-bin/services/imeiprofile.sh" ]; then
create_service_script
log_message "INFO" "Created service script"
fi
# Create init.d script if it doesn't exist
if [ ! -f "/etc/init.d/imeiprofile-service" ]; then
create_init_script
log_message "INFO" "Created init.d script"
fi
# Enable and start the service
/etc/init.d/imeiprofile-service enable
if /etc/init.d/imeiprofile-service restart; then
log_message "INFO" "Service started successfully"
echo '{"status": "success", "message": "IMEI profiles saved and service started"}'
else
log_message "ERROR" "Failed to start service"
echo '{"status": "error", "message": "Failed to start service"}'
fi

View File

@@ -0,0 +1,23 @@
#!/bin/sh
# Path: /www/cgi-bin/quecmanager
# Set content type to JSON
echo "Content-Type: application/json"
echo ""
# Fetch public IP using multiple fallback methods
PUBLIC_IP=$(
curl -s https://api.ipify.org 2>/dev/null || \
wget -qO- https://api.ipify.org 2>/dev/null || \
uclient-fetch -qO- https://api.ipify.org 2>/dev/null
)
# Handle errors
if [ -z "$PUBLIC_IP" ]; then
echo '{"error": "Failed to fetch public IP"}'
exit 1
fi
# Return JSON response
echo "{\"public_ip\": \"$PUBLIC_IP\"}"

View File

@@ -1,11 +0,0 @@
#!/bin/sh
export HOME=/tmp/home
# Create named pipe for speedtest output if it doesn't exist
[ ! -p /tmp/realtime_spd.json ] && mkfifo /tmp/realtime_spd.json
# Run speedtest in background
/usr/bin/speedtest --accept-license -f json -p yes --progress-update-interval=100 > /tmp/realtime_spd.json
# Remove named pipe
rm /tmp/realtime_spd.json

View File

@@ -1,13 +0,0 @@
#!/bin/sh
echo "Content-Type: text/event-stream"
echo "Cache-Control: no-cache"
echo "Connection: keep-alive"
echo ""
# Use cat to read from the FIFO
cat /tmp/realtime_spd.json | while read line; do
echo "data: $line"
echo
sleep 0.1
done