func_devstate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@digium.com>
  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 Manually controlled blinky lights
  21. *
  22. * \author Russell Bryant <russell@digium.com>
  23. *
  24. * \ingroup functions
  25. *
  26. * \todo Delete the entry from AstDB when set to nothing like Set(DEVICE_STATE(Custom:lamp1)=)
  27. *
  28. * \note Props go out to Ahrimanes in \#asterisk for requesting this at 4:30 AM
  29. * when I couldn't sleep. :)
  30. */
  31. /*** MODULEINFO
  32. <support_level>core</support_level>
  33. ***/
  34. #include "asterisk.h"
  35. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  36. #include "asterisk/module.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/pbx.h"
  39. #include "asterisk/utils.h"
  40. #include "asterisk/linkedlists.h"
  41. #include "asterisk/devicestate.h"
  42. #include "asterisk/cli.h"
  43. #include "asterisk/astdb.h"
  44. #include "asterisk/app.h"
  45. /*** DOCUMENTATION
  46. <function name="DEVICE_STATE" language="en_US">
  47. <synopsis>
  48. Get or Set a device state.
  49. </synopsis>
  50. <syntax>
  51. <parameter name="device" required="true" />
  52. </syntax>
  53. <description>
  54. <para>The DEVICE_STATE function can be used to retrieve the device state from any
  55. device state provider. For example:</para>
  56. <para>NoOp(SIP/mypeer has state ${DEVICE_STATE(SIP/mypeer)})</para>
  57. <para>NoOp(Conference number 1234 has state ${DEVICE_STATE(MeetMe:1234)})</para>
  58. <para>The DEVICE_STATE function can also be used to set custom device state from
  59. the dialplan. The <literal>Custom:</literal> prefix must be used. For example:</para>
  60. <para>Set(DEVICE_STATE(Custom:lamp1)=BUSY)</para>
  61. <para>Set(DEVICE_STATE(Custom:lamp2)=NOT_INUSE)</para>
  62. <para>You can subscribe to the status of a custom device state using a hint in
  63. the dialplan:</para>
  64. <para>exten => 1234,hint,Custom:lamp1</para>
  65. <para>The possible values for both uses of this function are:</para>
  66. <para>UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING |
  67. RINGINUSE | ONHOLD</para>
  68. </description>
  69. </function>
  70. <function name="HINT" language="en_US">
  71. <synopsis>
  72. Get the devices set for a dialplan hint.
  73. </synopsis>
  74. <syntax>
  75. <parameter name="extension" required="true" argsep="@">
  76. <argument name="extension" required="true" />
  77. <argument name="context" />
  78. </parameter>
  79. <parameter name="options">
  80. <optionlist>
  81. <option name="n">
  82. <para>Retrieve name on the hint instead of list of devices.</para>
  83. </option>
  84. </optionlist>
  85. </parameter>
  86. </syntax>
  87. <description>
  88. <para>The HINT function can be used to retrieve the list of devices that are
  89. mapped to a dialplan hint. For example:</para>
  90. <para>NoOp(Hint for Extension 1234 is ${HINT(1234)})</para>
  91. </description>
  92. </function>
  93. ***/
  94. static const char astdb_family[] = "CustomDevstate";
  95. static int devstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  96. {
  97. ast_copy_string(buf, ast_devstate_str(ast_device_state(data)), len);
  98. return 0;
  99. }
  100. static int devstate_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  101. {
  102. size_t len = strlen("Custom:");
  103. enum ast_device_state state_val;
  104. if (strncasecmp(data, "Custom:", len)) {
  105. ast_log(LOG_WARNING, "The DEVICE_STATE function can only be used to set 'Custom:' device state!\n");
  106. return -1;
  107. }
  108. data += len;
  109. if (ast_strlen_zero(data)) {
  110. ast_log(LOG_WARNING, "DEVICE_STATE function called with no custom device name!\n");
  111. return -1;
  112. }
  113. state_val = ast_devstate_val(value);
  114. if (state_val == AST_DEVICE_UNKNOWN) {
  115. ast_log(LOG_ERROR, "DEVICE_STATE function given invalid state value '%s'\n", value);
  116. return -1;
  117. }
  118. ast_db_put(astdb_family, data, value);
  119. ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", data);
  120. return 0;
  121. }
  122. enum {
  123. HINT_OPT_NAME = (1 << 0),
  124. };
  125. AST_APP_OPTIONS(hint_options, BEGIN_OPTIONS
  126. AST_APP_OPTION('n', HINT_OPT_NAME),
  127. END_OPTIONS );
  128. static int hint_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  129. {
  130. char *exten, *context;
  131. AST_DECLARE_APP_ARGS(args,
  132. AST_APP_ARG(exten);
  133. AST_APP_ARG(options);
  134. );
  135. struct ast_flags opts = { 0, };
  136. int res;
  137. if (ast_strlen_zero(data)) {
  138. ast_log(LOG_WARNING, "The HINT function requires an extension\n");
  139. return -1;
  140. }
  141. AST_STANDARD_APP_ARGS(args, data);
  142. if (ast_strlen_zero(args.exten)) {
  143. ast_log(LOG_WARNING, "The HINT function requires an extension\n");
  144. return -1;
  145. }
  146. context = exten = args.exten;
  147. strsep(&context, "@");
  148. if (ast_strlen_zero(context))
  149. context = "default";
  150. if (!ast_strlen_zero(args.options))
  151. ast_app_parse_options(hint_options, &opts, NULL, args.options);
  152. if (ast_test_flag(&opts, HINT_OPT_NAME))
  153. res = ast_get_hint(NULL, 0, buf, len, chan, context, exten);
  154. else
  155. res = ast_get_hint(buf, len, NULL, 0, chan, context, exten);
  156. return !res; /* ast_get_hint returns non-zero on success */
  157. }
  158. static enum ast_device_state custom_devstate_callback(const char *data)
  159. {
  160. char buf[256] = "";
  161. /* Ignore check_return warning from Coverity fow ast_db_get below */
  162. ast_db_get(astdb_family, data, buf, sizeof(buf));
  163. return ast_devstate_val(buf);
  164. }
  165. static char *handle_cli_devstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  166. {
  167. struct ast_db_entry *db_entry, *db_tree;
  168. switch (cmd) {
  169. case CLI_INIT:
  170. e->command = "devstate list";
  171. e->usage =
  172. "Usage: devstate list\n"
  173. " List all custom device states that have been set by using\n"
  174. " the DEVICE_STATE dialplan function.\n";
  175. return NULL;
  176. case CLI_GENERATE:
  177. return NULL;
  178. }
  179. if (a->argc != e->args)
  180. return CLI_SHOWUSAGE;
  181. ast_cli(a->fd, "\n"
  182. "---------------------------------------------------------------------\n"
  183. "--- Custom Device States --------------------------------------------\n"
  184. "---------------------------------------------------------------------\n"
  185. "---\n");
  186. db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
  187. for (; db_entry; db_entry = db_entry->next) {
  188. const char *dev_name = strrchr(db_entry->key, '/') + 1;
  189. if (dev_name <= (const char *) 1)
  190. continue;
  191. ast_cli(a->fd, "--- Name: 'Custom:%s' State: '%s'\n"
  192. "---\n", dev_name, db_entry->data);
  193. }
  194. ast_db_freetree(db_tree);
  195. db_tree = NULL;
  196. ast_cli(a->fd,
  197. "---------------------------------------------------------------------\n"
  198. "---------------------------------------------------------------------\n"
  199. "\n");
  200. return CLI_SUCCESS;
  201. }
  202. static char *handle_cli_devstate_change(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  203. {
  204. size_t len;
  205. const char *dev, *state;
  206. enum ast_device_state state_val;
  207. switch (cmd) {
  208. case CLI_INIT:
  209. e->command = "devstate change";
  210. e->usage =
  211. "Usage: devstate change <device> <state>\n"
  212. " Change a custom device to a new state.\n"
  213. " The possible values for the state are:\n"
  214. "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
  215. "RINGINUSE | ONHOLD\n"
  216. "\n"
  217. "Examples:\n"
  218. " devstate change Custom:mystate1 INUSE\n"
  219. " devstate change Custom:mystate1 NOT_INUSE\n"
  220. " \n";
  221. return NULL;
  222. case CLI_GENERATE:
  223. {
  224. static const char * const cmds[] = { "UNKNOWN", "NOT_INUSE", "INUSE", "BUSY",
  225. "UNAVAILABLE", "RINGING", "RINGINUSE", "ONHOLD", NULL };
  226. if (a->pos == e->args + 1)
  227. return ast_cli_complete(a->word, cmds, a->n);
  228. return NULL;
  229. }
  230. }
  231. if (a->argc != e->args + 2)
  232. return CLI_SHOWUSAGE;
  233. len = strlen("Custom:");
  234. dev = a->argv[e->args];
  235. state = a->argv[e->args + 1];
  236. if (strncasecmp(dev, "Custom:", len)) {
  237. ast_cli(a->fd, "The devstate command can only be used to set 'Custom:' device state!\n");
  238. return CLI_FAILURE;
  239. }
  240. dev += len;
  241. if (ast_strlen_zero(dev))
  242. return CLI_SHOWUSAGE;
  243. state_val = ast_devstate_val(state);
  244. if (state_val == AST_DEVICE_UNKNOWN)
  245. return CLI_SHOWUSAGE;
  246. ast_cli(a->fd, "Changing %s to %s\n", dev, state);
  247. ast_db_put(astdb_family, dev, state);
  248. ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", dev);
  249. return CLI_SUCCESS;
  250. }
  251. static struct ast_cli_entry cli_funcdevstate[] = {
  252. AST_CLI_DEFINE(handle_cli_devstate_list, "List currently known custom device states"),
  253. AST_CLI_DEFINE(handle_cli_devstate_change, "Change a custom device state"),
  254. };
  255. static struct ast_custom_function devstate_function = {
  256. .name = "DEVICE_STATE",
  257. .read = devstate_read,
  258. .write = devstate_write,
  259. };
  260. static struct ast_custom_function hint_function = {
  261. .name = "HINT",
  262. .read = hint_read,
  263. };
  264. static int unload_module(void)
  265. {
  266. int res = 0;
  267. res |= ast_custom_function_unregister(&devstate_function);
  268. res |= ast_custom_function_unregister(&hint_function);
  269. res |= ast_devstate_prov_del("Custom");
  270. res |= ast_cli_unregister_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
  271. return res;
  272. }
  273. static int load_module(void)
  274. {
  275. int res = 0;
  276. struct ast_db_entry *db_entry, *db_tree;
  277. /* Populate the device state cache on the system with all of the currently
  278. * known custom device states. */
  279. db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
  280. for (; db_entry; db_entry = db_entry->next) {
  281. const char *dev_name = strrchr(db_entry->key, '/') + 1;
  282. if (dev_name <= (const char *) 1)
  283. continue;
  284. ast_devstate_changed(ast_devstate_val(db_entry->data),
  285. AST_DEVSTATE_CACHABLE, "Custom:%s", dev_name);
  286. }
  287. ast_db_freetree(db_tree);
  288. db_tree = NULL;
  289. res |= ast_custom_function_register(&devstate_function);
  290. res |= ast_custom_function_register(&hint_function);
  291. res |= ast_devstate_prov_add("Custom", custom_devstate_callback);
  292. res |= ast_cli_register_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
  293. return res;
  294. }
  295. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Gets or sets a device state in the dialplan",
  296. .support_level = AST_MODULE_SUPPORT_CORE,
  297. .load = load_module,
  298. .unload = unload_module,
  299. .load_pri = AST_MODPRI_DEVSTATE_PROVIDER,
  300. );