Added cgi-bin scripts

This commit is contained in:
Russel Yasol
2025-01-17 14:57:15 +08:00
parent 397e0de45a
commit b8f9feef20
46 changed files with 3603 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
#!/bin/sh
echo "Content-type: application/json"
echo ""
# Initialize error flag
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
}
# Remove the entire quecmanager directory
if [ -d "/etc/quecmanager/apn_profile/" ]; then
rm -rf /etc/quecmanager/apn_profile/
if [ $? -ne 0 ]; then
append_error "Failed to remove quecmanager directory"
fi
else
append_error "quecmanager directory not found"
fi
# Remove the line from rc.local
if [ -f "/etc/rc.local" ]; then
# Create a temporary file
temp_file=$(mktemp)
# Remove the apnProfiles.sh line and copy to temp file
sed '/\/etc\/quecmanager\/apnProfiles.sh/d' /etc/rc.local > "$temp_file"
# Check if sed command was successful
if [ $? -eq 0 ]; then
# Replace original file with modified version
mv "$temp_file" /etc/rc.local
if [ $? -ne 0 ]; then
append_error "Failed to update rc.local"
fi
else
append_error "Failed to modify rc.local"
rm -f "$temp_file"
fi
else
append_error "rc.local file not found"
fi
# Remove temporary files that might have been created
rm -f /tmp/apn_result.txt
rm -f /tmp/debug.log
rm -f /tmp/inputICCID.txt
rm -f /tmp/outputICCID.txt
rm -f /tmp/inputAPN.txt
rm -f /tmp/outputAPN.txt
# Return appropriate JSON response
if [ "$has_error" = true ]; then
echo "{\"status\": \"error\", \"message\": \"$error_message\"}"
else
echo "{\"status\": \"success\", \"message\": \"APN profiles and configuration successfully removed\"}"
fi

View File

@@ -0,0 +1,45 @@
#!/bin/sh
echo "Content-type: application/json"
echo ""
CONFIG_FILE="/etc/quecmanager/apn_profile/apn_config.txt"
if [ ! -f "$CONFIG_FILE" ]; then
echo "{}"
exit 0
fi
# Read the configuration file
iccidProfile1=$(grep "^iccidProfile1=" "$CONFIG_FILE" | cut -d'=' -f2)
apnProfile1=$(grep "^apnProfile1=" "$CONFIG_FILE" | cut -d'=' -f2)
pdpType1=$(grep "^pdpType1=" "$CONFIG_FILE" | cut -d'=' -f2)
iccidProfile2=$(grep "^iccidProfile2=" "$CONFIG_FILE" | cut -d'=' -f2)
apnProfile2=$(grep "^apnProfile2=" "$CONFIG_FILE" | cut -d'=' -f2)
pdpType2=$(grep "^pdpType2=" "$CONFIG_FILE" | cut -d'=' -f2)
# Build the JSON response
echo "{"
# Add Profile 1 if it exists
if [ -n "$iccidProfile1" ]; then
echo " \"profile1\": {"
echo " \"iccid\": \"$iccidProfile1\","
echo " \"apn\": \"$apnProfile1\","
echo " \"pdpType\": \"$pdpType1\""
echo " }"
# Add comma if Profile 2 exists
[ -n "$iccidProfile2" ] && echo " ,"
fi
# Add Profile 2 if it exists
if [ -n "$iccidProfile2" ]; then
echo " \"profile2\": {"
echo " \"iccid\": \"$iccidProfile2\","
echo " \"apn\": \"$apnProfile2\","
echo " \"pdpType\": \"$pdpType2\""
echo " }"
fi
echo "}"

View File

