读书人

How-to: Write an Asterisk Module, P

发布时间: 2012-08-13 13:21:53 作者: rapoo

How-to: Write an Asterisk Module, Part 3

Greetings fellow developers!

This is part 3 of a series on implementing the basic interfaces to provide features to Asterisk.

Part 1? Basic Module Structure
Part 2? CDR Handling Interface

In this section, you will see how to implement an Asterisk CLI command. CLI commands are extremely useful for showing configuration, statistics, and other debugging purposes.

We are going to continue editing the module from part 2,?res_helloworld2.c.

The first step is to include the header file which defines the CLI command interface.

#include "asterisk/cli.h"

Here is the code for a CLI command, “echo”. It simply prints back the first argument to the CLI command. The individual parts of this function will be described later.

static char *handle_cli_echo(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a){    switch (cmd) {    case CLI_INIT:        e->command = "echo";        e->usage =            "Usage: echo <stuff>\n"            "       Print back the first argument.\n"            "Examples:\n"            "       echo foo\n"            "       echo \"multiple words\"\n"            "";        return NULL;    case CLI_GENERATE:        return NULL;    }    if (a->argc == e->args) {        ast_cli(a->fd, "You did not provide an argument to echo\n\n");        return CLI_SHOWUSAGE;    }    ast_cli(a->fd, "%s\n", a->argv[1]);    return CLI_SUCCESS;}

The first line of this CLI handler matches the defined function prototype for CLI command handlers. The ast_cli_entry argument includes static information about the CLI command being executed. The cmd argument is set when the CLI command is being invoked for a reason other than normal execution from the CLI, which will be explained more later. The ast_cli_args argument contains information specific to this one time execution of the command, like the arguments to the command.

static char *handle_cli_echo(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)

The switch statement at the beginning of the function handles when the cmd argument is set to indicate that the function is being called for a reason other than normal execution. There are two cases handled.

读书人网 >移动开发

热点推荐