res_ari_device_states.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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_stasis</depend>
  34. <support_level>core</support_level>
  35. ***/
  36. #include "asterisk.h"
  37. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  38. #include "asterisk/app.h"
  39. #include "asterisk/module.h"
  40. #include "asterisk/stasis_app.h"
  41. #include "ari/resource_device_states.h"
  42. #if defined(AST_DEVMODE)
  43. #include "ari/ari_model_validators.h"
  44. #endif
  45. #define MAX_VALS 128
  46. /*!
  47. * \brief Parameter parsing callback for /deviceStates.
  48. * \param get_params GET parameters in the HTTP request.
  49. * \param path_vars Path variables extracted from the request.
  50. * \param headers HTTP headers.
  51. * \param[out] response Response to the HTTP request.
  52. */
  53. static void ast_ari_device_states_list_cb(
  54. struct ast_tcptls_session_instance *ser,
  55. struct ast_variable *get_params, struct ast_variable *path_vars,
  56. struct ast_variable *headers, struct ast_ari_response *response)
  57. {
  58. struct ast_ari_device_states_list_args args = {};
  59. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  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_ari_response *response)
  104. {
  105. struct ast_ari_device_states_get_args args = {};
  106. struct ast_variable *i;
  107. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  108. #if defined(AST_DEVMODE)
  109. int is_valid;
  110. int code;
  111. #endif /* AST_DEVMODE */
  112. for (i = path_vars; i; i = i->next) {
  113. if (strcmp(i->name, "deviceName") == 0) {
  114. args.device_name = (i->value);
  115. } else
  116. {}
  117. }
  118. ast_ari_device_states_get(headers, &args, response);
  119. #if defined(AST_DEVMODE)
  120. code = response->response_code;
  121. switch (code) {
  122. case 0: /* Implementation is still a stub, or the code wasn't set */
  123. is_valid = response->message == NULL;
  124. break;
  125. case 500: /* Internal Server Error */
  126. case 501: /* Not Implemented */
  127. is_valid = 1;
  128. break;
  129. default:
  130. if (200 <= code && code <= 299) {
  131. is_valid = ast_ari_validate_device_state(
  132. response->message);
  133. } else {
  134. ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
  135. is_valid = 0;
  136. }
  137. }
  138. if (!is_valid) {
  139. ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
  140. ast_ari_response_error(response, 500,
  141. "Internal Server Error", "Response validation failed");
  142. }
  143. #endif /* AST_DEVMODE */
  144. fin: __attribute__((unused))
  145. return;
  146. }
  147. int ast_ari_device_states_update_parse_body(
  148. struct ast_json *body,
  149. struct ast_ari_device_states_update_args *args)
  150. {
  151. struct ast_json *field;
  152. /* Parse query parameters out of it */
  153. field = ast_json_object_get(body, "deviceState");
  154. if (field) {
  155. args->device_state = ast_json_string_get(field);
  156. }
  157. return 0;
  158. }
  159. /*!
  160. * \brief Parameter parsing callback for /deviceStates/{deviceName}.
  161. * \param get_params GET parameters in the HTTP request.
  162. * \param path_vars Path variables extracted from the request.
  163. * \param headers HTTP headers.
  164. * \param[out] response Response to the HTTP request.
  165. */
  166. static void ast_ari_device_states_update_cb(
  167. struct ast_tcptls_session_instance *ser,
  168. struct ast_variable *get_params, struct ast_variable *path_vars,
  169. struct ast_variable *headers, struct ast_ari_response *response)
  170. {
  171. struct ast_ari_device_states_update_args args = {};
  172. struct ast_variable *i;
  173. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  174. #if defined(AST_DEVMODE)
  175. int is_valid;
  176. int code;
  177. #endif /* AST_DEVMODE */
  178. for (i = get_params; i; i = i->next) {
  179. if (strcmp(i->name, "deviceState") == 0) {
  180. args.device_state = (i->value);
  181. } else
  182. {}
  183. }
  184. for (i = path_vars; i; i = i->next) {
  185. if (strcmp(i->name, "deviceName") == 0) {
  186. args.device_name = (i->value);
  187. } else
  188. {}
  189. }
  190. /* Look for a JSON request entity */
  191. body = ast_http_get_json(ser, headers);
  192. if (!body) {
  193. switch (errno) {
  194. case EFBIG:
  195. ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
  196. goto fin;
  197. case ENOMEM:
  198. ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
  199. goto fin;
  200. case EIO:
  201. ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
  202. goto fin;
  203. }
  204. }
  205. if (ast_ari_device_states_update_parse_body(body, &args)) {
  206. ast_ari_response_alloc_failed(response);
  207. goto fin;
  208. }
  209. ast_ari_device_states_update(headers, &args, response);
  210. #if defined(AST_DEVMODE)
  211. code = response->response_code;
  212. switch (code) {
  213. case 0: /* Implementation is still a stub, or the code wasn't set */
  214. is_valid = response->message == NULL;
  215. break;
  216. case 500: /* Internal Server Error */
  217. case 501: /* Not Implemented */
  218. case 404: /* Device name is missing */
  219. case 409: /* Uncontrolled device specified */
  220. is_valid = 1;
  221. break;
  222. default:
  223. if (200 <= code && code <= 299) {
  224. is_valid = ast_ari_validate_void(
  225. response->message);
  226. } else {
  227. ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
  228. is_valid = 0;
  229. }
  230. }
  231. if (!is_valid) {
  232. ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
  233. ast_ari_response_error(response, 500,
  234. "Internal Server Error", "Response validation failed");
  235. }
  236. #endif /* AST_DEVMODE */
  237. fin: __attribute__((unused))
  238. return;
  239. }
  240. /*!
  241. * \brief Parameter parsing callback for /deviceStates/{deviceName}.
  242. * \param get_params GET parameters in the HTTP request.
  243. * \param path_vars Path variables extracted from the request.
  244. * \param headers HTTP headers.
  245. * \param[out] response Response to the HTTP request.
  246. */
  247. static void ast_ari_device_states_delete_cb(
  248. struct ast_tcptls_session_instance *ser,
  249. struct ast_variable *get_params, struct ast_variable *path_vars,
  250. struct ast_variable *headers, struct ast_ari_response *response)
  251. {
  252. struct ast_ari_device_states_delete_args args = {};
  253. struct ast_variable *i;
  254. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  255. #if defined(AST_DEVMODE)
  256. int is_valid;
  257. int code;
  258. #endif /* AST_DEVMODE */
  259. for (i = path_vars; i; i = i->next) {
  260. if (strcmp(i->name, "deviceName") == 0) {
  261. args.device_name = (i->value);
  262. } else
  263. {}
  264. }
  265. ast_ari_device_states_delete(headers, &args, response);
  266. #if defined(AST_DEVMODE)
  267. code = response->response_code;
  268. switch (code) {
  269. case 0: /* Implementation is still a stub, or the code wasn't set */
  270. is_valid = response->message == NULL;
  271. break;
  272. case 500: /* Internal Server Error */
  273. case 501: /* Not Implemented */
  274. case 404: /* Device name is missing */
  275. case 409: /* Uncontrolled device specified */
  276. is_valid = 1;
  277. break;
  278. default:
  279. if (200 <= code && code <= 299) {
  280. is_valid = ast_ari_validate_void(
  281. response->message);
  282. } else {
  283. ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
  284. is_valid = 0;
  285. }
  286. }
  287. if (!is_valid) {
  288. ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
  289. ast_ari_response_error(response, 500,
  290. "Internal Server Error", "Response validation failed");
  291. }
  292. #endif /* AST_DEVMODE */
  293. fin: __attribute__((unused))
  294. return;
  295. }
  296. /*! \brief REST handler for /api-docs/deviceStates.{format} */
  297. static struct stasis_rest_handlers deviceStates_deviceName = {
  298. .path_segment = "deviceName",
  299. .is_wildcard = 1,
  300. .callbacks = {
  301. [AST_HTTP_GET] = ast_ari_device_states_get_cb,
  302. [AST_HTTP_PUT] = ast_ari_device_states_update_cb,
  303. [AST_HTTP_DELETE] = ast_ari_device_states_delete_cb,
  304. },
  305. .num_children = 0,
  306. .children = { }
  307. };
  308. /*! \brief REST handler for /api-docs/deviceStates.{format} */
  309. static struct stasis_rest_handlers deviceStates = {
  310. .path_segment = "deviceStates",
  311. .callbacks = {
  312. [AST_HTTP_GET] = ast_ari_device_states_list_cb,
  313. },
  314. .num_children = 1,
  315. .children = { &deviceStates_deviceName, }
  316. };
  317. static int load_module(void)
  318. {
  319. int res = 0;
  320. stasis_app_ref();
  321. res |= ast_ari_add_handler(&deviceStates);
  322. return res;
  323. }
  324. static int unload_module(void)
  325. {
  326. ast_ari_remove_handler(&deviceStates);
  327. stasis_app_unref();
  328. return 0;
  329. }
  330. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Device state resources",
  331. .support_level = AST_MODULE_SUPPORT_CORE,
  332. .load = load_module,
  333. .unload = unload_module,
  334. .nonoptreq = "res_ari,res_stasis",
  335. );