Asterisk - The Open Source Telephony Project  GIT-master-a24979a
Data Structures | Macros | Enumerations | Functions | Variables
app_skel.c File Reference

Skeleton application. More...

#include "asterisk.h"
#include <math.h>
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/app.h"
#include "asterisk/config.h"
#include "asterisk/config_options.h"
#include "asterisk/say.h"
#include "asterisk/astobj2.h"
#include "asterisk/acl.h"
#include "asterisk/netsock2.h"
#include "asterisk/strings.h"
#include "asterisk/cli.h"
Include dependency graph for app_skel.c:

Go to the source code of this file.

Data Structures

struct  skel_config
 A container that holds all config-related information. More...
 
struct  skel_current_game
 Information about a currently running set of games. More...
 
struct  skel_global_config
 A structure to hold global configuration-related options. More...
 
struct  skel_level
 Object to hold level config information. More...
 
struct  skel_level_state
 A structure to maintain level state across reloads. More...
 

Macros

#define LEVEL_BUCKETS   1
 
#define SKEL_FORMAT   "%-15.15s %-15.15s %-15.15s\n"
 
#define SKEL_FORMAT   "%-15.15s %-11.11s %-12.12s %-8.8s %-8.8s %-12.12s\n"
 
#define SKEL_FORMAT1   "%-15.15s %-15u %-15u\n"
 
#define SKEL_FORMAT1   "%-15.15s %-11u %-12u %-8u %-8u %-8f\n"
 

Enumerations

enum  option_args { OPTION_ARG_NUMGAMES , OPTION_ARG_ARRAY_SIZE }
 
enum  option_flags { OPTION_CHEAT = (1 << 0) , OPTION_NUMGAMES = (1 << 1) , OPTION_PATTERNS_DISABLED = (1 << 0) }
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static AO2_GLOBAL_OBJ_STATIC (globals)
 A global object container that will contain the skel_config that gets swapped out on reloads. More...
 
static int app_exec (struct ast_channel *chan, const char *data)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 CONFIG_INFO_STANDARD (cfg_info, globals, skel_config_alloc,.files=ACO_FILES(&app_skel_conf),)
 Register information about the configs being processed by this module. More...
 
static int custom_bitfield_handler (const struct aco_option *opt, struct ast_variable *var, void *obj)
 A custom bitfield handler. More...
 
