Getting started with Asterisk

About

This page contains instructions for an elementary setup of Asterisk in order to work with the Open USB FXS dahdi driver (oufxs). The setup of the driver is described elsewhere in this blog. For this Asterisk setup, it is assumed that you have successfully compiled and loaded the driver, and that you have configured a single Open USB FXS device as dahdi channel #1 in /etc/dahdi/system.conf (please remember that you must have executed the dahdi_cfg utility before Asterisk can use your device).

Working environment

The setup instructions in the next section are for a Debian “lenny” system, and there is a note also for Ubuntu (a Debian derivative). For Red Hat-flavored systems, one should go the Red Hat way, i.e., use yum  instead of apt-get, and so on. Other than that, the rest of the setup should be more or less the same.

Setup

I strongly suggest using Asterisk version 1.6. This page is about Asterisk 1.6 only, and I will not be providing any support for Asterisk 1.4. If you run on a Debian system, you may need to do some tweaking. Ubuntu, on the other hand, comes with Asterisk 1.6 already in the standard package repositories, so you should skip the following sources.list setup and go directly to the apt-get step.

If Asterisk 1.4 is already present on your system when you start, you are advised to remove it first by typing

apt-get purge asterisk

(Note: if you have been using Asterisk and already have changed any of its configuration files, it’s a good idea to keep the files somewhere handy before removing them).

Add the following two lines in /etc/apt/sources.list (the second line is only required if you plan to download source packages, to e.g. rebuild Asterisk from its source).

deb http://backports.debian.org/debian-backports lenny-backports main contrib
deb-src http://backports.debian.org/debian-backports lenny-backports main contrib

(note that the above will not affect the way your system loads standard packages, unless you specify the -t lenny-backports in the apt-get command line). Then, run

apt-get update

When this finishes, you are ready to install Astetrisk 1.6. Issue the following command:

apt-get -t lenny-backports install asterisk

and answer ‘y’ to the prompt that follows (and to any other prompts apt-get produces). Apt-get will now produce a long list of messages as it downloads and installs the new packages.

Configuration

In order to configure a simple Asterisk setup, you will need to adjust three  files: /etc/asterisk/modules.conf, /etc/asterisk/users.conf and /etc/asterisk/extensions.conf. In the first file you tell Asterisk which modules to load; in the second file, you must add a “user” for your device and in the third one you will be adding a small “dial-plan”, i.e., instructions on how to place calls. Let’s start looking at those files, one-at-a-time.

Edit (or create) the file /etc/asterisk/modules.conf, then copy-paste the following lines:

[modules]
autoload=yes
noload => pbx_gtkconsole.so
noload => pbx_kdeconsole.so
noload => app_intercom.so
noload => chan_modem.so
noload => chan_modem_aopen.so
noload => chan_modem_bestdata.so
noload => chan_modem_i4l.so
noload => chan_capi.so
load => res_musiconhold.so
noload => chan_alsa.so
noload => cdr_sqlite.so
noload => app_directory_odbc.so
noload => app_voicemail_odbc.so
noload => app_voicemail_imap.so
noload => res_config_odbc.so
noload => res_config_pgsql.so
[global]

With these, you are instructing Asterisk to load each and every module it has built-in except the modules listed with a “noload” directive. These modules are exempted for good reason (for example, chan_alsa clashes with chan_oss; they both serve for placing calls using the console and the built-in Linux sound system, ALSA or OSS), however I am not going into discussing the details here. After you get Asterisk working with your Open USB FXS device, you may experiment freely by moving modules in and out as it suits your needs. However, if you happen to run a memory-constrained system, it is best to limit the number of  loaded modules. Here is a  minimal setting for /etc/asterisk/modules.conf (should work, but I haven’t tested it):

[modules]
load => res_musiconhold.so
load => chan_iax2.so
load => codec_alaw.so
load => codec_ulaw.so
load => codec_adpcm.so
load => codec_gsm.so
load => codec_dahdi.so
load => bridge_simple.so
load => bridge_softmix.so
load => pbx_config.so
load => app_verbose.so
load => chan_dahdi.so
load => chan_local.so
load => chan_sip.so
load => chan_oss.so
load => app_playback.so
load => app_dial.so
load => format_sln.so
load => format_gsm.so
load => app_macro.so
load => app_voicemail.so
load => app_echo.so
[global]

After setting up the above file, you should be able to run Asterisk in console mode:

