Raspberry Pi B+ with Asterisk and chan_dongle on Arch Linux

25/07/2014 - 03:12 by poney

It is possible to build a small unexpensive box for making and receiving GSM calls and SMS. It is possible to i When travelling outside the country, it is interesting to be reachable and be able to perform outgoing calls through VoIP without paying roaming fees. A Raspberry Pi can be transformed into a cheap VoIP box that routes VoIP/GSM calls in both sides (outgoing and incoming calls) as well as SMS. All we need is the following :

  • Raspberry Pi B+ (the older model have some throubles with USB voltage)
  • 3G USB Dongle
  • SIM card
  • Linux Distribution (Ubuntu is easy to use and install, but Arch Linux is faster and simpler. We do not use Gentoo Linux because of the heavy filesystem I/O activity when compiling)
  • Asterisk : compiling the last version is always an interesting experimentation
  • chan_dongle : the module that will allow us to use Asterisk with the 3G USB Dongle

In order to install Asterisk on a Raspberry Pi B+ with Arch Linux, the following guideline could be helpful. The configuration of Asterisk is widely described on the Internet.

Installing Arch Linux

Download the Arch Linux image from the official Raspberry Pi website

adeleda@tank ~ $ wget http://downloads.raspberrypi.org/arch_latest -O arch_latest.zip
adeleda@tank ~ $ unzip arch_latest.zip
Archive:  arch_latest.zip
  inflating: ArchLinuxARM-2014.06-rpi.img

Connect the MicroSD and check its associated device name. If it was connected with a USB adaptor :

adeleda@tank  $ dmesg | tail
[204209.660695] sd 12:0:0:0: [sdb] 15810560 512-byte logical blocks: (8.09 GB/7.53 GiB)
[204209.661591] sd 12:0:0:0: [sdb] Write Protect is off
[204209.661595] sd 12:0:0:0: [sdb] Mode Sense: 43 00 00 00
[204209.662449] sd 12:0:0:0: [sdb] No Caching mode page found
[204209.662452] sd 12:0:0:0: [sdb] Assuming drive cache: write through
[204209.667383] sd 12:0:0:0: [sdb] No Caching mode page found
[204209.667384] sd 12:0:0:0: [sdb] Assuming drive cache: write through
[204209.668383]  sdb: sdb1
[204209.672980] sd 12:0:0:0: [sdb] No Caching mode page found
[204209.672982] sd 12:0:0:0: [sdb] Assuming drive cache: write through
[204209.672984] sd 12:0:0:0: [sdb] Attached SCSI removable disk

In this case, the device path is /dev/sdb, so let's write the image on it :

adeleda@tank ~ $ dd if=ArchLinuxARM-2014.06-rpi.img of=/dev/sdb
adeleda@tank ~ $ sync

OK, now the Arch Linux is installed. The next step is to install Asterisk from the sources.

Installing minimal dependencies for Asterisk

After creating a new user and adding it to sudoers, we need to install some packages to be able to compile Asterisk and chan_dongle.

Initialize the keyering :

root@raspy ~ # pacman-key --init

Update the system : .. code-block:: bash

root@raspy ~ # pacman -Syu

Install useful dependencies :

root@raspy ~ # pacman -S make gcc iksemel libnewt libxml2 autoconf automake libtool

Install useful utilities for everydays life :

root@raspy ~ # pacman -S screen vim

Compile and install mawk :

