resource_device_states.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * Kevin Harwell <kharwell@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 /api-docs/deviceStates.{format} implementation- Device state resources
  21. *
  22. * \author Kevin Harwell <kharwell@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <depend type="module">res_stasis_device_state</depend>
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "resource_device_states.h"
  31. #include "asterisk/stasis_app_device_state.h"
  32. void ast_ari_device_states_list(
  33. struct ast_variable *headers,
  34. struct ast_ari_device_states_list_args *args,
  35. struct ast_ari_response *response)
  36. {
  37. struct ast_json *json;
  38. if (!(json = stasis_app_device_states_to_json())) {
  39. ast_ari_response_error(response, 500,
  40. "Internal Server Error", "Error building response");
  41. return;
  42. }
  43. ast_ari_response_ok(response, json);
  44. }
  45. void ast_ari_device_states_get(struct ast_variable *headers,
  46. struct ast_ari_device_states_get_args *args,
  47. struct ast_ari_response *response)
  48. {
  49. struct ast_json *json;
  50. if (!(json = stasis_app_device_state_to_json(
  51. args->device_name, ast_device_state(args->device_name)))) {
  52. ast_ari_response_error(response, 500,
  53. "Internal Server Error", "Error building response");
  54. return;
  55. }
  56. ast_ari_response_ok(response, json);
  57. }
  58. void ast_ari_device_states_update(struct ast_variable *headers,
  59. struct ast_ari_device_states_update_args *args,
  60. struct ast_ari_response *response)
  61. {
  62. switch (stasis_app_device_state_update(
  63. args->device_name, args->device_state)) {
  64. case STASIS_DEVICE_STATE_NOT_CONTROLLED:
  65. ast_ari_response_error(response, 409,
  66. "Conflict", "Uncontrolled device specified");
  67. return;
  68. case STASIS_DEVICE_STATE_MISSING:
  69. ast_ari_response_error(response, 404,
  70. "Not Found", "Device name is missing");
  71. return;
  72. case STASIS_DEVICE_STATE_UNKNOWN:
  73. ast_ari_response_error(response, 500, "Internal Server Error",
  74. "Unknown device");
  75. return;
  76. case STASIS_DEVICE_STATE_OK:
  77. case STASIS_DEVICE_STATE_SUBSCRIBERS: /* shouldn't be returned for update */
  78. ast_ari_response_no_content(response);
  79. }
  80. }
  81. void ast_ari_device_states_delete(struct ast_variable *headers,
  82. struct ast_ari_device_states_delete_args *args,
  83. struct ast_ari_response *response)
  84. {
  85. switch (stasis_app_device_state_delete(args->device_name)) {
  86. case STASIS_DEVICE_STATE_NOT_CONTROLLED:
  87. ast_ari_response_error(response, 409,
  88. "Conflict", "Uncontrolled device specified");
  89. return;
  90. case STASIS_DEVICE_STATE_MISSING:
  91. ast_ari_response_error(response, 404,
  92. "Not Found", "Device name is missing");
  93. return;
  94. case STASIS_DEVICE_STATE_SUBSCRIBERS:
  95. ast_ari_response_error(response, 500,
  96. "Internal Server Error",
  97. "Cannot delete device with subscribers");
  98. return;
  99. case STASIS_DEVICE_STATE_OK:
  100. case STASIS_DEVICE_STATE_UNKNOWN:
  101. ast_ari_response_no_content(response);
  102. }
  103. }