summaryrefslogtreecommitdiffstats
path: root/src/sbus/codegen/templates/client_sync.c.tpl
blob: fe9a3a4726014aa2bcb221a1bbcc949f7d900237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<template name="file-header">
    /*
        Generated by sbus code generator

        Copyright (C) 2017 Red Hat

        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 3 of the License, or
        (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
    */

    #include <errno.h>
    #include <talloc.h>
    #include <dbus/dbus.h>

    #include "${sbus-path}/sbus_sync.h"
    #include "${sbus-path}/sbus_sync_private.h"
    #include "${sbus-path}/sbus_message.h"
    #include "${sbus-path}/interface/sbus_iterator_readers.h"
    #include "${sbus-path}/interface_dbus/sbus_dbus_client_sync.h"
    #include "${header:arguments}"
    #include "${header:client_properties}"

</template>

<template name="method-invoker">
    static errno_t
    sbus_method_in_${input-signature}_out_${output-signature}
        (<toggle name="if-use-talloc">TALLOC_CTX *mem_ctx,
         struct sbus_sync_connection *conn,
         <or>struct sbus_sync_connection *conn,
         </toggle><toggle name="if-raw-input">
         DBusMessage *raw_message<or>
         const char *bus,
         const char *path,
         const char *iface,
         const char *method<loop name="in">,
         ${type} arg${index}</loop></toggle>
         <toggle name="if-raw-output">,
         DBusMessage **_reply)
         <or><loop name="out">,
         ${type} _arg${index}</loop>)
         </toggle>
    {
        TALLOC_CTX *tmp_ctx;
        <toggle name="if-input-arguments">
        struct _sbus_invoker_args_${input-signature} in;
        </toggle>
        <toggle name="if-output-arguments">
        struct _sbus_invoker_args_${output-signature} *out;
        </toggle>
        DBusMessage *reply;
        errno_t ret;

        tmp_ctx = talloc_new(NULL);
        if (tmp_ctx == NULL) {
            DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
            return ENOMEM;
        }

        <toggle name="if-output-arguments">
        out = talloc_zero(tmp_ctx, struct _sbus_invoker_args_${output-signature});
        if (out == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "Unable to allocate space for output parameters!\n");
            ret = ENOMEM;
            goto done;
        }

        </toggle>
        <loop name="in">
        in.arg${index} = arg${index};
        </loop>

        <toggle name="if-raw-input">
        ret = sbus_sync_call_method(tmp_ctx, conn, raw_message, NULL, NULL,
                                    dbus_message_get_path(raw_message),
                                    dbus_message_get_interface(raw_message),
                                    dbus_message_get_member(raw_message),
                                    NULL, &reply);
        </toggle>
        <toggle name="if-input-arguments">
        ret = sbus_sync_call_method(tmp_ctx, conn, NULL,
                                    (sbus_invoker_writer_fn)_sbus_invoker_write_${input-signature},
                                    bus, path, iface, method, &in, &reply);
        </toggle>
        <toggle name="if-empty-input">
        ret = sbus_sync_call_method(tmp_ctx, conn, NULL, NULL,
                                    bus, path, iface, method, NULL, &reply);
        </toggle>
        if (ret != EOK) {
            goto done;
        }

        <toggle name="if-raw-output">
        /* Bounded reference cannot be unreferenced with dbus_message_unref.
         * For that reason we do not allow NULL memory context as it would
         * result in leaking the message memory. */
        if (mem_ctx == NULL) {
            ret = EINVAL;
            goto done;
        }

        ret = sbus_message_bound_steal(mem_ctx, reply);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to steal message [%d]: %s\n",
                  ret, sss_strerror(ret));
            goto done;
        }

        *_reply = reply;

        </toggle>
        <toggle name="if-output-arguments">
        ret = sbus_read_output(out, reply, (sbus_invoker_reader_fn)_sbus_invoker_read_${output-signature}, out);
        if (ret != EOK) {
            goto done;
        }

        <loop name="out-static">
        *_arg${index} = out->arg${index};
        </loop>
        <loop name="out-talloc">
        *_arg${index} = talloc_steal(mem_ctx, out->arg${index});
        </loop>

        </toggle>
        ret = EOK;

    done:
        talloc_free(tmp_ctx);

        return ret;
    }

</template>