asterisk -vvvvvvvvvvc

and check for errors (if Asterisk is already running, make sure to stop it first by issuing “/etc/init.d/asterisk stop” or else you won’t be able to start it in console mode). You can also try a “console” call, by entering

console dial 600

and see what happens: Asterisk is creating a call between your console and the local “echo” application (enter “console hangup” to terminate the call). Note, you can safely ignore Asterisk’s complaints about /dev/dsp not being found. Use “core stop now” to exit from Asterisk’s console mode.

Next step is to configure an extension for your local device. Edit /etc/asterisk/users.conf and add the following lines at the end:

[6000]
fullname = Open USB FXS board 1
dahdichan = 1

This tells Asterisk that your device is dahdi channel #1 and that it should be given the local extension number 6000.

[Note: there is an alternative configuration method that fiddles with the file /etc/asterisk/chan_dahdi.conf. However I do not recommed this method, because, at least on some versions, if your device is configured on that file but it is not plugged in at the time it starts, Asterisk will crash].

After adding the above lines to users.conf, start again asterisk in console mode, then at the *CLI> prompt, enter:

dialplan show default

Asterisk should respond with something like this:

[ Context ‘default’ created by ‘pbx_config’ ]
  ‘6000’ =>         hint: SIP/6000&IAX2/6000&DAHDI/1              [pbx_config]
                    1. Macro(stdexten,6000,${HINT})               [pbx_config]
  Include =>        ‘demo’                                        [pbx_config]

-= 1 extension (2 priorities) in 1 context. =-

What this jargon translates to is that Asterisk will try to dial 6000 using the SIP, the IAX2 and the DAHDI channel methods; to this, it will invoke the macro “stdexten”. Unfortunately, this macro is not defined in the distribution-provided configuration files, so you must add it yourself. This is the last step.

Before trying anything else (and with your Open USB FXS device plugged, initialized, and dahdi_cfg’ed), you can pick up the phone. You should hear a dialtone. However, because the dial plan is not ready yet, if you try to dial any number, you should get back a busy tone (and some messages on the console, saying that Asterisk does not know how to route your call).

If, at this stage, dialing does not work, you can try the following two workarounds.

  1. Edit the file /etc/asterisk/chan_dahdi.conf and uncomment the “relaxdtmf” line (then restart Asterisk).
  2. Edit /etc/dahdi/system.conf and add an echo canceller for your device, e.g., add the following at the end of the file:
            echocanceller=oslec,1
    , of course then re-run dahdi_cfg and restart Asterisk.

We are now ready to fix the dial plan. Stop Asterisk, then edit the file /etc/asterisk/extensions.conf and add the following at the end of the file:

[macro-stdexten]
exten => s,1,Dial(${ARG2},20)              ; Ring the interface, 20 seconds max
exten => s,2,Goto(s-${DIALSTATUS},1)       ; Jump based on status (NOANSWER,BUSY,CHANUNAVAIL,CONGESTION,ANSWER)
exten => s-NOANSWER,1,Voicemail(u${ARG1})  ; If unavailable, send to voicemail w/ unavail announce
exten => s-NOANSWER,2,Goto(default,s,1)    ; If they press #, return to start
exten => s-BUSY,1,Voicemail(b${ARG1})      ; If busy, send to voicemail w/ busy announce
exten => s-BUSY,2,Goto(default,s,1)        ; If they press #, return to start
exten => _s-.,1,Goto(s-NOANSWER,1)         ; Treat anything else as no answer
exten => a,1,VoicemailMain(${ARG1})        ; If they press *, send the user into VoicemailMain

The comments on the above directives make them pretty self-explanatory, so it’s easy to see what the “standard” dialplan does: it attempts to dial its argument and handles various non-OK return status conditions.

After editing the dial plan, restart Asterisk and try to dial 600. This time, you should first listen to a message and then be directed to the local “echo” application. You are done!

2 Responses to “Getting started with Asterisk”

  1. micha Says:

    Hi.
    do you know where I can find documentation of DAHDI API?
    10x

  2. Angelos Varvitsiotis Says:

    Hi Micha,

    I am not sure if you can find something like that. I have written my driver by just looking at the source of the other drivers. Plus, I have been able to track API changes by diff’ing the drivers between versions. Why would you be interested in the API? I guess that you would like to write your own Dahdi device driver?

    Cheers,

    Angelos

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: