summaryrefslogtreecommitdiff
path: root/idlc/c_gen/c_proxy_body_gen_cb.h
blob: f80f8480ae89584c3e4d56950a7e1b680d486066 (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
/*
 * 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_PROXY_BODY_GEN_CB_H_
#define IDLC_C_GEN_C_PROXY_BODY_GEN_CB_H_

const char CB_INTERFACE_STRUCT[] =
R"__c_cb(
struct ##_s {
    char *stub_appid;
    rpc_port_proxy_h proxy;
    rpc_port_h port;
    rpc_port_proxy_##_callback_s callback;
    void *user_data;
    GList *delegates;
    GRecMutex mutex;
};
)__c_cb";

const char CB_DELEGATE_STRUCT[] =
R"__c_cb(
struct ##_s {
    rpc_port_parcelable_t parcelable;
    int id;
    int seq_id;
    ## callback;
    bool once;
    void *user_data;
};
)__c_cb";

const char CB_DELEGATE_SERIALIZER[] =
R"__c_cb(
static void __##_to(rpc_port_parcel_h parcel, void *data)
{
    struct ##_s *handle = data;

    if (!handle) {
        dlog_print(DLOG_ERROR, LOG_TAG, "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_DELEGATE_DESERIALIZER[] =
R"__c_cb(
static void __##_from(rpc_port_parcel_h parcel, void *data)
{
    struct ##_s *handle = data;

    if (!handle) {
        dlog_print(DLOG_ERROR, LOG_TAG, "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_DELEGATE_CTOR[] =
R"__c_cb(
rpc_port_##_h rpc_port_##_create(## callback, bool once, void *user_data)
{
    struct ##_s *handle;
    static int seq_num;

    handle = calloc(1, sizeof(struct ##_s));
    if (!handle) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Out of memory");
        return NULL;
    }

    handle->parcelable.to = __##_to;
    handle->parcelable.from= __##_from;
    handle->id = $$_DELEGATE_$$;
    handle->seq_id = seq_num++;
    handle->callback = callback;
    handle->once = once;
    handle->user_data = user_data;

    return handle;
}
)__c_cb";

const char CB_DELEGATE_DISPOSER[] =
R"__c_cb(
int rpc_port_proxy_##_dispose(rpc_port_proxy_$$_h proxy, rpc_port_##_h delegate)
{
    struct ##_s *handle;
    GList *iter;

    if (!proxy || !delegate) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Invalid handle %p %p", proxy, delegate);
        return -1;
    }

    iter = proxy->delegates;
    while (iter) {
        handle = (struct ##_s *)iter->data;
        if (handle == delegate) {
            proxy->delegates = g_list_remove_link(proxy->delegates, iter);
            free(handle);
            g_list_free(iter);
            return 0;
        }
        iter = g_list_next(iter);
    }

    return -1;
}
)__c_cb";

const char CB_DELEGATE_INVOKER[] =
R"__c_cb(
static void __$$_delegate_$$(GList **list, rpc_port_parcel_h parcel, int seq_id, int id)
{
$$
    do {
        struct ##_s *handle;
        GList *iter;

        iter = *list;
        while (iter) {
            handle = (struct ##_s *)iter->data;
            if (handle->seq_id == seq_id && handle->id == id) {
                $$
                if (handle->once) {
                    *list = g_list_remove_link(*list, iter);
                    free(handle);
                    g_list_free(iter);
                }
                break;
            }
            iter = g_list_next(iter);
        }
    } while (0);
$$
}
)__c_cb";

const char CB_PROCESS_RECEIVED_EVENT[] =
R"__c_cb(
static void __##_process_received_event(GList **list, rpc_port_parcel_h parcel)
{
$$
}
)__c_cb";

const char CB_PROCESS_RECEIVED_EVENT_IMPL[] =
R"__c_cb(int id;
int seq_id;
bool once;

rpc_port_parcel_read_int32(parcel, &id);
rpc_port_parcel_read_int32(parcel, &seq_id);
rpc_port_parcel_read_bool(parcel, &once);

if (id > 0 && id < (sizeof(__##_delegate_table) / sizeof(__##_delegate_table[0]))) {
    if (__##_delegate_table[id])
        __##_delegate_table[id](list, parcel, seq_id, id);
} else {
    dlog_print(DLOG_WARN, LOG_TAG, "Unknown id(%d)", id);
})__c_cb";

const char CB_CONSUME_COMMAND[] =
R"__c_cb(
static rpc_port_parcel_h __##_consume_command(rpc_port_proxy_##_h h)
{
    rpc_port_parcel_h parcel = NULL;
    int cmd = -1;

    do {
        rpc_port_parcel_create_from_port(&parcel, h->port);
        if (!parcel)
            break;

        rpc_port_parcel_read_int32(parcel, &cmd);
        if (cmd == ##_METHOD_Result)
            return parcel;

        __##_process_received_event(&h->delegates, parcel);
        rpc_port_parcel_destroy(parcel);
        parcel = NULL;
    } while (true);

    return NULL;
}
)__c_cb";

const char CB_ON_CONNECTED[] =
R"__c_cb(
static void __##_on_connected(const char *endpoint, const char *port_name, rpc_port_h port, void *data)
{
    rpc_port_proxy_##_h handle = data;

    handle->port = port;
    if (handle->callback.connected)
        handle->callback.connected(handle, handle->user_data);
    dlog_print(DLOG_INFO, LOG_TAG, "[__RPC_PORT__] endpoint(%s), port_name(%s)", endpoint, port_name);
}
)__c_cb";

const char CB_ON_DISCONNECTED[] =
R"__c_cb(
static void __##_on_disconnected(const char *endpoint, const char *port_name, void *data)
{
    rpc_port_proxy_##_h handle = data;

    handle->port = NULL;
    if (handle->callback.disconnected)
        handle->callback.disconnected(handle, handle->user_data);
    dlog_print(DLOG_INFO, LOG_TAG, "[__RPC_PORT__] endpoint(%s), port_name(%s)", endpoint, port_name);
}
)__c_cb";

const char CB_ON_REJECTED[] =
R"__c_cb(
static void __##_on_rejected(const char *endpoint, const char *port_name, void *data)
{
    rpc_port_proxy_##_h handle = data;

    handle->port = NULL;
    if (handle->callback.rejected)
        handle->callback.rejected(handle, handle->user_data);
    dlog_print(DLOG_INFO, LOG_TAG, "[__RPC_PORT__] endpoint(%s), port_name(%s)", endpoint, port_name);
}
)__c_cb";

const char CB_ON_RECEIVED[] =
R"__c_cb(
static void __##_on_received(const char *endpoint, const char *port_name, void *data)
{
    rpc_port_proxy_##_h handle = data;
    rpc_port_parcel_h parcel_received;
    int cmd = -1;

    if (g_rec_mutex_trylock(&handle->mutex) == FALSE) {
        dlog_print(DLOG_WARN, LOG_TAG, "Failed to try to lock the mutex");
        return;
    }

    rpc_port_parcel_create_from_port(&parcel_received, handle->port);
    rpc_port_parcel_read_int32(parcel_received, &cmd);
    if (cmd != ##_METHOD_Callback) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Invalid protocol");
        rpc_port_parcel_destroy(parcel_received);
        g_rec_mutex_unlock(&handle->mutex);
        return;
    }

    __##_process_received_event(&handle->delegates, parcel_received);
    rpc_port_parcel_destroy(parcel_received);
    dlog_print(DLOG_INFO, LOG_TAG, "[__RPC_PORT__] endpoint(%s), port_name(%s)", endpoint, port_name);
    g_rec_mutex_unlock(&handle->mutex);
}
)__c_cb";

const char CB_INTERFACE_METHODS[] =
R"__c_cb(
$$rpc_port_proxy_##_invoke_$$(rpc_port_proxy_##_h h$$)
{
    rpc_port_parcel_h parcel;
$$

    if (!h$$) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Invalid parameter");
        return$$;
    }

    if (!h->port) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Not connected");
        return$$;
    }

    rpc_port_parcel_create(&parcel);
    rpc_port_parcel_write_int32(parcel, ##_METHOD_$$);
$$
    rpc_port_parcel_send(parcel, h->port);
    rpc_port_parcel_destroy(parcel);
$$
}
)__c_cb";

const char CB_INTERFACE_CTOR[] =
R"__c_cb(
static struct ##_s *__create_##(const char *stub_appid, rpc_port_proxy_##_callback_s *callback, void *user_data)
{
    struct ##_s *handle;

    handle = calloc(1, sizeof(struct ##_s));
    if (!handle) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Out of memory");
        return NULL;
    }

    handle->stub_appid = strdup(stub_appid);
    if (!handle->stub_appid) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Out of memory");
        free(handle);
        return NULL;
    }

    rpc_port_proxy_create(&handle->proxy);
    if (!handle->proxy) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Failed to create proxy");
        free(handle->stub_appid);
        free(handle);
        return NULL;
    }

    g_rec_mutex_init(&handle->mutex);

    handle->callback = *callback;
    handle->user_data = user_data;

    return handle;
}
)__c_cb";

const char CB_INTERFACE_DTOR[] =
R"__c_cb(
static void __destroy_##(struct ##_s *h)
{
    if (!h)
        return;

    g_rec_mutex_clear(&h->mutex);

    if (h->delegates)
        g_list_free_full(h->delegates, free);

    if (h->proxy)
        rpc_port_proxy_destroy(h->proxy);

    if (h->stub_appid)
        free(h->stub_appid);

    free(h);
}
)__c_cb";

const char CB_INTERFACE_CONNECT[] =
R"__c_cb(
int rpc_port_proxy_##_connect(const char *stub_appid, rpc_port_proxy_##_callback_s *callback, void *user_data, rpc_port_proxy_##_h *h)
{
    struct ##_s *handle;
    int r;

    if (!stub_appid || !callback || !h) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Invalid parameter");
        return -1;
    }

    handle = __create_##(stub_appid, callback, user_data);
    if (!handle)
        return -1;

    r = rpc_port_proxy_add_connected_event_cb(handle->proxy, __##_on_connected, handle);
    if (r != 0) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Failed to add connected event cb. err = %d", r);
        __destroy_##(handle);
        return r;
    }

    r = rpc_port_proxy_add_disconnected_event_cb(handle->proxy, __##_on_disconnected, handle);
    if (r != 0) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Failed to add disconnected event cb. err = %d", r);
        __destroy_##(handle);
        return r;
    }

    r = rpc_port_proxy_add_rejected_event_cb(handle->proxy, __##_on_rejected, handle);
    if (r != 0) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Failed to add rejected event cb. err = %d", r);
        __destroy_##(handle);
        return r;
    }

    r = rpc_port_proxy_add_received_event_cb(handle->proxy, __##_on_received, handle);
    if (r != 0) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Failed to add received event cb. err = %d", r);
        __destroy_##(handle);
        return r;
    }

    r = rpc_port_proxy_connect(handle->proxy, stub_appid, "##");
    if (r != 0) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Failed to connect ##(%s)", stub_appid);
        __destroy_##(handle);
        return r;
    }

    *h = handle;

    return 0;
}
)__c_cb";

const char CB_INTERFACE_DISCONNECT[] =
R"__c_cb(
int rpc_port_proxy_##_disconnect(rpc_port_proxy_##_h h)
{
    if (!h) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Invalid parameter");
        return -1;
    }

    __destroy_##(h);
    return 0;
}
)__c_cb";

const char CB_DELEGATE_BLOCK[] =
R"__c_cb(
do {
    struct ##_s *handle = $$;

    rpc_port_parcel_write(parcel, &handle->parcelable, handle);
    h->delegates = g_list_append(h->delegates, handle);
} while (0);
)__c_cb";

const char CB_RECEIVE_BLOCK[] =
R"__c_cb(
g_rec_mutex_lock(&h->mutex);
do {
    rpc_port_parcel_h parcel_received;
$$
    parcel_received = __$$_consume_command(h);
    if (!parcel_received) {
        dlog_print(DLOG_ERROR, LOG_TAG, "Invalid protocol");
        break;
    }

$$
    rpc_port_parcel_destroy(parcel_received);
} while (0);
g_rec_mutex_unlock(&h->mutex);
)__c_cb";

#endif  // IDLC_C_GEN_C_PROXY_BODY_GEN_CB_H_