gam-resource-manager: adjust to updated proxied call callback signature.
[profile/ivi/murphy.git] / src / core / domain.c
1 /*
2  * Copyright (c) 2012-2014, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
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>
35
36 #include <murphy/core/context.h>
37 #include <murphy/core/domain.h>
38
39 typedef struct {
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 */
45 } method_t;
46
47
48 void domain_setup(mrp_context_t *ctx)
49 {
50     mrp_list_init(&ctx->domain_methods);
51 }
52
53
54 int mrp_set_domain_invoke_handler(mrp_context_t *ctx,
55                                   mrp_domain_invoke_handler_t handler,
56                                   void *handler_data)
57 {
58     if (ctx->domain_invoke != NULL)
59         return FALSE;
60
61     ctx->domain_invoke = handler;
62     ctx->domain_data   = handler_data;
63
64     return TRUE;
65 }
66
67
68 int mrp_register_domain_methods(mrp_context_t *ctx,
69                                 mrp_domain_method_def_t *defs, size_t ndef)
70 {
71     mrp_domain_method_def_t *def;
72     method_t                *m;
73     size_t                   i;
74
75     for (i = 0, def = defs; i < ndef; i++, def++) {
76         m = mrp_allocz(sizeof(*m));
77
78         if (m == NULL)
79             return FALSE;
80
81         mrp_list_init(&m->hook);
82
83         m->name      = mrp_strdup(def->name);
84         m->max_out   = def->max_out;
85         m->cb        = def->cb;
86         m->user_data = def->user_data;
87
88         if (m->name == NULL) {
89             mrp_free(m);
90             return FALSE;
91         }
92
93         mrp_list_append(&ctx->domain_methods, &m->hook);
94     }
95
96     return TRUE;
97 }
98
99
100 static method_t *find_method(mrp_context_t *ctx, const char *name)
101 {
102     mrp_list_hook_t *p, *n;
103     method_t        *m;
104
105     mrp_list_foreach(&ctx->domain_methods, p, n) {
106         m = mrp_list_entry(p, typeof(*m), hook);
107
108         if (!strcmp(m->name, name))
109             return m;
110     }
111
112     return NULL;
113 }
114
115
116 int mrp_lookup_domain_method(mrp_context_t *ctx, const char *name,
117                              mrp_domain_invoke_cb_t *cb, int *max_out,
118                              void **user_data)
119 {
120     method_t *m;
121
122     m = find_method(ctx, name);
123
124     if (m == NULL)
125         return FALSE;
126
127     *cb        = m->cb;
128     *max_out   = m->max_out;
129     *user_data = m->user_data;
130
131     return TRUE;
132 }
133
134
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)
138 {
139     mrp_domain_invoke_handler_t  handler      = ctx->domain_invoke;
140     void                        *handler_data = ctx->domain_data;
141
142     if (handler == NULL)
143         return FALSE;
144
145     return handler(handler_data, domain,
146                    method, narg, args, return_cb, user_data);
147 }