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