russellbryant.net

Asterisk Development, and Open Source Software Engineering

russellbryant.net header image 2

How-to: Write an Asterisk Module, Part 2

June 20th, 2008 · 11 Comments

In part 1, I explained the “Hello World” of Asterisk modules. It implemented just enough to properly compile, get loaded into Asterisk, and print something to the Asterisk log when the module was loaded and unloaded. Now, it’s time to start making modules that do something useful.

In res_helloworld.c, we implemented a load_module and unload_module function. In that module, they just printed to the log. They actually have a much more important purpose in life.

The job of load_module() is to say, “Hello Asterisk, I’m a module. I can provide some features to Asterisk and here is how you use them.”

Conversely, the job of unload module is to say, “Hello Asterisk, again. I’m about to disappear, so please don’t use any of these features that I was providing to you anymore. If you do, you’ll explode. kthx!”

So, it’s time to update this module to provide a feature to Asterisk. We’ll start with a CDR (Call Detail Record) handler. It is one of, if not the simplest interface to implement. To keep things simple, we’re going to implement an Asterisk CDR handler that once again, just uses the Asterisk logging interface.

The first thing to do is to add the appropriate header file so that module has the appropriate definitions for the CDR interface.


#include "asterisk/cdr.h"

Next, we’re going to add a new function. This function will get called every time that there is a new CDR to post. It takes a single argument, which is the CDR itself.

static int cdr_helloworld(struct ast_cdr *cdr)
{
    ast_log(LOG_NOTICE, "We got a CDR for channel '%s'.  "
        "Source: '%s', Dest: '%s', Duration: %ld\n",
        cdr->channel, cdr->src, cdr->dst, cdr->duration);

    return 0;
}

Next, as I described before, we have to make the load_module() and unload_module() functions aware of this new feature that is implemented by the module.

In load_module(), we will add a function call to “register” the CDR handler with the Asterisk core. The arguments are the name of the handler, a short description, and the function that the Asterisk core needs to call when a CDR is ready for posting.


ast_cdr_register("HelloWorld", "Hello World CDR Handler", cdr_helloworld);

In unload_module(), we will add a function call to “unregister” the CDR handler form the Asterisk core.


ast_cdr_unregister("HelloWorld");

That’s it! See the finished code here, res_helloworld2.c.

Compile it and install it. When you start Asterisk, you should be able to verify that your new CDR handler has been registered with the Asterisk core.


*CLI> cdr show status
...
CDR registered backend: HelloWorld
...

When you hang up a call, you should see the result of your CDR handler being executed.


[Jun 20 18:08:29] NOTICE[4922]: res_helloworld.c:36 cdr_helloworld: We got a CDR for channel ‘SIP/5001-007e9da8′. Source: ‘5001′, Dest: ‘586′, Duration: 1

And now, you have implemented an Asterisk module that adds custom handling of CDR records!

Tags: Asterisk · Development · How-to

11 responses so far ↓

  • 1 Programando módulos personalizados para Asterisk at Mi Brain-Training Personal // Jun 20, 2008 at 5:25 pm

    [...] ACTUALIZACIÓN: Aquí etnéis la segunda parte: http://www.russellbryant.net/blog/index.php/2008/06/20/how-to-write-an-asterisk-module-part-2/ [...]

  • 2 gitguy // Jun 22, 2008 at 2:27 pm

    Great, thanks for your hard work Russell, keep it up.

    Can’t wait for 1.6, I’m sure it will kick ass =D

  • 3 Bharat // Jun 23, 2008 at 3:56 am

    Hi Russell,

    Thanx for nice explanation of module creation in Asterisk.

    I want to know that should i implement multithreading and socket programming concept using module in Asterisk ???

    Actually, i want to pop-up at Agent desk using socket programming in Queue Application. Currently i am using manager events for it. But i want to know that should it possible with Asterisk Module to handle multiple socket communication with Multithreading concept ???

    Thanxs in advance

  • 4 Allan GooD // Jun 23, 2008 at 1:04 pm

    Russel, thank you for your brilliant work. Your res_ais (openAIS) is a real killer app.

  • 5 russell // Jun 23, 2008 at 9:12 pm

    Allan, thank you very much for the kind words!

    Bharat, without knowing more, it sounds like writing an external application using the manager interface is the best way to achieve what you want to do. Another thing that you should look at is using Jabber for agent pop-ups. You can send jabber messages to your agents from the dialplan. Some people have done some very nice things for call centers just using that.

  • 6 Bharat // Jun 23, 2008 at 11:08 pm

    Thanx Russell.

    I have three IVR server with 8 PRI (Zaptel) in each server for load sharing. One Agent server connected with all IVR server with SIP trunk ( To provide single ACD and common queue).

    Call transfer to Agent from Queue Application only. We integrate manager events of all 3 ivr server and Agent server. When ever i get event on manager for Agent Ringing, i send pop-up to Agent desk using external application.

    However, its working fine with Manager events intergration. I just want to know is it possible to include the same in Queue Application.

    I am studying Jabber also as per your suggession.

    Thanks again.

  • 7 gitguy // Jun 23, 2008 at 11:30 pm

    is asterisk going to have sockets support for programming languages?

    i know there is cdr, but sockets would be great…

  • 8 russell // Jun 24, 2008 at 3:30 pm

    I don’t understand the question, but all of this is off topic with this specific post. :)

  • 9 Fernando // Jun 25, 2008 at 9:26 pm

    Congratulations for the article.
    Where can I find more information about it?
    Thanks a lot

  • 10 russell // Jun 26, 2008 at 12:34 pm

    Thanks. To find more in depth information, I will be posting more tutorials. Beyond that, the Asterisk source is your best friend, as there are hundreds of examples already there. You can also find API documentation in header files (include/asterisk/) and the doxygen generated documentation:

    http://www.asterisk.org/doxygen

  • 11 How-to: Write an Asterisk Module, Part 3 // Jun 30, 2008 at 7:32 am

    [...] XHTML ← How-to: Write an Asterisk Module, Part 2 [...]

Leave a Comment