adeleda@raspy $ wget http://invisible-island.net/datafiles/release/mawk.tar.gz
adeleda@raspy $ tar xzf mawk.tar.gz
adeleda@raspy $ cd mawk-1.3.4-20131226/
adeleda@raspy $ sed -ie "s|trap  *0|trap 'exit 0' 0|g" test/*
adeleda@raspy ~/mawk-1.3.4-20131226 $ sed -ie 's|log()|log(1.0)|g' configure
adeleda@raspy ~/mawk-1.3.4-20131226 $ ./configure
adeleda@raspy ~/mawk-1.3.4-20131226 $ make -j1
root@raspy mawk-1.3.4-20131226 # make install

Compiling and installing Asterisk for Raspberry Pi B+

The source code of the community version of Asterisk is available on the official download web page.

Download the source code :

adeleda@raspy ~ $ wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-11-current.tar.gz

Extract it to the /usr/src/ folder :

adeleda@raspy ~ $ tar xzf asterisk-11-current.tar.gz -C /usr/src/

Move to the /usr/src/$asterisk_name folder and compile it :

adeleda@raspy ~ $ cd /usr/src/asterisk-11.11.0
adeleda@raspy /usr/src/asterisk-11.11.0 $ ./configure

If you are upgrading from an old version of Asterisk, it is possible to reuse the old Asterisk menuselect.makeopts and menuselect.makedepsconfiguration files with the new version. Just copy them to the /usr/src/asterisk-11.11.0/ folder :

adeleda@raspy /usr/src/asterisk-11.11.0 $ cp /home/adeleda/config/asterisk/compile_config/menuselect.makeopts ./
adeleda@raspy /usr/src/asterisk-11.11.0 $ cp /home/adeleda/config/asterisk/compile_config/menuselect.makedeps ./

Configure Asterisk compilation by enabling or disabling all needed modules (the compilation time and size of the final result when compiled will be highly impacted) :

adeleda@raspy /usr/src/asterisk-11.11.0 $ make menuselect
adeleda@raspy /usr/src/asterisk-11.11.0 $ make
adeleda@raspy /usr/src/asterisk-11.11.0 $ sudo make install

Install sample configuration files :

adeleda@raspy /usr/src/asterisk-11.11.0 $ sudo make samples

The compilation and installation of Asterisk is finished. Now, let's prepare the environment for Asterisk. As this package is actually compiled and distributed by Arch, it is possible to obtain some useful configuration files and use them with our custom version.

Create the user associated with Asterisk :

adeleda@raspy ~ $ sudo useradd -G audio -s /bin/false -d /var/lib/asterisk -c 'Asterisk PBX daemon'

The Asterisk package for Arch Linux web page is available here. When reading the PKGBUILD file, it is possible to see how the package is installed and configured in the system by pacman. We just have to download the tarball extract it and take the useful files for us.

adeleda@raspy ~ $ mkdir /tmp/asterisk_tarball
adeleda@raspy ~ $ cd /tmp/asterisk_tarball
adeleda@raspy /tmp/asterisk_tarball $ wget https://aur.archlinux.org/packages/as/asterisk/asterisk.tar.gz
adeleda@raspy /tmp/asterisk_tarball $ tar xzf asterisk.tar.gz
adeleda@raspy /tmp/asterisk_tarball $ cd asterisk/
adeleda@raspy /tmp/asterisk_tarball/asterisk $ ls
asterisk.install  asterisk.logrotated  asterisk.service  asterisk.tmpfile  PKGBUILD

Fix the /var/run path in asterisk.conf file :

root@raspy /tmp/asterisk_tarball/asterisk # sed -i -e "s/\/var\/run/\/run/" /etc/asterisk/asterisk.conf

Fix the Asterisk log folder :

root@raspy /tmp/asterisk_tarball/asterisk # mkdir /var/log/asterisk
root@raspy /tmp/asterisk_tarball/asterisk # chown -R asterisk:asterisk /var/log/asterisk

Enable Asterisk automatic start with systemd : In order to allow Asterisk to be run automatically when booting the operating system, it is necessary to create a service file for systemd :

root@raspy /tmp/asterisk_tarball/asterisk # install -D -m 644 asterisk.service /usr/lib/systemd/system/asterisk.service

The logrotate file if you need to rotate your Asterisk logs :

root@raspy /tmp/asterisk_tarball/asterisk # install -D -m 644 asterisk.logrotated /etc/logrotate.d/asterisk

Temporary file :

root@raspy /tmp/asterisk_tarball/asterisk # install -D -m 644 asterisk.tmpfile /usr/lib/tmpfiles.d/asterisk.conf

Run Asterisk :

root@raspy /tmp/asterisk_tarball/asterisk # systemctl start
root@raspy /tmp/asterisk_tarball/asterisk # ps aux | grep asterisk
asterisk   475  1.5  4.0  48304 17952 ?        Ssl  06:26   1:05       /usr/bin/asterisk -f -C /etc/asterisk/asterisk.conf

It seems to be running, let's conntect with the CLI :

root@raspy /tmp/asterisk_tarball/asterisk # asterisk -rvvvv
Asterisk 11.11.0, Copyright (C) 1999 - 2013 Digium, Inc. and others.
Created by Mark Spencer <markster@digium.com>
Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for
This is free software, with components licensed under the GNU General Public
License version 2 and other licenses; you are welcome to redistribute it under
certain conditions. Type 'core show license' for details.
Connected to Asterisk 11.11.0 currently running on raspy (pid = 475)

Compiling and installing chan_dongle for VoIP GSM Calls and SMS support

In order to perform GSM calls by using the Raspberry Pi B+ device, we need to use the chan_dongle.so module for Asterisk. The official documentation is well detailed.

The original version does not compile (check here for more details).

with new versions of Asterisk. When compiling, the user will get the following error messages :

root@raspy /tmp/asterisk-chan-dongle # make
gcc -g -O2 -O6 -I.  -D_GNU_SOURCE -I/usr/src/asterisk-11.11.0/include -I/usr/include -DHAVE_CONFIG_H  -fvisibility=hidden -fPIC -Wall -Wextra -MD -MT app.o -MF .app.o.d -MP  -o app.o -c app.c
gcc -g -O2 -O6 -I.  -D_GNU_SOURCE -I/usr/src/asterisk-11.11.0/include -I/usr/include -DHAVE_CONFIG_H  -fvisibility=hidden -fPIC -Wall -Wextra -MD -MT at_command.o -MF .at_command.o.d -MP  -o at_command.o -c at_command.c
gcc -g -O2 -O6 -I.  -D_GNU_SOURCE -I/usr/src/asterisk-11.11.0/include -I/usr/include -DHAVE_CONFIG_H  -fvisibility=hidden -fPIC -Wall -Wextra -MD -MT at_parse.o -MF .at_parse.o.d -MP  -o at_parse.o -c at_parse.c
gcc -g -O2 -O6 -I.  -D_GNU_SOURCE -I/usr/src/asterisk-11.11.0/include -I/usr/include -DHAVE_CONFIG_H  -fvisibility=hidden -fPIC -Wall -Wextra -MD -MT at_queue.o -MF .at_queue.o.d -MP  -o at_queue.o -c at_queue.c
gcc -g -O2 -O6 -I.  -D_GNU_SOURCE -I/usr/src/asterisk-11.11.0/include -I/usr/include -DHAVE_CONFIG_H  -fvisibility=hidden -fPIC -Wall -Wextra -MD -MT at_read.o -MF .at_read.o.d -MP  -o at_read.o -c at_read.c
gcc -g -O2 -O6 -I.  -D_GNU_SOURCE -I/usr/src/asterisk-11.11.0/include -I/usr/include -DHAVE_CONFIG_H  -fvisibility=hidden -fPIC -Wall -Wextra -MD -MT at_response.o -MF .at_response.o.d -MP  -o at_response.o -c at_response.c
at_response.c: In function 'start_pbx':
at_response.c:854:16: error: dereferencing pointer to incomplete type
  cpvt = channel->tech_pvt;
at_response.c:861:10: error: dereferencing pointer to incomplete type
   channel->tech_pvt = NULL;
at_response.c: In function 'at_response_clcc':
at_response.c:923:23: error: dereferencing pointer to incomplete type
          cpvt->channel->rings += pvt->rings;
Makefile:65: recipe for target 'at_response.o' failed
make: *** [at_response.o] Error 1

Or an other error message related to version.h that has been renamed to ast_version.h in new versions of Asterisk :

root@raspy /tmp/asterisk-chan-dongle # make

gcc -g -O2 -O6 -I.  -D_GNU_SOURCE -I/usr/include -I/usr/include -DHAVE_CONFIG_H  -fvisibility=hidden -fPIC -Wall -Wextra -MD -MT app.o -MF .app.o.d -MP  -o app.o -c app.c
In file included from app.c:21:0:
/usr/include/asterisk/version.h:1:2: error: #error "Do not include 'asterisk/version.h'; use 'asterisk/ast_version.h' instead."
app.c: In function 'app_register':
app.c:162:3: warning: passing argument 2 of 'ast_register_application2' from incompatible pointer type [enabled by default]
In file included from app.c:20:0:
/usr/include/asterisk/module.h:458:5: note: expected 'int (*)(struct ast_channel *, const char *)' but argument is of type 'int (*)(struct ast_channel *, void *)'
make: *** [app.o] Error 1

Hopefully, Jakuba Stasiak has released a patch in order to fix that and made it public through his github repository.

Chan_dongle could be obtained like the following :

adeleda@raspy /tmp $ wget https://github.com/jstasiak/asterisk-chan-dongle/archive/asterisk11.zip
adeleda@raspy /tmp $ cd asterisk-chan-dongle-asterisk11
adeleda@raspy /tmp/asterisk-chan-dongle-asterisk11 $ aclocal && autoconf && automake -a
adeleda@raspy /tmp/asterisk-chan-dongle-asterisk11 $ ./configure --disable-debug  --disable-apps --disable-manager --with-asterisk=/usr/src/asterisk-11.11.0/include
adeleda@raspy /tmp/asterisk-chan-dongle-asterisk11 $ make
adeleda@raspy /tmp/asterisk-chan-dongle-asterisk11 $ sudo make install

After finishing the installation, the dongle-read-only/etc/dongle.conf configuration file should be copied and edited with personal configuration.

Then, to load and unload the module, it is possible to do it through the

Asterisk CLI :
raspy*CLI> module load chan_dongle.so
raspy*CLI> module unload chan_dongle.so
raspy*CLI> module reload chan_dongle.so

That's it, now we have a fully working Asterisk 11.11.0 LTS on a Raspberry Pi B+ with GSM calls/SMS capability enabled. The next step should be a standard configuration of users, peers and dialplan :-)



comments powered by Disqus