Use a Mobile Phone as a GSM Gateway in Asterisk (Asterisk + chan_mobile)

Asterisk + chan_mobileMy task is to use mobile phone as GSM Gateway to my Asterisk with a help of chan_mobile so in case I’m unavailable on mobile people can leave a message in my voicemail box. SMS text messages arrive as emails, my replies to which are sent by SMS.
So I used my old Sony Ericsson k530i phone in pair with Asterisk by connecting it to ID 1131:1001 Integrated System Solution Corp. KY-BT100 Bluetooth Adapter USB dongle.

Asterisk support for bluetooth connections to mobile phones and headsets is accomplished through chan_mobile. Not all phones are supported, so it’s worth taking a look at voip-info.org’s page which lists the confirmed compatible dongles and phones. I am getting good results with a KY-BT100 dongle and Sony Ericsson k530i phone, Samsung Galaxy S4 phone.

Here is how to connect your mobile phone to Asterisk with chan_mobile

Connecting mobile phone to Asterisk via bluetooth by chan_mobile

chan_mobile is an addon, so it needs to be enabled before Asterisk is compiled. On Debian, it’s pretty simple, just add a few packages:

then, go to your Asterisk source directory and use make menuselect to enable chan_mobile. It’s in Add-ons -> chan_mobile:

Whilst it compiled and installed OK, I had to make a modification to the chan_mobile source before it would recognize my phone:

Build Asterisk and (re)install:

In order to use a bluetooth-connected phone as a GSM gateway, it’s necessary to pair the phone with the Asterisk server. In Debian, this can be accomplished painlessly through the CLI. First, make your phone discoverable and then scan for it:

Make a note of the MAC address. In order to pair, a helper is required to handle the PIN. Run the helper in the background and begin the pairing process:

Once the pairing has succeeded, make sure your phone is configured to automatically accept connections for this paring in future. You can verify that the paring is working at any time by running:

Now, Asterisk needs to be configured to use the paired phone. We need to know which rfcomm channel offers the voice service. The easiest way is to use chan_mobile:

Don’t worry about any errors loading the module, it’ll do for now:

In this case it is rfcomm channel 2. In addition, we need to know the MAC address of the bluetooth dongle installed in the Asterisk server. Exit the Asterisk CLI and use hcitool:

At last we have all the information needed. Edit or create the chan_mobile configuration file:

You will need something in the dialplan to handle this, at minimum something like:

When the mobile rings, you should get a call on SIP extension 100. Dialing *12 will cause the phone to dial 5433, which in my case gives me life:)’s customer services. I’m sure you get the idea.

Sending SMS with Asterisk and chan_mobile

Trickier, but there is a solution. None of the phones I had spare were supported by chan_mobile’s SMS capabilities. According to the chan_mobile wiki page, only three phones are known to support SMS: the Nokia models E51, 6021 and 6230i..

Once the phone is paired in the normal way, it will send any incoming SMS messages to Asterisk over the bluetooth connection. Asterisk looks for an ‘sms’ extension in the context you specified in chan_mobile.conf. I suggest something like this in your dialplan:

At first, incoming messages were all arriving with a blank ${SMSSRC}, the easy solution was to apply a patch and re-compile:

In the latest version of chan_mobile this has to be fixed already!

Now, incoming messages are delivered to me as emails claiming to be from +MOBILENUMBER@itgala.xyz. Obviously, this requires the Asterisk system to have a working MTA, the setup of which I won’t cover here. If you don’t have an MTA at present, take a look at postfix.

Outgoing SMS messages are more work because it’s necessary to parse the contents of the email message, the format of which will be a little less predicatable than an SMS. I elected to use python to do this because it already has a library to do this.

Copy the above script to /usr/sbin/email2sms.py and make executable:

The script uses the Asterisk Manager Interface, so it will need an AMI user. Append this to manager.conf:

and also make sure it is enabled in the general section:

You’ll note that I’m using the context ‘mobiles’. You’ll need to make sure that the extensions you’ll be using exist in this context in extensions.conf:

Secondly, there is a dummy extension which the ‘call’ needs to connect to. A NoOp isn’t quite sufficient, I could only get it to work if the extension answered and then did something, in this case answer and wait 10 seconds:

Reload Asterisk to pick up the changes.

So, calling email2sms.py with the argument ‘itg’ uses the k530i mobile.

You need to make sure that email for the domain you have chosen – in my case sms.itgala.xyz – is routed to the Asterisk box. This will normally be accomplished by creating an MX record or creating a transport for the domain on your mail server. Again, I’m not going to cover that part here, but I will cover how to pipe the incoming messages into the python script.

Assuming that you are using postfix, you’ll need a new transport for each mobile you want to use. In my case:

postfix needs to know that it must use these transports for SMS domains:

If postfix doesn’t already have a transport_maps setting, create one. Obviously this could break any existing postfix setup you might have, but if so I’m expecting you to know what you’re doing:

Restart postfix and that should be all that’s necessary.

You need to satisfy yourself that you are not allowing the entire world to relay through your SMS gateway! Understand and make use of postfix’s security features! Don’t wait until you’ve racked up a collosal SMS bill!

If things aren’t quite working, start by checking your mail log:

You can do a packet trace to see what’s happening on the Asterisk Manager Interface:

Try talking to the AMI directly:

Watch out for whitespace in the AMI – exten ‘itg’ != ‘itg ‘.

Good luck.

Be sociable 🙂 Share!

Comments/Коментарі/Комментарии