summaryrefslogtreecommitdiff
path: root/cesar/maximus/prototest/fcall/src/test_fcall.c
blob: b8e48d10d18b17e6e2afe8ece34727b453d7dbf8 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    test_fcall.c
 * \brief   test program for fcall
 * \ingroup maximus/prototest/fcall
 *
 * this is a test program to check fcall module well work
 */
#include <cyg/infra/diag.h>
#include <cyg/kernel/kapi.h>
#include <malloc.h>
#include <errno.h>
#include "common/std.h"
#include "interface/fcall/interface_fcall.h"
#include "interface/fcall/inc/interface_fcall.h"
#include "lib/swap.h" // for 'swap16()' and 'swap32()'
#include "lib/read_word.h" // for 'read_u16_from_word()'
#include "maximus/prototest/fcall/inc/syscall.h"

#define MY_THREAD_STACK_SIZE (8192 / sizeof(int))

int my_thread_stack[MY_THREAD_STACK_SIZE];
cyg_handle_t my_thread_handle;
cyg_thread my_thread_obj;

interface_fcall_t *ctx;

int proto_in_fd = -1, proto_out_fd = -1;

void my_thread(cyg_addrword_t index)
{
    int len = 0, data_length = 0;
    u8 *my_mme;

    diag_write_string("=> my_thread\n");
    while(1)
    {
        len = 0;
        my_mme = malloc(SCI_MSG_MAX_SIZE);
        memset(my_mme, '\0', SCI_MSG_MAX_SIZE);
        while (INTERFACE_FCALL_PAYLOAD_OFFSET != len)
        {
            len = read(proto_in_fd, my_mme, INTERFACE_FCALL_PAYLOAD_OFFSET);
        }
        dbg_assert(INTERFACE_FCALL_PAYLOAD_OFFSET == len);
        data_length = swap16(read_u16_from_word(my_mme + INTERFACE_FCALL_PAYLOAD_OFFSET - sizeof(u16)));
        if ((len = read(proto_in_fd, my_mme + INTERFACE_FCALL_PAYLOAD_OFFSET, data_length)) < 0)
        {
            diag_printf("errno = %d: error when reading proto in file (data_length = %d)\n", errno, data_length);
            return;
        }
        diag_printf("READ len = %d - data length = %d\n", len, data_length);
        dbg_assert(len == data_length);
        diag_write_string("=> interface_fcall_mme_recv\n");
        interface_fcall_mme_recv (ctx, my_mme);
    }
}

void
my_interface_hle_send (void *user_data, uint *message, uint length)
{
    int len = 0, data_length = 0;
    u8 *data = NULL;

    dbg_assert_ptr(user_data);
    dbg_assert_ptr(message);
    dbg_assert(2 == length);

    diag_write_string("=> interface_hle_send\n");
    data = (u8 *)message[1];
    data_length = swap16(read_u16_from_word(data + INTERFACE_FCALL_PAYLOAD_OFFSET - sizeof(u16))) + INTERFACE_FCALL_PAYLOAD_OFFSET;
    if ((len = write(proto_out_fd, data, data_length)) < 0)
    {
        diag_write_string("error when writing proto out file\n");
        return;
    }
    dbg_assert(len == data_length);
    diag_printf("WRITE len = %d - data length = %d\n", len, data_length);
    diag_write_string("END interface_hle_send\n");

    free(data);
    return;
}

void
my_interface_hle_send_done (void *user_data, u8 *message)
{
    dbg_assert_ptr(user_data);
    dbg_assert_ptr(message);

    diag_write_string("=> interface_hle_send_done\n");
    free(message);
    diag_write_string("END interface_hle_send_done\n");

    return;
}

u8 *
my_interface_fcall_get_buffer_cb (void *user_data)
{
    u8 *buffer;
    buffer = malloc(SCI_MSG_MAX_SIZE);
    return buffer;
}

typedef struct my_struct
{
    int i1;
    int i2;
    bool b;
} my_struct_t;

typedef long long my_type_t;

my_type_t my_param4;

