How-to: Write an Asterisk Module, Part 2

来源:互联网 发布:手机淘宝装修模块使用 编辑:程序博客网 时间:2024/06/16 16:41

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!

 

From:http://www.russellbryant.net/blog/2008/06/20/how-to-write-an-asterisk-module-part-2

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果8icloud满了怎么办 吃人参回奶了怎么办 邮箱被黑客黑了怎么办 传图识字有表格怎么办 手机qq收件箱图片打不开怎么办 腾讯企业邮箱一直被攻击怎么办 qq邮箱发送文件太大怎么办 苹果手机邮箱被删除了怎么办 亚马逊卖家登录邮箱被盗怎么办 邮箱名字被注册了怎么办 忘了注册的邮箱名字怎么办 大众车钥匙丢了怎么办 锁柜钥匙丢了怎么办 邮箱的储存空间太小怎么办 扣扣邮箱不支持打开文件怎么办 邮箱大师群发不了邮件怎么办 邮政uk密码忘了怎么办 dns配置错误网页打不开怎么办 手机邮箱收不到邮件怎么办 wifi的那个用户密码错了怎么办 天翼校园客户端连不上怎么办 公司不小心外发邮件怎么办 steam账号被盗邮箱被改怎么办 steam被盗绑定邮箱被改怎么办 简历邮件发错了怎么办 发了简历不回复怎么办 发了简历没回复怎么办 发了简历邮件没有回复怎么办 快手绑定的手机号不用了怎么办 绑定银行卡的手机号不用了怎么办 oppo账号密码忘了怎么办 魅族账号密码忘了怎么办 苹果手机账号密码忘了怎么办 oppo手机账号密码忘了怎么办 华为手机账号密码忘了怎么办 vivo手机账号密码忘了怎么办 金立s9密码忘了怎么办 手机密号忘了怎么办 qq号忘了账号怎么办 推特密码忘了怎么办 沃邮箱改密码短信延迟怎么办