Debian Codegraph @

apps/app_skel.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) <Year>, <Your Name Here>
5 *
6 * <Your Name Here> <<Your Email Here>>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 *
18 * Please follow coding guidelines
19 * https://docs.asterisk.org/Development/Policies-and-Procedures/Coding-Guidelines/
20 */
21
22 /*! \file
23 *
24 * \brief Skeleton application
25 *
26 * \author\verbatim <Your Name Here> <<Your Email Here>> \endverbatim
27 *
28 * This is a skeleton for development of an Asterisk application
29 * \ingroup applications
30 */
31
32 /*! \li \ref app_skel.c uses configuration file \ref app_skel.conf
33 * \addtogroup configuration_file Configuration Files
34 */
35
36 /*!
37 * \page app_skel.conf app_skel.conf
38 * \verbinclude app_skel.conf.sample
39 */
40
41 /*** MODULEINFO
42 <defaultenabled>no</defaultenabled>
43 <support_level>core</support_level>
44 ***/
45
46 #include "asterisk.h"
47
48 #include <math.h> /* log10 */
49 #include "asterisk/file.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/pbx.h"
52 #include "asterisk/module.h"
53 #include "asterisk/lock.h"
54 #include "asterisk/app.h"
55 #include "asterisk/config.h"
56 #include "asterisk/config_options.h"
57 #include "asterisk/say.h"
58 #include "asterisk/astobj2.h"
59 #include "asterisk/acl.h"
60 #include "asterisk/netsock2.h"
61 #include "asterisk/strings.h"
62 #include "asterisk/cli.h"
63
64 /*** DOCUMENTATION
65 <application name="SkelGuessNumber" language="en_US">
66 <since>
67 <version>11.0.0</version>
68 </since>
69 <synopsis>
70 An example number guessing game
71 </synopsis>
72 <syntax>
73 <parameter name="level" required="true"/>
74 <parameter name="options">
75 <optionlist>
76 <option name="c">
77 <para>The computer should cheat</para>
78 </option>
79 <option name="n">
80 <para>How many games to play before hanging up</para>
81 </option>
82 </optionlist>
83 </parameter>
84 </syntax>
85 <description>
86 <para>This simple number guessing application is a template to build other applications
87 from. It shows you the basic structure to create your own Asterisk applications.</para>
88 </description>
89 </application>
90
91 <configInfo name="app_skel" language="en_US">
92 <configFile name="app_skel.conf">
93 <configObject name="globals">
94 <since>
95 <version>12.0.0</version>
96 </since>
97 <synopsis>Options that apply globally to app_skel</synopsis>
98 <configOption name="games">
99 <since>
100 <version>11.0.0</version>
101 </since>
102 <synopsis>The number of games a single execution of SkelGuessNumber will play</synopsis>
103 </configOption>
104 <configOption name="cheat">
105 <since>
106 <version>11.0.0</version>
107 </since>
108 <synopsis>Should the computer cheat?</synopsis>
109 <description><para>If enabled, the computer will ignore winning guesses.</para></description>
110 </configOption>
111 </configObject>
112 <configObject name="sounds">
113 <since>
114 <version>12.0.0</version>
115 </since>
116 <synopsis>Prompts for SkelGuessNumber to play</synopsis>
117 <configOption name="prompt" default="please-enter-your&amp;number&amp;queue-less-than">
118 <since>
119 <version>11.0.0</version>
120 </since>
121 <synopsis>A prompt directing the user to enter a number less than the max number</synopsis>
122 </configOption>
123 <configOption name="wrong_guess" default="vm-pls-try-again">
124 <since>
125 <version>11.0.0</version>
126 </since>
127 <synopsis>The sound file to play when a wrong guess is made</synopsis>
128 </configOption>
129 <configOption name="right_guess" default="auth-thankyou">
130 <since>
131 <version>11.0.0</version>
132 </since>
133 <synopsis>The sound file to play when a correct guess is made</synopsis>
134 </configOption>
135 <configOption name="too_low">
136 <since>
137 <version>11.0.0</version>
138 </since>
139 <synopsis>The sound file to play when a guess is too low</synopsis>
140 </configOption>
141 <configOption name="too_high">
142 <since>
143 <version>11.0.0</version>
144 </since>
145 <synopsis>The sound file to play when a guess is too high</synopsis>
146 </configOption>
147 <configOption name="lose" default="vm-goodbye">
148 <since>
149 <version>11.0.0</version>
150 </since>
151 <synopsis>The sound file to play when a player loses</synopsis>
152 </configOption>
153 </configObject>
154 <configObject name="level">
155 <since>
156 <version>12.0.0</version>
157 </since>
158 <synopsis>Defined levels for the SkelGuessNumber game</synopsis>
159 <configOption name="max_number">
160 <since>
161 <version>11.0.0</version>
162 </since>
163 <synopsis>The maximum in the range of numbers to guess (1 is the implied minimum)</synopsis>
164 </configOption>
165 <configOption name="max_guesses">
166 <since>
167 <version>11.22.0</version>
168 <version>13.9.0</version>
169 </since>
170 <synopsis>The maximum number of guesses before a game is considered lost</synopsis>
171 </configOption>
172 </configObject>
173 </configFile>
174 </configInfo>
175 ***/
176
177 static char *app = "SkelGuessNumber";
178
179 enum option_flags {
180 OPTION_CHEAT = (1 << 0),
181 OPTION_NUMGAMES = (1 << 1),
182 };
183
184 enum option_args {
185 OPTION_ARG_NUMGAMES,
186 /* This *must* be the last value in this enum! */
187 OPTION_ARG_ARRAY_SIZE,
188 };
189
190 AST_APP_OPTIONS(app_opts,{
191 AST_APP_OPTION('c', OPTION_CHEAT),
192 AST_APP_OPTION_ARG('n', OPTION_NUMGAMES, OPTION_ARG_NUMGAMES),
193 });
194
195 /*! \brief A structure to hold global configuration-related options */
196 struct skel_global_config {
197 AST_DECLARE_STRING_FIELDS(
198 AST_STRING_FIELD(prompt); /*!< The comma-separated list of sounds to prompt to enter a number */
199 AST_STRING_FIELD(wrong); /*!< The comma-separated list of sounds to indicate a wrong guess */
200 AST_STRING_FIELD(right); /*!< The comma-separated list of sounds to indicate a right guess */
201 AST_STRING_FIELD(high); /*!< The comma-separated list of sounds to indicate a high guess */
202 AST_STRING_FIELD(low); /*!< The comma-separated list of sounds to indicate a low guess */
203 AST_STRING_FIELD(lose); /*!< The comma-separated list of sounds to indicate a lost game */
204 );
205 uint32_t num_games; /*!< The number of games to play before hanging up */
206 unsigned char cheat:1; /*!< Whether the computer can cheat or not */
207 };
208
209 /*! \brief A structure to maintain level state across reloads */
210 struct skel_level_state {
211 uint32_t wins; /*!< How many wins for this level */
212 uint32_t losses; /*!< How many losses for this level */
213 double avg_guesses; /*!< The average number of guesses to win for this level */
214 };
215
216 /*! \brief Object to hold level config information.
217 * \note This object should hold a reference to an object that holds state across reloads.
218 * The other fields are just examples of the kind of data that might be stored in an level.
219 */
220 struct skel_level {
221 AST_DECLARE_STRING_FIELDS(
222 AST_STRING_FIELD(name); /*!< The name of the level */
223 );
224 uint32_t max_num; /*!< The upper value on th range of numbers to guess */
225 uint32_t max_guesses; /*!< The maximum number of guesses before losing */
226 struct skel_level_state *state; /*!< A pointer to level state that must exist across all reloads */
227 };
228
229 /*! \brief Information about a currently running set of games
230 * \note Because we want to be able to show true running information about the games
231 * regardless of whether or not a reload has modified what the level looks like, it
232 * is important to either copy the information we need from the level to the
233 * current_game struct, or as we do here, store a reference to the level as it is for
234 * the running game.
235 */
236 struct skel_current_game {
237 uint32_t total_games; /*! The total number of games for this call to the app */
238 uint32_t games_left; /*! How many games are left to play in this set */
239 uint32_t cheat; /*! Whether or not cheating was enabled for the game */
240 struct skel_level *level_info; /*! The level information for the running game */
241 };
242
243 /* Treat the levels as an array--there won't be many and this will maintain the order */
244 #define LEVEL_BUCKETS 1
245
246 /*! \brief A container that holds all config-related information
247 * \note This object should contain a pointer to structs for global data and containers for
248 * any levels that are configured. Objects of this type will be swapped out on reload. If an
249 * level needs to maintain state across reloads, it needs to allocate a refcounted object to
250 * hold that state and ensure that a reference is passed to that state when creating a new
251 * level for reload. */
252 struct skel_config {
253 struct skel_global_config *global;
254 struct ao2_container *levels;
255 };
256
257 /* Config Options API callbacks */
258
259 /*! \brief Allocate a skel_config to hold a snapshot of the complete results of parsing a config
260 * \internal
261 * \returns A void pointer to a newly allocated skel_config
262 */
263 static void *skel_config_alloc(void);
264
265 /*! \brief Allocate a skel_level based on a category in a configuration file
266 * \param cat The category to base the level on
267 * \returns A void pointer to a newly allocated skel_level
268 */
269 static void *skel_level_alloc(const char *cat);
270
271 /*! \brief Find a skel level in the specified container
272 * \note This function *does not* look for a skel_level in the active container. It is used
273 * internally by the Config Options code to check if an level has already been added to the
274 * container that will be swapped for the live container on a successful reload.
275 *
276 * \param tmp_container A non-active container to search for a level
277 * \param category The category associated with the level to check for
278 * \retval non-NULL The level from the container
279 * \retval NULL The level does not exist in the container
280 */
281 static void *skel_level_find(struct ao2_container *tmp_container, const char *category);
282
283 /*! \brief An aco_type structure to link the "general" category to the skel_global_config type */
284 static struct aco_type global_option = {
285 .type = ACO_GLOBAL,
286 .name = "globals",
287 .item_offset = offsetof(struct skel_config, global),
288 .category_match = ACO_WHITELIST_EXACT,
289 .category = "general",
290 };
291
292 struct aco_type *global_options[] = ACO_TYPES(&global_option);
293
294 /*! \brief An aco_type structure to link the "sounds" category to the skel_global_config type */
295 static struct aco_type sound_option = {
296 .type = ACO_GLOBAL,
297 .name = "sounds",
298 .item_offset = offsetof(struct skel_config, global),
299 .category_match = ACO_WHITELIST_EXACT,
300 .category = "sounds",
301 };
302
303 struct aco_type *sound_options[] = ACO_TYPES(&sound_option);
304
305 static const char *level_categories[] = {
306 "general",
307 "sounds",
308 NULL,
309 };
310
311 /*! \brief An aco_type structure to link the everything but the "general" and "sounds" categories to the skel_level type */
312 static struct aco_type level_option = {
313 .type = ACO_ITEM,
314 .name = "level",
315 .category_match = ACO_BLACKLIST_ARRAY,
316 .category = (const char *)level_categories,
317 .item_alloc = skel_level_alloc,
318 .item_find = skel_level_find,
319 .item_offset = offsetof(struct skel_config, levels),
320 };
321
322 struct aco_type *level_options[] = ACO_TYPES(&level_option);
323
324 struct aco_file app_skel_conf = {
325 .filename = "app_skel.conf",
326 .types = ACO_TYPES(&global_option, &sound_option, &level_option),
327 };
328
329 /*! \brief A global object container that will contain the skel_config that gets swapped out on reloads */
330 static AO2_GLOBAL_OBJ_STATIC(globals);
331
332 /*! \brief The container of active games */
333 static struct ao2_container *games;
334
335 /*! \brief Register information about the configs being processed by this module */
336 CONFIG_INFO_STANDARD(cfg_info, globals, skel_config_alloc,
337 .files = ACO_FILES(&app_skel_conf),
338 );
339
340 static void skel_global_config_destructor(void *obj)
341 {
342 struct skel_global_config *global = obj;
343 ast_string_field_free_memory(global);
344 }
345
346 static void skel_game_destructor(void *obj)
347 {
348 struct skel_current_game *game = obj;
349 ao2_cleanup(game->level_info);
350 }
351
352 static void skel_state_destructor(void *obj)
353 {
354 return;
355 }
356
357 static struct skel_current_game *skel_game_alloc(struct skel_level *level)
358 {
359 struct skel_current_game *game;
360 if (!(game = ao2_alloc(sizeof(struct skel_current_game), skel_game_destructor))) {
361 return NULL;
362 }
363 ao2_ref(level, +1);
364 game->level_info = level;
365 return game;
366 }
367
368 static void skel_level_destructor(void *obj)
369 {
370 struct skel_level *level = obj;
371 ast_string_field_free_memory(level);
372 ao2_cleanup(level->state);
373 }
374
375 static int skel_level_hash(const void *obj, const int flags)
376 {
377 const struct skel_level *level = obj;
378 const char *name = (flags & OBJ_KEY) ? obj : level->name;
379 return ast_str_case_hash(name);
380 }
381
382 static int skel_level_cmp(void *obj, void *arg, int flags)
383 {
384 struct skel_level *one = obj, *two = arg;
385 const char *match = (flags & OBJ_KEY) ? arg : two->name;
386 return strcasecmp(one->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
387 }
388
389 /*! \brief A custom bitfield handler
390 * \internal
391 * \note It is not possible to take the address of a bitfield, therefor all
392 * bitfields in the config struct will have to use a custom handler
393 * \param opt The opaque config option
394 * \param var The ast_variable containing the option name and value
395 * \param obj The object registered for this option type
396 * \retval 0 Success
397 * \retval non-zero Failure
398 */
399 static int custom_bitfield_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
400 {
401 struct skel_global_config *global = obj;
402
403 if (!strcasecmp(var->name, "cheat")) {
404 global->cheat = ast_true(var->value);
405 } else {
406 return -1;
407 }
408
409 return 0;
410 }
411
412 static void play_files_helper(struct ast_channel *chan, const char *prompts)
413 {
414 char *prompt, *rest = ast_strdupa(prompts);
415
416 ast_stopstream(chan);
417 while ((prompt = ast_strsep(&rest, '&', AST_STRSEP_STRIP | AST_STRSEP_TRIM))
418 && !ast_stream_and_wait(chan, prompt, "")) {
419 ast_stopstream(chan);
420 }
421 }
422
423 static int app_exec(struct ast_channel *chan, const char *data)
424 {
425 int win = 0;
426 uint32_t guesses;
427 RAII_VAR(struct skel_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
428 RAII_VAR(struct skel_level *, level, NULL, ao2_cleanup);
429 RAII_VAR(struct skel_current_game *, game, NULL, ao2_cleanup);
430 char *parse, *opts[OPTION_ARG_ARRAY_SIZE];
431 struct ast_flags flags;
432 AST_DECLARE_APP_ARGS(args,
433 AST_APP_ARG(level);
434 AST_APP_ARG(options);
435 );
436
437 if (!cfg) {
438 ast_log(LOG_ERROR, "Couldn't access configuration data!\n");
439 return -1;
440 }
441
442 if (ast_strlen_zero(data)) {
443 ast_log(LOG_WARNING, "%s requires an argument (level[,options])\n", app);
444 return -1;
445 }
446
447 /* We need to make a copy of the input string if we are going to modify it! */
448 parse = ast_strdupa(data);
449
450 AST_STANDARD_APP_ARGS(args, parse);
451
452 if (args.argc == 2) {
453 ast_app_parse_options(app_opts, &flags, opts, args.options);
454 }
455
456 if (ast_strlen_zero(args.level)) {
457 ast_log(LOG_ERROR, "%s requires a level argument\n", app);
458 return -1;
459 }
460
461 if (!(level = ao2_find(cfg->levels, args.level, OBJ_KEY))) {
462 ast_log(LOG_ERROR, "Unknown level: %s\n", args.level);
463 return -1;
464 }
465
466 if (!(game = skel_game_alloc(level))) {
467 return -1;
468 }
469
470 ao2_link(games, game);
471
472 /* Use app-specified values, or the options specified in [general] if they aren't passed to the app */
473 if (!ast_test_flag(&flags, OPTION_NUMGAMES) ||
474 ast_strlen_zero(opts[OPTION_ARG_NUMGAMES]) ||
475 ast_parse_arg(opts[OPTION_ARG_NUMGAMES], PARSE_UINT32, &game->total_games)) {
476 game->total_games = cfg->global->num_games;
477 }
478 game->games_left = game->total_games;
479 game->cheat = ast_test_flag(&flags, OPTION_CHEAT) || cfg->global->cheat;
480
481 for (game->games_left = game->total_games; game->games_left; game->games_left--) {
482 uint32_t num = ast_random() % level->max_num; /* random number between 0 and level->max_num */
483
484 ast_debug(1, "They should totally should guess %u\n", num);
485
486 /* Play the prompt */
487 play_files_helper(chan, cfg->global->prompt);
488 ast_say_number(chan, level->max_num, "", ast_channel_language(chan), "");
489
490 for (guesses = 0; guesses < level->max_guesses; guesses++) {
491 size_t buflen = log10(level->max_num) + 1;
492 char buf[buflen];
493 int guess;
494 buf[buflen] = '\0';
495
496 /* Read the number pressed */
497 ast_readstring(chan, buf, buflen - 1, 2000, 10000, "");
498 if (ast_parse_arg(buf, PARSE_INT32 | PARSE_IN_RANGE, &guess, 0, level->max_num)) {
499 if (guesses < level->max_guesses - 1) {
500 play_files_helper(chan, cfg->global->wrong);
501 }
502 continue;
503 }
504
505 /* Inform whether the guess was right, low, or high */
506 if (guess == num && !game->cheat) {
507 /* win */
508 win = 1;
509 play_files_helper(chan, cfg->global->right);
510 guesses++;
511 break;
512 } else if (guess < num) {
513 play_files_helper(chan, cfg->global->low);
514 } else {
515 play_files_helper(chan, cfg->global->high);
516 }
517
518 if (guesses < level->max_guesses - 1) {
519 play_files_helper(chan, cfg->global->wrong);
520 }
521 }
522
523 /* Process game stats */
524 ao2_lock(level->state);
525 if (win) {
526 ++level->state->wins;
527 level->state->avg_guesses = ((level->state->wins - 1) * level->state->avg_guesses + guesses) / level->state->wins;
528 } else {
529 /* lose */
530 level->state->losses++;
531 play_files_helper(chan, cfg->global->lose);
532 }
533 ao2_unlock(level->state);
534 }
535
536 ao2_unlink(games, game);
537
538 return 0;
539 }
540
541 static struct skel_level *skel_state_alloc(const char *name)
542 {
543 struct skel_level *level;
544
545 if (!(level = ao2_alloc(sizeof(*level), skel_state_destructor))) {
546 return NULL;
547 }
548
549 return level;
550 }
551
552 static void *skel_level_find(struct ao2_container *tmp_container, const char *category)
553 {
554 return ao2_find(tmp_container, category, OBJ_KEY);
555 }
556
557 /*! \brief Look up an existing state object, or create a new one
558 * \internal
559 * \note Since the reload code will create a new level from scratch, it
560 * is important for any state that must persist between reloads to be
561 * in a separate refcounted object. This function allows the level alloc
562 * function to get a ref to an existing state object if it exists,
563 * otherwise it will return a reference to a newly allocated state object.
564 */
565 static void *skel_find_or_create_state(const char *category)
566 {
567 RAII_VAR(struct skel_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
568 RAII_VAR(struct skel_level *, level, NULL, ao2_cleanup);
569 if (!cfg || !cfg->levels || !(level = ao2_find(cfg->levels, category, OBJ_KEY))) {
570 return skel_state_alloc(category);
571 }
572 ao2_ref(level->state, +1);
573 return level->state;
574 }
575
576 static void *skel_level_alloc(const char *cat)
577 {
578 struct skel_level *level;
579
580 if (!(level = ao2_alloc(sizeof(*level), skel_level_destructor))) {
581 return NULL;
582 }
583
584 if (ast_string_field_init(level, 128)) {
585 ao2_ref(level, -1);
586 return NULL;
587 }
588
589 /* Since the level has state information that needs to persist between reloads,
590 * it is important to handle that here in the level's allocation function.
591 * If not separated out into its own object, the data would be destroyed on
592 * reload. */
593 if (!(level->state = skel_find_or_create_state(cat))) {
594 ao2_ref(level, -1);
595 return NULL;
596 }
597
598 ast_string_field_set(level, name, cat);
599
600 return level;
601 }
602
603 static void skel_config_destructor(void *obj)
604 {
605 struct skel_config *cfg = obj;
606 ao2_cleanup(cfg->global);
607 ao2_cleanup(cfg->levels);
608 }
609
610 static void *skel_config_alloc(void)
611 {
612 struct skel_config *cfg;
613
614 if (!(cfg = ao2_alloc(sizeof(*cfg), skel_config_destructor))) {
615 return NULL;
616 }
617
618 /* Allocate/initialize memory */
619 if (!(cfg->global = ao2_alloc(sizeof(*cfg->global), skel_global_config_destructor))) {
620 goto error;
621 }
622
623 if (ast_string_field_init(cfg->global, 128)) {
624 goto error;
625 }
626
627 cfg->levels = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, LEVEL_BUCKETS,
628 skel_level_hash, NULL, skel_level_cmp);
629 if (!cfg->levels) {
630 goto error;
631 }
632
633 return cfg;
634 error:
635 ao2_ref(cfg, -1);
636 return NULL;
637 }
638
639 static char *handle_skel_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
640 {
641 RAII_VAR(struct skel_config *, cfg, NULL, ao2_cleanup);
642
643 switch(cmd) {
644 case CLI_INIT:
645 e->command = "skel show config";
646 e->usage =
647 "Usage: skel show config\n"
648 " List app_skel global config\n";
649 return NULL;
650 case CLI_GENERATE:
651 return NULL;
652 }
653
654 if (!(cfg = ao2_global_obj_ref(globals)) || !cfg->global) {
655 return NULL;
656 }
657
658 ast_cli(a->fd, "games per call: %u\n", cfg->global->num_games);
659 ast_cli(a->fd, "computer cheats: %s\n", AST_CLI_YESNO(cfg->global->cheat));
660 ast_cli(a->fd, "\n");
661 ast_cli(a->fd, "Sounds\n");
662 ast_cli(a->fd, " prompt: %s\n", cfg->global->prompt);
663 ast_cli(a->fd, " wrong guess: %s\n", cfg->global->wrong);
664 ast_cli(a->fd, " right guess: %s\n", cfg->global->right);
665
666 return CLI_SUCCESS;
667 }
668
669 static char *handle_skel_show_games(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
670 {
671 struct ao2_iterator iter;
672 struct skel_current_game *game;
673
674 switch(cmd) {
675 case CLI_INIT:
676 e->command = "skel show games";
677 e->usage =
678 "Usage: skel show games\n"
679 " List app_skel active games\n";
680 return NULL;
681 case CLI_GENERATE:
682 return NULL;
683 }
684
685 #define SKEL_FORMAT "%-15.15s %-15.15s %-15.15s\n"
686 #define SKEL_FORMAT1 "%-15.15s %-15u %-15u\n"
687 ast_cli(a->fd, SKEL_FORMAT, "Level", "Total Games", "Games Left");
688 iter = ao2_iterator_init(games, 0);
689 while ((game = ao2_iterator_next(&iter))) {
690 ast_cli(a->fd, SKEL_FORMAT1, game->level_info->name, game->total_games, game->games_left);
691 ao2_ref(game, -1);
692 }
693 ao2_iterator_destroy(&iter);
694 #undef SKEL_FORMAT
695 #undef SKEL_FORMAT1
696 return CLI_SUCCESS;
697 }
698
699 static char *handle_skel_show_levels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
700 {
701 RAII_VAR(struct skel_config *, cfg, NULL, ao2_cleanup);
702 struct ao2_iterator iter;
703 struct skel_level *level;
704
705 switch(cmd) {
706 case CLI_INIT:
707 e->command = "skel show levels";
708 e->usage =
709 "Usage: skel show levels\n"
710 " List the app_skel levels\n";
711 return NULL;
712 case CLI_GENERATE:
713 return NULL;
714 }
715
716 if (!(cfg = ao2_global_obj_ref(globals)) || !cfg->levels) {
717 return NULL;
718 }
719
720 #define SKEL_FORMAT "%-15.15s %-11.11s %-12.12s %-8.8s %-8.8s %-12.12s\n"
721 #define SKEL_FORMAT1 "%-15.15s %-11u %-12u %-8u %-8u %-8f\n"
722 ast_cli(a->fd, SKEL_FORMAT, "Name", "Max number", "Max Guesses", "Wins", "Losses", "Avg Guesses");
723 iter = ao2_iterator_init(cfg->levels, 0);
724 while ((level = ao2_iterator_next(&iter))) {
725 ast_cli(a->fd, SKEL_FORMAT1, level->name, level->max_num, level->max_guesses, level->state->wins, level->state->losses, level->state->avg_guesses);
726 ao2_ref(level, -1);
727 }
728 ao2_iterator_destroy(&iter);
729 #undef SKEL_FORMAT
730 #undef SKEL_FORMAT1
731
732 return CLI_SUCCESS;
733 }
734
735 static struct ast_cli_entry skel_cli[] = {
736 AST_CLI_DEFINE(handle_skel_show_config, "Show app_skel global config options"),
737 AST_CLI_DEFINE(handle_skel_show_levels, "Show app_skel levels"),
738 AST_CLI_DEFINE(handle_skel_show_games, "Show app_skel active games"),
739 };
740
741 static int reload_module(void)
742 {
743 if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
744 return AST_MODULE_LOAD_DECLINE;
745 }
746
747 return 0;
748 }
749
750 static int unload_module(void)
751 {
752 ast_cli_unregister_multiple(skel_cli, ARRAY_LEN(skel_cli));
753 aco_info_destroy(&cfg_info);
754 ao2_global_obj_release(globals);
755 ao2_cleanup(games);
756 return ast_unregister_application(app);
757 }
758
759 /*!
760 * \brief Load the module
761 *
762 * Module loading including tests for configuration or dependencies.
763 * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
764 * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
765 * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
766 * configuration file or other non-critical problem return
767 * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
768 */
769 static int load_module(void)
770 {
771 if (aco_info_init(&cfg_info)) {
772 goto error;
773 }
774
775 games = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL);
776 if (!games) {
777 goto error;
778 }
779
780 /* Global options */
781 aco_option_register(&cfg_info, "games", ACO_EXACT, global_options, "3", OPT_UINT_T, 0, FLDSET(struct skel_global_config, num_games));
782 aco_option_register_custom(&cfg_info, "cheat", ACO_EXACT, global_options, "no", custom_bitfield_handler, 0);
783
784 /* Sound options */
785 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));
786 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));
787 aco_option_register(&cfg_info, "right_guess", ACO_EXACT, sound_options, "auth-thankyou", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, right));
788 aco_option_register(&cfg_info, "too_high", ACO_EXACT, sound_options, "high", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, high));
789 aco_option_register(&cfg_info, "too_low", ACO_EXACT, sound_options, "low", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, low));
790 aco_option_register(&cfg_info, "lose", ACO_EXACT, sound_options, "vm-goodbye", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, lose));
791
792 /* Level options */
793 aco_option_register(&cfg_info, "max_number", ACO_EXACT, level_options, NULL, OPT_UINT_T, 0, FLDSET(struct skel_level, max_num));
794 aco_option_register(&cfg_info, "max_guesses", ACO_EXACT, level_options, NULL, OPT_UINT_T, 0, FLDSET(struct skel_level, max_guesses));
795
796 if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
797 goto error;
798 }
799
800 ast_cli_register_multiple(skel_cli, ARRAY_LEN(skel_cli));
801 if (ast_register_application_xml(app, app_exec)) {
802 goto error;
803 }
804 return AST_MODULE_LOAD_SUCCESS;
805
806 error:
807 aco_info_destroy(&cfg_info);
808 ao2_cleanup(games);
809 return AST_MODULE_LOAD_DECLINE;
810 }
811
812 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Skeleton (sample) Application",
813 .support_level = AST_MODULE_SUPPORT_CORE,
814 .load = load_module,
815 .unload = unload_module,
816 .reload = reload_module,
817 );