2 * Copyright (c) 2012-2014, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Intel Corporation nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <murphy/common/macros.h>
31 #include <murphy/common/debug.h>
32 #include <murphy/common/log.h>
33 #include <murphy/common/mm.h>
34 #include <murphy/common/list.h>
36 #include <murphy/core/context.h>
37 #include <murphy/core/domain.h>
40 mrp_list_hook_t hook; /* to list of registered methods */
41 char *name; /* method name */
42 int max_out; /* max returned arguments */
43 mrp_domain_invoke_cb_t cb; /* actual callback */
44 void *user_data; /* callback user data */
48 void domain_setup(mrp_context_t *ctx)
50 mrp_list_init(&ctx->domain_methods);
54 int mrp_set_domain_invoke_handler(mrp_context_t *ctx,
55 mrp_domain_invoke_handler_t handler,
58 if (ctx->domain_invoke != NULL)
61 ctx->domain_invoke = handler;
62 ctx->domain_data = handler_data;
68 int mrp_register_domain_methods(mrp_context_t *ctx,
69 mrp_domain_method_def_t *defs, size_t ndef)
71 mrp_domain_method_def_t *def;
75 for (i = 0, def = defs; i < ndef; i++, def++) {
76 m = mrp_allocz(sizeof(*m));
81 mrp_list_init(&m->hook);
83 m->name = mrp_strdup(def->name);
84 m->max_out = def->max_out;
86 m->user_data = def->user_data;
88 if (m->name == NULL) {
93 mrp_list_append(&ctx->domain_methods, &m->hook);
100 static method_t *find_method(mrp_context_t *ctx, const char *name)
102 mrp_list_hook_t *p, *n;
105 mrp_list_foreach(&ctx->domain_methods, p, n) {
106 m = mrp_list_entry(p, typeof(*m), hook);
108 if (!strcmp(m->name, name))
116 int mrp_lookup_domain_method(mrp_context_t *ctx, const char *name,
117 mrp_domain_invoke_cb_t *cb, int *max_out,
122 m = find_method(ctx, name);
128 *max_out = m->max_out;
129 *user_data = m->user_data;
135 int mrp_invoke_domain(mrp_context_t *ctx, const char *domain,
136 const char *method, int narg, mrp_domctl_arg_t *args,
137 mrp_domain_return_cb_t return_cb, void *user_data)
139 mrp_domain_invoke_handler_t handler = ctx->domain_invoke;
140 void *handler_data = ctx->domain_data;
145 return handler(handler_data, domain,
146 method, narg, args, return_cb, user_data);