<template name="method-caller">
    errno_t
    sbus_call_${token}
        (<toggle name="if-use-talloc">TALLOC_CTX *mem_ctx,
         struct sbus_sync_connection *conn,
         <or>struct sbus_sync_connection *conn,
         </toggle><toggle name="if-raw-input">
         DBusMessage *raw_message<or>
         const char *busname,
         const char *object_path<loop name="in">,
         ${type} arg_${name}</loop></toggle>
         <toggle name="if-raw-output">,
         DBusMessage **_reply)
         <or><loop name="out">,
         ${type} _arg_${name}</loop>)
         </toggle>
    {
         return sbus_method_in_${input-signature}_out_${output-signature}(<toggle name="if-use-talloc">mem_ctx, </toggle>conn,
              <toggle name="if-raw-input">
              raw_message<or>
              busname, object_path, "${iface}", "${method}"<loop name="in">, arg_${name}</loop></toggle>
              <toggle name="if-raw-output">,
              _reply);
              <or><loop name="out">,
              _arg_${name}</loop>);
              </toggle>
    }

</template>

<template name="signal-invoker">
    static void
    sbus_emit_signal_${input-signature}
        (struct sbus_sync_connection *conn,
         <toggle name="if-raw-input">
         DBusMessage *raw_message)
         <or>
         const char *path,
         const char *iface,
         const char *signal_name<loop name="in">,
         ${type} arg${index}</loop>)
         </toggle>
    {
        <toggle name="if-input-arguments">
        struct _sbus_invoker_args_${input-signature} args;

        <loop name="in">
        args.arg${index} = arg${index};
        </loop>

        </toggle>
        <toggle name="if-raw-input">
        sbus_sync_call_signal(conn, raw_message, NULL,
                              dbus_message_get_path(raw_message),
                              dbus_message_get_interface(raw_message),
                              dbus_message_get_member(raw_message), NULL);
        </toggle>
        <toggle name="if-input-arguments">
        sbus_sync_call_signal(conn, NULL, (sbus_invoker_writer_fn)_sbus_invoker_write_${input-signature},
                              path, iface, signal_name, &args);
        </toggle>
        <toggle name="if-empty-input">
        sbus_sync_call_signal(conn, NULL, NULL, path, iface, signal_name, NULL);
        </toggle>
    }

</template>

<template name="signal-caller">
    void
    sbus_sync_emit_${token}
        (struct sbus_sync_connection *conn,
         <toggle name="if-raw-input">
         DBusMessage *raw_message)
         <or>
         const char *object_path<loop name="in">,
         ${type} arg_${name}</loop>)
         </toggle>
    {
        <toggle name="if-raw-input">
        sbus_emit_signal_${input-signature}(conn, raw_message);
        <or>
        sbus_emit_signal_${input-signature}(conn, object_path,
            "${iface}", "${signal}"<loop name="in">, arg_${name}</loop>);
        </toggle>
    }

</template>

<template name="get-invoker">
    static errno_t
    sbus_get_${output-signature}
        (<toggle name="if-use-talloc">TALLOC_CTX *mem_ctx,
         struct sbus_sync_connection *conn,
         <or>struct sbus_sync_connection *conn,
         </toggle>
         const char *bus,
         const char *path,
         const char *iface,
         const char *property,
         ${output-type} _value)
    {
        TALLOC_CTX *tmp_ctx;
        struct _sbus_invoker_args_${output-signature} *out;
        sbus_value_reader_fn reader;
        sbus_value_reader_talloc_fn reader_talloc;
        DBusMessage *reply;
        errno_t ret;

        tmp_ctx = talloc_new(NULL);
        if (tmp_ctx == NULL) {
            DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
            return ENOMEM;
        }

        out = talloc_zero(tmp_ctx, struct _sbus_invoker_args_${output-signature});
        if (out == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "Unable to allocate space for output parameters!\n");
            ret = ENOMEM;
            goto done;
        }

        ret = sbus_call_DBusProperties_Get(tmp_ctx, conn,
                  bus, path, iface, property, &reply);
        if (ret != EOK) {
            goto done;
        }

        reader = <toggle line name="if-use-talloc">NULL<or>(sbus_value_reader_fn)sbus_iterator_read_${output-signature}</toggle>;
        reader_talloc = <toggle line name="if-use-talloc">(sbus_value_reader_talloc_fn)sbus_iterator_read_${output-signature}<or>NULL</toggle>;
        ret = sbus_parse_get_message(out, reader, reader_talloc, reply, &out->arg0);
        if (ret != EOK) {
            goto done;
        }

        *_value = <toggle line name="if-use-talloc">talloc_steal(mem_ctx, out->arg0)<or>out->arg0</toggle>;

        ret = EOK;

    done:
        talloc_free(tmp_ctx);

        return ret;
    }