@@ -0,0 +1,292 @@
#!/bin/sh
# Parse POST data (using busybox compatible method)
read -r QUERY_STRING
# Function to urldecode (busybox compatible version)
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
# Create directory with proper permissions
mkdir -p /etc/quecmanager/apn_profile
chmod 755 /etc/quecmanager/apn_profile
# Create a configuration file to store APN profiles (with proper permissions)
cat > /etc/quecmanager/apn_profile/apn_config.txt <<EOF
iccidProfile1=${iccidProfile1}
apnProfile1=${apnProfile1}
pdpType1=${pdpType1}
EOF
# Add second profile only if ICCID is provided
if [ -n "$iccidProfile2" ]; then
cat >> /etc/quecmanager/apn_profile/apn_config.txt <<EOF
iccidProfile2=${iccidProfile2}
apnProfile2=${apnProfile2}
pdpType2=${pdpType2}
EOF
fi
chmod 644 /etc/quecmanager/apn_profile/apn_config.txt
# Create the apnProfiles.sh script with proper locking mechanism and logging
cat > /etc/quecmanager/apn_profile/apnProfiles.sh <<'EOF'
#!/bin/sh
# Define file paths
QUEUE_FILE="/tmp/at_pipe.txt"
LOG_FILE="/tmp/apn_profiles.log"
[ ! -f "${QUEUE_FILE}" ] && touch "${QUEUE_FILE}"
# Enhanced logging function with debug level
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 apn_profiles "${level}: ${message}"
}
# Check for stale entries and clean them
check_and_clean_stale() {
local command_type="$1"
local wait_count=0
while [ $wait_count -lt 6 ]; do
if grep -q "\"command\":\"${command_type}\"" "$QUEUE_FILE"; then
log_message "DEBUG" "Waiting for ${command_type} to clear (attempt ${wait_count})"
sleep 1
wait_count=$((wait_count + 1))
else
return 0
fi
done
log_message "WARN" "Removing stale ${command_type} entry after ${wait_count}s"
sed -i "/\"command\":\"${command_type}\"/d" "$QUEUE_FILE"
return 0
}
# Simplified lock handling with debug
handle_lock() {
log_message "DEBUG" "Checking queue file status before lock"
if [ -f "$QUEUE_FILE" ]; then
log_message "DEBUG" "Current queue content: $(cat $QUEUE_FILE)"
else
log_message "DEBUG" "Queue file does not exist, creating it"
touch "$QUEUE_FILE"
fi
check_and_clean_stale "FETCH_LOCK"
log_message "DEBUG" "Adding AT_COMMAND entry to queue"
printf '{"command":"AT_COMMAND","pid":"%s","timestamp":"%s"}\n' \
"$$" \
"$(date '+%H:%M:%S')" >> "$QUEUE_FILE"
check_and_clean_stale "AT_COMMAND"
}
# Execute AT command without timeout dependency
execute_at_command() {
local command="$1"
local result=""
log_message "DEBUG" "Executing AT command: ${command}"
handle_lock
# Execute command and capture all output
result=$(sms_tool at "$command" -t 4 2>&1)
local status=$?
log_message "DEBUG" "Removing our entry from queue"
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
}
# Get current ICCID with enhanced debug
get_current_iccid() {
local result
local retry_count=0
local max_retries=3
log_message "INFO" "Attempting to get current ICCID"
while [ $retry_count -lt $max_retries ]; do
log_message "DEBUG" "ICCID attempt ${retry_count}"
result=$(execute_at_command "AT+ICCID")
local cmd_status=$?
log_message "DEBUG" "AT+ICCID command returned status: ${cmd_status}"
log_message "DEBUG" "AT+ICCID raw output: ${result}"
if [ $cmd_status -eq 0 ] && echo "$result" | grep -q "+ICCID:"; then
local iccid=$(echo "$result" | grep "+ICCID:" | cut -d' ' -f2 | tr -d '[:space:]')
log_message "INFO" "Retrieved current ICCID: ${iccid}"
echo "${iccid}"
return 0
else
log_message "WARN" "Attempt ${retry_count} failed to get valid ICCID"
log_message "WARN" "Result: ${result}"
fi
retry_count=$((retry_count + 1))
if [ $retry_count -lt $max_retries ]; then
log_message "INFO" "Waiting 2 seconds before retry"
sleep 2
fi
done
log_message "ERROR" "Failed to get ICCID after $max_retries attempts"
return 1
}
# Set APN with modified error handling - removed strict OK check
set_apn() {
local pdp_type="$1"
local apn="$2"
local result
local retry_count=0
local max_retries=3
if [ -z "$pdp_type" ] || [ -z "$apn" ]; then
log_message "ERROR" "Invalid PDP type or APN"
return 1
fi
while [ $retry_count -lt $max_retries ]; do
result=$(execute_at_command "AT+CGDCONT=1,\"$pdp_type\",\"$apn\";+COPS=2;+COPS=0")
if [ $? -eq 0 ]; then
log_message "INFO" "Successfully set APN: $apn with PDP type: $pdp_type"
return 0
fi
retry_count=$((retry_count + 1))
[ $retry_count -lt $max_retries ] && sleep 2
done
log_message "ERROR" "Failed to set APN: $apn after $max_retries attempts"
return 1
}
# Load configuration
if [ -f /etc/quecmanager/apn_profile/apn_config.txt ]; then
. /etc/quecmanager/apn_profile/apn_config.txt
log_message "INFO" "Loaded configuration - Profile1 ICCID: ${iccidProfile1}, Profile2 ICCID: ${iccidProfile2:-none}"
else
log_message "ERROR" "Configuration file not found"
echo "Configuration file not found" > /tmp/apn_result.txt
exit 1
fi
# Get current ICCID and trim any whitespace
current_iccid=$(get_current_iccid | tr -d '[:space:]')
if [ $? -ne 0 ]; then
log_message "ERROR" "Failed to get current ICCID"
echo "Failed to get current ICCID" > /tmp/apn_result.txt
exit 1
fi
# Trim any whitespace from profile ICCIDs
iccidProfile1=$(echo "${iccidProfile1}" | tr -d '[:space:]')
[ -n "$iccidProfile2" ] && iccidProfile2=$(echo "${iccidProfile2}" | tr -d '[:space:]')
# Log the comparison values
log_message "INFO" "Comparing ICCIDs:"
log_message "INFO" "Current ICCID: ${current_iccid}"
log_message "INFO" "Profile1 ICCID: ${iccidProfile1}"
[ -n "$iccidProfile2" ] && log_message "INFO" "Profile2 ICCID: ${iccidProfile2}"
# Match ICCID and apply corresponding profile
if [ "${current_iccid}" = "${iccidProfile1}" ]; then
log_message "INFO" "Matched with Profile1, applying settings..."
if set_apn "$pdpType1" "$apnProfile1"; then
echo "APN set successfully" > /tmp/apn_result.txt
else
echo "Failed to set APN" > /tmp/apn_result.txt
fi
elif [ -n "$iccidProfile2" ] && [ "${current_iccid}" = "${iccidProfile2}" ]; then
log_message "INFO" "Matched with Profile2, applying settings..."
if set_apn "$pdpType2" "$apnProfile2"; then
echo "APN set successfully" > /tmp/apn_result.txt
else
echo "Failed to set APN" > /tmp/apn_result.txt
fi
else
log_message "WARN" "No matching ICCID profile found"
echo "No matching ICCID profile found" > /tmp/apn_result.txt
fi
EOF
# Make the script executable
chmod 755 /etc/quecmanager/apn_profile/apnProfiles.sh
# Add to rc.local if not already present
if ! grep -q "^[^#]*\/etc\/quecmanager\/apn_profile\/apnProfiles.sh" /etc/rc.local; then
sed -i '/^exit 0/i /etc/quecmanager/apn_profile/apnProfiles.sh' /etc/rc.local
fi
# Run the script immediately
/etc/quecmanager/apn_profile/apnProfiles.sh
# Check the result
if [ -f /tmp/apn_result.txt ]; then
result=$(cat /tmp/apn_result.txt)
rm -f /tmp/apn_result.txt
case "$result" in
"APN set successfully")
echo '{"status": "success", "message": "APN profiles saved and applied successfully"}'
;;
"No matching ICCID profile found")
echo '{"status": "warning", "message": "APN profiles saved but no matching ICCID found"}'
;;
"Configuration file not found")
echo '{"status": "error", "message": "Configuration file not found"}'
;;
"Failed to get current ICCID")
echo '{"status": "error", "message": "Failed to get current ICCID"}'
;;
*)
echo '{"status": "error", "message": "APN profiles saved but failed to apply"}'
;;
esac
else
echo '{"status": "error", "message": "Something went wrong while processing APN profiles"}'
fi

