Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:49:11 2009

## Asterisk developer's documentation

# pbx_realtime.c File Reference

Realtime PBX Module. More...

`#include <stdlib.h>`
`#include <stdio.h>`
`#include <unistd.h>`
`#include <string.h>`
`#include <errno.h>`
`#include "asterisk.h"`
`#include "asterisk/file.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/config.h"`
`#include "asterisk/options.h"`
`#include "asterisk/pbx.h"`
`#include "asterisk/module.h"`
`#include "asterisk/frame.h"`
`#include "asterisk/term.h"`
`#include "asterisk/manager.h"`
`#include "asterisk/cli.h"`
`#include "asterisk/lock.h"`
`#include "asterisk/md5.h"`
`#include "asterisk/linkedlists.h"`
`#include "asterisk/chanvars.h"`
`#include "asterisk/sched.h"`
`#include "asterisk/io.h"`
`#include "asterisk/utils.h"`
`#include "asterisk/crypto.h"`
`#include "asterisk/astdb.h"`

Include dependency graph for pbx_realtime.c:

Go to the source code of this file.

## Defines

#define | EXT_DATA_SIZE 256
#define | MODE_CANMATCH 2
#define | MODE_MATCH 0
#define | MODE_MATCHMORE 1
#define | REALTIME_COMMON(mode)

## Functions

