app_skel.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) <Year>, <Your Name Here>
  5. *
  6. * <Your Name Here> <<You 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. /*! \file
  19. *
  20. * \brief Skeleton application
  21. *
  22. * This is a skeleton for development of an Asterisk application */
  23. * \ingroup applications
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/file.h"
  32. #include "asterisk/logger.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/lock.h"
  37. #include "asterisk/app.h"
  38. static char *tdesc = "Trivial skeleton Application";
  39. static char *app = "Skel";
  40. static char *synopsis =
  41. "Skeleton application.";
  42. static char *descrip = "This application is a template to build other applications from.\n"
  43. " It shows you the basic structure to create your own Asterisk applications.\n";
  44. #define OPTION_A (1 << 0) /* Option A */
  45. #define OPTION_B (1 << 1) /* Option B(n) */
  46. #define OPTION_C (1 << 2) /* Option C(str) */
  47. #define OPTION_NULL (1 << 3) /* Dummy Termination */
  48. AST_DECLARE_OPTIONS(app_opts,{
  49. ['a'] = { OPTION_A },
  50. ['b'] = { OPTION_B, 1 },
  51. ['c'] = { OPTION_C, 2 }
  52. });
  53. STANDARD_LOCAL_USER;
  54. LOCAL_USER_DECL;
  55. static int app_exec(struct ast_channel *chan, void *data)
  56. {
  57. int res = 0;
  58. struct ast_flags flags;
  59. struct localuser *u;
  60. char *options=NULL;
  61. char *dummy = NULL;
  62. char *args;
  63. int argc = 0;
  64. char *opts[2];
  65. char *argv[2];
  66. if (ast_strlen_zero(data)) {
  67. ast_log(LOG_WARNING, "%s requires an argument (dummy|[options])\n",app);
  68. LOCAL_USER_REMOVE(u);
  69. return -1;
  70. }
  71. LOCAL_USER_ADD(u);
  72. /* Do our thing here */
  73. /* We need to make a copy of the input string if we are going to modify it! */
  74. args = ast_strdupa(data);
  75. if (!args) {
  76. ast_log(LOG_ERROR, "Out of memory!\n");
  77. LOCAL_USER_REMOVE(u);
  78. return -1;
  79. }
  80. if ((argc = ast_app_separate_args(args, '|', argv, sizeof(argv) / sizeof(argv[0])))) {
  81. dummy = argv[0];
  82. options = argv[1];
  83. ast_parseoptions(app_opts, &flags, opts, options);
  84. }
  85. if (!ast_strlen_zero(dummy))
  86. ast_log(LOG_NOTICE, "Dummy value is : %s\n", dummy);
  87. if (ast_test_flag(&flags, OPTION_A))
  88. ast_log(LOG_NOTICE, "Option A is set\n");
  89. if (ast_test_flag(&flags, OPTION_B))
  90. ast_log(LOG_NOTICE,"Option B is set with : %s\n", opts[0] ? opts[0] : "<unspecified>");
  91. if (ast_test_flag(&flags, OPTION_C))
  92. ast_log(LOG_NOTICE,"Option C is set with : %s\n", opts[1] ? opts[1] : "<unspecified>");
  93. LOCAL_USER_REMOVE(u);
  94. return res;
  95. }
  96. int unload_module(void)
  97. {
  98. int res;
  99. res = ast_unregister_application(app);
  100. STANDARD_HANGUP_LOCALUSERS;
  101. return res;
  102. }
  103. int load_module(void)
  104. {
  105. return ast_register_application(app, app_exec, synopsis, descrip);
  106. }
  107. char *description(void)
  108. {
  109. return tdesc;
  110. }
  111. int usecount(void)
  112. {
  113. int res;
  114. STANDARD_USECOUNT(res);
  115. return res;
  116. }
  117. char *key()
  118. {
  119. return ASTERISK_GPL_KEY;
  120. }