app_skel.c 24 KB

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