resource_asterisk.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* -*- C -*-
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@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 Implementation for ARI stubs.
  21. *
  22. * \author David M. Lee, II <dlee@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/ast_version.h"
  30. #include "asterisk/buildinfo.h"
  31. #include "asterisk/paths.h"
  32. #include "asterisk/pbx.h"
  33. #include "resource_asterisk.h"
  34. void ast_ari_asterisk_get_info(struct ast_variable *headers,
  35. struct ast_ari_asterisk_get_info_args *args,
  36. struct ast_ari_response *response)
  37. {
  38. RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
  39. int show_all = args->only_count == 0;
  40. int show_build = show_all;
  41. int show_system = show_all;
  42. int show_config = show_all;
  43. int show_status = show_all;
  44. size_t i;
  45. int res = 0;
  46. for (i = 0; i < args->only_count; ++i) {
  47. if (strcasecmp("build", args->only[i]) == 0) {
  48. show_build = 1;
  49. } else if (strcasecmp("system", args->only[i]) == 0) {
  50. show_system = 1;
  51. } else if (strcasecmp("config", args->only[i]) == 0) {
  52. show_config = 1;
  53. } else if (strcasecmp("status", args->only[i]) == 0) {
  54. show_status = 1;
  55. } else {
  56. ast_log(LOG_WARNING, "Unrecognized info section '%s'\n",
  57. args->only[i]);
  58. }
  59. }
  60. json = ast_json_object_create();
  61. if (show_build) {
  62. res |= ast_json_object_set(json, "build",
  63. ast_json_pack(
  64. "{ s: s, s: s, s: s,"
  65. " s: s, s: s, s: s }",
  66. "os", ast_build_os,
  67. "kernel", ast_build_kernel,
  68. "machine", ast_build_machine,
  69. "options", AST_BUILDOPTS,
  70. "date", ast_build_date,
  71. "user", ast_build_user));
  72. }
  73. if (show_system) {
  74. char eid_str[128];
  75. ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
  76. res |= ast_json_object_set(json, "system",
  77. ast_json_pack("{ s: s, s: s }",
  78. "version", ast_get_version(),
  79. "entity_id", eid_str));
  80. }
  81. if (show_config) {
  82. struct ast_json *config = ast_json_pack(
  83. "{ s: s, s: s,"
  84. " s: { s: s, s: s } }",
  85. "name", ast_config_AST_SYSTEM_NAME,
  86. "default_language", ast_defaultlanguage,
  87. "setid",
  88. "user", ast_config_AST_RUN_USER,
  89. "group", ast_config_AST_RUN_GROUP);
  90. res |= ast_json_object_set(json, "config", config);
  91. if (ast_option_maxcalls) {
  92. res |= ast_json_object_set(config, "max_channels",
  93. ast_json_integer_create(ast_option_maxcalls));
  94. }
  95. if (ast_option_maxfiles) {
  96. res |= ast_json_object_set(config, "max_open_files",
  97. ast_json_integer_create(ast_option_maxfiles));
  98. }
  99. if (ast_option_maxload) {
  100. res |= ast_json_object_set(config, "max_load",
  101. ast_json_real_create(ast_option_maxload));
  102. }
  103. }
  104. if (show_status) {
  105. res |= ast_json_object_set(json, "status",
  106. ast_json_pack("{ s: o, s: o }",
  107. "startup_time",
  108. ast_json_timeval(ast_startuptime, NULL),
  109. "last_reload_time",
  110. ast_json_timeval(ast_lastreloadtime, NULL)));
  111. }
  112. if (res != 0) {
  113. ast_ari_response_alloc_failed(response);
  114. return;
  115. }
  116. ast_ari_response_ok(response, ast_json_ref(json));
  117. }
  118. void ast_ari_asterisk_get_global_var(struct ast_variable *headers,
  119. struct ast_ari_asterisk_get_global_var_args *args,
  120. struct ast_ari_response *response)
  121. {
  122. RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
  123. RAII_VAR(struct ast_str *, tmp, NULL, ast_free);
  124. const char *value;
  125. ast_assert(response != NULL);
  126. if (ast_strlen_zero(args->variable)) {
  127. ast_ari_response_error(
  128. response, 400, "Bad Request",
  129. "Variable name is required");
  130. return;
  131. }
  132. tmp = ast_str_create(32);
  133. if (!tmp) {
  134. ast_ari_response_alloc_failed(response);
  135. return;
  136. }
  137. value = ast_str_retrieve_variable(&tmp, 0, NULL, NULL, args->variable);
  138. if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
  139. ast_ari_response_alloc_failed(response);
  140. return;
  141. }
  142. ast_ari_response_ok(response, ast_json_ref(json));
  143. }
  144. void ast_ari_asterisk_set_global_var(struct ast_variable *headers,
  145. struct ast_ari_asterisk_set_global_var_args *args,
  146. struct ast_ari_response *response)
  147. {
  148. ast_assert(response != NULL);
  149. if (ast_strlen_zero(args->variable)) {
  150. ast_ari_response_error(
  151. response, 400, "Bad Request",
  152. "Variable name is required");
  153. return;
  154. }
  155. pbx_builtin_setvar_helper(NULL, args->variable, args->value);
  156. ast_ari_response_no_content(response);
  157. }