static char * handle_skel_show_config (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_skel_show_games (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_skel_show_levels (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static int load_module (void)
 Load the module. More...
 
static void play_files_helper (struct ast_channel *chan, const char *prompts)
 
static int reload_module (void)
 
static void * skel_config_alloc (void)
 Allocate a skel_config to hold a snapshot of the complete results of parsing a config. More...
 
static void skel_config_destructor (void *obj)
 
static void * skel_find_or_create_state (const char *category)
 Look up an existing state object, or create a new one. More...
 
static struct skel_current_gameskel_game_alloc (struct skel_level *level)
 
static void skel_game_destructor (void *obj)
 
static void skel_global_config_destructor (void *obj)
 
static void * skel_level_alloc (const char *cat)
 Allocate a skel_level based on a category in a configuration file. More...
 
static int skel_level_cmp (void *obj, void *arg, int flags)
 
static void skel_level_destructor (void *obj)
 
static void * skel_level_find (struct ao2_container *tmp_container, const char *category)
 Find a skel level in the specified container. More...
 
static int skel_level_hash (const void *obj, const int flags)
 
static struct skel_levelskel_state_alloc (const char *name)
 
static void skel_state_destructor (void *obj)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Skeleton (sample) Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, }
 
static char * app = "SkelGuessNumber"
 
static const struct ast_app_option app_opts [128] = { [ 'c' ] = { .flag = OPTION_CHEAT }, [ 'n' ] = { .flag = OPTION_NUMGAMES , .arg_index = OPTION_ARG_NUMGAMES + 1 }, }
 
struct aco_file app_skel_conf
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ao2_containergames
 The container of active games. More...
 
static struct aco_type global_option
 An aco_type structure to link the "general" category to the skel_global_config type. More...
 
struct aco_typeglobal_options [] = ACO_TYPES(&global_option)
 
static const char * level_categories []
 
static struct aco_type level_option
 An aco_type structure to link the everything but the "general" and "sounds" categories to the skel_level type. More...
 
struct aco_typelevel_options [] = ACO_TYPES(&level_option)
 
static struct ast_cli_entry skel_cli []
 
static struct aco_type sound_option
 An aco_type structure to link the "sounds" category to the skel_global_config type. More...
 
struct aco_typesound_options [] = ACO_TYPES(&sound_option)
 

Detailed Description

Skeleton application.

Author
<Your Name Here> <<Your Email Here>> 

This is a skeleton for development of an Asterisk application

Definition in file app_skel.c.

Macro Definition Documentation

◆ LEVEL_BUCKETS

#define LEVEL_BUCKETS   1
Examples
app_skel.c.

Definition at line 201 of file app_skel.c.

◆ SKEL_FORMAT [1/2]

#define SKEL_FORMAT   "%-15.15s %-15.15s %-15.15s\n"
Examples
app_skel.c.

◆ SKEL_FORMAT [2/2]

#define SKEL_FORMAT   "%-15.15s %-11.11s %-12.12s %-8.8s %-8.8s %-12.12s\n"

◆ SKEL_FORMAT1 [1/2]

#define SKEL_FORMAT1   "%-15.15s %-15u %-15u\n"
Examples
app_skel.c.

◆ SKEL_FORMAT1 [2/2]

#define SKEL_FORMAT1   "%-15.15s %-11u %-12u %-8u %-8u %-8f\n"

Enumeration Type Documentation

◆ option_args

Enumerator
OPTION_ARG_NUMGAMES 
OPTION_ARG_ARRAY_SIZE 

Definition at line 141 of file app_skel.c.

141  {
143  /* This *must* be the last value in this enum! */
145 };
@ OPTION_ARG_NUMGAMES
Definition: app_skel.c:142
@ OPTION_ARG_ARRAY_SIZE
Definition: app_skel.c:144

◆ option_flags

Enumerator
OPTION_CHEAT 
OPTION_NUMGAMES 
OPTION_PATTERNS_DISABLED 

Definition at line 136 of file app_skel.c.

136  {
137  OPTION_CHEAT = (1 << 0),
138  OPTION_NUMGAMES = (1 << 1),
139 };
@ OPTION_NUMGAMES
Definition: app_skel.c:138
@ OPTION_CHEAT
Definition: app_skel.c:137

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 773 of file app_skel.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 773 of file app_skel.c.

◆ AO2_GLOBAL_OBJ_STATIC()

static AO2_GLOBAL_OBJ_STATIC ( globals  )
static

A global object container that will contain the skel_config that gets swapped out on reloads.

Examples
app_skel.c.

◆ app_exec()

static int app_exec ( struct ast_channel chan,
const char *  data 
)
static
Examples
app_skel.c.

Definition at line 379 of file app_skel.c.

380 {
381  int win = 0;
382  uint32_t guesses;
384  RAII_VAR(struct skel_level *, level, NULL, ao2_cleanup);
385  RAII_VAR(struct skel_current_game *, game, NULL, ao2_cleanup);
386  char *parse, *opts[OPTION_ARG_ARRAY_SIZE];
387  struct ast_flags flags;
389  AST_APP_ARG(level);
391  );
392 
393  if (!cfg) {
394  ast_log(LOG_ERROR, "Couldn't access configuratino data!\n");
395  return -1;
396  }
397 
398  if (ast_strlen_zero(data)) {
399  ast_log(LOG_WARNING, "%s requires an argument (level[,options])\n", app);
400  return -1;
401  }
402 
403  /* We need to make a copy of the input string if we are going to modify it! */
404  parse = ast_strdupa(data);
405 
407 
408  if (args.argc == 2) {
409  ast_app_parse_options(app_opts, &flags, opts, args.options);
410  }
411 
412  if (ast_strlen_zero(args.level)) {
413  ast_log(LOG_ERROR, "%s requires a level argument\n", app);
414  return -1;
415  }
416 
417  if (!(level = ao2_find(cfg->levels, args.level, OBJ_KEY))) {
418  ast_log(LOG_ERROR, "Unknown level: %s\n", args.level);
419  return -1;
420  }
421 
422  if (!(game = skel_game_alloc(level))) {
423  return -1;
424  }
425 
426  ao2_link(games, game);
427 
428  /* Use app-specified values, or the options specified in [general] if they aren't passed to the app */
431  ast_parse_arg(opts[OPTION_ARG_NUMGAMES], PARSE_UINT32, &game->total_games)) {
432  game->total_games = cfg->global->num_games;
433  }
434  game->games_left = game->total_games;
435  game->cheat = ast_test_flag(&flags, OPTION_CHEAT) || cfg->global->cheat;
436 
437  for (game->games_left = game->total_games; game->games_left; game->games_left--) {
438  uint32_t num = ast_random() % level->max_num; /* random number between 0 and level->max_num */
439 
440  ast_debug(1, "They should totally should guess %u\n", num);
441 
442  /* Play the prompt */
443  play_files_helper(chan, cfg->global->prompt);
444  ast_say_number(chan, level->max_num, "", ast_channel_language(chan), "");
445 
446  for (guesses = 0; guesses < level->max_guesses; guesses++) {
447  size_t buflen = log10(level->max_num) + 1;
448  char buf[buflen];
449  int guess;
450  buf[buflen] = '\0';
451 
452  /* Read the number pressed */
453  ast_readstring(chan, buf, buflen - 1, 2000, 10000, "");
454  if (ast_parse_arg(buf, PARSE_INT32 | PARSE_IN_RANGE, &guess, 0, level->max_num)) {
455  if (guesses < level->max_guesses - 1) {
456  play_files_helper(chan, cfg->global->wrong);
457  }
458  continue;
459  }
460 
461  /* Inform whether the guess was right, low, or high */
462  if (guess == num && !game->cheat) {
463  /* win */
464  win = 1;
465  play_files_helper(chan, cfg->global->right);
466  guesses++;
467  break;
468  } else if (guess < num) {
469  play_files_helper(chan, cfg->global->low);
470  } else {
471  play_files_helper(chan, cfg->global->high);
472  }
473 
474  if (guesses < level->max_guesses - 1) {
475  play_files_helper(chan, cfg->global->wrong);
476  }
477  }
478 
479  /* Process game stats */
480  ao2_lock(level->state);
481  if (win) {
482  ++level->state->wins;
483  level->state->avg_guesses = ((level->state->wins - 1) * level->state->avg_guesses + guesses) / level->state->wins;
484  } else {
485  /* lose */
486  level->state->losses++;
487  play_files_helper(chan, cfg->global->lose);
488  }
489  ao2_unlock(level->state);
490  }
491 
492  ao2_unlink(games, game);
493 
494  return 0;
495 }
static void play_files_helper(struct ast_channel *chan, const char *prompts)
Definition: app_skel.c:369
static struct ao2_container * games
The container of active games.
Definition: app_skel.c:290
static const struct ast_app_option app_opts[128]
Definition: app_skel.c:150
static char * app
Definition: app_skel.c:134
static struct skel_current_game * skel_game_alloc(struct skel_level *level)
Definition: app_skel.c:314
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
#define OBJ_KEY
Definition: astobj2.h:1151
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_unlink(container, obj)
Remove an object from a container.
Definition: astobj2.h:1578
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
Definition: astobj2.h:918
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1736
#define ao2_unlock(a)
Definition: astobj2.h:729
#define ao2_lock(a)
Definition: astobj2.h:717
static struct console_pvt globals
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1844
const char * ast_channel_language(const struct ast_channel *chan)
int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int rtimeout, char *enders)
Reads multiple digits.
Definition: channel.c:6636
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:3126
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *p_result,...)
The argument parsing routine.
Definition: main/config.c:3726
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_WARNING
#define NULL
Definition: resample.c:96
int ast_say_number(struct ast_channel *chan, int num, const char *ints, const char *lang, const char *options)
says a number
Definition: channel.c:8318
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Structure used to handle boolean flags.
Definition: utils.h:199
unsigned int flags
Definition: utils.h:200
A container that holds all config-related information.
Definition: app_skel.c:209
Information about a currently running set of games.
Definition: app_skel.c:193
Object to hold level config information.
Definition: app_skel.c:177
const char * args
static struct test_options options
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:936
long int ast_random(void)
Definition: main/utils.c:2210

