segunda-feira, 8 de junho de 2015

GSM bridge between two raspbx hosts

Disclaimer: I know there's a lot of options on the market for free long distance calls like Skype, Viber, WhatsApp and so on. The goal of this personal project was all along to come up with a proof of concept that actually works, study something different other than my actual job and, why not, have some fun :) BUT, yes, there's one single use case that this setup will be useful: You're in the middle of nowhere, no internet connection and you need to call someone in the other side of the planet but you don't want to pay a long distance call: BINGO!

Disclaimer2: For this tutorial you'll have to be prepared for Asterisk terminologies like trunks and channels. If you're not familiar with this vocabulary, please take a quick look at this documentation.

Description of the environment: The server1 is placed on the Brazilian side. It's a Raspberry Pi model B that runs an asterisk-modded-for-raspberry-pi distro called raspbx. Attached to it there's an USB GSM dongle with a SIM card for the local Brazilian operator. In the same network of my server1 there's a Cisco PAP2t Internet Phone Adapter and, attached to it, a regular land line telephone. The same setup is duplicated on the German side, except that the Raspberry Pi is model B+.

5VpNk+MmEP01rkouqZFky7PHzGZ2ctmqVPmwyRFLjESNJFQIf82v30ZqJNBHxh47yFXxxaIFNLzmPRrsRfA1P74IUqbfeUyzhf8QHxfBHwvf95bBF/hSllNjeVyvGkMiWIyVOsOGvVM0PqB1x2JaWRUl55lkpW2MeFHQSFo2IgQ/2NVeeWZ7LUmiPXaGTUSyofUHi2WKs/DDzv4nZUmqPXshTnhLordE8F2B/hZ+8Fp/mtc50X3hRI8PTXGJ7U9YXi+1p8Ia0TvnuWUQtGrRwz5fGQ4Ly1suYioak7ZlrHgzMQqeIZyCc2ipnvLjV5qpkOpoNc2+Tbxt4RK0sHxPNgiaFpU8achpDBHAIhcy5QkvSPbcWZ9qWKnqAUB6SmWewaMHj/TI5N/G8z+qym8rKFWSCPm7WhCq1z2BFo3tG4PxYIsi7tUAi/E+2ol97VUVmmGrsU7OHE0V34kIa+FswXFCsVbNBwTnhfKcSnGqw5kRyfZ27wQjlbT1OmjhAdEdRxpdw8R22KkgVbmlonZXsoUfZjCkp5jt4TFRjyOmXyoKIAjvV/0O3I606Ft7IT6kTNJNSWpYDqAcdhhxpFRIqoVkAt0hbthAywzKTvCA5YNBYmRAavBX264BGglrAL2rtmB42XxXQsiLBGAYQTYH8cyngZ0bQt8bQrgegfDLDSDUri5SBYueM0nE51QBZ2uqQr2/OFAFdG2qAt9J2CTcr7c26pqzK3ecxe3cgIEVgEIB4XAPRLB6tIEIh0B4ev82kQhvQT29Pm+zIX/Al3M49zlOrYecqlF1wCkPdwCn6gVYiJN6pVrUhXtQtpEouMp30LVB6ZKU9VGBVREfrnLnG+rSYU6CgmKAIWlGy5QXdA4k9JQQidWZqYW2XYOEXhf/ITlrBt5JauGNMNCrj5guhHDIwSvOHP69nzlCvT5nOHR4Q4ZfcOqYhtY9iD1tcHns8PHuxak2tBt3IwJ3snE3GjHPmUT7vodDySqY71CiU/y7T8XNhaMM/YWj754uSJX0/a/OlR4R9qvwRI38n2z9Y5FwtvVr5xPpN76ddXt2mX+P3L62Cfg8WNiy5jIDD7APA4utIO8sY/ATh05Y4OblwMXbkLBZBj/9KGI60H57veh18NEtzC3WS4CDNTAaQtGpRpSRqmKRjYClPF2Nz16vDGEygNBQmTho29nagh7+4nD31kXB620F7baou2g0D1uZPzV90JHXvy5rhHLQUR2rdtrnhW+ofgkVuVrfIcnVSi22lfqqF/sgKW8X/1k5uUtKhD1OaBlxwomhht4fJybyF28kbfw3zpxPj76n9rx0KT0mh3w1PaDY/aDcVO/+LBA8/wQ=
What it does: This is the list of features that this project performs right now:
  1. Someone in Brazil calls my Brazilian number, the dongle1 answers the call, redirects to an IAX2 trunk directly to server2. The server2 takes this incoming call and uses the dongle2 to place a new call to my german cellphone. The other way is still pinned to a single phone -- like, call my dongle2 to reach a single number in Brazilian side -- but improvements are coming.
  2. Someone uses phone1 on Brazilian side to call extension phone2 on German side (and vice-versa) like a regular landline phone.
  3. Someone uses phone1 or phone2 to reach my german cellphone.

