[asterisk-dev] Using a timer in chan_sip (classic one) ?

Mark Michelson mmichelson at digium.com
Thu Sep 19 11:57:01 CDT 2013


On 09/19/2013 07:41 AM, Pavel Troller wrote:
> Hi!
>    I'm writing a custom patch to chan_sip.c. Its purpose is to receive some
> events through INFO messages (in principle, they are kind of a hookflash
> event), count them, and when they stop arriving, send out a DTMF digit
> towards the PBX (yes, it's intended as a pulse dialing emulation for
> cheap gateways like SPA 2102, which don't implement real pulse dialing
> reception).
>    I've done most of the trivial work - wrote an extension to the
> handle_request_info() function to receive these events, increment the
> "pulse" counter and properly reply with 200 Ok to the peer. Now I need
> help with the timer - I have to fire/reset a timer everytime such a message
> arrives, and when it times out (no more "pulses" coming), the timeout
> should trigger my routine, which will send the DTMF digit (I know how to
> accomplish this) and reset the counter back to zero. This timer shouldn't
> interfere with any other possible timers running for the session (Session
> timers etc.). An advice pointing to a code sample, which does something
> similar, would be enough.
>    WIth regards, Pavel
>
>

I think your best bet on this would be to use the scheduler API. The 
scheduler API allows for you to schedule a callback to be called some 
number of milliseconds in the future. So for your case, when the first 
INFO arrives, you would presumably schedule the task to run X 
milliseconds from now. You can schedule your routine by using the 
ast_sched_add() function. This function will return a > 0 ID for your 
callback, which you should store somewhere (presumably the sip_pvt). By 
storing the scheduler ID, you can know, from the fact that it is > 0, 
that you already have a callback scheduled, and you therefore don't need 
to keep scheduling the callback when further "pulses" arrive.

When your scheduled callback runs, you can return a value to indicate 
whether the task should be rescheduled. Given the nature of this task, 
you probably do not need the task rescheduled, so you can just return 0 
in your callback.

chan_sip.c already has a scheduler context, and it already has a thread 
that services the scheduler as needed. It should be enough for you to 
simply schedule your callback to be run, and then be sure to return 0 in 
your callback. There should be many examples in chan_sip.c of functions 
using the scheduler. You can use those as examples in case you need 
extra help.

Mark Michelson



More information about the asterisk-dev mailing list