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

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

/**
 * initialize a static probe context, called during station context creation.
 * \param probe probe context to initialize
 * \param fcall fcall which process the probe function
 * \return 0 if ok, -1 if failed with errno=
 * - EINVAL if probe or fcall is NULL
 */
int probe_init(probe_ctx_t *probe, fcall_ctx_t *fcall)
{
    DBG_ASSERT(probe);
    DBG_ASSERT(fcall);
    if((probe == NULL)
        || (fcall == NULL))
    {
        errno = EINVAL;
        return -1;
    }
    
    memset(probe, '\0', sizeof(probe_ctx_t));
    probe->fcall = fcall;
    fcall_register(fcall, PROBE_ID, probe_recv, probe);
    return 0;
}

/**
 * probe var registering
 * \param probe pointer to current probe context
 * \param id variable string id
 * \param addr pointer to variable
 * \return 0 if ok, -1 if fail with errno=
 * - EINVAL if probe or id is NULL
 * - ENAMETOOLONG if id length is >= PROBE_ID_MAX_SIZE
 * - ENOSPC if max number of registred variable is reached
 * - EEXIST if variable is already registered
 */
int probe_register(probe_ctx_t *probe, char *id, int length, void *addr)
{
    int index;
    
    DBG_ASSERT(probe);
    DBG_ASSERT(id);
    DBG_ASSERT(length > 0);
    DBG_ASSERT(length < FUNCTION_CALL_PARAM_MAX_SIZE);
    DBG_ASSERT(addr);
    if((probe == NULL)
        || (id == NULL)
        || (length <= 0)
        || (length >= FUNCTION_CALL_PARAM_MAX_SIZE)
        || (addr == NULL))
    {
        errno = EINVAL;
        return -1;
    }
    
    /* check id length */
    DBG_ASSERT(strlen(id) < PROBE_ID_MAX_SIZE);
    if(strlen(id) >= PROBE_ID_MAX_SIZE)
    {
        errno = ENAMETOOLONG;
        station_log(probe->fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_PROBE,
                    "%s: errno = %d because id length is invalid", __FUNCTION__, errno);
        return -1;
    }
    
    /* check if there is free place */
    DBG_ASSERT(probe->var_nb < PROBE_VAR_MAX_NB);
    if(probe->var_nb >= PROBE_VAR_MAX_NB)
    {
        errno = ENOSPC;
        station_log(probe->fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_PROBE,
                    "%s: errno = %d because there is no free place", __FUNCTION__, errno);
        return -1;
    }
    
    /* check if variable is already registered */
    for(index = 0; index < probe->var_nb; index++)
    {
        DBG_ASSERT(strcmp(probe->var_table[index].id, id));
        if(!strcmp(probe->var_table[index].id, id))
        {
            errno = EEXIST;
            station_log(probe->fcall->sci->station, STATION_LOG_ERROR, STATION_LOGTYPE_PROBE,
                        "%s: errno = %d because variable is already registered", __FUNCTION__, errno);
            return -1;
        }
    }
    
    /* register the function */
    strcpy(probe->var_table[probe->var_nb].id, id);
    probe->var_table[probe->var_nb].length = length;
    probe->var_table[probe->var_nb].addr = addr;

    /* increment the number of function */
    probe->var_nb++;
    
    return 0;
}

/**
 * probe message processing function; called by fcall_recv()
 * must be registred to fcall layer as "probe" function id
 * \param fcall pointer to the current fcall context
 * \param param pointer to param 
 * \param msg pointer to received message with fcall pre-processing
 * \return number of param if ok, -1 if failed with errno=
 * - EINVAL if param or msg is NULL
 */ 
int probe_recv(fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    int index_param, index_var;
    probe_ctx_t *probe;
    static fcall_param_t param_result;

    probe = (probe_ctx_t *)data;

    DBG_ASSERT(fcall);
    DBG_ASSERT(param);
    DBG_ASSERT(*param);
    DBG_ASSERT(msg);
    DBG_ASSERT(*msg);
    DBG_ASSERT(probe);
    if((fcall == NULL)
        || (param == NULL)
        || (*param == NULL)
        || (msg == NULL)
        || (*msg == NULL)
        || (probe == NULL))
    {
        errno = EINVAL;
        return -1;
    }

    /* init result structures */
    fcall_param_init(&param_result, (*param)->id, (*param)->msg_id);

    if((*param)->param_nb == 0)
    {
        /* we want all the var list */
        for(index_var = 0; index_var < probe->var_nb; index_var++)
        {
            if(strlen(probe->var_table[index_var].id) > 0)
            {
                fcall_param_add(
                    &param_result, 
                    *msg,
                    probe->var_table[index_var].id,
                    probe->var_table[index_var].length,
                    probe->var_table[index_var].addr
                );
            }
        }
    }

    else
    {
        /* normal get or set request */
        /* get all param for processing */
        for(index_param = 0; index_param < (*param)->param_nb; index_param++)
        {
            /* find the asked id */
            for(index_var = 0; index_var < probe->var_nb; index_var++)
            {
                if(!strcmp((*param)->param_table[index_param].id, probe->var_table[index_var].id))
                {
                    /* found !*/
                    if((*param)->param_table[index_param].length == 0)
                    {
                        /* this is a get request */
                        station_log(fcall->sci->station, STATION_LOG_INFO, STATION_LOGTYPE_PROBE,
                            "%s get '%s' (%d bytes @ %p)",
                            __FUNCTION__,
                            probe->var_table[index_var].id,
                            probe->var_table[index_var].length,
                            probe->var_table[index_var].addr
                        );
                        fcall_param_add(
                            &param_result, 
                            *msg,
                            probe->var_table[index_var].id,
                            probe->var_table[index_var].length,
                            probe->var_table[index_var].addr
                        );
                        station_log(fcall->sci->station, STATION_LOG_DEBUG, STATION_LOGTYPE_PROBE,
                            "content: %02x-%02x-%02x-%02x",
                            *((unsigned char *)probe->var_table[index_var].addr),
                            *((unsigned char *)probe->var_table[index_var].addr + 1),
                            *((unsigned char *)probe->var_table[index_var].addr + 2),
                            *((unsigned char *)probe->var_table[index_var].addr + 3)
                        );
                    }
                    else if((*param)->param_table[index_param].length <= probe->var_table[index_var].length)
                    {
                        /* this is a set request with a correct length */
                        memcpy(
                            probe->var_table[index_var].addr, 
                            (*param)->param_table[index_param].data,
                            (*param)->param_table[index_param].length
                        );
                        station_log(fcall->sci->station, STATION_LOG_INFO, STATION_LOGTYPE_PROBE,
                            "%s set '%s' (%d bytes @ %p)",
                            __FUNCTION__,
                            probe->var_table[index_var].id,
                            (*param)->param_table[index_param].length,
                            probe->var_table[index_var].addr
                        );
                        fcall_param_add(
                            &param_result, 
                            *msg, 
                            probe->var_table[index_var].id,
                            0,
                            NULL
                        );
                    }
                    /* do nothing else */
                    break;
                }
            }
            if(index_var >= probe->var_nb)
            {
                /* variable not registered */
                (*msg)->hdr.fcall->param_nb = 0;
                (*msg)->hdr.fcall->flags |= FUNCTION_CALL_FLAG_FAILED;
                station_log(fcall->sci->station, STATION_LOG_NOTICE, STATION_LOGTYPE_PROBE,
                    "%s var '%s' not registered",
                    __FUNCTION__,
                    (*param)->param_table[index_param].id
                );
            }
        }
    }

    /* switch param and msg for result */
    *param = &param_result;

    return param_result.param_nb;        
}