View File

@@ -0,0 +1,38 @@
#!/bin/sh
# handle_sms.sh - CGI script to handle SMS web requests
# Content type declaration for CGI
echo "Content-type: application/json"
echo ""
# Check if atinout and jq are installed
if ! command -v atinout &> /dev/null || ! command -v jq &> /dev/null; then
echo '{"error": "Required tools (atinout or jq) are not installed"}'
exit 1
fi
# Check if the device exists
if [ ! -c "/dev/smd7" ]; then
echo '{"error": "Device /dev/smd7 not found"}'
exit 1
fi
# # Fetch all SMS messages and update the JSON file
# Disabled until the atinout bug is fixed
# if ! echo "AT+CMGL=\"ALL\"" | atinout - /dev/smd7 - | jq -R -s '
# split("\n") |
# map(select(length > 0)) |
# map(
# select(startswith("+CMGL:") or (. != "OK" and . != "ERROR"))
# ) |
# {messages: .}
# ' > /tmp/sms_inbox.json; then
# echo '{"error": "Failed to fetch SMS messages"}'
# exit 1
# fi
# Return the contents of the JSON file
if [ -f "/tmp/sms_inbox.json" ]; then
cat /tmp/sms_inbox.json
else
echo '{"error": "SMS inbox file not found"}'
fi

