
Hello, Habrahabr! In this article I want to tell how you can solve the issue of automatic switching of the mode of receiving calls in the “Multifone” (gsm-to-gsm / gsm-to-sip) when registering a smartphone on the Asterisk server. Below we will explain why I needed this, what solutions were considered, and how it was finally implemented. The following example was used on the basis of a home server running Debian Lenny along with Asterisk 1.6, but will most likely work on other common Linux platforms.
Briefly about the background of the issue
Being a MegaFon subscriber, recently began using their VoIP-service. One of the features of the service is that in addition to making outgoing calls, you can receive incoming calls to your mobile number through a SIP client. I wanted to use this opportunity by setting up the connection of my smartphone to the home Asterisk server via SIP. I will not dwell on the registration procedure itself, the connection to the service and the setting up of a SIP account - it is already written enough about this. I wanted to do the following: when the smartphone is connected to the server, all incoming cellular calls begin to arrive via SIP; when disconnected from the server, the usual mode of receiving calls by the phone (gsm-to-gsm) should be restored. The solution should be as automated as possible and require no more than one movement from me - pressing the button to connect to the server on the smartphone's desktop.
What is needed to complete the task
To implement our plans, it is necessary to fulfill at least two conditions:
1. "Multiphone" should provide the ability to switch the mode of receiving cellular calls: reception on the cellular, reception in the sip-client. The service has such an opportunity, and it is implemented by sending a specific https request to the server (for more information about the request format, you can read the link at the end of the article). The request can be made from the console (for example, using curl or wget), which makes it possible to use it in scripts.
2. The server must “know” that a certain peer (smartphone) has registered with Asterisk, and when connecting / disconnecting, make an https request. Consider this question in more detail below.
Looking for a solution
It is necessary to determine the moment when the smartphone will register on the server. Having studied Asterisk's reaction to the client connection and the available means of informing about the event, I determined for myself 2 main solutions: learn about the event through AMI (Asterisk manager API) or process the list of connected clients that Asterisk displays with the “sip” command via an external script. show peers. I was closer to the second option, so I decided to stay on it. (
By the way, now I thought that there is a third way - to parse the output of the file / var / log / asterisk / full for the presence of the lines "Registered SIP 'peername'" and "Unregistered SIP 'peername'." Perhaps that would be even simpler, but I went the other way. If you know simpler options, tell me, I will listen to them with pleasure ).
')
Let's see what the server writes:
asterisk*CLI> sip show peers Name/username Host Dyn Nat ACL Port Status peer1/peer1 192.168.XXX.XXX D 5060 Unmonitored peer2/peer2 (Unspecified) D 5060 Unmonitored peer3/peer3 192.168.XXX.XXX D 5060 Unmonitored multifon/7922XXXXXXX 193.201.229.35 5060 OK (34 ms) sipnet/sipnet 212.53.40.40 5060 Unmonitored mywifipeer/mywifipeer (Unspecified) D 0 Unmonitored 6 sip peers [Monitored: 1 online, 0 offline Unmonitored: 4 online, 1 offline]
In the “Host” column, we see an inscription (Unspecified) for disabled clients, and an IP address for registered clients. This data is sufficient to determine the registration status of the client. Thus, the task has been reduced to writing a script that will poll the status of the required sip account on the server (smartphone mywifipeer), and if its registration status has changed, send an https request. To control the changes, information about the event will be duplicated in jabber. Next, the script is placed in cron and runs every minute.
Since the script will work on demand, rather than as a daemon, to determine that the connection status
has changed , you must have information about how it was at the time of the previous launch. If this is not done, our script will DOS'it a megaphone server, sending it a request to install a GSM mode every time it checks for a bad thing. Therefore, on the first call, the script will put the current status information ($ peer_state_now) into an external file ($ peer_state_last_file), and all subsequent ones will read the information from this file ($ peer_state_last) and check with the current status.
Curl was used to send an http request, and sendxmpp ($ xmpp_bin and $ xmpp_jid) were used to notify the gabber. Before using the script, you must set the login-password corresponding to the Multifon-SIP registration record ($ multifon_login and $ multifon_password).
Asterisk_peer_check script
It remains to add the launch of the script in / etc / crontab
Done! Now you can check the performance of the entire system. Connect your phone to the server and in a minute we get a confirmation message in jabber. We make a test call to the phone and make sure that everything works as it was intended.
References:
Multiphone: http-request format, server responses and code handling