unsigned int my_param6 = 123; // 0x0000007B
unsigned int my_param7 = 456; // 0x000001C8
bool my_param8 = false;
bool my_param9 = true;
unsigned short my_param10 = 0xFFEE;
unsigned long my_param11 = 0xFFEEDDCC;

void
my_return_function (void *data)
{
    static fcall_param_t return_param;
    static sci_msg_t return_msg;
    static unsigned char return_buffer[SCI_MSG_MAX_SIZE];
    unsigned short *return_msg_id = (unsigned short *)data;

    /* init structures */
    fcall_param_init(&return_param, "function_1_rsp", *return_msg_id);
    if (0 != sci_msg_init(&return_msg, return_buffer, SCI_MSG_MAX_SIZE))
    {
        diag_printf("%s: errno = %d", __FUNCTION__, errno);
    }

    /* do other tasks ... */
    diag_write_string("=> my_return_function\n");

    /* now make the return parameter list */
    fcall_param_reset(&return_param);
    fcall_param_add(&return_param, &return_msg, "result_2", sizeof(my_type_t), &my_param4);
    fcall_return(ctx->fcall_ctx, &return_param, &return_msg);

    return; 
}

int
my_function1 (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    unsigned short asynchronous_msg_id = 0;
    my_struct_t my_param1;
    int my_param2[10];
    char my_param3[6];
    int i;

    /* get parameters content */
    fcall_param_bind(*param, *msg, "param_1", sizeof(my_struct_t), &my_param1);
    fcall_param_bind(*param, *msg, "param_2", 10*sizeof(int), my_param2);
    fcall_param_bind(*param, *msg, "param_3", 6, (char*)my_param3);
    fcall_param_bind(*param, *msg, "param_4", sizeof(my_type_t), &my_param4);

    /* do other tasks ... */
    diag_write_string("=> my_function1\n");
    diag_printf("param 1 . i1 = %d\n", my_param1.i1);
    diag_printf("param 1 . i2 = %d\n", my_param1.i2);
    diag_printf("param 1 . b = %d\n", my_param1.b);
    diag_write_string("param 2 = ");
    for (i=0; i<10; i++)
    {
        diag_printf(" %d", my_param2[i]);
    }
    diag_write_string("\n");
    diag_printf("param 3 = %s\n", my_param3);
    diag_printf("param 4 = %lld\n", my_param4);

    /* prepare the future asynchronous return */
    fcall_param_set_async(*param, 1);
    asynchronous_msg_id = (*param)->msg_id;

    /* my_return_function() call */
    my_return_function(&asynchronous_msg_id);

    /* now return */    
    return 0; 
}

int
my_function2 (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    bool my_param5;
    char my_result1[] = "this is result 1\0";
    
    /* get parameters content */
    if (-1 == fcall_param_bind(*param, *msg, "param_5", sizeof(bool), &my_param5))
    {
        diag_printf("errno = %d\n", errno);
        diag_printf("param nb = %d\n", (*param)->param_nb);
    }

    /* do other tasks ... */
    diag_write_string("=> my_function2\n");

    /* now make the return parameter list */
    fcall_param_reset(*param);
    fcall_param_add(*param, *msg, "result_1", strlen(my_result1)+1, &my_result1);
    
    return 0; 
}

void
my_function_rsp (void *data)
{
    static fcall_param_t return_param;
    static sci_msg_t return_msg;
    static unsigned char return_buffer[SCI_MSG_MAX_SIZE];
    unsigned short *return_msg_id = (unsigned short *)data;

    /* init structures */
    fcall_param_init(&return_param, "function_rsp", *return_msg_id);
    if (0 != sci_msg_init(&return_msg, return_buffer, SCI_MSG_MAX_SIZE))
    {
        diag_printf("%s: errno = %d", __FUNCTION__, errno);
    }

    /* do other tasks ... */
    diag_write_string("=> my_function_rsp\n");

    /* now make the return parameter list */
    fcall_param_reset(&return_param);
    fcall_return(ctx->fcall_ctx, &return_param, &return_msg);

    return; 
}