View File

@@ -0,0 +1,66 @@
#!/bin/sh
echo "Content-type: application/json"
echo ""
# Initialize error flag
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
}
# Remove the entire quecmanager directory
if [ -d "/etc/quecmanager/imei_profile/" ]; then
rm -rf /etc/quecmanager/imei_profile/
if [ $? -ne 0 ]; then
append_error "Failed to remove quecmanager directory"
fi
else
append_error "quecmanager directory not found"
fi
# Remove the line from rc.local
if [ -f "/etc/rc.local" ]; then
# Create a temporary file
temp_file=$(mktemp)
# Remove the imeiProfiles.sh line and copy to temp file
sed '/\/etc\/quecmanager\/imeiProfiles.sh/d' /etc/rc.local > "$temp_file"
# Check if sed command was successful
if [ $? -eq 0 ]; then
# Replace original file with modified version
mv "$temp_file" /etc/rc.local
if [ $? -ne 0 ]; then
append_error "Failed to update rc.local"
fi
else
append_error "Failed to modify rc.local"
rm -f "$temp_file"
fi
else
append_error "rc.local file not found"
fi
# Remove temporary files that might have been created
rm -f /tmp/imei_result.txt
rm -f /tmp/debug.log
rm -f /tmp/inputICCID.txt
rm -f /tmp/outputICCID.txt
rm -f /tmp/inputIMEI.txt
rm -f /tmp/outputIMEI.txt
# Return appropriate JSON response
if [ "$has_error" = true ]; then
echo "{\"status\": \"error\", \"message\": \"$error_message\"}"
else
echo "{\"status\": \"success\", \"message\": \"IMEI profiles and configuration successfully removed\"}"
fi

View File

@@ -0,0 +1,39 @@
#!/bin/sh
echo "Content-type: application/json"
echo ""
CONFIG_FILE="/etc/quecmanager/imei_profile/imei_config.txt"
if [ ! -f "$CONFIG_FILE" ]; then
echo "{}"
exit 0
fi
# Read the configuration file
iccidProfile1=$(grep "^iccidProfile1=" "$CONFIG_FILE" | cut -d'=' -f2)
imeiProfile1=$(grep "^imeiProfile1=" "$CONFIG_FILE" | cut -d'=' -f2)
iccidProfile2=$(grep "^iccidProfile2=" "$CONFIG_FILE" | cut -d'=' -f2)
imeiProfile2=$(grep "^imeiProfile2=" "$CONFIG_FILE" | cut -d'=' -f2)
# Build the JSON response
echo "{"
# Add Profile 1 if it exists
if [ -n "$iccidProfile1" ]; then
echo " \"profile1\": {"
echo " \"iccid\": \"$iccidProfile1\","
echo " \"imei\": \"$imeiProfile1\""
echo " }"
# Add comma if Profile 2 exists
[ -n "$iccidProfile2" ] && echo " ,"
fi
# Add Profile 2 if it exists
if [ -n "$iccidProfile2" ]; then
echo " \"profile2\": {"
echo " \"iccid\": \"$iccidProfile2\","
echo " \"imei\": \"$imeiProfile2\""
echo " }"
fi
echo "}"

View File

