How-to: Write an Asterisk Module
来源:互联网 发布:linux修改时间 编辑:程序博客网 时间:2024/05/24 01:39
How-to: Write an Asterisk Module, Part 1
June 19th, 2008 · 22 Comments
Have you ever wanted to write an Asterisk module? While some of the Asterisk modules are quite complicated, the anatomy of an Asterisk module is pretty simple. Let’s start with the “Hello World” of Asterisk modules: res_helloworld.
This example is based on Asterisk 1.6. However, creating Asterisk modules for Asterisk 1.4 is almost the exact same.
Create a file called res_helloworld.c in the res/ directory of an Asterisk source tree.
The first thing in every Asterisk module is to include the main Asterisk header file, asterisk.h.
#include "asterisk.h"
Next, include the ASTERISK_FILE_VERSION macro. This registers the file with the “core show file version” CLI command. This CLI command lists the last SVN revision where that file changed.
ASTERISK_FILE_VERSION(__FILE__, "$Revision: $")
Include the Asterisk module header file. This includes the definitions necessary for implementing an Asterisk module.
#include "asterisk/module.h"
Let’s go ahead and include the header file that lets us use the Asterisk logging interface, as well. This will let us print messages to the Asterisk log so that our new module actually does something.
#include "asterisk/logger.h"
It is now time to include the two functions that every Asterisk module must implement. Those are load_module() and unload_module(). These functions get called when Asterisk loads and unloads the module.
static int load_module(void)
{
ast_log(LOG_NOTICE, "Hello World!\n");
return AST_MODULE_LOAD_SUCCESS;
}
static int unload_module(void)
{
ast_log(LOG_NOTICE, "Goodbye World!\n");
return 0;
}
Finally, every module must include an instance of one of the AST_MODULE_INFO macros. This macro includes the necessary code for the module to properly register itself with the Asterisk core when it gets loaded.
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hello World");
The final result should look something like this: res_helloworld.c
When you run make
to compile Asterisk, the build system should automatically find your module. It will be compiled and installed with the rest of Asterisk. Compile, install, and run Asterisk. Then, verify that your module has been loaded:
*CLI> module show like helloworld
Module Description Use Count
res_helloworld.so Hello World 0
1 modules loaded
You should also be able to unload and load your module, and see the appropriate message in the Asterisk logger.
*CLI> module unload res_helloworld.so
[Jun 19 10:50:57] NOTICE[26612]: res_helloworld.c:35 unload_module: Goodbye World!
*CLI> module load res_helloworld.so
[Jun 19 10:51:05] NOTICE[26612]: res_helloworld.c:42 load_module: Hello World!
Loaded res_helloworld.so => (Hello World)
Congratulations! You have successfully written an Asterisk module!
Next, we will start looking at how to start implementing more useful things inside of this module structure.
- How-to: Write an Asterisk Module
- How-to: Write an Asterisk Module, Part 1
- How-to: Write an Asterisk Module, Part 2
- How-to: Write an Asterisk Module, Part 3
- How to Write An Email
- how to write a module dumper & disassembler
- how to write a module dumper & disassembler
- How to Write a Linux usb module
- How to Write an Action Listener
- How to write an operating system
- How to write an operating system
- How to write an immutable Class?
- How to Write an Effective Design Document
- how to write a nginx module for http living stream
- How to read/write files within a Linux kernel module?
- How to Write an Equality Method in Java
- How to write Bibliography
- how to write makefile
- hibernate一级缓存和二级缓存的区别
- Oracle Golden Gate 系列十七 -- GG 一对多 real-time data distribution 说明 与 示例
- myeclipse的代码提示
- 一些很有启发的电影或者书
- Struts2中的ActionContext
- How-to: Write an Asterisk Module
- 利用FFT 及 IFFT实现傅立叶正反变换
- 多线程的socket编程
- android Handler 学习
- Android中的文件存储数据方式 .
- listView和ListAdapter
- java i/o 几个主要类的使用 和 介绍
- SQL基础 学习小记四
- 终端服务器的激活!