Getting started with Asterisk and Sipdroid

Okay, now this is a diversion from my usual blog posts. Today: Telephony! Over the internet!

Shortly after I got my handy little HTC Desire running Android, I ran into Sipdroid on the Android Market, which is basically a really nicely integrated softphone. And knowing how often I’m out of signal (living in rural England does have some drawbacks) I figured that having a phone I could use with just a wifi link would be handy. What followed was a month of fiddling, messing around, tweaking, failed attempts, and confusion. And with all such attempts, now I’ve got it working I thought some other poor souls might be spared the suffering with a quick how-to.

So, for starters, here’s what we need to achieve this goal. We need our softphone – Sipdroid in this case. We want to be able to handle calls from and to the public phone network (or PSTN – public switching telephony network). In order to achieve that we need a gateway. This can be done by getting some hardware on a phone line and such, but for my solution I couldn’t achieve that due to cost constraints. Instead we’ll use a trunk provider who will provide a SIP gateway to the PSTN. I went with a company called Hostcomm who do a fairly decent job for around £4 a month. We also need a way of routing calls between the SIP trunk and all the various softphones we might want to have on this network. To do this we use a private branch exchange – we’re going to use Asterisk for this.

First off, we install Asterisk from source. This is easy – ./configure, make, make install. Asterisk will install a truly scary number of config files to /etc/asterisk/, which we’ll now deal with.

cd /etc/asterisk
mkdir examples
mv * examples

Now we’ve gotten rid of what we don’t need, let’s grab what we do need.

cp examples/asterisk.asdi examples/asterisk.conf examples/asdi.conf examples/cdr.conf examples/codecs.conf examples/modules.conf examples/voicemail.conf .

Most of those files are good ‘as-is’, but you will want to set up some form of call data recording system for debugging – this is in cdr.conf. You might also want to log to a database – I use PostgreSQL – which is detailed in the cdr_mydb.conf files in the examples folder. I’m not going to cover that here since it’s simple enough, and I want to press on. Let’s set up our SIP trunk and softphones.

nano -w sip.conf
# We must explicitly tell Asterisk to register itself with our SIP trunk
register =>
bindaddr= # Bind to all hosts on the machine
context=default # Context is where things end up in the dial plan. More on that later.

host=dynamic # Allow from any IP address
qualify=yes # Require regular check-ins from the peer
nat=yes # Allow NATted connections - does some TCP/UDP magic
reinvite=nonat # Don't try and make direct connections between peers when NATted


context=incoming # Note the context we're using

So far, so good. We can add more sections for other phones and devices in the list above if we need to. Let’s sort out voicemail now. We want to be able to dial up for our voicemail, and we want to get copies of messages left in our email inbox. Copy the voicemail.conf file out and edit it – you’ll want to set serveremail, possibly the fromstring variable, and any others that look like you should change them frankly. It’s well documented. All that remains is to crucially define a mailbox for the SIP trunk.

0203123456 => 1234,My Mailbox,

Now we’ve defined a mailbox we have to route to it- so let’s hit the dialplan! This is defined by extensions.conf. Create the file and open it up.

exten => jamesmobile,1,Dial(SIP/jamesmobile)
exten => jamesdesktop,n,Dial(SIP/jamesdesktop)
include => internal
include => remote

# This is how we get to our voicemail. Dial 123 from any SIP connected phone.
exten => 123,1,Answer()
exten => 123,2,VoiceMailMain(0203123456)
exten => 123,3,Hangup()
# If we're trying to call any extension that starts with the number 2 and has 4 digits only, assume internal.
exten => _2XXX,1,NoOp()
exten => _2XXX,n,Dial(SIP/${EXTEN},30)
exten => _2XXX,n,Playback(the-party-you-are-calling&is-curntly-unavail)
exten => _2XXX,n,Hangup()

# Anything that isn't internal we send to the PSTN.
exten => _X!,1,NoOp()
exten => _X!,n,Dial(SIP/siptrunk/${EXTEN})
exten => _X!,n,Hangup()

# This is where calls coming in from the PSTN are directed - see context setting in sip.conf
exten => _X.,1,NoOp()
# Try and call the desktop and mobile. If this fails, direct to voicemail.
exten => _X.,n,Dial(SIP/jamesdesktop)
exten => _X.,n,Dial(SIP/jamesmobile)
exten => _X.,n,VoiceMail(0203123456,u)
exten => _X.,n,Hangup()

Now, let’s start Asterisk.

asterisk -cvvvv

This should spew out lots of debug info. If all looks to be well, you can now try and set up your Sipdroid client and any other softphones. You’ll have to open port 5060 (TCP/UDP) on the firewall of the box hosting Asterisk. If everything works, congratulations! You’ve got a working system! Now go forth and tinker. If not, from the Asterisk console run ‘sip set debug on’ and you can get even more information to help debug things. Good luck!

One thought on “Getting started with Asterisk and Sipdroid”

Comments are closed.