Merging beta 1.1.1-4 release candidate
This commit is contained in:
@@ -1,126 +0,0 @@
|
||||
#!/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
|
||||
@@ -0,0 +1,200 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Set content-type for JSON response
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Define file paths and configuration
|
||||
QUEUE_FILE="/tmp/at_pipe.txt"
|
||||
TEMP_FILE="/tmp/network_info_output.txt"
|
||||
LOG_FILE="/var/log/network_info.log"
|
||||
LOCK_KEYWORD="AT_COMMAND_LOCK"
|
||||
MAX_WAIT=6
|
||||
COMMAND_TIMEOUT=4
|
||||
|
||||
# Function to log messages
|
||||
log_message() {
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "${LOG_FILE}"
|
||||
logger -t network_info "$1"
|
||||
}
|
||||
|
||||
# Function to output JSON error
|
||||
output_error() {
|
||||
printf '{"status":"error","message":"%s","timestamp":"%s"}\n' "$1" "$(date '+%H:%M:%S')"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Function to clean and add lock
|
||||
add_clean_lock() {
|
||||
local TIMESTAMP=$(date +%s)
|
||||
local WAIT_START=$(date +%s)
|
||||
|
||||
while true; do
|
||||
local CURRENT_TIME=$(date +%s)
|
||||
|
||||
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
|
||||
|
||||
printf '{"id":"%s","timestamp":"%s","command":"%s","status":"lock","pid":"%s","start_time":"%s","priority":"high"}\n' \
|
||||
"${LOCK_KEYWORD}" \
|
||||
"$(date '+%H:%M:%S')" \
|
||||
"${LOCK_KEYWORD}" \
|
||||
"$$" \
|
||||
"$TIMESTAMP" >> "$QUEUE_FILE"
|
||||
|
||||
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 execute AT command with retries
|
||||
execute_at_command() {
|
||||
local CMD="$1"
|
||||
local RETRY_COUNT=0
|
||||
local MAX_RETRIES=3
|
||||
local OUTPUT=""
|
||||
|
||||
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
|
||||
OUTPUT=$(timeout $COMMAND_TIMEOUT sms_tool at "$CMD" -D 2>&1)
|
||||
local EXIT_CODE=$?
|
||||
|
||||
if [ $EXIT_CODE -eq 0 ]; then
|
||||
if echo "$OUTPUT" | grep -q "CME ERROR"; then
|
||||
RETRY_COUNT=$((RETRY_COUNT + 1))
|
||||
[ $RETRY_COUNT -lt $MAX_RETRIES ] && sleep 1
|
||||
continue
|
||||
fi
|
||||
echo "$OUTPUT"
|
||||
return 0
|
||||
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
|
||||
}
|
||||
|
||||
# Function to check network mode
|
||||
check_network_mode() {
|
||||
local OUTPUT=$(execute_at_command "AT+QENG=\"servingcell\"")
|
||||
echo "$OUTPUT" > "$TEMP_FILE"
|
||||
|
||||
# Check for both LTE and NR5G-NSA (NSA mode)
|
||||
if echo "$OUTPUT" | grep -q "\"LTE\"" && echo "$OUTPUT" | grep -q "\"NR5G-NSA\""; then
|
||||
echo "NRLTE"
|
||||
# Check for LTE only
|
||||
elif echo "$OUTPUT" | grep -q "\"LTE\""; then
|
||||
echo "LTE"
|
||||
# Check for NR5G-SA
|
||||
elif echo "$OUTPUT" | grep -q "\"NR5G-SA\""; then
|
||||
echo "NR5G"
|
||||
else
|
||||
echo "UNKNOWN"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check NR5G measurement info setting
|
||||
check_nr5g_meas_info() {
|
||||
local OUTPUT=$(execute_at_command "AT+QNWCFG=\"nr5g_meas_info\"")
|
||||
if echo "$OUTPUT" | grep -q "\"nr5g_meas_info\",1"; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to escape JSON string
|
||||
escape_json_string() {
|
||||
echo "$1" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | tr -d '\n' | sed 's/\r//g'
|
||||
}
|
||||
|
||||
# Function to parse and format output as JSON
|
||||
format_output_json() {
|
||||
local MODE="$1"
|
||||
local NEIGHBOR_OUTPUT="$2"
|
||||
local MEAS_OUTPUT="$3"
|
||||
|
||||
# Basic JSON structure
|
||||
printf '{"status":"success","timestamp":"%s","mode":"%s","data":{' "$(date '+%H:%M:%S')" "$MODE"
|
||||
|
||||
# Add neighbor cell info if available
|
||||
if [ -n "$NEIGHBOR_OUTPUT" ]; then
|
||||
printf '"neighborCells":"%s"' "$(escape_json_string "$NEIGHBOR_OUTPUT")"
|
||||
fi
|
||||
|
||||
# Add measurement info if available
|
||||
if [ -n "$MEAS_OUTPUT" ]; then
|
||||
[ -n "$NEIGHBOR_OUTPUT" ] && printf ','
|
||||
printf '"meas":"%s"' "$(escape_json_string "$MEAS_OUTPUT")"
|
||||
fi
|
||||
|
||||
printf '}}\n'
|
||||
}
|
||||
|
||||
# Main execution
|
||||
{
|
||||
if ! add_clean_lock; then
|
||||
output_error "Failed to acquire lock for command processing"
|
||||
fi
|
||||
|
||||
# Check network mode
|
||||
NETWORK_MODE=$(check_network_mode)
|
||||
log_message "Detected network mode: $NETWORK_MODE"
|
||||
|
||||
SERVING_OUTPUT=""
|
||||
MEAS_OUTPUT=""
|
||||
|
||||
case "$NETWORK_MODE" in
|
||||
"NRLTE")
|
||||
SERVING_OUTPUT=$(execute_at_command "AT+QENG=\"neighbourcell\"")
|
||||
if ! check_nr5g_meas_info; then
|
||||
MEAS_OUTPUT=$(execute_at_command "AT+QNWCFG=\"nr5g_meas_info\",1;+QNWCFG=\"nr5g_meas_info\"")
|
||||
else
|
||||
MEAS_OUTPUT=$(execute_at_command "AT+QNWCFG=\"nr5g_meas_info\"")
|
||||
fi
|
||||
;;
|
||||
"LTE")
|
||||
SERVING_OUTPUT=$(execute_at_command "AT+QENG=\"neighbourcell\"")
|
||||
;;
|
||||
"NR5G")
|
||||
if ! check_nr5g_meas_info; then
|
||||
MEAS_OUTPUT=$(execute_at_command "AT+QNWCFG=\"nr5g_meas_info\",1;+QNWCFG=\"nr5g_meas_info\"")
|
||||
else
|
||||
MEAS_OUTPUT=$(execute_at_command "AT+QNWCFG=\"nr5g_meas_info\"")
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
output_error "Unknown or unsupported network mode"
|
||||
;;
|
||||
esac
|
||||
|
||||
format_output_json "$NETWORK_MODE" "$SERVING_OUTPUT" "$MEAS_OUTPUT"
|
||||
remove_lock
|
||||
rm -f "$TEMP_FILE"
|
||||
|
||||
} || {
|
||||
# Error handler
|
||||
remove_lock
|
||||
rm -f "$TEMP_FILE"
|
||||
output_error "Internal error occurred"
|
||||
}
|
||||
Reference in New Issue
Block a user