resource_device_states.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include "resource_device_states.h"
  27. #include "asterisk/stasis_app_device_state.h"
  28. void ast_ari_device_states_list(
  29. struct ast_variable *headers,
  30. struct ast_ari_device_states_list_args *args,
  31. struct ast_ari_response *response)
  32. {
  33. struct ast_json *json;
  34. if (!(json = stasis_app_device_states_to_json())) {
  35. ast_ari_response_error(response, 500,
  36. "Internal Server Error", "Error building response");
  37. return;
  38. }
  39. ast_ari_response_ok(response, json);
  40. }
  41. void ast_ari_device_states_get(struct ast_variable *headers,
  42. struct ast_ari_device_states_get_args *args,
  43. struct ast_ari_response *response)
  44. {
  45. struct ast_json *json;
  46. if (!(json = stasis_app_device_state_to_json(
  47. args->device_name, ast_device_state(args->device_name)))) {
  48. ast_ari_response_error(response, 500,
  49. "Internal Server Error", "Error building response");
  50. return;
  51. }
  52. ast_ari_response_ok(response, json);
  53. }
  54. void ast_ari_device_states_update(struct ast_variable *headers,
  55. struct ast_ari_device_states_update_args *args,
  56. struct ast_ari_response *response)
  57. {
  58. switch (stasis_app_device_state_update(
  59. args->device_name, args->device_state)) {
  60. case STASIS_DEVICE_STATE_NOT_CONTROLLED:
  61. ast_ari_response_error(response, 409,
  62. "Conflict", "Uncontrolled device specified");
  63. return;
  64. case STASIS_DEVICE_STATE_MISSING:
  65. ast_ari_response_error(response, 404,
  66. "Not Found", "Device name is missing");
  67. return;
  68. case STASIS_DEVICE_STATE_UNKNOWN:
  69. ast_ari_response_error(response, 500, "Internal Server Error",
  70. "Unknown device");
  71. return;
  72. case STASIS_DEVICE_STATE_OK:
  73. case STASIS_DEVICE_STATE_SUBSCRIBERS: /* shouldn't be returned for update */
  74. ast_ari_response_no_content(response);
  75. }
  76. }
  77. void ast_ari_device_states_delete(struct ast_variable *headers,
  78. struct ast_ari_device_states_delete_args *args,
  79. struct ast_ari_response *response)
  80. {
  81. switch (stasis_app_device_state_delete(args->device_name)) {
  82. case STASIS_DEVICE_STATE_NOT_CONTROLLED:
  83. ast_ari_response_error(response, 409,
  84. "Conflict", "Uncontrolled device specified");
  85. return;
  86. case STASIS_DEVICE_STATE_MISSING:
  87. ast_ari_response_error(response, 404,
  88. "Not Found", "Device name is missing");
  89. return;
  90. case STASIS_DEVICE_STATE_SUBSCRIBERS:
  91. ast_ari_response_error(response, 500,
  92. "Internal Server Error",
  93. "Cannot delete device with subscribers");
  94. return;
  95. case STASIS_DEVICE_STATE_OK:
  96. case STASIS_DEVICE_STATE_UNKNOWN:
  97. ast_ari_response_no_content(response);
  98. }
  99. }