int
my_function3 (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    unsigned short asynchronous_msg_id = 0;
    
    /* do other tasks ... */
    diag_write_string("=> my_function3\n");
    
    /* prepare the future asynchronous return */
    fcall_param_set_async(*param, 1);
    asynchronous_msg_id = (*param)->msg_id;
    
    /* call my_function_rsp() */
    my_function_rsp((void*)&asynchronous_msg_id);
    
    return 0; 
}

int
my_function4 (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    unsigned short asynchronous_msg_id = 0;
    
    /* do other tasks ... */
    diag_write_string("=> my_function4\n");

    /* prepare the future asynchronous return */
    fcall_param_set_async(*param, 1);
    asynchronous_msg_id = (*param)->msg_id;
    
    /* call my_function_rsp() */
    my_function_rsp((void*)&asynchronous_msg_id);
    
    return 0; 
}

int
my_function5 (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    unsigned short asynchronous_msg_id = 0;
    
    /* do other tasks ... */
    diag_write_string("=> my_function5\n");

    /* prepare the future asynchronous return */
    fcall_param_set_async(*param, 1);
    asynchronous_msg_id = (*param)->msg_id;
    
    /* call my_function_rsp() */
    my_function_rsp((void*)&asynchronous_msg_id);
    
    return 0; 
}

void
cyg_user_start(void)
{
    int user_data = 123;
    ctx = interface_fcall_init (&my_interface_hle_send,
                                &my_interface_hle_send_done,
                                &my_interface_fcall_get_buffer_cb,
                                &user_data);

    fcall_register(ctx->fcall_ctx, "function_1", (void*)&my_function1, NULL);
    fcall_register(ctx->fcall_ctx, "function_2", (void*)&my_function2, NULL);
    fcall_register(ctx->fcall_ctx, "function_3", (void*)&my_function3, NULL);
    fcall_register(ctx->fcall_ctx, "function_4", (void*)&my_function4, NULL);
    fcall_register(ctx->fcall_ctx, "function_5", (void*)&my_function5, NULL);

    probe_register(ctx->probe_ctx, "param_6", sizeof(unsigned int), &my_param6);
    probe_register(ctx->probe_ctx, "param_7", sizeof(unsigned int), &my_param7);
    probe_register(ctx->probe_ctx, "param_8", sizeof(bool), &my_param8);
    probe_register(ctx->probe_ctx, "param_9", sizeof(bool), &my_param9);
    probe_register(ctx->probe_ctx, "param_10", sizeof(unsigned short), &my_param10);
    probe_register(ctx->probe_ctx, "param_11", sizeof(unsigned long), &my_param11);

    my_param6 = swap32(my_param6);
    my_param7 = swap32(my_param7);
    my_param10 = swap16(my_param10);
    my_param11 = swap32(my_param11);

    unlink("/tmp/proto_in");
    unlink("/tmp/proto_out");
    if ( (mknod("/tmp/proto_in", 0770 | S_IFIFO, 0) < 0)
         || (mknod("/tmp/proto_out", 0770 | S_IFIFO, 0) < 0) )
    {
        diag_write_string("error when creating proto inout files\n");
        return;
    }
    if ((proto_in_fd = open("/tmp/proto_in", O_RDWR, S_IRWXU | S_IRWXG)) < 0)
    {
        diag_write_string("error when opening proto in file\n");
        return;
    }
    if ((proto_out_fd = open("/tmp/proto_out", O_RDWR, S_IRWXU | S_IRWXG)) < 0)
    {
        diag_write_string("error when opening proto out file\n");
        return;
    }

    cyg_thread_create(12, my_thread, (cyg_addrword_t) 0,
                      "My Thread", &my_thread_stack, MY_THREAD_STACK_SIZE,
                      &my_thread_handle, &my_thread_obj);
    cyg_thread_resume(my_thread_handle);
    diag_write_string("scheduler starting...\n");
    cyg_scheduler_start();

    //interface_fcall_uninit(ctx);
    //close(proto_in_fd);
    //close(proto_out_fd);

    return;
}