summaryrefslogtreecommitdiff
path: root/cesar/host/src/fcall.c
blob: 97fb83fd2e953867ec9106e27addb2feb6ba63c2 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
 
/**
 * \file    fcall.c
 * \brief   The 'function call' context management functions
 * \ingroup host
 *
 * This file provides 'function call' context management functions
 *
 * \todo   
 */

#include "common/std.h"
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "host/fcall.h"
#ifndef UNIT_TEST
#include "lib/swap.h"
#include "host/syscall.h"
#else /* UNIT_TEST */
#include <unistd.h>
#include <arpa/inet.h>
#endif /* UNIT_TEST */

/**
 * initialize a static fcall context, called during station context creation.
 * \param fcall fcall context to initialize
 * \param station station which uses the fcall context
 * \return 0 if ok, -1 if failed with errno=
 * - EINVAL if fcall or station is NULL
 */
int fcall_init(fcall_ctx_t *fcall, sci_ctx_t *sci)
{
    DBG_ASSERT(fcall);
    DBG_ASSERT(sci);
    if((fcall == NULL)
        || (sci == NULL))
    {
        errno = EINVAL;
        return -1;
    }
    
    memset(fcall, '\0', sizeof(fcall_ctx_t));
    fcall->sci = sci;
    sci_register_callback(sci, SCI_MSG_TYPE_FUNCTION_CALL, fcall_recv, fcall);
    return 0;
}

/**
 * fcall callback function registering
 * \param fcall pointer to current fcall context
 * \param id function string id
 * \param function pointer to callback function
 * \return 0 if ok, -1 if fail with errno=
 * - EINVAL if fcall or id is NULL
 * - ENAMETOOLONG if id length is >= FUNCTION_CALL_ID_MAX_SIZE
 * - ENOSPC if max number of registred function is reached
 * - EEXIST if function is already registered
 */
int fcall_register(
    fcall_ctx_t *fcall, 
    char *id, 
    int (* function)(fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data), 
    void *data)
{
    int index;
    
    DBG_ASSERT(fcall);
    DBG_ASSERT(id);
    DBG_ASSERT(function);
    if((fcall == NULL)
        || (id == NULL)
        || (function == NULL))
    {
        errno = EINVAL;
        return -1;
    }
    
    /* check id length */
    DBG_ASSERT(strlen(id) < FUNCTION_CALL_ID_MAX_SIZE);
    if(strlen(id) >= FUNCTION_CALL_ID_MAX_SIZE)
    {
        errno = ENAMETOOLONG;
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d because id length is invalid", __FUNCTION__, errno);
        return -1;
    }
    
    /* check if there is free place */
    DBG_ASSERT(fcall->function_nb < FUNCTION_CALL_FUNCTION_MAX_NB);
    if(fcall->function_nb >= FUNCTION_CALL_FUNCTION_MAX_NB)
    {
        errno = ENOSPC;
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d because there is no free space", __FUNCTION__, errno);
        return -1;
    }
    
    /* check if function is already registered */
    for(index = 0; index < fcall->function_nb; index++)
    {
        DBG_ASSERT(strcmp(fcall->function_table[index].id, id));
        if(!strcmp(fcall->function_table[index].id, id))
        {
            errno = EEXIST;
            station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                        "%s: errno = %d because function is already registered", __FUNCTION__, errno);
            return -1;
        }
    }
    
    /* register the function */
    strcpy(fcall->function_table[fcall->function_nb].id, id);
    fcall->function_table[fcall->function_nb].function = function;
    fcall->function_table[fcall->function_nb].data = data;

    /* increment the number of function */
    fcall->function_nb++;
    
    return 0;
}

/**
 * fill a blank fcall header with needed parameter number
 * \param fcall current fcall context
 * \param msg sci message to fill header
 * \param flags flags of message
 * \return 0 if ok, -1 if failed with errno:
 * - EINVAL if param or msg are NULL 
 */
