summaryrefslogtreecommitdiff
path: root/idlc/c_gen/c_stub_body_gen_cb.h
blob: 1b766a8743869080111c66bd8dbd919013052337 (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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IDLC_C_GEN_C_STUB_BODY_GEN_CB_H_
#define IDLC_C_GEN_C_STUB_BODY_GEN_CB_H_

const char CB_INTERFACE_METHOD[] =
R"__c_cb(
static int __$$_method_$$(rpc_port_h port, rpc_port_parcel_h parcel, void *data)
{
    rpc_port_stub_$$_context_h context = data;
    rpc_port_h callback_port;
    int r = rpc_port_stub_get_port(__$$_stub, RPC_PORT_PORT_CALLBACK,
            context->instance, &callback_port);

    if (r != 0) {
      _E("Failed to get callback port");
    }
$$
    return 0;
}
)__c_cb";

const char CB_INTERFACE_METHOD_TABLE[] =
R"__c_cb(static stub_method __$$_method_table[] = {$$
};
)__c_cb";

const char CB_INTERFACE_METHOD_FORMAT[] =
R"__c_cb(
[$$] = $$,)__c_cb";

const char CB_INTERFACE_ON_CONNECTED[] =
R"__c_cb(
static void __##_on_connected(const char *sender, const char *instance, void *data)
{
    rpc_port_stub_##_context_h context;

    _I("[__RPC_PORT__] sender(%s), instance(%s)", sender, instance);
    context = __create_##_context(sender, instance);
    if (!context)
        return;

    if (context->callback.create)
        context->callback.create(context, context->user_data);
    __##_contexts = g_list_append(__##_contexts, context);
}
)__c_cb";

const char CB_INTERFACE_ON_DISCONNECTED[] =
R"__c_cb(
static void __##_on_disconnected(const char *sender, const char *instance, void *data)
{
    rpc_port_stub_##_context_h context;

    _I("[__RPC_PORT__] sender(%s), instance(%s)", sender, instance);
    context = __find_##_context(instance);
    if (!context)
        return;

    if (context->callback.terminate)
        context->callback.terminate(context, context->user_data);
    __##_contexts = g_list_remove(__##_contexts, context);
    __destroy_##_context(context);
}
)__c_cb";