References ao2_cleanup, ao2_find, ao2_global_obj_ref, ao2_link, ao2_lock, ao2_unlink, ao2_unlock, app, app_opts, args, AST_APP_ARG, ast_app_parse_options(), ast_channel_language(), ast_debug, AST_DECLARE_APP_ARGS, ast_log, ast_parse_arg(), ast_random(), ast_readstring(), ast_say_number(), AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), ast_test_flag, buf, ast_flags::flags, games, globals, LOG_ERROR, LOG_WARNING, NULL, OBJ_KEY, OPTION_ARG_ARRAY_SIZE, OPTION_ARG_NUMGAMES, OPTION_CHEAT, OPTION_NUMGAMES, options, parse(), PARSE_IN_RANGE, PARSE_INT32, PARSE_UINT32, play_files_helper(), RAII_VAR, and skel_game_alloc().

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 773 of file app_skel.c.

◆ CONFIG_INFO_STANDARD()

CONFIG_INFO_STANDARD ( cfg_info  ,
globals  ,
skel_config_alloc  ,
files = ACO_FILES(&app_skel_conf) 
)

Register information about the configs being processed by this module.

Examples
app_skel.c.

◆ custom_bitfield_handler()

static int custom_bitfield_handler ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

A custom bitfield handler.

Examples
app_skel.c.

Definition at line 356 of file app_skel.c.

357 {
358  struct skel_global_config *global = obj;
359 
360  if (!strcasecmp(var->name, "cheat")) {
361  global->cheat = ast_true(var->value);
362  } else {
363  return -1;
364  }
365 
366  return 0;
367 }
#define var
Definition: ast_expr2f.c:614
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: main/utils.c:2097
A structure to hold global configuration-related options.
Definition: app_skel.c:153
static struct aco_type global
Definition: test_config.c:1445

