Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gammu modem vanishes or changes tty port... Here's the fix #209

Open
deajan opened this issue May 31, 2023 · 0 comments
Open

gammu modem vanishes or changes tty port... Here's the fix #209

deajan opened this issue May 31, 2023 · 0 comments

Comments

@deajan
Copy link
Contributor

deajan commented May 31, 2023

Hello,

This is not an issue, merely a workaround for USB modems that like to change ports when disconnected / reconnected.
So whenever you disconnect / reconnect a USB modem, ports vary from /dev/ttyUSB0 to /dev/ttyUSB2. If while reconnecting, some program was trying to read /dev/ttyUSB2, then the modem will be connected as /dev/ttyUSB3, rendering your gammu setup unusable.

Here's a script I launch every 10 minutes as a cron task, that detects which port has a AT modem connected, and updates the corresponding gammu file.

The script needs picocom version >=3.0 as only dependency.


log_file="/var/log/modem_update.log"
gammu_config_file="/etc/gammutc"
modem=""
# Optional command when changed modem detected
#optional_cmd="systemctl restart gammu-smsd"

function log {
    line="${1}"

    echo "${line}" >> "${log_file}"
    echo "${line}"
}

for tty in $(ls /dev/ttyUSB*); do
    log "Checking $tty"
    picocom -qrX -b 9600 $tty
    sleep 1
    result=$(echo "AT&F" | picocom -qrix 1000 $tty)
    if [ "${result}" = "AT&F" ]; then
        log "Found AT compatible modem at $tty"
        modem="${tty}"
    else
        log "No AT compatible modem at $tty"
    fi
done

curr_gammu_modem=$(cat "${gammu_config_file}" | grep "device = " | awk -F'=' '{gsub(/ /,""); print $2}')
if [ "${modem}" != "" ]; then
    if [ "${modem}" != "${curr_gammu_modem}" ]; then
        log "Updating modem from ${curr_gammu_modem} to ${modem}"
        sed -i "s%${curr_gammu_modem}%${modem}%" "${gammu_config_file}"
        if [ "${optional_cmd}" != "" ]; then
            log "Running ${optional_cmd}"
            ${optional_cmd} >> "${log_file}"
            if [ $? -ne 0 ]; then
                log "Could not run cmd, exitcode: $?"
            fi
        fi
    fi
fi

Disclaimer:

  • Bear in mind that this script is supposed to fix a "one modem only" usecase. If you have multiple modems, you'll have to add a bit of udevadm magic to know which /dev/ttyUSBx belong to which USB modem.
  • Don't bug me if your machine explodes while executing this script

@OsaAjani Would you mind accepting this as optional script in a PR ? I'm pretty sure this can handle loads of gammu failure related scenarios

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant