顺德碧桂园西苑平面图:Asterisk模块编写2

来源:百度文库 编辑:九乡新闻网 时间:2024/04/26 04:03:15
上一篇文章《Asterisk模块编写入门》里已经解释了Hello World Asterisk 模块。该模块只实现了能够给Asterisk 编译,能够加载到Asteisk 中去,当被加载或卸载时打印简单的日志信息。现在让该模块做些更有意义的事情。 l*_%K}%?V  
Q;{[U!\:  
作者/ Russell Bryant  翻译/ 高志 ]vj4E"2;  
H }w"4s  
        在res_helloworld.c 中,我们应用了load_module 和unload_module 函数。在那个模块中,我们仅仅是打印日志,其实在该模块中可以实现更多的功能。 Gh}k9-L  
load_module 的工作是说:“hello Asterisk, I’m a module. I can provide some features to Asterisk and here is how you use them." par| j]  
        相反的,unload_module 的工作是“ 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! ” #v!(uuq,  
        现在是更新模块提供功能给Asterisk 的时候了。我们将从CDR 处理开始,CDR 接口不算是一个非常简单的应用。首先是添加合适的头文件。                 #include "asterisk/cdr.h" #U45H.Rz  
        然后,我们增加一个新的函数,该函数在每次CDR post 时将会调用,参数是CDR 本身。 2,,zN-9mt  
复制代码
  1. static int cdr_helloworld(struct ast_cdr *cdr)   
  2. {   
  3.     ast_log(LOG_NOTICE, "We got a CDR for channel '%s'.  "  
  4.         "Source: '%s', Dest: '%s', Duration: %ld\n",   
  5.         cdr->channel, cdr->src, cdr->dst, cdr->duration);   
  6.   
  7.     return 0;   
  8. }  
"#bL/b'{  
        接下来,将刚刚实现的应用通过load_module()unload_module() 函数经新功能添加到该模块中去。 lr SdFJ%  
        在load_module() 函数中,将增加一个函数调用用于向Asterisk Core 注册该CDR 处理。参数是该应用的名称,简短的描述,以及当CDR postAsterisk Core 调用函数。 cx%[hM09  
                ast_cdr_register("HelloWorld", "Hello World CDR Handler", cdr_helloworld);   Wy^43g38'p  
        在unload_module() 中,需要对刚刚添加的应用注销。 rl-r8?H}  
                ast_cdr_unregister("HelloWorld"); L B`=+FD  
        编译安装后,运行Asterisk ,可以去确认你的新CDR 应用是否被注册到了Asterisk Core 。 VB6EM|bphl  
                *CLI> cdr show status    i! G^=N  
                ...    s+Qm/ h2  
                CDR registered backend: HelloWorld    !(\OT  
                …   -$Bom  
        当你挂电话,将会看到CDR 处理被执行。 WK7=z3mu  
        [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   -:9E+b  
        你已经可以在Asterisk 模块中添加CDR 处理了!英文原文如下: v~-z["=}!  
How-to: Write an Asterisk Module, Part 2 B ;Zsp  
作者/ Russell Bryant      摘自 http://www.russellbryant.net/blog/ 4ujw/`:/m  
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. g Z3VT{  
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. ?>47!):-*  
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.” + >o/Ob  
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!” zjd]65P  
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. /|] %0B  
The first thing to do is to add the appropriate header file so that module has the appropriate definitions for the CDR interface. C[ k#k!AcC  
#include "asterisk/cdr.h" xM85^B'  
b{H&%Jx)  
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. e#`wsht N:  
DmAMr=p  
[pre]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;}[/pre] W&`_cGoP  
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. w2y{3O"p=  
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. rly3f  
Vf6lu)Z c1  
ast_cdr_register("HelloWorld", "Hello World CDR Handler", cdr_helloworld); "^~f.N  
6TS+z7S81L  
In unload_module(), we will add a function call to “unregister” the CDR handler form the Asterisk core. }_}C ^  
^.jIus5  
ast_cdr_unregister("HelloWorld"); M5 ep\^  
VkFTIyt  
That’s it! See the finished code here, res_helloworld2.c. 7)G- EAF  
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. +#O?a`f  
_m0H gLS~  
*CLI> cdr show status 4 W}8?&T  
... u*`acm S>N  
CDR registered backend: HelloWorld qQ%zSJ?  
... AS`0.RC-  
V| z|H$-  
When you hang up a call, you should see the result of your CDR handler being executed. zAt!jP0E  
"QACQ-  
[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 wcP0PfY  
qrO] t\  
And now, you have implemented an Asterisk module that adds custom handling of CDR records! qdm5dQ (c