References ast_true(), global, and var.

◆ handle_skel_show_config()

static char* handle_skel_show_config ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static
Examples
app_skel.c.

Definition at line 595 of file app_skel.c.

596 {
597  RAII_VAR(struct skel_config *, cfg, NULL, ao2_cleanup);
598 
599  switch(cmd) {
600  case CLI_INIT:
601  e->command = "skel show config";
602  e->usage =
603  "Usage: skel show config\n"
604  " List app_skel global config\n";
605  return NULL;
606  case CLI_GENERATE:
607  return NULL;
608  }
609 
610  if (!(cfg = ao2_global_obj_ref(globals)) || !cfg->global) {
611  return NULL;
612  }
613 
614  ast_cli(a->fd, "games per call: %u\n", cfg->global->num_games);
615  ast_cli(a->fd, "computer cheats: %s\n", AST_CLI_YESNO(cfg->global->cheat));
616  ast_cli(a->fd, "\n");
617  ast_cli(a->fd, "Sounds\n");
618  ast_cli(a->fd, " prompt: %s\n", cfg->global->prompt);
619  ast_cli(a->fd, " wrong guess: %s\n", cfg->global->wrong);
620  ast_cli(a->fd, " right guess: %s\n", cfg->global->right);
621 
622  return CLI_SUCCESS;
623 }
#define AST_CLI_YESNO(x)
Return Yes or No depending on the argument.
Definition: cli.h:71
#define CLI_SUCCESS
Definition: cli.h:44
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
static struct test_val a

References a, ao2_cleanup, ao2_global_obj_ref, ast_cli(), AST_CLI_YESNO, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, globals, NULL, RAII_VAR, and ast_cli_entry::usage.

◆ handle_skel_show_games()

static char* handle_skel_show_games ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static
Examples
app_skel.c.

Definition at line 625 of file app_skel.c.

626 {
627  struct ao2_iterator iter;
628  struct skel_current_game *game;
629 
630  switch(cmd) {
631  case CLI_INIT:
632  e->command = "skel show games";
633  e->usage =
634  "Usage: skel show games\n"
635  " List app_skel active games\n";
636  return NULL;
637  case CLI_GENERATE:
638  return NULL;
639  }
640 
641 #define SKEL_FORMAT "%-15.15s %-15.15s %-15.15s\n"
642 #define SKEL_FORMAT1 "%-15.15s %-15u %-15u\n"
643  ast_cli(a->fd, SKEL_FORMAT, "Level", "Total Games", "Games Left");
644  iter = ao2_iterator_init(games, 0);
645  while ((game = ao2_iterator_next(&iter))) {
646  ast_cli(a->fd, SKEL_FORMAT1, game->level_info->name, game->total_games, game->games_left);
647  ao2_ref(game, -1);
648  }
649  ao2_iterator_destroy(&iter);
650 #undef SKEL_FORMAT
651 #undef SKEL_FORMAT1
652  return CLI_SUCCESS;
653 }
#define SKEL_FORMAT
#define SKEL_FORMAT1
#define ao2_iterator_next(iter)
Definition: astobj2.h:1911
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
struct skel_level * level_info
Definition: app_skel.c:197
uint32_t games_left
Definition: app_skel.c:195
uint32_t total_games
Definition: app_skel.c:194
const ast_string_field name
Definition: app_skel.c:180

References a, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, games, skel_current_game::games_left, skel_current_game::level_info, skel_level::name, NULL, SKEL_FORMAT, SKEL_FORMAT1, skel_current_game::total_games, and ast_cli_entry::usage.

◆ handle_skel_show_levels()

static char* handle_skel_show_levels ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static
Examples
app_skel.c.

Definition at line 655 of file app_skel.c.