@@ -0,0 +1,333 @@
#!/bin/sh
# Parse POST data (using busybox compatible method)
read -r QUERY_STRING
# Function to urldecode (busybox compatible version)
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
# Create directory with proper permissions
mkdir -p /etc/quecmanager/imei_profile
chmod 755 /etc/quecmanager/imei_profile
# Create a configuration file to store IMEI profiles
cat > /etc/quecmanager/imei_profile/imei_config.txt <<EOF
iccidProfile1=${iccidProfile1}
imeiProfile1=${imeiProfile1}
EOF
# Add second profile only if ICCID is provided
if [ -n "$iccidProfile2" ]; then
cat >> /etc/quecmanager/imei_profile/imei_config.txt <<EOF
iccidProfile2=${iccidProfile2}
imeiProfile2=${imeiProfile2}
EOF
fi
chmod 644 /etc/quecmanager/imei_profile/imei_config.txt
# Create the imeiProfiles.sh script with proper locking mechanism and logging
cat > /etc/quecmanager/imei_profile/imeiProfiles.sh <<'EOF'
#!/bin/sh
# Define file paths
QUEUE_FILE="/tmp/at_pipe.txt"
LOG_FILE="/tmp/imei_profiles.log"
[ ! -f "${QUEUE_FILE}" ] && touch "${QUEUE_FILE}"
# Enhanced logging function with debug level
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 imei_profiles "${level}: ${message}"
}
# Check for stale entries and clean them
check_and_clean_stale() {
local command_type="$1"
local wait_count=0
while [ $wait_count -lt 6 ]; do
if grep -q "\"command\":\"${command_type}\"" "$QUEUE_FILE"; then
log_message "DEBUG" "Waiting for ${command_type} to clear (attempt ${wait_count})"
sleep 1
wait_count=$((wait_count + 1))
else
return 0
fi
done
log_message "WARN" "Removing stale ${command_type} entry after ${wait_count}s"
sed -i "/\"command\":\"${command_type}\"/d" "$QUEUE_FILE"
return 0
}
# Simplified lock handling with debug
handle_lock() {
log_message "DEBUG" "Checking queue file status before lock"
if [ -f "$QUEUE_FILE" ]; then
log_message "DEBUG" "Current queue content: $(cat $QUEUE_FILE)"
else
log_message "DEBUG" "Queue file does not exist, creating it"
touch "$QUEUE_FILE"
fi
check_and_clean_stale "FETCH_LOCK"
log_message "DEBUG" "Adding AT_COMMAND entry to queue"
printf '{"command":"AT_COMMAND","pid":"%s","timestamp":"%s"}\n' \
"$$" \
"$(date '+%H:%M:%S')" >> "$QUEUE_FILE"
check_and_clean_stale "AT_COMMAND"
}
# Execute AT command without timeout dependency
execute_at_command() {
local command="$1"
local result=""
log_message "DEBUG" "Executing AT command: ${command}"
handle_lock
# Execute command and capture all output
result=$(sms_tool at "$command" -t 4 2>&1)
local status=$?
log_message "DEBUG" "Removing our entry from queue"
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
}
# Get current ICCID with enhanced debug
get_current_iccid() {
local result
local retry_count=0
local max_retries=3
log_message "INFO" "Attempting to get current ICCID"
while [ $retry_count -lt $max_retries ]; do
log_message "DEBUG" "ICCID attempt ${retry_count}"
result=$(execute_at_command "AT+ICCID")
local cmd_status=$?
log_message "DEBUG" "AT+ICCID command returned status: ${cmd_status}"
log_message "DEBUG" "AT+ICCID raw output: ${result}"
if [ $cmd_status -eq 0 ] && echo "$result" | grep -q "+ICCID:"; then
local iccid=$(echo "$result" | grep "+ICCID:" | cut -d' ' -f2 | tr -d '[:space:]')
log_message "INFO" "Retrieved current ICCID: ${iccid}"
echo "${iccid}"
return 0
else
log_message "WARN" "Attempt ${retry_count} failed to get valid ICCID"
log_message "WARN" "Result: ${result}"
fi
retry_count=$((retry_count + 1))
if [ $retry_count -lt $max_retries ]; then
log_message "INFO" "Waiting 2 seconds before retry"
sleep 2
fi
done
log_message "ERROR" "Failed to get ICCID after $max_retries attempts"
return 1
}
# Get current IMEI with enhanced debug
get_current_imei() {
local result
local retry_count=0
local max_retries=3
log_message "INFO" "Attempting to get current IMEI"
while [ $retry_count -lt $max_retries ]; do
log_message "DEBUG" "IMEI attempt ${retry_count}"
result=$(execute_at_command "AT+CGSN")
local cmd_status=$?
log_message "DEBUG" "AT+CGSN command returned status: ${cmd_status}"
log_message "DEBUG" "AT+CGSN raw output: ${result}"
if [ $cmd_status -eq 0 ]; then
local imei=$(echo "$result" | grep -v "AT+CGSN" | grep -v "OK" | tr -d '\r\n[:space:]')
if [ -n "$imei" ]; then
log_message "INFO" "Retrieved current IMEI: ${imei}"
echo "${imei}"
return 0
fi
fi
log_message "WARN" "Attempt ${retry_count} failed to get valid IMEI"
retry_count=$((retry_count + 1))
[ $retry_count -lt $max_retries ] && sleep 2
done
log_message "ERROR" "Failed to get IMEI after $max_retries attempts"
return 1
}
# Set IMEI with enhanced debug
set_imei() {
local imei="$1"
local retry_count=0
local max_retries=3
log_message "INFO" "Attempting to set IMEI: ${imei}"
while [ $retry_count -lt $max_retries ]; do
log_message "DEBUG" "IMEI set attempt ${retry_count}"
result=$(execute_at_command "AT+EGMR=1,7,\"$imei\";+QPOWD=1")
local cmd_status=$?
log_message "DEBUG" "AT+EGMR command returned status: ${cmd_status}"
log_message "DEBUG" "AT+EGMR raw output: ${result}"
if [ $cmd_status -eq 0 ] && echo "$result" | grep -q "OK"; then
log_message "INFO" "Successfully set IMEI: ${imei}"
return 0
fi
retry_count=$((retry_count + 1))
[ $retry_count -lt $max_retries ] && sleep 2
done
log_message "ERROR" "Failed to set IMEI after $max_retries attempts"
return 1
}
# Load configuration
if [ -f /etc/quecmanager/imei_profile/imei_config.txt ]; then
. /etc/quecmanager/imei_profile/imei_config.txt
log_message "INFO" "Loaded configuration - Profile1 ICCID: ${iccidProfile1}, IMEI: ${imeiProfile1}"
[ -n "$iccidProfile2" ] && log_message "INFO" "Profile2 ICCID: ${iccidProfile2}, IMEI: ${imeiProfile2}"
else
log_message "ERROR" "Configuration file not found"
echo "Configuration file not found" > /tmp/imei_result.txt
exit 1
fi
# Get current ICCID and IMEI
current_iccid=$(get_current_iccid)
current_imei=$(get_current_imei)
if [ $? -ne 0 ]; then
log_message "ERROR" "Failed to get current ICCID or IMEI"
echo "Failed to get current ICCID or IMEI" > /tmp/imei_result.txt
exit 1
fi
log_message "INFO" "Current ICCID: ${current_iccid}"
log_message "INFO" "Current IMEI: ${current_imei}"
# Match ICCID and apply corresponding profile
if [ "${current_iccid}" = "${iccidProfile1}" ]; then
log_message "INFO" "Matched with Profile1"
if [ "${current_imei}" != "${imeiProfile1}" ]; then
log_message "INFO" "IMEI needs updating for Profile1"
if set_imei "${imeiProfile1}"; then
echo "IMEI set successfully" > /tmp/imei_result.txt
else
echo "Failed to set IMEI" > /tmp/imei_result.txt
fi
else
log_message "INFO" "IMEI already matches Profile1"
echo "IMEI already correct" > /tmp/imei_result.txt
fi
elif [ -n "${iccidProfile2}" ] && [ "${current_iccid}" = "${iccidProfile2}" ]; then
log_message "INFO" "Matched with Profile2"
if [ "${current_imei}" != "${imeiProfile2}" ]; then
log_message "INFO" "IMEI needs updating for Profile2"
if set_imei "${imeiProfile2}"; then
echo "IMEI set successfully" > /tmp/imei_result.txt
else
echo "Failed to set IMEI" > /tmp/imei_result.txt
fi
else
log_message "INFO" "IMEI already matches Profile2"
echo "IMEI already correct" > /tmp/imei_result.txt
fi
else
log_message "WARN" "No matching ICCID profile found"
echo "No matching ICCID profile found" > /tmp/imei_result.txt
fi
EOF
# Make the script executable
chmod 755 /etc/quecmanager/imei_profile/imeiProfiles.sh
# Add to rc.local if not already present
if ! grep -q "^[^#]*\/etc\/quecmanager\/imei_profile\/imeiProfiles.sh" /etc/rc.local; then
sed -i '/^exit 0/i /etc/quecmanager/imei_profile/imeiProfiles.sh' /etc/rc.local
fi
# Run the script immediately
/etc/quecmanager/imei_profile/imeiProfiles.sh
# Check the result
if [ -f /tmp/imei_result.txt ]; then
result=$(cat /tmp/imei_result.txt)
rm -f /tmp/imei_result.txt
case "$result" in
"IMEI set successfully")
echo '{"status": "success", "message": "IMEI profiles saved and applied successfully"}'
;;
"IMEI already correct")
echo '{"status": "success", "message": "IMEI profiles saved, no changes needed"}'
;;
"No matching ICCID profile found")
echo '{"status": "warning", "message": "IMEI profiles saved but no matching ICCID found"}'
;;
"Configuration file not found")
echo '{"status": "error", "message": "Configuration file not found"}'
;;
"Failed to get current ICCID or IMEI")
echo '{"status": "error", "message": "Failed to get current ICCID or IMEI"}'
;;
*)
echo '{"status": "error", "message": "IMEI profiles saved but failed to apply"}'
;;
esac
else
echo '{"status": "error", "message": "Something went wrong while processing IMEI profiles"}'
fi

