Building modules in asterisk part 1


This is a basics of "How to build a module in asterisk " the following example was tested on asterisk 11.5

Create a res_foobar.c file on the asterisk source folder/res directory;

1) Include asterisk header file
	#include "asterisk.h"

2) Define asterisk file version
	ASTERISK_FILE_VERSION(__FILE__, "$Revision: XXXXXX $") /* where XXXXXX  is a revision number*/

3) Include asterisk module header file
	#include "asterisk/module.h"


4) Since, on this tutorial we are going to load and unload the module we are going to code. Create two function to load and unload the modules.

	/* To load module */
	static int load_module(void)
	{
	ast_log(LOG_NOTICE, "Hello Foobar! I am loaded !");
	return AST_MODULE_LOAD_SUCCESS;
	}

	/* To unload module */
	static int unload_module(void)
	{
	ast_log(LOG_NOTICE, "Hello Foobar! I am unloaded !");
	return 0;
	}
	
5) Define Asterisk module info
	
	AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Test - module to check load and unload");


6) Save file and run make && make install from asterisk source directory. Once it complies and the module is copied to asterisk lib directory you should be able to load and unload modules as;

module load res_foobar.so
	 NOTICE[18009]: res_foobar.c:13 load_module: Hello Foobar! I am loaded !
module unload res_foobar.so
 	 NOTICE[18009]: res_foobar.c:19 load_module: Hello Foobar! I am unloaded !

The complete code :

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328259 $")

//#include "asterisk/channel.h"
#include "asterisk/module.h"


static int unload_module(void)
{
	ast_log(LOG_NOTICE, "Hello Foobar ! I am unloaded !\n");
	return 0;
}

static int load_module(void)
{
	ast_log(LOG_NOTICE, "Hello Foobar ! I am loaded !\n");
	return AST_MODULE_LOAD_SUCCESS;
}


AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Test - module to check load and unload");

, , , ,

Comments are closed.