656 {
657  RAII_VAR(struct skel_config *, cfg, NULL, ao2_cleanup);
658  struct ao2_iterator iter;
659  struct skel_level *level;
660 
661  switch(cmd) {
662  case CLI_INIT:
663  e->command = "skel show levels";
664  e->usage =
665  "Usage: skel show levels\n"
666  " List the app_skel levels\n";
667  return NULL;
668  case CLI_GENERATE:
669  return NULL;
670  }
671 
672  if (!(cfg = ao2_global_obj_ref(globals)) || !cfg->levels) {
673  return NULL;
674  }
675 
676 #define SKEL_FORMAT "%-15.15s %-11.11s %-12.12s %-8.8s %-8.8s %-12.12s\n"
677 #define SKEL_FORMAT1 "%-15.15s %-11u %-12u %-8u %-8u %-8f\n"
678  ast_cli(a->fd, SKEL_FORMAT, "Name", "Max number", "Max Guesses", "Wins", "Losses", "Avg Guesses");
679  iter = ao2_iterator_init(cfg->levels, 0);
680  while ((level = ao2_iterator_next(&iter))) {
681  ast_cli(a->fd, SKEL_FORMAT1, level->name, level->max_num, level->max_guesses, level->state->wins, level->state->losses, level->state->avg_guesses);
682  ao2_ref(level, -1);
683  }
684  ao2_iterator_destroy(&iter);
685 #undef SKEL_FORMAT
686 #undef SKEL_FORMAT1
687 
688  return CLI_SUCCESS;
689 }
uint32_t wins
Definition: app_skel.c:168
double avg_guesses
Definition: app_skel.c:170
uint32_t losses
Definition: app_skel.c:169
uint32_t max_guesses
Definition: app_skel.c:182
struct skel_level_state * state
Definition: app_skel.c:183
uint32_t max_num
Definition: app_skel.c:181

References a, ao2_cleanup, ao2_global_obj_ref, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_cli(), skel_level_state::avg_guesses, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, globals, skel_level_state::losses, skel_level::max_guesses, skel_level::max_num, skel_level::name, NULL, RAII_VAR, SKEL_FORMAT, SKEL_FORMAT1, skel_level::state, ast_cli_entry::usage, and skel_level_state::wins.

◆ load_module()

static int load_module ( void  )
static

Load the module.

Module loading including tests for configuration or dependencies. This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE, or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails tests return AST_MODULE_LOAD_FAILURE. If the module can not load the configuration file or other non-critical problem return AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.

Examples
app_skel.c.

Definition at line 725 of file app_skel.c.

726 {
727  if (aco_info_init(&cfg_info)) {
728  goto error;
729  }
730 
732  if (!games) {
733  goto error;
734  }
735 
736  /* Global options */
737  aco_option_register(&cfg_info, "games", ACO_EXACT, global_options, "3", OPT_UINT_T, 0, FLDSET(struct skel_global_config, num_games));
739 
740  /* Sound options */
741  aco_option_register(&cfg_info, "prompt", ACO_EXACT, sound_options, "please-enter-your&number&queue-less-than", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, prompt));
742  aco_option_register(&cfg_info, "wrong_guess", ACO_EXACT, sound_options, "vm-pls-try-again", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, wrong));
743  aco_option_register(&cfg_info, "right_guess", ACO_EXACT, sound_options, "auth-thankyou", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, right));
744  aco_option_register(&cfg_info, "too_high", ACO_EXACT, sound_options, "high", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, high));
745  aco_option_register(&cfg_info, "too_low", ACO_EXACT, sound_options, "low", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, low));
746  aco_option_register(&cfg_info, "lose", ACO_EXACT, sound_options, "vm-goodbye", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, lose));
747 
748  /* Level options */
749  aco_option_register(&cfg_info, "max_number", ACO_EXACT, level_options, NULL, OPT_UINT_T, 0, FLDSET(struct skel_level, max_num));
750  aco_option_register(&cfg_info, "max_guesses", ACO_EXACT, level_options, NULL, OPT_UINT_T, 0, FLDSET(struct skel_level, max_guesses));
751 
752  if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
753  goto error;
754  }
755 
758  goto error;
759  }
761 
762 error:
763  aco_info_destroy(&cfg_info);
766 }
struct aco_type * global_options[]
Definition: app_skel.c:249
static int custom_bitfield_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
A custom bitfield handler.
Definition: app_skel.c:356
struct aco_type * level_options[]
Definition: app_skel.c:279
static struct ast_cli_entry skel_cli[]
Definition: app_skel.c:691
static int app_exec(struct ast_channel *chan, const char *data)
Definition: app_skel.c:379
struct aco_type * sound_options[]
Definition: app_skel.c:260
static struct ast_str * prompt
Definition: asterisk.c:2761
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:363
#define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn)
Allocate and initialize a list container.
Definition: astobj2.h:1327
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
@ ACO_EXACT
void aco_info_destroy(struct aco_info *info)
Destroy an initialized aco_info struct.
@ ACO_PROCESS_ERROR
Their was an error and no changes were applied.
#define STRFLDSET(type,...)
Convert a struct and a list of stringfield fields to an argument list of field offsets.
int aco_info_init(struct aco_info *info)
Initialize an aco_info structure.
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
#define aco_option_register(info, name, matchtype, types, default_val, opt_type, flags,...)
Register a config option.
@ OPT_UINT_T
Type for default option handler for unsigned integers.
@ OPT_STRINGFIELD_T
Type for default option handler for stringfields.
#define aco_option_register_custom(info, name, matchtype, types, default_val, handler, flags)
Register a config option.
enum aco_process_status aco_process_config(struct aco_info *info, int reload)
Process a config info via the options registered with an aco_info.
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626
int error(const char *format,...)
Definition: utils/frame.c:999
#define ARRAY_LEN(a)
Definition: utils.h:661

