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

/**
 * init the parameter table to add new paramters inside the message
 * \param param pointer to param context
 * \param id string id of parameters' function
 * \param msg_id id of fcall message to process asynchronous calls
 * \return 0 if ok, -1 if failed with errno=
 * - EINVAL if fcall or id is NULL
 * - ENAMETOOLONG if id length >= FUNCTION_CALL_ID_MAX_SIZE
 */
int fcall_param_init(fcall_param_t *param, char *id, unsigned short msg_id)
{  
    
    DBG_ASSERT(param);
    DBG_ASSERT(id);
    if((param == NULL)
        || (id == NULL))
    {
        errno = EINVAL;
        return -1;
    }
    
    memset(param, '\0', sizeof(fcall_param_t));
    /* check id length */
    if(id != NULL)
    {
        if(strlen(id) >=  FUNCTION_CALL_ID_MAX_SIZE)
        {
            errno = ENAMETOOLONG;
            return -1;
        }
    
        /* get the function id */
        strcpy(param->id, id);
    }
    
    param->msg_id = msg_id;
    
    return 0;
}   

/**
 * reset the parameter table and buffer to add new paramters inside the message
 * function id, msg id and is_async stay unmodified
 * \param param pointer to param context
 * \return 0 if ok, -1 if failed with errno=
 * - EINVAL if fcall is NULL
 */
int fcall_param_reset(fcall_param_t *param)
{
    int i;
    
    DBG_ASSERT(param);
    if(param == NULL)
    {
        errno = EINVAL;
        return -1;
    }
    
    param->param_nb = 0;
    for(i = 0; i < FUNCTION_CALL_PARAM_MAX_NB; i++)
    {
        param->param_table[i].id = NULL;
    }
    return 0;
}       

/**
 * find a parameter according to its id and bind it into a user variable
 * \param param pointer to param context
 * \param msg sci message containing the parameters
 * \param id identifier of wanted parameter as string format
 * \param max_length maximum length of receiving data area
 * \param data pointer to data area where to put the found parameter
 * \return actual length of binded data if ok, -1 if failed with errno=
 * - EINVAL if param, id or data is NULL
 * - ENOENT if id has not been found into parameter context
 */
int fcall_param_bind(fcall_param_t *param, sci_msg_t *msg, char *id, unsigned int max_length, void *data)
{
    int index;
    unsigned int length;
    
    DBG_ASSERT(param);
    DBG_ASSERT(msg);
    DBG_ASSERT(id);
    DBG_ASSERT(data);
    
    if((param == NULL)
        || (msg == NULL)
        || (id == NULL)
        || (data == NULL))
    {

        errno = EINVAL;
        return -1;
    }
    
    DBG_ASSERT(max_length);
    if(max_length == 0)
    {
        errno = EINVAL;
        return -1;
    }
    
    /* check param_nb validity */
    if(param->param_nb > FUNCTION_CALL_PARAM_MAX_NB)
        param->param_nb = FUNCTION_CALL_PARAM_MAX_NB;
    else if(param->param_nb <= 0)
    {
        errno = ENOENT;
        return -1;
    }
        
    /* get the wanted index */
    for(index = 0; index < param->param_nb; index++)
    {
        if(!strcmp(param->param_table[index].id, id))
        {
            /* found id */
            /* get length */
            length = ntohs(*param->param_table[index].length);
            /* check the maximum available length */
            if(length > max_length)
                length = max_length;
            /* copy data into user buffer */
            if(length > 0)
                memcpy(data, param->param_table[index].data, length);
            break;
        }
    }
    
    if(index >= param->param_nb)
    {
        /* not found */
        errno = ENOENT;
        return -1;
    }
    
    return length;
}

/**
 * add a new parameter element into the parameter context
 * \param param pointer to param context where to add new parameter
 * \param msg pointer to sci message to add the paramter
 * \param id id of new parameter
 * \param length length of new parameter
 * \param data pointer to data of new added parameter
 * \return 0 if ok, -1 if failed with errno=
 * - EINVAL if param, id of data is NULL, or if length < 0 or > FUNCTION_CALL_PARAM_MAX_SIZE
 * - ENAMETOOLONG if id length >= FUNCTION_CALL_ID_MAX_SIZE
 * - ENOSPC if parameter nb is already FUNCTION_CALL_PARAM_MAX_NB
 */
int fcall_param_add(fcall_param_t *param, sci_msg_t *msg, char *id, int length, void *data)
{
    int total_len;
    
    DBG_ASSERT(param);
    DBG_ASSERT(msg);
    DBG_ASSERT(id);
    DBG_ASSERT(strlen(id));
    DBG_ASSERT(length >= 0);
    DBG_ASSERT(length <= FUNCTION_CALL_PARAM_MAX_SIZE);
    DBG_ASSERT((length <= 0) || (data != NULL));
    if((param == NULL)
        || (msg == NULL)
        || (id == NULL)
        || (strlen(id) == 0)
        || (length < 0)
        || (length > FUNCTION_CALL_PARAM_MAX_SIZE)
        || ((length > 0) 
            && (data == NULL)))
    {
        errno = EINVAL;
        return -1;
    }
    
    /* check id length */
    if(strlen(id) >= FUNCTION_CALL_ID_MAX_SIZE)
    {
        errno = ENAMETOOLONG;
        return -1;
    }
    
    /* check available free param */
    if(param->param_nb >= FUNCTION_CALL_PARAM_MAX_NB)
    {
        errno = ENOSPC;
        return -1;
    }
    
    /* compute total data length to reserve */
    total_len = strlen(id) + 1 + sizeof(uint16_t) + length;
    if(sci_msg_push(msg, total_len) < 0)
        return -1;
    
    /* copy id, length and data into msg and match the param structure */
    param->param_table[param->param_nb].id = (char *)(msg->data_begin);
    strcpy(param->param_table[param->param_nb].id, id);
    param->param_table[param->param_nb].length = (unsigned short *)(param->param_table[param->param_nb].id + strlen(id) + 1);
    *param->param_table[param->param_nb].length = htons(length);
    param->param_table[param->param_nb].data = (unsigned char *)param->param_table[param->param_nb].length + sizeof(unsigned short);
    memcpy(param->param_table[param->param_nb].data, data, length);
    
    /* increment the parameter number */
    param->param_nb++;
    
    return 0;
}

void fcall_param_dump(fcall_param_t *param, int fd, char *buffer, int size)
{
    int i, j;
    buffer[size - 1] = '\0';
    snprintf(buffer, size - 1, "fcall_param: id='%s', nb=%d\n", param->id, param->param_nb);
    write(fd, buffer, strlen(buffer));
    for(i = 0; i < param->param_nb; i++)
    {
        snprintf(buffer, size - 1, "    id=%s, length=%d, data=", param->param_table[i].id, ntohs(*param->param_table[i].length));
        write(fd, buffer, strlen(buffer));
        for(j = 0; j < ntohs(*param->param_table[i].length); j++)
        {
            snprintf(buffer, size - 1, "%02x-", param->param_table[i].data[j]);
            write(fd, buffer, 3);
        }
        write(fd, "\n", 1);
    }
}