git migration: Refactor the ASTERISK_FILE_VERSION macro
[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_REGISTER_FILE()
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_asterisk_get_info(struct ast_variable *headers,
41         struct ast_ari_asterisk_get_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_asterisk_get_global_var(struct ast_variable *headers,
144         struct ast_ari_asterisk_get_global_var_args *args,
145         struct ast_ari_response *response)
146 {
147         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
148         RAII_VAR(struct ast_str *, tmp, NULL, ast_free);
149
150         const char *value;
151
152         ast_assert(response != NULL);
153
154         if (ast_strlen_zero(args->variable)) {
155                 ast_ari_response_error(
156                         response, 400, "Bad Request",
157                         "Variable name is required");
158                 return;
159         }
160
161         tmp = ast_str_create(32);
162         if (!tmp) {
163                 ast_ari_response_alloc_failed(response);
164                 return;
165         }
166
167         value = ast_str_retrieve_variable(&tmp, 0, NULL, NULL, args->variable);
168
169         if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
170                 ast_ari_response_alloc_failed(response);
171                 return;
172         }
173
174         ast_ari_response_ok(response, ast_json_ref(json));
175 }
176
177 void ast_ari_asterisk_set_global_var(struct ast_variable *headers,
178         struct ast_ari_asterisk_set_global_var_args *args,
179         struct ast_ari_response *response)
180 {
181         ast_assert(response != NULL);
182
183         if (ast_strlen_zero(args->variable)) {
184                 ast_ari_response_error(
185                         response, 400, "Bad Request",
186                         "Variable name is required");
187                 return;
188         }
189
190         pbx_builtin_setvar_helper(NULL, args->variable, args->value);
191
192         ast_ari_response_no_content(response);
193 }