◆ play_files_helper()

static void play_files_helper ( struct ast_channel chan,
const char *  prompts 
)
static
Examples
app_skel.c.

Definition at line 369 of file app_skel.c.

370 {
371  char *prompt, *rest = ast_strdupa(prompts);
372 
373  ast_stopstream(chan);
374  while ((prompt = strsep(&rest, "&")) && !ast_stream_and_wait(chan, prompt, "")) {
375  ast_stopstream(chan);
376  }
377 }
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:222
int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
stream file until digit If the file name is non-empty, try to play it.
Definition: file.c:1855
char * strsep(char **str, const char *delims)

References ast_stopstream(), ast_strdupa, ast_stream_and_wait(), prompt, and strsep().

Referenced by app_exec().

◆ reload_module()

static int reload_module ( void  )
static
Examples
app_skel.c.

Definition at line 697 of file app_skel.c.

698 {
699  if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
701  }
702 
703  return 0;
704 }

References aco_process_config(), ACO_PROCESS_ERROR, and AST_MODULE_LOAD_DECLINE.

◆ skel_config_alloc()

static void * skel_config_alloc ( void  )
static

Allocate a skel_config to hold a snapshot of the complete results of parsing a config.

Examples
app_skel.c.

Definition at line 566 of file app_skel.c.

567 {
568  struct skel_config *cfg;
569 
570  if (!(cfg = ao2_alloc(sizeof(*cfg), skel_config_destructor))) {
571  return NULL;
572  }
573 
574  /* Allocate/initialize memory */
575  if (!(cfg->global = ao2_alloc(sizeof(*cfg->global), skel_global_config_destructor))) {
576  goto error;
577  }
578 
579  if (ast_string_field_init(cfg->global, 128)) {
580  goto error;
581  }
582 
585  if (!cfg->levels) {
586  goto error;
587  }
588 
589  return cfg;
590 error:
591  ao2_ref(cfg, -1);
592  return NULL;
593 }
static int skel_level_hash(const void *obj, const int flags)
Definition: app_skel.c:332
#define LEVEL_BUCKETS
Definition: app_skel.c:201
static void skel_global_config_destructor(void *obj)
Definition: app_skel.c:297
static void skel_config_destructor(void *obj)
Definition: app_skel.c:559
static int skel_level_cmp(void *obj, void *arg, int flags)
Definition: app_skel.c:339
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
struct skel_global_config * global
Definition: app_skel.c:210
struct ao2_container * levels
Definition: app_skel.c:211

References ao2_alloc, AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_hash, ao2_ref, ast_string_field_init, error(), skel_config::global, LEVEL_BUCKETS, skel_config::levels, NULL, skel_config_destructor(), skel_global_config_destructor(), skel_level_cmp(), and skel_level_hash().

◆ skel_config_destructor()

static void skel_config_destructor ( void *  obj)
static
Examples
app_skel.c.

Definition at line 559 of file app_skel.c.

560 {
561  struct skel_config *cfg = obj;
562  ao2_cleanup(cfg->global);
563  ao2_cleanup(cfg->levels);
564 }

References ao2_cleanup, skel_config::global, and skel_config::levels.

Referenced by skel_config_alloc().

◆ skel_find_or_create_state()

static void* skel_find_or_create_state ( const char *  category)
static

Look up an existing state object, or create a new one.

Examples
app_skel.c.

Definition at line 521 of file app_skel.c.

522 {
524  RAII_VAR(struct skel_level *, level, NULL, ao2_cleanup);
525  if (!cfg || !cfg->levels || !(level = ao2_find(cfg->levels, category, OBJ_KEY))) {
526  return skel_state_alloc(category);
527  }
528  ao2_ref(level->state, +1);
529  return level->state;
530 }
static struct skel_level * skel_state_alloc(const char *name)
Definition: app_skel.c:497

References ao2_cleanup, ao2_find, ao2_global_obj_ref, ao2_ref, globals, NULL, OBJ_KEY, RAII_VAR, and skel_state_alloc().

Referenced by skel_level_alloc().

◆ skel_game_alloc()

static struct skel_current_game* skel_game_alloc ( struct skel_level level)
static
Examples
app_skel.c.

Definition at line 314 of file app_skel.c.

315 {
316  struct skel_current_game *game;
317  if (!(game = ao2_alloc(sizeof(struct skel_current_game), skel_game_destructor))) {
318  return NULL;
319  }
320  ao2_ref(level, +1);
321  game->level_info = level;
322  return game;
323 }
static void skel_game_destructor(void *obj)
Definition: app_skel.c:303