char * | description (void)
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
int | load_module (void)
| Initialize the module.
static int | realtime_canmatch (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
static int | realtime_exec (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, int newstack, const char *data)
static int | realtime_exists (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
static int | realtime_matchmore (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
static struct ast_variable * | realtime_switch_common (const char *table, const char *context, const char *exten, int priority, int mode)
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

static struct ast_switch | realtime_switch
static char * | tdesc = "Realtime Switch"

## Detailed Description

Realtime PBX Module.

- See also: ARA - The Asterisk Realtime Interface

Definition in file pbx_realtime.c.

## Define Documentation

#define EXT_DATA_SIZE 256

Definition at line 62 of file pbx_realtime.c.

#define MODE_CANMATCH 2

Definition at line 60 of file pbx_realtime.c.

Referenced by realtime_canmatch(), and realtime_switch_common().

#define MODE_MATCH 0

Definition at line 58 of file pbx_realtime.c.

Referenced by realtime_exec(), realtime_exists(), and realtime_switch_common().

#define MODE_MATCHMORE 1

Definition at line 59 of file pbx_realtime.c.

Referenced by realtime_matchmore(), and realtime_switch_common().

#define REALTIME_COMMON | ( | mode |  | ) |

Definition at line 80 of file pbx_realtime.c.

Referenced by realtime_canmatch(), realtime_exec(), realtime_exists(), and realtime_matchmore().

## Function Documentation

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 248 of file pbx_realtime.c.

    00249 {
    00250    return tdesc;
    00251 }

char* key | ( | void |  | ) |

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

     char *key(void) {
             return ASTERISK_GPL_KEY;
     }

Returns:
  ASTERISK_GPL_KEY

Definition at line 258 of file pbx_realtime.c.

References ASTERISK_GPL_KEY.

    00259 {
    00260    return ASTERISK_GPL_KEY;
    00261 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
  int Always 0.

TE STUFF END

Definition at line 269 of file pbx_realtime.c.

References ast_register_switch().

    00270 {
    00271    ast_register_switch(&realtime_switch);
    00272    return 0;
    00273 }

static int realtime_canmatch | ( | struct ast_channel * | chan,
|  | const char * | context,
|  | const char * | exten,
|  | int | priority,
|  | const char * | callerid,
|  | const char * | data |
| ) |  |  | `[static]`

Definition at line 171 of file pbx_realtime.c.

References ast_variables_destroy(), MODE_CANMATCH, REALTIME_COMMON, and var.

    00172 {
    00173    REALTIME_COMMON(MODE_CANMATCH);
    00174    if (var) ast_variables_destroy(var);
    00175    if (var)
    00176       res = 1;
    00177    return res > 0 ? res : 0;
    00178 }

static int realtime_exec | ( | struct ast_channel * | chan,
|  | const char * | context,
|  | const char * | exten,
|  | int | priority,
|  | const char * | callerid,
|  | int | newstack,
|  | const char * | data |
| ) |  |  | `[static]`

Definition at line 180 of file pbx_realtime.c.

References app, ast_log(), ast_strdupa, ast_strlen_zero(), ast_variables_destroy(), ast_verbose(), COLOR_BRCYAN, COLOR_BRMAGENTA, ast_channel::context, EVENT_FLAG_CALL, EXT_DATA_SIZE, ast_channel::exten, LOG_NOTICE, manager_event(), MODE_MATCH, ast_channel::name, ast_variable::name, ast_variable::next, option_verbose, pbx_exec(), pbx_findapp(), pbx_substitute_variables_helper(), ast_channel::priority, REALTIME_COMMON, term_color(), ast_channel::uniqueid, ast_variable::value, var, and VERBOSE_PREFIX_3.

    00181 {
    00182    char app[256];
    00183    char appdata[512]="";
    00184    char *tmp="";
    00185     char tmp1[80];
    00186     char tmp2[80];
    00187     char tmp3[EXT_DATA_SIZE];
    00188    struct ast_app *a;
    00189    struct ast_variable *v;
    00190    REALTIME_COMMON(MODE_MATCH);
    00191    if (var) {
    00192       v = var;
    00193       while(v) {
    00194          if (!strcasecmp(v->name, "app"))
    00195             strncpy(app, v->value, sizeof(app) -1 );
    00196          else if (!strcasecmp(v->name, "appdata"))
    00197             tmp = ast_strdupa(v->value);
    00198          v = v->next;
    00199       }
    00200       ast_variables_destroy(var);
    00201       if (!ast_strlen_zero(app)) {
    00202          a = pbx_findapp(app);
    00203          if (a) {
    00204             if(!ast_strlen_zero(tmp))
    00205                pbx_substitute_variables_helper(chan, tmp, appdata, sizeof(appdata) - 1);
    00206                 if (option_verbose > 2)
    00207                ast_verbose( VERBOSE_PREFIX_3 "Executing %s(\"%s\", \"%s\")\n",
    00208                          term_color(tmp1, app, COLOR_BRCYAN, 0, sizeof(tmp1)),
    00209                          term_color(tmp2, chan->name, COLOR_BRMAGENTA, 0, sizeof(tmp2)),
    00210                          term_color(tmp3, (!ast_strlen_zero(appdata) ? (char *)appdata : ""), COLOR_BRMAGENTA, 0, sizeof(tmp3)));
    00211                 manager_event(EVENT_FLAG_CALL, "Newexten",
    00212                        "Channel: %s\r\n"
    00213                        "Context: %s\r\n"
    00214                        "Extension: %s\r\n"
    00215                        "Priority: %d\r\n"
    00216                        "Application: %s\r\n"
    00217                        "AppData: %s\r\n"
    00218                        "Uniqueid: %s\r\n",
    00219                        chan->name, chan->context, chan->exten, chan->priority, app, appdata ? appdata : "(NULL)", chan->uniqueid);
    00220
    00221             res = pbx_exec(chan, a, appdata, newstack);
    00222          } else
    00223             ast_log(LOG_NOTICE, "No such application '%s' for extension '%s' in context '%s'\n", app, exten, context);
    00224       }
    00225    }
    00226    return res;
    00227 }

static int realtime_exists | ( | struct ast_channel * | chan,
|  | const char * | context,
|  | const char * | exten,
|  | int | priority,
|  | const char * | callerid,
|  | const char * | data |
| ) |  |  | `[static]`

Definition at line 162 of file pbx_realtime.c.

References ast_variables_destroy(), MODE_MATCH, REALTIME_COMMON, and var.

    00163 {
    00164    REALTIME_COMMON(MODE_MATCH);
    00165    if (var) ast_variables_destroy(var);
    00166    if (var)
    00167       res = 1;
    00168    return res > 0 ? res : 0;
    00169 }

static int realtime_matchmore | ( | struct ast_channel * | chan,
|  | const char * | context,
|  | const char * | exten,
|  | int | priority,
|  | const char * | callerid,
|  | const char * | data |
| ) |  |  | `[static]`

Definition at line 229 of file pbx_realtime.c.

References ast_variables_destroy(), MODE_MATCHMORE, REALTIME_COMMON, and var.

    00230 {
    00231    REALTIME_COMMON(MODE_MATCHMORE);
    00232    if (var) ast_variables_destroy(var);
    00233    if (var)
    00234       res = 1;
    00235    return res > 0 ? res : 0;
    00236 }

static struct ast_variable* realtime_switch_common | ( | const char * | table,
|  | const char * | context,
|  | const char * | exten,
|  | int | priority,
|  | int | mode |
| ) |  |  | `[static, read]`

Definition at line 109 of file pbx_realtime.c.

References ast_category_browse(), ast_category_detach_variables(), ast_category_get(), ast_config_destroy(), ast_extension_close(), ast_extension_match(), ast_load_realtime(), ast_load_realtime_multientry(), AST_MAX_EXTENSION, cfg, match(), MODE_CANMATCH, MODE_MATCH, MODE_MATCHMORE, and var.

    00110 {
    00111    struct ast_variable *var;
    00112    struct ast_config *cfg;
    00113    char pri[20];
    00114    char *ematch;
    00115    char rexten[AST_MAX_EXTENSION + 20]="";
    00116    int match;
    00117    snprintf(pri, sizeof(pri), "%d", priority);
    00118    switch(mode) {
    00119    case MODE_MATCHMORE:
    00120       ematch = "exten LIKE";
    00121       snprintf(rexten, sizeof(rexten), "%s_%%", exten);
    00122       break;
    00123    case MODE_CANMATCH:
    00124       ematch = "exten LIKE";
    00125       snprintf(rexten, sizeof(rexten), "%s%%", exten);
    00126       break;
    00127    case MODE_MATCH:
    00128    default:
    00129       ematch = "exten";
    00130       strncpy(rexten, exten, sizeof(rexten) - 1);
    00131    }
    00132    var = ast_load_realtime(table, ematch, rexten, "context", context, "priority", pri, NULL);
    00133    if (!var) {
    00134       cfg = ast_load_realtime_multientry(table, "exten LIKE", "\\_%", "context", context, "priority", pri, NULL);
    00135       if (cfg) {
    00136          char *cat = ast_category_browse(cfg, NULL);
    00137
    00138          while(cat) {
    00139             switch(mode) {
    00140             case MODE_MATCHMORE:
    00141                match = ast_extension_close(cat, exten, 1);
    00142                break;
    00143             case MODE_CANMATCH:
    00144                match = ast_extension_close(cat, exten, 0);
    00145                break;
    00146             case MODE_MATCH:
    00147             default:
    00148                match = ast_extension_match(cat, exten);
    00149             }
    00150             if (match) {
    00151                var = ast_category_detach_variables(ast_category_get(cfg, cat));
    00152                break;
    00153             }
    00154             cat = ast_category_browse(cfg, cat);
    00155          }
    00156          ast_config_destroy(cfg);
    00157       }
    00158    }
    00159    return var;
    00160 }

int unload_module | ( | void |  | ) |

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
  Zero on success, or non-zero on error.

Standard module functions ...

Definition at line 263 of file pbx_realtime.c.

References ast_unregister_switch().

    00264 {
    00265    ast_unregister_switch(&realtime_switch);
    00266    return 0;
    00267 }

int usecount | ( | void |  | ) |

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
  The module's usecount.

Definition at line 253 of file pbx_realtime.c.

    00254 {
    00255    return 1;
    00256 }

## Variable Documentation

struct ast_switch realtime_switch`[static]`

Definition at line 238 of file pbx_realtime.c.

char* tdesc = "Realtime Switch"`[static]`

Definition at line 64 of file pbx_realtime.c.