扩展Asterisk1.8.7的CLI接口
我之前有一篇文章(http://www.cnblogs.com/MikeZhang/archive/2012/04/14/asteriskCLIAppTest20120414.html)介紹過如何擴展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接口函數:
View Code 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 <print> <something2print>\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 編寫模塊加載函數:
View Code 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 編寫模塊卸載函數:
View Code 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"
運行效果:?
轉載于:https://www.cnblogs.com/MikeZhang/archive/2012/06/05/asterisk187CLIAddOns.html
總結
以上是生活随笔為你收集整理的扩展Asterisk1.8.7的CLI接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu 编译 /usr/bin/l
- 下一篇: js 之for..in、表单及事件触发