References ao2_alloc, ao2_ref, skel_current_game::level_info, NULL, and skel_game_destructor().

Referenced by app_exec().

◆ skel_game_destructor()

static void skel_game_destructor ( void *  obj)
static
Examples
app_skel.c.

Definition at line 303 of file app_skel.c.

304 {
305  struct skel_current_game *game = obj;
306  ao2_cleanup(game->level_info);
307 }

References ao2_cleanup, and skel_current_game::level_info.

Referenced by skel_game_alloc().

◆ skel_global_config_destructor()

static void skel_global_config_destructor ( void *  obj)
static
Examples
app_skel.c.

Definition at line 297 of file app_skel.c.

298 {
299  struct skel_global_config *global = obj;
301 }
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:374

References ast_string_field_free_memory, and global.

Referenced by skel_config_alloc().

◆ skel_level_alloc()

static void * skel_level_alloc ( const char *  cat)
static

Allocate a skel_level based on a category in a configuration file.

Parameters
catThe category to base the level on
Returns
A void pointer to a newly allocated skel_level
Examples
app_skel.c.

Definition at line 532 of file app_skel.c.

533 {
534  struct skel_level *level;
535 
536  if (!(level = ao2_alloc(sizeof(*level), skel_level_destructor))) {
537  return NULL;
538  }
539 
540  if (ast_string_field_init(level, 128)) {
541  ao2_ref(level, -1);
542  return NULL;
543  }
544 
545  /* Since the level has state information that needs to persist between reloads,
546  * it is important to handle that here in the level's allocation function.
547  * If not separated out into its own object, the data would be destroyed on
548  * reload. */
549  if (!(level->state = skel_find_or_create_state(cat))) {
550  ao2_ref(level, -1);
551  return NULL;
552  }
553 
554  ast_string_field_set(level, name, cat);
555 
556  return level;
557 }
static void skel_level_destructor(void *obj)
Definition: app_skel.c:325
static void * skel_find_or_create_state(const char *category)
Look up an existing state object, or create a new one.
Definition: app_skel.c:521
static const char name[]
Definition: format_mp3.c:68
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521

References ao2_alloc, ao2_ref, ast_string_field_init, ast_string_field_set, name, NULL, skel_find_or_create_state(), skel_level_destructor(), and skel_level::state.

◆ skel_level_cmp()

static int skel_level_cmp ( void *  obj,
void *  arg,
int  flags 
)
static
Examples
app_skel.c.

Definition at line 339 of file app_skel.c.