</template>

<template name="set-invoker">
    static errno_t
    sbus_set_${input-signature}
        (struct sbus_sync_connection *conn,
         const char *bus,
         const char *path,
         const char *iface,
         const char *property,
         ${input-type} value)
    {
        TALLOC_CTX *tmp_ctx;
        struct _sbus_invoker_args_${input-signature} in;
        DBusMessage *raw_message;
        errno_t ret;

        tmp_ctx = talloc_new(NULL);
        if (tmp_ctx == NULL) {
            DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
            return ENOMEM;
        }

        in.arg0 = value;

        raw_message = sbus_create_set_call(tmp_ctx,
                          (sbus_invoker_writer_fn)_sbus_invoker_write_${input-signature},
                          bus, path, iface, property,
                          "${dbus-type}", &in);
        if (raw_message == NULL) {
            ret = ENOMEM;
            goto done;
        }

        ret = sbus_call_DBusProperties_Set(conn, raw_message);
        if (ret != EOK) {
            goto done;
        }

        ret = EOK;

    done:
        talloc_free(tmp_ctx);

        return ret;
    }

</template>

<template name="property-caller">
    <toggle name="get-static">
    errno_t
    sbus_get_${token}
        (struct sbus_sync_connection *conn,
         const char *busname,
         const char *object_path,
         ${output-type} _value)
    {
        return sbus_get_${output-signature}(conn, busname, object_path,
                    "${iface}", "${property}", _value);
    }

    </toggle>
    <toggle name="get-talloc">
    errno_t
    sbus_get_${token}
        (TALLOC_CTX *mem_ctx,
         struct sbus_sync_connection *conn,
         const char *busname,
         const char *object_path,
         ${output-type} _value)
    {
        return sbus_get_${output-signature}(mem_ctx, conn, busname, object_path,
                    "${iface}", "${property}", _value);
    }

    </toggle>
    <toggle name="set">
    errno_t
    sbus_set_${token}
        (struct sbus_sync_connection *conn,
         const char *busname,
         const char *object_path,
         ${input-type} value)
    {
        return sbus_set_${input-signature}(conn, busname, object_path,
                   "${iface}", "${property}", value);
    }

    </toggle>
</template>

<template name="getall-caller">
    errno_t
    sbus_getall_${token}
        (TALLOC_CTX *mem_ctx,
         struct sbus_sync_connection *conn,
         const char *busname,
         const char *object_path,
         struct sbus_all_${token} **_properties)
    {
        TALLOC_CTX *tmp_ctx;
        struct sbus_all_${token} *properties;
        DBusMessage *reply;
        errno_t ret;

        tmp_ctx = talloc_new(NULL);
        if (tmp_ctx == NULL) {
            DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
            return ENOMEM;
        }

        properties = talloc_zero(tmp_ctx, struct sbus_all_${token});
        if (properties == NULL) {
            ret = ENOMEM;
            goto done;
        }

        struct sbus_parse_getall_table table[] = {
            <loop name="property-static">
            {"${name}", (sbus_value_reader_fn)sbus_iterator_read_${output-signature}, NULL,
             &properties->${name}.value, &properties->${name}.is_set},
            </loop>
            <loop name="property-talloc">
            {"${name}", NULL, (sbus_value_reader_talloc_fn)sbus_iterator_read_${output-signature},
             &properties->${name}.value, &properties->${name}.is_set},
            </loop>
            {NULL, NULL, NULL, NULL, NULL}
        };

        ret = sbus_call_DBusProperties_GetAll(tmp_ctx, conn,
                  busname, object_path, "${iface}", &reply);
        if (ret != EOK) {
            goto done;
        }

        ret = sbus_parse_getall_message(properties, table, reply);
        if (ret != EOK) {
            goto done;
        }

        *_properties = talloc_steal(mem_ctx, properties);

        ret = EOK;

    done:
        talloc_free(tmp_ctx);

        return ret;
    }

</template>