View File

@@ -0,0 +1,201 @@
#!/bin/sh
# Configuration
CONFIG_FILE="/etc/cell_lock_schedule.conf"
STATUS_FILE="/tmp/cell_lock_status"
CELL_LOCK_SCRIPT="/usr/bin/set_cell_lock.sh"
# 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
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 -
fi
# Enable NR5G lock if parameters exist
if [ -n "$NR5G_PARAMS" ]; then
echo "AT+QNWLOCK=\"common/5g\",$NR5G_PARAMS" | atinout - /dev/smd11 -
fi
;;
disable)
# Disable LTE lock
echo 'AT+QNWLOCK="common/4g",0' | atinout - /dev/smd11 -
# Disable NR5G lock
echo 'AT+QNWLOCK="common/5g",0' | atinout - /dev/smd11 -
;;
*)
echo "Invalid action"
exit 1
;;
esac
# Restart network registration to apply changes
echo "AT+COPS=2" | atinout - /dev/smd11 -
sleep 2
echo "AT+COPS=0" | atinout - /dev/smd11 -
exit 0
EOL
# Make the script executable
chmod +x "$CELL_LOCK_SCRIPT"
fi
}
# Function to remove set_cell_lock.sh script
remove_cell_lock_script() {
if [ -f "$CELL_LOCK_SCRIPT" ]; then
rm "$CELL_LOCK_SCRIPT"
fi
}
# Function to urldecode
urldecode() {
echo -e "$(echo "$1" | sed 's/+/ /g;s/%\([0-9A-F][0-9A-F]\)/\\x\1/g')"
}
# Function to convert HH:MM to cron format
convert_to_cron_time() {
echo "$1" | awk -F: '{print $2, $1}'
}
# Function to save configuration
save_config() {
echo "START_TIME=$1" >"$CONFIG_FILE"
echo "END_TIME=$2" >>"$CONFIG_FILE"
echo "ENABLED=1" >>"$CONFIG_FILE"
}
# Function to disable scheduling
disable_scheduling() {
if [ -f "$CONFIG_FILE" ]; then
sed -i 's/ENABLED=1/ENABLED=0/' "$CONFIG_FILE"
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
}
# Function to get current status
get_status() {
if [ -f "$CONFIG_FILE" ]; then
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 ""
echo "{\"enabled\":$ENABLED,\"start_time\":\"$START_TIME\",\"end_time\":\"$END_TIME\"}"
else
echo "Status: 200 OK"
echo "Content-Type: application/json"
echo ""
echo "{\"enabled\":0,\"start_time\":\"\",\"end_time\":\"\"}"
fi
}
# 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
disable_scheduling
echo "Status: 200 OK"
echo "Content-Type: application/json"
echo ""
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
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_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"
echo "Status: 200 OK"
echo "Content-Type: application/json"
echo ""
echo "{\"status\":\"success\",\"message\":\"Scheduling enabled\"}"
exit 0
fi
# Parse query string for GET requests
if [ "$REQUEST_METHOD" = "GET" ]; then
QUERY_STRING=$(echo "$QUERY_STRING" | sed 's/&/\n/g')
for param in $QUERY_STRING; do
case "$param" in
status=*)
get_status
exit 0
;;
esac
done
fi
# If no valid request is made
echo "Status: 400 Bad Request"
echo "Content-Type: application/json"
echo ""
echo "{\"error\":\"Invalid request\"}"
exit 1

