From e1fb14118bef41288f0ac9ae9ac198a7db8fe169 Mon Sep 17 00:00:00 2001 From: Cameron Thompson <50184035+iamromulan@users.noreply.github.com> Date: Sun, 24 Mar 2024 17:51:48 -0400 Subject: [PATCH] Add the ability to specify an AT command as an argument Usage: atcmd '' or atcmd (starts an interactive session as before) Example: root@sdxlemur:~# atcmd 'ATI' ATI Quectel RM521F-GL Revision: RM521FGLEAR05A02M4G OK --- socat-at-bridge/atcmd | 46 ++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/socat-at-bridge/atcmd b/socat-at-bridge/atcmd index 8d272a4..df5006c 100644 --- a/socat-at-bridge/atcmd +++ b/socat-at-bridge/atcmd @@ -10,24 +10,15 @@ setup_device() { -echoctl -echoke noflsh -ixon -crtscts } -# Prepare the device for communication -setup_device - -echo -e "\033[0;36mType 'exit' to end the session.\033[0m" -while true; do - echo -en "\033[0;36mEnter AT Command: \033[0m" - read user_input - - if [[ "$user_input" == "exit" ]]; then - echo -e "\033[0;32mExiting...\033[0m" - break - fi - +# Function to send AT command and capture the output +send_at_command() { + local command="$1" + # Clear the device buffer before sending a new command echo -n > $DEVICE - # Send the AT command - echo -e "$user_input\r" > $DEVICE + # Send the AT command, preserving the integrity of the input + echo -e "$command\r" > $DEVICE # Use a temporary file to capture the command output tmpfile=$(mktemp) @@ -52,4 +43,27 @@ while true; do # Clean up rm "$tmpfile" -done +} + +# Prepare the device for communication +setup_device + +# Check if an AT command is provided as an argument +if [ $# -gt 0 ]; then + # Concatenate all arguments to handle commands with spaces and/or quotes correctly + FULL_CMD="$*" + send_at_command "$FULL_CMD" +else + echo -e "\033[0;36mType 'exit' to end the session.\033[0m" + while true; do + echo -en "\033[0;36mEnter AT Command: \033[0m" + read user_input + + if [[ "$user_input" == "exit" ]]; then + echo -e "\033[0;32mExiting...\033[0m" + break + fi + + send_at_command "$user_input" + done +fi