const char CB_INTERFACE_ON_RECEIVED[] =
R"__c_cb(
static int __##_on_received(const char *sender, const char *instance, rpc_port_h port, void *data)
{
    rpc_port_stub_##_context_h context;
    rpc_port_parcel_h parcel;
    int cmd = -1;
    int r;

    _I("[__RPC_PORT__] sender(%s), instance(%s)", sender, instance);
    context = __find_##_context(instance);
    if (!context) {
        _E("Failed to find ## context(%s)", instance);
        return -1;
    }

    context->port = port;
    r = rpc_port_parcel_create_from_port(&parcel, port);
    if (r != 0) {
        _E("Failed to create parcel from port. context(%s)", instance);
        __##_on_disconnected(sender, instance, data);
        return r;
    }

    rpc_port_parcel_read_int32(parcel, &cmd);
    if (cmd > 1 && cmd < (sizeof(__##_method_table) / sizeof(__##_method_table[0]))) {
         if (__##_method_table[cmd])
             r = __##_method_table[cmd](port, parcel, context);
    } else {
         _E("Unknown Command(%d)", cmd);
         r = -1;
    }

    rpc_port_parcel_destroy(parcel);

    return r;
}
)__c_cb";

const char CB_INTERFACE_REGISTER[] =
R"__c_cb(
int rpc_port_stub_##_register(rpc_port_stub_##_callback_s *callback, void *user_data)
{
    int r;

    if (__##_stub) {
        _W("Already exists");
        return -1;
    }

    if (!callback) {
        _E("Invalid parameter");
        return -1;
    }

    __##_callback = *callback;
    __##_user_data = user_data;
    r = rpc_port_stub_create(&__##_stub, "##");
    if (r != 0) {
        _E("Failed to create stub handle");
        return r;
    }

    r = rpc_port_stub_add_received_event_cb(__##_stub, __##_on_received, NULL);
    if (r != 0) {
        _E("Failed to add received event callback");
        rpc_port_stub_destroy(__##_stub);
        __##_stub = NULL;
        return r;
    }

    r = rpc_port_stub_add_connected_event_cb(__##_stub, __##_on_connected, NULL);
    if (r != 0) {
        _E("Failed to add connected event callback");
        rpc_port_stub_destroy(__##_stub);
        __##_stub = NULL;
        return r;
    }

    r = rpc_port_stub_add_disconnected_event_cb(__##_stub, __##_on_disconnected, NULL);
    if (r != 0) {
        _E("Failed to add disconnected event callback");
        rpc_port_stub_destroy(__##_stub);
        __##_stub = NULL;
        return r;
    }

    r = __##_add_privileges();
    if (r != 0) {
        _E("Failed to add privileges");
        rpc_port_stub_destroy(__##_stub);
        __##_stub = NULL;
        return r;
    }

    r = rpc_port_stub_listen(__##_stub);
    if (r != 0) {
        _E("Failed to listen stub");
        rpc_port_stub_destroy(__##_stub);
        __##_stub = NULL;
        return r;
    }

    return 0;
}
)__c_cb";

const char CB_INTERFACE_UNREGISTER[] =
R"__c_cb(
int rpc_port_stub_##_unregister(void)
{
    int r;

    if (!__##_stub)
        return -1;

    if (__##_contexts) {
        g_list_free_full(__##_contexts, __destroy_##_context);
        __##_contexts = NULL;
    }

    r = rpc_port_stub_destroy(__##_stub);
    __##_stub = NULL;

    return r;
}
)__c_cb";

const char CB_GLOBALS[] =
R"__c_cb(
static rpc_port_stub_h __##_stub;
static rpc_port_stub_##_callback_s __##_callback;
static void *__##_user_data;
static GList *__##_contexts;
)__c_cb";

const char CB_INTERFACE_DELEGATOR_DECL[] =
R"__c_cb(
struct $$_s {
    rpc_port_parcelable_t parcelable;
    rpc_port_h port;
    int id;
    int seq_id;
    bool once;
    bool valid;
};
)__c_cb";

const char CB_INTERFACE_DELEGATOR_CTOR[] =
R"__c_cb(
int rpc_port_##_create(rpc_port_##_h *h)
{
    struct ##_s *handle;
    static int seq_num;

    if (!h) {
        _E("Invalid parameter");
        return -1;
    }

    handle = calloc(1, sizeof(struct ##_s));
    if (!handle) {
        _E("Out of memory");
        return -1;
    }

    handle->parcelable.to = __##_to;
    handle->parcelable.from = __##_from;
    handle->id = $$_DELEGATE_$$;
    handle->seq_id = g_atomic_int_add(&seq_num, 1) + 1;
    handle->once = false;
    handle->valid = true;

    *h = handle;

    return 0;
}
)__c_cb";

const char CB_INTERFACE_DELEGATOR_DTOR[] =
R"__c_cb(
int rpc_port_##_destroy(rpc_port_##_h h)
{
    if (!h) {
        _E("Invalid parameter");
        return -1;
    }

    free(h);

    return 0;
}
)__c_cb";

const char CB_INTERFACE_DELEGATOR_SERIALIZER[] =
R"__c_cb(
static void __##_to(rpc_port_parcel_h parcel, void *data)
{
    rpc_port_##_h handle = data;

    if (!handle) {
        _E("Invalid parameter");
        return;
    }

    rpc_port_parcel_write_int32(parcel, handle->id);
    rpc_port_parcel_write_int32(parcel, handle->seq_id);
    rpc_port_parcel_write_bool(parcel, handle->once);
}
)__c_cb";

const char CB_INTERFACE_DELEGATOR_DESERIALIZER[] =
R"__c_cb(
static void __##_from(rpc_port_parcel_h parcel, void *data)
{
    rpc_port_##_h handle = data;

    if (!handle) {
        _E("Invalid parameter");
        return;
    }

    rpc_port_parcel_read_int32(parcel, &handle->id);
    rpc_port_parcel_read_int32(parcel, &handle->seq_id);
    rpc_port_parcel_read_bool(parcel, &handle->once);
}
)__c_cb";

const char CB_INTERFACE_DELEGATOR_CLONER[] =
R"__c_cb(
int rpc_port_##_clone(rpc_port_##_h h, rpc_port_##_h *clone)
{
    rpc_port_##_h handle;

    if (!h || !clone) {
        _E("Invalid parameter");
        return -1;
    }

    handle = calloc(1, sizeof(struct ##_s));
    if (!handle) {
        _E("Out of memory");
        return -1;
    }

    handle->parcelable = h->parcelable;
    handle->port = h->port;
    handle->id = h->id;
    handle->seq_id = h->seq_id;
    handle->once = h->once;
    handle->valid = h->valid;

    *clone = handle;

    return 0;
}
)__c_cb";

const char CB_INTERFACE_DELEGATOR_INVOKER[] =
R"__c_cb(
int rpc_port_$$_invoke($$)
{
    rpc_port_parcel_h parcel = NULL;

    if (!h || !h->port) {
        _E("Invalid parameter");
        return -1;
    }

    if (h->once && !h->valid) {
        _E("Invalid callback");
        return -1;
    }

    rpc_port_parcel_create(&parcel);
    if (!parcel) {
        _E("Failed to create parcel");
        return -1;
    }

    rpc_port_parcel_write_int32(parcel, $$_METHOD_Callback);
    rpc_port_parcel_write(parcel, &h->parcelable, h);
$$
    rpc_port_parcel_send(parcel, h->port);
    rpc_port_parcel_destroy(parcel);
    h->valid = false;

    return 0;
}
)__c_cb";

const char CB_INTERFACE_DELEGATOR_PORT_SETTER[] =
R"__c_cb(
int rpc_port_##_set_port(rpc_port_##_h h, rpc_port_h port)
{
    if (!h || !port) {
        _E("Invalid parameter");
        return -1;
    }

    h->port = port;

    return 0;
}
)__c_cb";

const char CB_INTERFACE_CONTEXT_DECL[] =
R"__c_cb(
struct ##_context_s {
    char *sender;
    char *instance;
    rpc_port_h port;
    void *tag;
    rpc_port_stub_##_callback_s callback;
    void *user_data;
};
)__c_cb";

const char CB_INTERFACE_CONTEXT_CTOR[] =
R"__c_cb(
static struct ##_context_s *__create_##_context(const char *sender, const char *instance)
{
    struct ##_context_s *handle;

    handle = calloc(1, sizeof(struct ##_context_s));
    if (!handle) {
        _E("Out of memory");
        return NULL;
    }

    handle->sender = strdup(sender);
    if (!handle->sender) {
        _E("Out of memory");
        free(handle);
        return NULL;
    }

    handle->instance = strdup(instance);
    if (!handle->instance) {
        _E("Out of memory");
        free(handle->sender);
        free(handle);
        return NULL;
    }

    handle->callback = __##_callback;
    handle->user_data = __##_user_data;

    return handle;
}
)__c_cb";

const char CB_INTERFACE_CONTEXT_DTOR[] =
R"__c_cb(
static void __destroy_##_context(gpointer data)
{
    struct ##_context_s *handle = data;

    if (!handle) {
        _E("Critical error!");
        return;
    }

    free(handle->instance);
    free(handle->sender);
    free(handle);
}
)__c_cb";

const char CB_INTERFACE_CONTEXT_FINDER[] =
R"__c_cb(
static struct ##_context_s *__find_##_context(const char *instance)
{
    struct ##_context_s *handle;
    GList *iter;

    iter = __##_contexts;
    while (iter) {
        handle = (struct ##_context_s *)iter->data;
        if (!strcmp(handle->instance, instance))
            return handle;
        iter = g_list_next(iter);
    }

    return NULL;
}
)__c_cb";

const char CB_INTERFACE_CONTEXT_TAG_SETTER[] =
R"__c_cb(
int rpc_port_stub_##_context_set_tag(rpc_port_stub_##_context_h ctx, void *tag)
{
    if (!ctx) {
        _E("Invalid parameter");
        return -1;
    }

    ctx->tag = tag;

    return 0;
}
)__c_cb";

const char CB_INTERFACE_CONTEXT_TAG_GETTER[] =
R"__c_cb(
int rpc_port_stub_##_context_get_tag(rpc_port_stub_##_context_h ctx, void **tag)
{
    if (!ctx || !tag) {
        _E("Invalid parameter");
        return -1;
    }

    *tag = ctx->tag;

    return 0;
}
)__c_cb";

const char CB_INTERFACE_CONTEXT_GET_SENDER[] =
R"__c_cb(
int rpc_port_stub_##_context_get_sender(rpc_port_stub_##_context_h ctx, char **sender)
{
    if (!ctx || !sender) {
        _E("Invalid parameter");
        return -1;
    }

    *sender = strdup(ctx->sender);
    if (*sender == NULL) {
        _E("Out of memory");
        return -1;
    }

    return 0;
}
)__c_cb";

const char CB_STUB_METHOD_TYPE[] =
R"__c_cb(
typedef int (*stub_method)(rpc_port_h, rpc_port_parcel_h, void *data);
)__c_cb";

const char CB_INTERFACE_ADD_PRIVILEGE[] =
R"__c_cb(
static int __$$_add_privileges(void)
{
$$
    return 0;
}
)__c_cb";

const char CB_INTERFACE_PRIVILEGE_BLOCK[] =
R"__c_cb(r = $$;
if (r != 0) {
    _E("Failed to add privilege($$)");
    return r;
}
)__c_cb";

const char CB_INTERFACE_TRUSTED_MODE_BLOCK[] =
R"__c_cb(r = $$;
if (r != 0) {
    _E("Failed to set trusted mode($$)");
    return r;
}
)__c_cb";

#endif  // IDLC_C_GEN_C_STUB_BODY_GEN_CB_H_