View File

@@ -0,0 +1,50 @@
#!/bin/sh
# Set content type
printf "Content-Type: application/json\n\n"
# URL decode function
urldecode() {
echo "$*" | sed 's/+/ /g;s/%\([0-9A-F][0-9A-F]\)/\\\\x\1/g' | xargs -0 printf '%b'
}
# Extract indexes from query string
query=$(echo "$QUERY_STRING" | grep -o 'indexes=[^&]*' | cut -d= -f2)
indexes=$(urldecode "$query")
# Function to output JSON response
send_json() {
printf '{"status":"%s","message":"%s"}\n' "$1" "$2"
}
# Validate input
if [ -z "$indexes" ]; then
send_json "error" "No indexes provided"
exit 0
fi
# Initialize counters
success=0
failure=0
# Process each index
echo "$indexes" | tr ',' '\n' | while read -r index; do
if [ -n "$index" ] && [ "$index" -eq "$index" ] 2>/dev/null; then
if sms_tool delete "$index" 2>/dev/null; then
success=$((success + 1))
else
failure=$((failure + 1))
fi
fi
done
# Send response
if [ $success -gt 0 ]; then
if [ $failure -eq 0 ]; then
send_json "success" "Successfully deleted $success message(s)"
else
send_json "partial" "Deleted $success message(s), failed to delete $failure message(s)"
fi
else
send_json "error" "Failed to delete messages"
fi