int fcall_fill_hdr(
    fcall_ctx_t *fcall,
    sci_msg_t *msg,
    fcall_param_t *param,
    int flags)
{
    DBG_ASSERT(fcall);
    DBG_ASSERT(msg);
    DBG_ASSERT(param);
    if((fcall == NULL)
        || (msg == NULL)
        || (param == NULL))
    {
        errno = EINVAL;
        return -1;
    }
    
    param->id[FUNCTION_CALL_ID_MAX_SIZE - 1] = '\0'; /* just for security */
    /* add the function id */
    if(sci_msg_push(msg, strlen(param->id) + 1) < (int)strlen(param->id) + 1)
    {
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d because cannot add function id", __FUNCTION__, errno);
        return -1;
    }
    memcpy(msg->data_begin, param->id, strlen(param->id) + 1);
    
    /* add fcall header */
    if(sci_msg_push(msg, sizeof(struct fcall_msg_hdr)) < (int)sizeof(struct fcall_msg_hdr))
    {
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d because cannot add fcall header", __FUNCTION__, errno);
        return -1;
    }
    msg->hdr.fcall = (struct fcall_msg_hdr *)(msg->data_begin);
    /* fill it */
    msg->hdr.fcall->version = FUNCTION_CALL_VERSION;
    msg->hdr.fcall->type = FUNCTION_CALL_TYPE_RSP;
    msg->hdr.fcall->param_nb = param->param_nb;
    msg->hdr.fcall->msg_id = htons(param->msg_id);
    msg->hdr.fcall->flags = flags;
    
    return 0;
}

/**
 * send the return parameter list of asynchronous function call
 * \param fcall pointer to the current fcall context
 * \param pointer to returned parameter structure
 * \param msg pointer to returned parameter message buffer
 * \return 0 if ok, -1 if failed with errno:
 *  - EINVAL if fcall, param or msg is NULL
 *  - errno set by sci_send()
 */
int fcall_return(fcall_ctx_t *fcall, fcall_param_t *param, sci_msg_t *msg)
{
    DBG_ASSERT(fcall);
    DBG_ASSERT(param);
    DBG_ASSERT(msg);
    if((fcall == NULL)
        || (param == NULL)
        || (msg == NULL))
    {
        errno = EINVAL;
        return -1;
    }
    
    /* fill the fcall msg header to prepare the answer */
    if(fcall_fill_hdr(fcall, msg, param, 0) < 0)
    {
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d because cannot fill fcall msg header", __FUNCTION__, errno);
        return -1;
    }

    /* now fill the sci header */
    if(sci_fill_hdr(fcall->sci, msg, SCI_MSG_TYPE_FUNCTION_CALL, 0) < 0)
    {
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d because cannot fill sci header", __FUNCTION__, errno);
        return -1;
    }
        
    station_log(fcall->sci->station, STATION_LOG_DEBUG, STATION_LOGTYPE_FCALL,
        "%s id=%s, msg_id=%u, param_nb=%d", __FUNCTION__, param->id, param->msg_id, param->param_nb);

    /* send the message */
    if(sci_send(fcall->sci, msg) < 0)
    {
        station_log(fcall->sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_FCALL, "%s failed to send fcall (errno=%d)", __FUNCTION__, errno);
        return -1;
    }
    else
        return 0;
}

/**
 * fill fcall body with all function parameters
 * sci_msg_push() is called each time a parameter is added
 * \param fcall current fcall context
 * \param msg sci message to fill body
 * \param param current fcall param context
 * \return 0 if ok, -1 if failed with errno:
 * - EINVAL if param or msg are NULL
 * - ENOSPC if body overwhelm msg storage capacity
 */
//int fcall_fill_body(
//    fcall_ctx_t *fcall,
//    sci_msg_t *msg, 
//    fcall_param_t *param);

/**
 * fill fcall full content: header + body inside a message
 * \param fcall current fcall context
 * \param msg sci message to fill
 * \param flags flags of message
 * \return 0 if ok, -1 if failed with errno:
 * - EINVAL if param or msg are NULL
 * - ENOSPC if body = header overwhelmed msg storage capacity
 */
//int fcall_fill_msg(
//    fcall_ctx_t *fcall,
//    sci_msg_t *msg, 
//    int flags);

