Asterisk - The Open Source Telephony Project  21.4.1
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"

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.
 
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.
 
static int custom_bitfield_handler (const struct aco_option *opt, struct ast_variable *var, void *obj)
 A custom bitfield handler.
 
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.
 
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.
 
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 = "da6642af068ee5e6490c5b1d2cc1d238" , .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.
 
static struct aco_type global_option
 An aco_type structure to link the "general" category to the skel_global_config type.
 
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.
 
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.
 
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.

Function Documentation

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 726 of file app_skel.c.

References aco_info_destroy(), aco_info_init(), aco_option_register, aco_option_register_custom, aco_process_config(), ACO_PROCESS_ERROR, AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_list, ast_cli_register_multiple, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_register_application_xml, custom_bitfield_handler(), FLDSET, OPT_STRINGFIELD_T, OPT_UINT_T, and STRFLDSET.

727 {
728  if (aco_info_init(&cfg_info)) {
729  goto error;
730  }
731 
733  if (!games) {
734  goto error;
735  }
736 
737  /* Global options */
738  aco_option_register(&cfg_info, "games", ACO_EXACT, global_options, "3", OPT_UINT_T, 0, FLDSET(struct skel_global_config, num_games));
739  aco_option_register_custom(&cfg_info, "cheat", ACO_EXACT, global_options, "no", custom_bitfield_handler, 0);
740 
741  /* Sound options */
742  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));
743  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));
744  aco_option_register(&cfg_info, "right_guess", ACO_EXACT, sound_options, "auth-thankyou", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, right));
745  aco_option_register(&cfg_info, "too_high", ACO_EXACT, sound_options, "high", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, high));
746  aco_option_register(&cfg_info, "too_low", ACO_EXACT, sound_options, "low", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, low));
747  aco_option_register(&cfg_info, "lose", ACO_EXACT, sound_options, "vm-goodbye", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, lose));
748 
749  /* Level options */
750  aco_option_register(&cfg_info, "max_number", ACO_EXACT, level_options, NULL, OPT_UINT_T, 0, FLDSET(struct skel_level, max_num));
751  aco_option_register(&cfg_info, "max_guesses", ACO_EXACT, level_options, NULL, OPT_UINT_T, 0, FLDSET(struct skel_level, max_guesses));
752 
753  if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
754  goto error;
755  }
756 
757  ast_cli_register_multiple(skel_cli, ARRAY_LEN(skel_cli));
758  if (ast_register_application_xml(app, app_exec)) {
759  goto error;
760  }
762 
763 error:
764  aco_info_destroy(&cfg_info);
765  ao2_cleanup(games);
767 }
#define aco_option_register_custom(info, name, matchtype, types, default_val, handler, flags)
Register a config option.
#define aco_option_register(info, name, matchtype, types, default_val, opt_type, flags,...)
Register a config option.
#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
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.
static struct ao2_container * games
The container of active games.
Definition: app_skel.c:290
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
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
int aco_info_init(struct aco_info *info)
Initialize an aco_info structure.
Type for default option handler for unsigned integers.
Their was an error and no changes were applied.
Object to hold level config information.
Definition: app_skel.c:177
void aco_info_destroy(struct aco_info *info)
Destroy an initialized aco_info struct.
#define STRFLDSET(type,...)
Convert a struct and a list of stringfield fields to an argument list of field offsets.
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
A structure to hold global configuration-related options.
Definition: app_skel.c:153
Type for default option handler for stringfields.
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
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 533 of file app_skel.c.

References ao2_ref, ast_string_field_init, ast_string_field_set, skel_find_or_create_state(), and skel_level::state.

534 {
535  struct skel_level *level;
536 
537  if (!(level = ao2_alloc(sizeof(*level), skel_level_destructor))) {
538  return NULL;
539  }
540 
541  if (ast_string_field_init(level, 128)) {
542  ao2_ref(level, -1);
543  return NULL;
544  }
545 
546  /* Since the level has state information that needs to persist between reloads,
547  * it is important to handle that here in the level's allocation function.
548  * If not separated out into its own object, the data would be destroyed on
549  * reload. */
550  if (!(level->state = skel_find_or_create_state(cat))) {
551  ao2_ref(level, -1);
552  return NULL;
553  }
554 
555  ast_string_field_set(level, name, cat);
556 
557  return level;
558 }
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct skel_level_state * state
Definition: app_skel.c:183
Object to hold level config information.
Definition: app_skel.c:177
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:522
const ast_string_field name
Definition: app_skel.c:180
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521
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 509 of file app_skel.c.

References OBJ_KEY.

510 {
511  return ao2_find(tmp_container, category, OBJ_KEY);
512 }
#define OBJ_KEY
Definition: astobj2.h:1151

Variable Documentation

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
#define ACO_TYPES(...)
A helper macro to ensure that aco_info types always have a sentinel.
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

Definition at line 281 of file app_skel.c.

const char* level_categories[]
static
Initial value:
= {
"general",
"sounds",
NULL,
}

Definition at line 262 of file app_skel.c.

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" ,},
}

Definition at line 692 of file app_skel.c.