res_ari_device_states.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. /*
  19. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  20. * !!!!! DO NOT EDIT !!!!!
  21. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  22. * This file is generated by a mustache template. Please see the original
  23. * template in rest-api-templates/res_ari_resource.c.mustache
  24. */
  25. /*! \file
  26. *
  27. * \brief Device state resources
  28. *
  29. * \author Kevin Harwell <kharwell@digium.com>
  30. */
  31. /*** MODULEINFO
  32. <depend type="module">res_ari</depend>
  33. <depend type="module">res_ari_model</depend>
  34. <depend type="module">res_stasis</depend>
  35. <support_level>core</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include "asterisk/app.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/stasis_app.h"
  42. #include "ari/resource_device_states.h"
  43. #if defined(AST_DEVMODE)
  44. #include "ari/ari_model_validators.h"
  45. #endif
  46. #define MAX_VALS 128
  47. /*!
  48. * \brief Parameter parsing callback for /deviceStates.
  49. * \param get_params GET parameters in the HTTP request.
  50. * \param path_vars Path variables extracted from the request.
  51. * \param headers HTTP headers.
  52. * \param[out] response Response to the HTTP request.
  53. */
  54. static void ast_ari_device_states_list_cb(
  55. struct ast_tcptls_session_instance *ser,
  56. struct ast_variable *get_params, struct ast_variable *path_vars,
  57. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  58. {
  59. struct ast_ari_device_states_list_args args = {};
  60. #if defined(AST_DEVMODE)
  61. int is_valid;
  62. int code;
  63. #endif /* AST_DEVMODE */
  64. ast_ari_device_states_list(headers, &args, response);
  65. #if defined(AST_DEVMODE)
  66. code = response->response_code;
  67. switch (code) {
  68. case 0: /* Implementation is still a stub, or the code wasn't set */
  69. is_valid = response->message == NULL;
  70. break;
  71. case 500: /* Internal Server Error */
  72. case 501: /* Not Implemented */
  73. is_valid = 1;
  74. break;
  75. default:
  76. if (200 <= code && code <= 299) {
  77. is_valid = ast_ari_validate_list(response->message,
  78. ast_ari_validate_device_state_fn());
  79. } else {
  80. ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates\n", code);
  81. is_valid = 0;
  82. }
  83. }
  84. if (!is_valid) {
  85. ast_log(LOG_ERROR, "Response validation failed for /deviceStates\n");
  86. ast_ari_response_error(response, 500,
  87. "Internal Server Error", "Response validation failed");
  88. }
  89. #endif /* AST_DEVMODE */
  90. fin: __attribute__((unused))
  91. return;
  92. }
  93. /*!
  94. * \brief Parameter parsing callback for /deviceStates/{deviceName}.
  95. * \param get_params GET parameters in the HTTP request.
  96. * \param path_vars Path variables extracted from the request.
  97. * \param headers HTTP headers.
  98. * \param[out] response Response to the HTTP request.
  99. */
  100. static void ast_ari_device_states_get_cb(
  101. struct ast_tcptls_session_instance *ser,
  102. struct ast_variable *get_params, struct ast_variable *path_vars,
  103. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  104. {
  105. struct ast_ari_device_states_get_args args = {};
  106. struct ast_variable *i;
  107. #if defined(AST_DEVMODE)
  108. int is_valid;
  109. int code;
  110. #endif /* AST_DEVMODE */
  111. for (i = path_vars; i; i = i->next) {
  112. if (strcmp(i->name, "deviceName") == 0) {
  113. args.device_name = (i->value);
  114. } else
  115. {}
  116. }
  117. ast_ari_device_states_get(headers, &args, response);
  118. #if defined(AST_DEVMODE)
  119. code = response->response_code;
  120. switch (code) {
  121. case 0: /* Implementation is still a stub, or the code wasn't set */
  122. is_valid = response->message == NULL;
  123. break;
  124. case 500: /* Internal Server Error */
  125. case 501: /* Not Implemented */
  126. is_valid = 1;
  127. break;
  128. default:
  129. if (200 <= code && code <= 299) {
  130. is_valid = ast_ari_validate_device_state(
  131. response->message);
  132. } else {
  133. ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
  134. is_valid = 0;
  135. }
  136. }
  137. if (!is_valid) {
  138. ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
  139. ast_ari_response_error(response, 500,
  140. "Internal Server Error", "Response validation failed");
  141. }
  142. #endif /* AST_DEVMODE */
  143. fin: __attribute__((unused))
  144. return;
  145. }
  146. int ast_ari_device_states_update_parse_body(
  147. struct ast_json *body,
  148. struct ast_ari_device_states_update_args *args)
  149. {
  150. struct ast_json *field;
  151. /* Parse query parameters out of it */
  152. field = ast_json_object_get(body, "deviceState");
  153. if (field) {
  154. args->device_state = ast_json_string_get(field);
  155. }
  156. return 0;
  157. }
  158. /*!
  159. * \brief Parameter parsing callback for /deviceStates/{deviceName}.
  160. * \param get_params GET parameters in the HTTP request.
  161. * \param path_vars Path variables extracted from the request.
  162. * \param headers HTTP headers.
  163. * \param[out] response Response to the HTTP request.
  164. */
  165. static void ast_ari_device_states_update_cb(
  166. struct ast_tcptls_session_instance *ser,
  167. struct ast_variable *get_params, struct ast_variable *path_vars,
  168. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  169. {
  170. struct ast_ari_device_states_update_args args = {};
  171. struct ast_variable *i;
  172. #if defined(AST_DEVMODE)
  173. int is_valid;
  174. int code;
  175. #endif /* AST_DEVMODE */
  176. for (i = get_params; i; i = i->next) {
  177. if (strcmp(i->name, "deviceState") == 0) {
  178. args.device_state = (i->value);
  179. } else
  180. {}
  181. }
  182. for (i = path_vars; i; i = i->next) {
  183. if (strcmp(i->name, "deviceName") == 0) {
  184. args.device_name = (i->value);
  185. } else
  186. {}
  187. }
  188. if (ast_ari_device_states_update_parse_body(body, &args)) {
  189. ast_ari_response_alloc_failed(response);
  190. goto fin;
  191. }
  192. ast_ari_device_states_update(headers, &args, response);
  193. #if defined(AST_DEVMODE)
  194. code = response->response_code;
  195. switch (code) {
  196. case 0: /* Implementation is still a stub, or the code wasn't set */
  197. is_valid = response->message == NULL;
  198. break;
  199. case 500: /* Internal Server Error */
  200. case 501: /* Not Implemented */
  201. case 404: /* Device name is missing */
  202. case 409: /* Uncontrolled device specified */
  203. is_valid = 1;
  204. break;
  205. default:
  206. if (200 <= code && code <= 299) {
  207. is_valid = ast_ari_validate_void(
  208. response->message);
  209. } else {
  210. ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
  211. is_valid = 0;
  212. }
  213. }
  214. if (!is_valid) {
  215. ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
  216. ast_ari_response_error(response, 500,
  217. "Internal Server Error", "Response validation failed");
  218. }
  219. #endif /* AST_DEVMODE */
  220. fin: __attribute__((unused))
  221. return;
  222. }
  223. /*!
  224. * \brief Parameter parsing callback for /deviceStates/{deviceName}.
  225. * \param get_params GET parameters in the HTTP request.
  226. * \param path_vars Path variables extracted from the request.
  227. * \param headers HTTP headers.
  228. * \param[out] response Response to the HTTP request.
  229. */
  230. static void ast_ari_device_states_delete_cb(
  231. struct ast_tcptls_session_instance *ser,
  232. struct ast_variable *get_params, struct ast_variable *path_vars,
  233. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  234. {
  235. struct ast_ari_device_states_delete_args args = {};
  236. struct ast_variable *i;
  237. #if defined(AST_DEVMODE)
  238. int is_valid;
  239. int code;
  240. #endif /* AST_DEVMODE */
  241. for (i = path_vars; i; i = i->next) {
  242. if (strcmp(i->name, "deviceName") == 0) {
  243. args.device_name = (i->value);
  244. } else
  245. {}
  246. }
  247. ast_ari_device_states_delete(headers, &args, response);
  248. #if defined(AST_DEVMODE)
  249. code = response->response_code;
  250. switch (code) {
  251. case 0: /* Implementation is still a stub, or the code wasn't set */
  252. is_valid = response->message == NULL;
  253. break;
  254. case 500: /* Internal Server Error */
  255. case 501: /* Not Implemented */
  256. case 404: /* Device name is missing */
  257. case 409: /* Uncontrolled device specified */
  258. is_valid = 1;
  259. break;
  260. default:
  261. if (200 <= code && code <= 299) {
  262. is_valid = ast_ari_validate_void(
  263. response->message);
  264. } else {
  265. ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
  266. is_valid = 0;
  267. }
  268. }
  269. if (!is_valid) {
  270. ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
  271. ast_ari_response_error(response, 500,
  272. "Internal Server Error", "Response validation failed");
  273. }
  274. #endif /* AST_DEVMODE */
  275. fin: __attribute__((unused))
  276. return;
  277. }
  278. /*! \brief REST handler for /api-docs/deviceStates.json */
  279. static struct stasis_rest_handlers deviceStates_deviceName = {
  280. .path_segment = "deviceName",
  281. .is_wildcard = 1,
  282. .callbacks = {
  283. [AST_HTTP_GET] = ast_ari_device_states_get_cb,
  284. [AST_HTTP_PUT] = ast_ari_device_states_update_cb,
  285. [AST_HTTP_DELETE] = ast_ari_device_states_delete_cb,
  286. },
  287. .num_children = 0,
  288. .children = { }
  289. };
  290. /*! \brief REST handler for /api-docs/deviceStates.json */
  291. static struct stasis_rest_handlers deviceStates = {
  292. .path_segment = "deviceStates",
  293. .callbacks = {
  294. [AST_HTTP_GET] = ast_ari_device_states_list_cb,
  295. },
  296. .num_children = 1,
  297. .children = { &deviceStates_deviceName, }
  298. };
  299. static int unload_module(void)
  300. {
  301. ast_ari_remove_handler(&deviceStates);
  302. stasis_app_unref();
  303. return 0;
  304. }
  305. static int load_module(void)
  306. {
  307. int res = 0;
  308. CHECK_ARI_MODULE_LOADED();
  309. stasis_app_ref();
  310. res |= ast_ari_add_handler(&deviceStates);
  311. if (res) {
  312. unload_module();
  313. return AST_MODULE_LOAD_DECLINE;
  314. }
  315. return AST_MODULE_LOAD_SUCCESS;
  316. }
  317. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Device state resources",
  318. .support_level = AST_MODULE_SUPPORT_CORE,
  319. .load = load_module,
  320. .unload = unload_module,
  321. );