/**
 * fcall message processing function; called by sci_recv()
 * must be registred to sci layer with SCI_MSG_TYPE_FUNCTION_CALL type
 * \param fcall pointer to current fcall context
 * \param msg pointer to received message with sci pre-processing
 * \return 0 if ok, -1 if failed with errno=
 * - EINVAL if fcall or msg is NULL
 * - EPROTOTYPE if fcall type != FUNCTION_CALL_TYPE_REQ
 */ 
int fcall_recv(sci_msg_t *msg, void *fcall_data)
{
    int function_index, index;
    fcall_ctx_t *fcall;
    fcall_param_t param_recv, *param_ref;
    unsigned char return_flags = 0;
    int len = 0;

    fcall = (fcall_ctx_t *)fcall_data;

    DBG_ASSERT(fcall);
    DBG_ASSERT(msg);
    if((fcall == NULL)
        || (msg == NULL))
    {
        errno = EINVAL;
        return -1;
    }
    
    param_ref = &param_recv;
    fcall_param_init(&param_recv, "", 0);
    
    /* set header pointer in case of not already done */
    msg->hdr.fcall = (struct fcall_msg_hdr *)(msg->data_begin);
    /* check header content */
    DBG_ASSERT(msg->hdr.fcall->type == FUNCTION_CALL_TYPE_REQ);
    if(msg->hdr.fcall->type != FUNCTION_CALL_TYPE_REQ)
    {
        errno = EPROTOTYPE;
        station_log(fcall->sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d: bad type %x", __FUNCTION__, errno, msg->hdr.fcall->type);
        return -1;
    }
    
    /* fill the param structure */
    /* get the parameter number */
    param_ref->param_nb = msg->hdr.fcall->param_nb;
    param_ref->msg_id = ntohs(msg->hdr.fcall->msg_id);
    if(sci_msg_pop(msg, sizeof(struct fcall_msg_hdr)) < (int)sizeof(struct fcall_msg_hdr))
    {
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d because cannot get fcall header", __FUNCTION__, errno);
        return -1;
    }
    
    /* check function id len */
    DBG_ASSERT((strlen((char *)(msg->data_begin)) != 0)
                && (strlen((char *)(msg->data_begin)) < FUNCTION_CALL_ID_MAX_SIZE));
    if((strlen((char *)(msg->data_begin)) == 0)
        || (strlen((char *)(msg->data_begin)) >= FUNCTION_CALL_ID_MAX_SIZE))
    {
        errno = ENOENT;
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d: bad function id length", __FUNCTION__, errno);
        return -1;
    }

    /* init param structure */
    strcpy(param_ref->id, (char *)(msg->data_begin));
    len = (int)strlen((char *)(msg->data_begin)) + 1;
    if(sci_msg_pop(msg, len) < len)
    {
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d because cannot get data", __FUNCTION__, errno);
        return -1;
    }
    
    /* find the registred function */
    DBG_ASSERT(fcall->function_nb > 0);
    if(fcall->function_nb <= 0)
    {
        errno = ENOENT;
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d because cannot find registered function", __FUNCTION__, errno);
        return -1;
    }
    for(function_index = 0; function_index < fcall->function_nb; function_index++)
    {
        if(!strcmp(fcall->function_table[function_index].id, param_ref->id)
            && (fcall->function_table[function_index].function != NULL))
            break;
    }
    DBG_ASSERT(function_index < fcall->function_nb);
    if(function_index >= fcall->function_nb)
    {
        errno = ENOENT;
        station_log(fcall->sci->station, STATION_LOG_NOTICE, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d: function '%s' not registered", __FUNCTION__, errno, param_ref->id);
        return -1;
    }
    
    /* get all param */
    for(index = 0; index < param_ref->param_nb; index++)
    {
        DBG_ASSERT(msg->length != 0);
        if(msg->length == 0)
        {
            errno = ENOSPC;
            station_log(fcall->sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_FCALL,
                        "%s: errno = %d: no space into msg for param #%d", __FUNCTION__, errno, index);
            return -1;
        }
        param_ref->param_table[index].id = (char *)(msg->data_begin);
        len = (int)strlen((char *)(msg->data_begin)) + 1;
        if(sci_msg_pop(msg, len) < len)
        {
            station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                        "%s: errno = %d because cannot get data", __FUNCTION__, errno);
            return -1;
        }
        param_ref->param_table[index].length = (unsigned short *)(msg->data_begin);
        if(sci_msg_pop(msg, sizeof(unsigned short)) < (int)sizeof(unsigned short))
        {
            station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                        "%s: errno = %d because cannot fill param length", __FUNCTION__, errno);
            return -1;
        }
        param_ref->param_table[index].data = msg->data_begin;
        if(sci_msg_pop(msg, ntohs(*param_ref->param_table[index].length)) < ntohs(*param_ref->param_table[index].length))
        {
            station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                        "%s: errno = %d because cannot fill param data", __FUNCTION__, errno);
            return -1;
        }
    }
    
    DBG_ASSERT(msg->length <= 0);
    if(msg->length > 0)
    {
        station_log(fcall->sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d: msg length > 0 (%d) after parameter extraction", __FUNCTION__, errno, msg->length);
        return -1;
    }
         
    /* call the function */
    station_log(fcall->sci->station, STATION_LOG_INFO, STATION_LOGTYPE_FCALL,
        "%s calling function '%s'(%p) with %d parameters", 
        __FUNCTION__, 
        fcall->function_table[function_index].id, 
        fcall->function_table[function_index].function, 
        param_ref->param_nb);            
    if((*fcall->function_table[function_index].function)(
             fcall,
             &param_ref,
             &msg,
             fcall->function_table[function_index].data) < 0)
    {
        DBG_ASSERT(errno == 0);
        /* function returned error */
        param_ref->param_nb = 0;
        return_flags |= FUNCTION_CALL_FLAG_FAILED;
        station_log(fcall->sci->station, STATION_LOG_NOTICE, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d: function '%s' returned error", __FUNCTION__, errno, fcall->function_table[function_index].id);
        /* reset errno */
        errno = 0;
    }
    else
    {
        if(param_ref->is_async)
        {
            station_log(fcall->sci->station, STATION_LOG_DEBUG, STATION_LOGTYPE_FCALL,
                "%s function '%s' returned from asynchronous call", __FUNCTION__, fcall->function_table[function_index].id);            
        }
        else            
        {
            station_log(fcall->sci->station, STATION_LOG_DEBUG, STATION_LOGTYPE_FCALL,
                "%s function '%s' returned %d parameters (msg->length=%d)", __FUNCTION__, fcall->function_table[function_index].id, param_ref->param_nb, msg->length);            
        }
    }

    if(param_ref->is_async)
        /* asynchronous call: return immediately */
        return 0;

    /* after function return, we get a param structure update with return results */
    
    /* be sure to have the same function name */
    strcpy(param_ref->id, fcall->function_table[function_index].id);
    /* fill the fcall msg header to prepare the answer */
    if(fcall_fill_hdr(fcall, msg, param_ref, return_flags) < 0)
    {
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d because cannot fill fcall msg header", __FUNCTION__, errno);
        return -1;
    }

    /* now fill the sci header */
    if(sci_fill_hdr(fcall->sci, msg, SCI_MSG_TYPE_FUNCTION_CALL, 0) < 0)
    {
        station_log(fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_FCALL,
                    "%s: errno = %d because cannot fill sci header", __FUNCTION__, errno);
        return -1;
    }
        
    /* send the message */
    if (sci_send(fcall->sci, msg) < 0)
    {
        station_log(fcall->sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_FCALL, "%s failed to send fcall (errno=%d)", __FUNCTION__, errno);
        return -1;
    }
    else
        return 0;
}

void fcall_hdr_dump(struct fcall_msg_hdr *hdr, int fd, char *buffer, int size)
{
    buffer[size - 1] = '\0';
    snprintf(buffer, size - 1, "fcall_hdr: version=%d, type=%d, param_nb=%d, flags=0x%x\n",
        hdr->version, hdr->type, hdr->param_nb, hdr->flags);
    write(fd, buffer, strlen(buffer));
}