340 {
341  struct skel_level *one = obj, *two = arg;
342  const char *match = (flags & OBJ_KEY) ? arg : two->name;
343  return strcasecmp(one->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
344 }
@ CMP_MATCH
Definition: astobj2.h:1027
@ CMP_STOP
Definition: astobj2.h:1028
static int match(struct ast_sockaddr *addr, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
Definition: chan_iax2.c:2312

References CMP_MATCH, CMP_STOP, match(), skel_level::name, and OBJ_KEY.

Referenced by skel_config_alloc().

◆ skel_level_destructor()

static void skel_level_destructor ( void *  obj)
static
Examples
app_skel.c.

Definition at line 325 of file app_skel.c.

326 {
327  struct skel_level *level = obj;
329  ao2_cleanup(level->state);
330 }

References ao2_cleanup, ast_string_field_free_memory, and skel_level::state.

Referenced by skel_level_alloc().

◆ skel_level_find()

static void * skel_level_find ( struct ao2_container tmp_container,
const char *  category 
)
static

Find a skel level in the specified container.

Note
This function does not look for a skel_level in the active container. It is used internally by the Config Options code to check if an level has already been added to the container that will be swapped for the live container on a successul reload.
Parameters
tmp_containerA non-active container to search for a level
categoryThe category associated with the level to check for
Return values
non-NULLThe level from the container
NULLThe level does not exist in the container
Examples
app_skel.c.

Definition at line 508 of file app_skel.c.

509 {
510  return ao2_find(tmp_container, category, OBJ_KEY);
511 }

References ao2_find, and OBJ_KEY.

◆ skel_level_hash()

static int skel_level_hash ( const void *  obj,
const int  flags 
)
static
Examples
app_skel.c.

Definition at line 332 of file app_skel.c.

333 {
334  const struct skel_level *level = obj;
335  const char *name = (flags & OBJ_KEY) ? obj : level->name;
336  return ast_str_case_hash(name);
337 }
static force_inline int attribute_pure ast_str_case_hash(const char *str)
Compute a hash value on a case-insensitive string.
Definition: strings.h:1281

References ast_str_case_hash(), name, skel_level::name, and OBJ_KEY.

Referenced by skel_config_alloc().

◆ skel_state_alloc()

static struct skel_level* skel_state_alloc ( const char *  name)
static
Examples
app_skel.c.

Definition at line 497 of file app_skel.c.

498 {
499  struct skel_level *level;
500 
501  if (!(level = ao2_alloc(sizeof(*level), skel_state_destructor))) {
502  return NULL;
503  }
504 
505  return level;
506 }
static void skel_state_destructor(void *obj)
Definition: app_skel.c:309

References ao2_alloc, NULL, and skel_state_destructor().

Referenced by skel_find_or_create_state().

◆ skel_state_destructor()

static void skel_state_destructor ( void *  obj)
static
Examples
app_skel.c.

Definition at line 309 of file app_skel.c.

310 {
311  return;
312 }

Referenced by skel_state_alloc().

◆ unload_module()

static int unload_module ( void  )
static
Examples
app_skel.c.

Definition at line 706 of file app_skel.c.

707 {
709  aco_info_destroy(&cfg_info);
713 }
#define ao2_global_obj_release(holder)
Release the ao2 object held in the global holder.
Definition: astobj2.h:859
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392

References aco_info_destroy(), ao2_cleanup, ao2_global_obj_release, app, ARRAY_LEN, ast_cli_unregister_multiple(), ast_unregister_application(), games, globals, and skel_cli.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Skeleton (sample) Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, }
static

Definition at line 725 of file app_skel.c.

◆ app

char* app = "SkelGuessNumber"
static
Examples
app_skel.c.

Definition at line 134 of file app_skel.c.

Referenced by app_exec(), and unload_module().

◆ app_opts

const struct ast_app_option app_opts[128] = { [ 'c' ] = { .flag = OPTION_CHEAT }, [ 'n' ] = { .flag = OPTION_NUMGAMES , .arg_index = OPTION_ARG_NUMGAMES + 1 }, }
static
Examples
app_skel.c.

Definition at line 134 of file app_skel.c.

Referenced by app_exec().

◆ app_skel_conf

struct aco_file app_skel_conf
Initial value:
= {
.filename = "app_skel.conf",
}
static struct aco_type sound_option
An aco_type structure to link the "sounds" category to the skel_global_config type.
Definition: app_skel.c:252
static struct aco_type level_option
An aco_type structure to link the everything but the "general" and "sounds" categories to the skel_le...
Definition: app_skel.c:269
static struct aco_type global_option
An aco_type structure to link the "general" category to the skel_global_config type.
Definition: app_skel.c:241
#define ACO_TYPES(...)
A helper macro to ensure that aco_info types always have a sentinel.
Examples
app_skel.c.

Definition at line 279 of file app_skel.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 773 of file app_skel.c.

◆ games

struct ao2_container* games
static

The container of active games.

Examples
app_skel.c.

Definition at line 290 of file app_skel.c.

Referenced by app_exec(), handle_skel_show_games(), and unload_module().

◆ global_option

struct aco_type global_option
static

An aco_type structure to link the "general" category to the skel_global_config type.

Examples
app_skel.c.

Definition at line 238 of file app_skel.c.

◆ global_options

struct aco_type* global_options[] = ACO_TYPES(&global_option)
Examples
app_skel.c.

Definition at line 249 of file app_skel.c.

◆ level_categories

const char* level_categories[]
static
Initial value:
= {
"general",
"sounds",
}
Examples
app_skel.c.

Definition at line 262 of file app_skel.c.

◆ level_option

struct aco_type level_option
static

An aco_type structure to link the everything but the "general" and "sounds" categories to the skel_level type.

Examples
app_skel.c.

Definition at line 262 of file app_skel.c.

◆ level_options

struct aco_type* level_options[] = ACO_TYPES(&level_option)
Examples
app_skel.c.

Definition at line 279 of file app_skel.c.

◆ skel_cli

struct ast_cli_entry skel_cli[]
static
Initial value:
= {
{ .handler = handle_skel_show_config , .summary = "Show app_skel global config options" ,},
{ .handler = handle_skel_show_levels , .summary = "Show app_skel levels" ,},
{ .handler = handle_skel_show_games , .summary = "Show app_skel active games" ,},
}
static char * handle_skel_show_levels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: app_skel.c:655
static char * handle_skel_show_games(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: app_skel.c:625
static char * handle_skel_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: app_skel.c:595
Examples
app_skel.c.

Definition at line 655 of file app_skel.c.

Referenced by unload_module().

◆ sound_option

struct aco_type sound_option
static

An aco_type structure to link the "sounds" category to the skel_global_config type.

Examples
app_skel.c.

Definition at line 249 of file app_skel.c.

◆ sound_options

struct aco_type* sound_options[] = ACO_TYPES(&sound_option)
Examples
app_skel.c.

Definition at line 260 of file app_skel.c.