sergei-mironov / asterisk-dongle-setup

Playground project aimed at setting up Asterisk server and the GSM stack on Nix.
31 stars 4 forks source link
asterisk chan-dongle nix sip telegram-api telethon

Asterisk-dongle-setup

This project aims at wiring together many pieces of Linux software required to do the following:

Specifically, the setup includes Asterisk server with the GSM-modem dongle and tg2sip bridge and many other components glued together with Shell and Python scripts. The high degree of build automation is achieved thanks to the Nix package manager.

Scheme

Contents

  1. Contents
  2. Setup
  3. Usage
  4. Hardware notes
  5. Known issues
  6. Thirdparty issues
  7. Administration hints
  8. References

Setup

Walkthrough

  1. Install Nix package manager and update Nixpkgs repositories. This whole project is designed as a collection of nix-build expressions.
    • Note that Nix could co-exist with your native package manager.
    • We use 21.05 Nixpkgs branch commit as a base, but older 20.03 commits should also work.
  2. Go and get a GSM modem.

    Modem E173

    You need to find a supported GSM modem and plug it into the USB port of your computer. We tested Huawei-E173 only, but in theory any model supported by chan_dongle should work.

    • A somewhat outdated document about supported hardware is available here. We typically do care about Voice and SMS functions and don't care about USSD.
    • ./run.sh will check for the presence of /dev/ttyUSB0. If it is not present, the script would attempt to run the usb_modeswitch procedure.
    • Currently we automate switching only for Huawei E173 modem. For other models, try to follow the section about manual mode-switching.
  3. git clone --recursive <this-repo-url> ; cd ...
  4. Create ./secrets.nix file by copying and editing ./secrets_template.nix.
    • You need a mobile phone which is bound to some Telegram account.
    • Go to https://my.telegram.org/auth and register an API Client instance. You will be provided with api_id and api_hash values.
    • The bot token field is not currently used.
    • The Chat id field is a (typically negative) identifier of a chat to send SMS messages to. ./run.sh will print available chat identifiers at some point during the startup.
  5. Please be informed that the main script ./run.sh is VERY INSECURE. It configures Asterisk to use binary codec and then runs it as root.
  6. If you are OK with the above notice, run ./run.sh.
    • The script asks Nix to (re-)build everything that is required.
    • At first run script will initialize Telegram session for python relay script.
      • As a part of initialization, Telegram server will send a digital code to your Telegram account.
      • You are to type this code back into the script.
    • At first run the script will also initialize another Telegram session, this time for tg2sip (TODO: find out how to reuse the first session).
      • It will ask you a phone number with Telegram account.
      • Telegram will send a code to your account. You have to read it and type it back into the script.
    • At every run, the script runs a number of background services including the Asteris ARI client, SMS spool script, tg2sip application etc.
    • Finally, the Asterisk is run in foreground. You should see its *CLI> console prompt and be able type commands.
  7. ???
  8. Check the Usage section.

Doing USB Modeswitch manually

run.sh attempts to run usb_modeswitch procedure automatically for devices known to author. In case the procedure fails, one could attempt the manual way:

  1. nix-build -A usb_modeswitch.
  2. lsusb to find out your modem's vendor:product numbers
  3. sudo ./result/usr/sbin/usb_modeswitch -v <vendor> -p <product> -X
  4. /dec/ttyUSB[01] devices should appear. You should be able to minicom -D /dev/ttyUSB0 and type some AT command, say ATI.
  5. Update run.sh script by adding new line like below to the corresponding place
    try_to_deal_with "<your_device_id>" "<your_device_vendor>" && wait_for_chardev "/dev/ttyUSB0"
  6. Send us a PR with it.

Nix-shell

Author uses VIM as the main development IDE. The start procedure is as follows:

$ nix-shell
(nix-shell) $ vim .   # Edit sources enjoying code navigaiton
(nix-shell) $ ipython # Testing telethon bot, etc

Usage

Hardware notes

We use the following USB dongle:

*CLI> dongle show devices
ID           Group State      RSSI Mode Submode Provider Name  Model      Firmware          IMEI             IMSI             Number
dongle0      0     Free       9    0    0       Beeline        E173       11.126.85.00.209  ***************  ***************  Unknown

See also somewhat outdated list of supported devices

Known issues

Thirdparty issues

Administration hints

Ulaw audio operations:

Cutting last x seconds from an audio file

cutlast() {
  ffmpeg -i "$2" -ss 0 -to $(echo $(ffprobe -i "$2" -show_entries format=duration -v quiet -of csv="p=0") - "$1" | bc) -c copy "$3"
}

Clearing dongle SMS messages

#!/bin/bash -x
cmds()
{
cat <<EOF
dongle cmd dongle0 AT+CPMS=\"SM\",\"SM\",\"SM\"
dongle cmd dongle0 AT+CMGD=1,4
dongle cmd dongle0 AT+CPMS=\"ME\",\"ME\",\"ME\"
dongle cmd dongle0 AT+CMGD=1,4
exit
EOF
}

cmds | asterisk -r

Useful Asterisk commands

Show channels

core show channels

References

GSM modems

Asterisk

PJSIP

ARI

Telegram

Hardware

Fun