Ok, so LET'S DO IT!!

I'm gonna skip the PAP2t configuration because I think it's too much. This is only needed if you want to use land line telephones, it's not required for the GSM bridge. If anyone is interested in doing that, please leave a comment and I'll write a new post only for that configuration.

Important note: All the configuration explained from now on is identical on both servers.
  1. Install raspbx on your RPi's SD card. Also take a time to read through the raspbx documentation, which is very useful
  2. Use the script install-dongle (built-in inside raspbx) to install your dongle on your RPi. Important notes regarding problems I got:
    1. There's a list of GSM dongles that are tested with voice, SMS and USSD. I bought the Huawei E160;
    2. I had to unlock the voice feature with DC-Unlocker for Windows;
    3. Plug your dongle and only after that plug your RPi power. If you try to plug your dongle while the RPi turned on, you may experience your RPi to reboot due to power consumption failures;
    4. If your RPi starts rebooting it's because it can't handle the gsm dongle power consumption, in this case, use a proper powered USB hub. I bought the D-Link Dub-H7
    5. Find out the IMEI and the IMSI code of your dongle and set the correct values under /etc/asterisk/dongle.conf because /dev/ttyUSB1 or /dev/ttyUSB2 may change;
  3. Before you start configuring raspbx itself, it's important that you have both sides with network configured properly with static IPs and (in my case) DynDNS. I didn't want to open too many ports on the routers on both sides so I just setup an OpenVPN vpn; 
  4. Setup a new custom trunk for your dongle under Connectivity  Trunks  Add Custom Trunk and set these values:

    Trunk Name: to-my-cellphone
    Outbound CallerID: Your SIM card number with country code (with a plus sign at the beginning)
    Custom Dial String: dongle/dongle0/$OUTNUM$

    Note: This is the trunk that will actually place the inbound calls to your personal telephone.

  5. Setup a new IAX2 trunk under Connectivity  Trunks  Add IAX2 Trunk and set these values:

    General Settings:
    Trunk Name: Something that would remember the incoming connection from the other server
    Outbound CallerID: Your personal phone number

    Outgoing Settings:
    Trunk Name: (same as above, I also used the same name and it's ok)

    host=static IP under vpn of the remote server
    username=any username
    secret=any password
    type=friend
    trunk=yes
    qualify=yes
    qualifyfreqok=25000
    tos=0x18

    Note: The IAX2 trunk is responsible for redirecting calls from this server to the other.

  6. Setup a new inbound route under Connectivity  Inbound Routes → Add Incoming Route:

    DID Number: Your SIM card number with country code (with a plus sign at the beginning)
    CallerID Number: Your personal cell phone number
    Set Destinations: Trunk and your recently created IAX2 trunk.

    Note: This is the route responsible to redirect an inbound call on your local dongle over the IAX2 trunk and then to your remote server.
If you noticed any omitted fields in the above configurations, it's because I didn't fill with anything or it's not important. I also did some extra work on Claning the SIM card memory for SMS every once in a while, I set a static IP for the OpenVPN so each server can always see each other, an additional service to check my credit balance every month and so and so forth. If you also want details like that please let me know in the comments :-) 

That's it! You did it! If you need more help setting up anything else please let me know in the comments of this post. Good luck and have fun! :-)