View File

@@ -0,0 +1,10 @@
#!/bin/sh
printf "Content-type: application/json\r\n\r\n"
# Execute the command and return the JSON response
if command -v sms_tool > /dev/null 2>&1; then
sms_tool -j recv
else
printf '{"error": "sms_tool not found"}\n'
fi

View File

@@ -0,0 +1,57 @@
#!/bin/sh
echo "Content-Type: application/json"
echo "Cache-Control: no-cache"
echo ""
# Function to URL decode the string
urldecode() {
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
# Function to escape JSON string
escape_json() {
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\n/\\n/g; s/\r/\\r/g; s/\t/\\t/g'
}
# Read POST data
read -r QUERY_STRING
# Extract phone and message from POST data
phone=$(echo "$QUERY_STRING" | grep -o 'phone=[^&]*' | cut -d= -f2)
message=$(echo "$QUERY_STRING" | grep -o 'message=[^&]*' | cut -d= -f2)
# URL decode the message
decoded_message=$(urldecode "$message")
# Validate inputs
if [ -z "$phone" ] || [ -z "$message" ]; then
echo '{"success":false,"error":"Phone number and message are required"}'
exit 0
fi
# Validate phone number (only numbers allowed)
if ! echo "$phone" | grep -q '^[0-9]\+$'; then
echo '{"success":false,"error":"Invalid phone number format"}'
exit 0
fi
# Try to send SMS and capture output
result=$(sms_tool send "$phone" "$decoded_message" 2>&1)
escaped_result=$(escape_json "$result")
# Check if SMS was sent successfully by looking for "sms sent sucessfully"
if echo "$result" | grep -q "sms sent sucessfully"; then
# Extract the message ID if present
message_id=$(echo "$result" | grep -o '[0-9]*$')
echo "{\"success\":true,\"message\":\"SMS sent successfully\",\"messageId\":\"$message_id\",\"raw\":\"$escaped_result\"}"
elif echo "$result" | grep -q "sms not sent, code 350"; then
# Kill any hanging sms_tool process
pkill -f "sms_tool send"
echo '{"success":false,"error":"No prepaid credit available"}'
else
# Kill any hanging sms_tool process
pkill -f "sms_tool send"
echo "{\"success\":false,\"error\":\"Failed to send SMS\",\"raw\":\"$escaped_result\"}"
fi