2 This file is part of PulseAudio.
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License,
7 or (at your option) any later version.
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
25 #include <pulse/xmalloc.h>
27 #include <pulsecore/core.h>
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/log.h>
30 #include <pulsecore/macro.h>
32 #include "message-handler.h"
34 /* Check if a path string starts with a / and only contains valid characters.
35 * Also reject double slashes. */
36 static bool object_path_is_valid(const char *test_string) {
42 /* Make sure the string starts with a / */
43 if (test_string[0] != '/')
46 for (i = 0; test_string[i]; i++) {
48 if ((test_string[i] >= 'a' && test_string[i] <= 'z') ||
49 (test_string[i] >= 'A' && test_string[i] <= 'Z') ||
50 (test_string[i] >= '0' && test_string[i] <= '9') ||
51 test_string[i] == '.' ||
52 test_string[i] == '_' ||
53 test_string[i] == '-' ||
54 (test_string[i] == '/' && test_string[i + 1] != '/'))
60 /* Make sure the string does not end with a / */
61 if (test_string[i - 1] == '/')
67 /* Message handler functions */
69 /* Register message handler for the specified object. object_path must be a unique name starting with "/". */
70 void pa_message_handler_register(pa_core *c, const char *object_path, const char *description, pa_message_handler_cb_t cb, void *userdata) {
71 struct pa_message_handler *handler;
74 pa_assert(object_path);
78 /* Ensure that object path is valid */
79 pa_assert(object_path_is_valid(object_path));
81 handler = pa_xnew0(struct pa_message_handler, 1);
82 handler->userdata = userdata;
83 handler->callback = cb;
84 handler->object_path = pa_xstrdup(object_path);
85 handler->description = pa_xstrdup(description);
87 pa_assert_se(pa_hashmap_put(c->message_handlers, handler->object_path, handler) == 0);
90 /* Unregister a message handler */
91 void pa_message_handler_unregister(pa_core *c, const char *object_path) {
92 struct pa_message_handler *handler;
95 pa_assert(object_path);
97 pa_assert_se(handler = pa_hashmap_remove(c->message_handlers, object_path));
99 pa_xfree(handler->object_path);
100 pa_xfree(handler->description);
104 /* Send a message to an object identified by object_path */
105 int pa_message_handler_send_message(pa_core *c, const char *object_path, const char *message, const char *message_parameters, char **response) {
106 struct pa_message_handler *handler;
109 pa_json_object *parameters = NULL;
112 pa_assert(object_path);
118 path_copy = pa_xstrdup(object_path);
120 /* Remove trailing / from path name if present */
121 if (path_copy[strlen(path_copy) - 1] == '/')
122 path_copy[strlen(path_copy) - 1] = 0;
124 if (!(handler = pa_hashmap_get(c->message_handlers, path_copy))) {
126 return -PA_ERR_NOENTITY;
131 if (message_parameters) {
132 parameters = pa_json_parse(message_parameters);
135 return -PA_ERR_INVALID;
138 /* The handler is expected to return an error code and may also
139 return an error string in response */
140 ret = handler->callback(handler->object_path, message, parameters, response, handler->userdata);
143 pa_json_object_free(parameters);
147 /* Set handler description */
148 int pa_message_handler_set_description(pa_core *c, const char *object_path, const char *description) {
149 struct pa_message_handler *handler;
152 pa_assert(object_path);
154 if (!(handler = pa_hashmap_get(c->message_handlers, object_path)))
155 return -PA_ERR_NOENTITY;
157 pa_xfree(handler->description);
158 handler->description = pa_xstrdup(description);