func_devstate.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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, "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. ast_db_get(astdb_family, data, buf, sizeof(buf));
  162. return ast_devstate_val(buf);
  163. }
  164. static char *handle_cli_devstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  165. {
  166. struct ast_db_entry *db_entry, *db_tree;
  167. switch (cmd) {
  168. case CLI_INIT:
  169. e->command = "devstate list";
  170. e->usage =
  171. "Usage: devstate list\n"
  172. " List all custom device states that have been set by using\n"
  173. " the DEVICE_STATE dialplan function.\n";
  174. return NULL;
  175. case CLI_GENERATE:
  176. return NULL;
  177. }
  178. if (a->argc != e->args)
  179. return CLI_SHOWUSAGE;
  180. ast_cli(a->fd, "\n"
  181. "---------------------------------------------------------------------\n"
  182. "--- Custom Device States --------------------------------------------\n"
  183. "---------------------------------------------------------------------\n"
  184. "---\n");
  185. db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
  186. for (; db_entry; db_entry = db_entry->next) {
  187. const char *dev_name = strrchr(db_entry->key, '/') + 1;
  188. if (dev_name <= (const char *) 1)
  189. continue;
  190. ast_cli(a->fd, "--- Name: 'Custom:%s' State: '%s'\n"
  191. "---\n", dev_name, db_entry->data);
  192. }
  193. ast_db_freetree(db_tree);
  194. db_tree = NULL;
  195. ast_cli(a->fd,
  196. "---------------------------------------------------------------------\n"
  197. "---------------------------------------------------------------------\n"
  198. "\n");
  199. return CLI_SUCCESS;
  200. }
  201. static char *handle_cli_devstate_change(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  202. {
  203. size_t len;
  204. const char *dev, *state;
  205. enum ast_device_state state_val;
  206. switch (cmd) {
  207. case CLI_INIT:
  208. e->command = "devstate change";
  209. e->usage =
  210. "Usage: devstate change <device> <state>\n"
  211. " Change a custom device to a new state.\n"
  212. " The possible values for the state are:\n"
  213. "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
  214. "RINGINUSE | ONHOLD\n",
  215. "\n"
  216. "Examples:\n"
  217. " devstate change Custom:mystate1 INUSE\n"
  218. " devstate change Custom:mystate1 NOT_INUSE\n"
  219. " \n";
  220. return NULL;
  221. case CLI_GENERATE:
  222. {
  223. static const char * const cmds[] = { "UNKNOWN", "NOT_INUSE", "INUSE", "BUSY",
  224. "UNAVAILABLE", "RINGING", "RINGINUSE", "ONHOLD", NULL };
  225. if (a->pos == e->args + 1)
  226. return ast_cli_complete(a->word, cmds, a->n);
  227. return NULL;
  228. }
  229. }
  230. if (a->argc != e->args + 2)
  231. return CLI_SHOWUSAGE;
  232. len = strlen("Custom:");
  233. dev = a->argv[e->args];
  234. state = a->argv[e->args + 1];
  235. if (strncasecmp(dev, "Custom:", len)) {
  236. ast_cli(a->fd, "The devstate command can only be used to set 'Custom:' device state!\n");
  237. return CLI_FAILURE;
  238. }
  239. dev += len;
  240. if (ast_strlen_zero(dev))
  241. return CLI_SHOWUSAGE;
  242. state_val = ast_devstate_val(state);
  243. if (state_val == AST_DEVICE_UNKNOWN)
  244. return CLI_SHOWUSAGE;
  245. ast_cli(a->fd, "Changing %s to %s\n", dev, state);
  246. ast_db_put(astdb_family, dev, state);
  247. ast_devstate_changed(state_val, "Custom:%s", dev);
  248. return CLI_SUCCESS;
  249. }
  250. static struct ast_cli_entry cli_funcdevstate[] = {
  251. AST_CLI_DEFINE(handle_cli_devstate_list, "List currently known custom device states"),
  252. AST_CLI_DEFINE(handle_cli_devstate_change, "Change a custom device state"),
  253. };
  254. static struct ast_custom_function devstate_function = {
  255. .name = "DEVICE_STATE",
  256. .read = devstate_read,
  257. .write = devstate_write,
  258. };
  259. static struct ast_custom_function hint_function = {
  260. .name = "HINT",
  261. .read = hint_read,
  262. };
  263. static int unload_module(void)
  264. {
  265. int res = 0;
  266. res |= ast_custom_function_unregister(&devstate_function);
  267. res |= ast_custom_function_unregister(&hint_function);
  268. res |= ast_devstate_prov_del("Custom");
  269. res |= ast_cli_unregister_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
  270. return res;
  271. }
  272. static int load_module(void)
  273. {
  274. int res = 0;
  275. struct ast_db_entry *db_entry, *db_tree;
  276. /* Populate the device state cache on the system with all of the currently
  277. * known custom device states. */
  278. db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
  279. for (; db_entry; db_entry = db_entry->next) {
  280. const char *dev_name = strrchr(db_entry->key, '/') + 1;
  281. if (dev_name <= (const char *) 1)
  282. continue;
  283. ast_devstate_changed(ast_devstate_val(db_entry->data),
  284. "Custom:%s\n", dev_name);
  285. }
  286. ast_db_freetree(db_tree);
  287. db_tree = NULL;
  288. res |= ast_custom_function_register(&devstate_function);
  289. res |= ast_custom_function_register(&hint_function);
  290. res |= ast_devstate_prov_add("Custom", custom_devstate_callback);
  291. res |= ast_cli_register_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
  292. return res;
  293. }
  294. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Gets or sets a device state in the dialplan",
  295. .load = load_module,
  296. .unload = unload_module,
  297. .load_pri = AST_MODPRI_DEVSTATE_PROVIDER,
  298. );