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 changes /dev/ttyUSBx name #649

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

Gammu modem changes /dev/ttyUSBx name #649

deajan opened this issue May 31, 2023 · 0 comments

Comments

@deajan
Copy link

deajan commented May 31, 2023

Hello,

Not an issue actually, but merely a post with a script to detect whenever a USB modem changes it's name (when disconnected / reconnected while it's old port is being read).

The following script updates the modem device path in /etc/gammu-smsdrc whenever needed.
You can configure it as cron task every 15 minutes or so

#!/usr/bin/env bash

log_file="/var/log/modem_update.log"
gammu_config_file="/etc/gammu-smsdrc"
modem=""
# Optional command when modem port change 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 restart service, exitcode: $?"
            fi
        fi
    fi
fi

Hope this helps someone ;)

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

No branches or pull requests

1 participant