
本文共 2584 字,大约阅读时间需要 8 分钟。
我之前有一篇文章()介绍过如何扩展asterisk的cli接口,本篇是它的继续,总结下,也方便我以后查阅。
大部分情况下,配置asterisk的拨号方案,用CLI、AMI之类的就可以满足我们的需求。可有些情况下涉及到业务的东东,需要数据库的参与(比如用sqlserve存储asterisk的录音记录等等),拨号方案那种静态的做法完全不用考虑,而原始的CLI、AMI已经不能满足需求。这时就需要考虑从源码入手,扩展asterisk了。
asterisk是基于插件的,很容易扩展。手动编译过asterisk源码的朋友应该知道,在asterisk源码目录里有一个addons的目录,里面就是asterisk的插件(其实apps下也可以看做是插件)。
这里有个小例子,主要演示怎么从源码扩展asterisk的CLI接口。
一、建立目录结构,配置Makefile
1、为了方便代码的管理,我决定新建立一个叫addons_test的文件夹;
2、将apps下的Makefile复制到该目录;
3、打开asterisk主目录下的Makefile文件,在MOD_SUBDIRS变量中加入addons_test(我的Makefile是在266行)。
二、编写CLI插件代码
1、在addons_test目录添加文件app_testApp20120605.c和文件app_testApp20120605.exports
说明:
app_testApp20120605.c为程序代码
app_testApp20120605.exports 为动态库导出配置
2、编写文件内容
app_testApp20120605.exports文件简单,可以将apps目录下的任一”.exports”文件copy至本目录改名即可,这里主要介绍app_testApp20120605.c的书写。
2.1 首先需要添加头文件:
#include "asterisk.h"#include "asterisk/module.h"#include "asterisk/cli.h"
2.2 定义Application名称:
static char *app_testApp = "testApp20120605";
2.3 写模块加载函数:
static int testApp_exec(struct ast_channel *chan, const char *data){ ast_verb(2,"testApp_exec : %s\r\n",data); return0;}
说明:这个要用此格式,尽管chan变量没有用到,但加载模块的函数指针是这种格式。
2.4 编写CLI接口函数:
1 static char *handle_cli_testApp(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) 2 { 3 struct ast_channel *chan=NULL; 4 5 if(CLI_INIT == cmd) { 6 e->command = "testApp20120605 {print}"; 7 e->usage = 8 "Usage: testApp20120605\n" 9 " Print something to test application\n"10 " application when the 'print' command is used.\n";11 returnNULL;12 }13 14 if (a->argc < 2)15 return CLI_SHOWUSAGE;16 17 if (!strcasecmp(a->argv[1], "print")) {18 testApp_exec(chan, a->argv[2]);19 }else{20 return CLI_SHOWUSAGE;21 }22 23 return CLI_SUCCESS;24 }
2.5 编写模块加载函数:
1 static int load_module(void)2 {3 int res;4 ast_cli_register_multiple(cli_testApp, ARRAY_LEN(cli_testApp));5 res = ast_register_application_xml(app_testApp,testApp_exec);6 return res;7 }
2.6 编写模块卸载函数:
static int unload_module(void){ int res; ast_cli_unregister_multiple(cli_testApp, ARRAY_LEN(cli_testApp)); res = ast_unregister_application(app_testApp); return res;}
三、测试CLI插件
1、编译运行
执行如下命令:
make && make install && asterisk && asterisk -rvvvvvvvv
2、测试
启动后,执行如下命令:
testApp20120605 print "Just a test"
运行效果:
发表评论
最新留言
关于作者
