summaryrefslogtreecommitdiff
path: root/interface/src/interface.c
blob: fe0ad7173e1fcfd95f46071ed8656d2cdc22bf2c (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    interface/src/interface.c
 * \brief   Interface functions sources.
 * \ingroup interface 
 *
 */
#include "common/std.h"
#include "common/defs/ethernet.h"
#include "lib/read_word.h"
#include "lib/circular_buffer.h"

#include "hal/hle/ipmbox.h"
#include "interface/interface.h"
#include "interface/sniffer/sniffer.h"

#include "interface/inc/interface.h"
#include "interface/inc/context.h"

static interface_t interface_global;

/**
 * Initialise the interface module.
 * \param  ipmbox  the ipmbox context.
 * \param  cl  the cl context.
 * \param  sar  the sar context.
 * \param  mac_config  the mac config context.
 * \return  the interface module context.
 */
interface_t*
interface_init (ipmbox_t *ipmbox, cl_t *cl, sar_t *sar, mac_config_t
                *mac_config)
{
    dbg_assert (cl);
    dbg_assert (sar);
    dbg_assert (mac_config);

    interface_global.cl = cl;
    interface_global.sar = sar;
    interface_global.mac_config = mac_config;

    interface_global.sniffer = interface_sniffer_init
        ((interface_sniffer_send_message_cb_t) interface_ipmbox_send,
         &interface_global);

    circular_buffer_init (&interface_global.buffers,
                          interface_global.buffer_list,
                          INTERFACE_BUFFER_LIST_NUM_SLOTS);

    cyg_mutex_init (&interface_global.buffer_mutex);

    return &interface_global;
}

/** 
 * Interface uninit.
 * \param  ctx  the interface context.
 */
void
interface_uninit (interface_t *ctx)
{
    dbg_assert (ctx);
    dbg_assert (ctx->sniffer);

    interface_sniffer_uninit (ctx->sniffer);
}

/**
 * Initialise the callbacks functions.
 * \param  ctx the interface context.
 * \param  mme_recv_cb the function to call on reception of a MME.
 * \param  buffer_add_cb the function to call on buffer reception.
 * \param  beacon_add_cb the function to call on beacon reception
 * \param  user_data the data to provide on each callback function.
 */
void
interface_callback_init (interface_t *ctx, interface_mme_recv_cb_t mme_recv_cb,
                         interface_mme_buffer_add_cb_t buffer_add_cb, 
                         interface_beacon_add_cb_t beacon_add_cb, void *user_data)
{
    dbg_assert (ctx);
    dbg_assert (mme_recv_cb);
    dbg_assert (buffer_add_cb);
    dbg_assert (beacon_add_cb);
    
    ctx->mme_recv_cb = mme_recv_cb;
    ctx->buffer_add_cb = buffer_add_cb;
    ctx->beacon_add_cb = beacon_add_cb;
    ctx->actor_user_data = user_data;
}


/**
 * Configure the interface.
 * \param  ctx the interface context.
 * \param  data the data to configure the module of sub module.
 */
void
interface_configure (interface_t *ctx, u8 *data)
{
    dbg_assert (ctx);
    dbg_assert (data);

    switch (read_u8_from_word(data))
    {
    case INTERFACE_MODULE_SNIFFER:
        /** The length contained in the second octet shall be equal to 1. */
        dbg_assert (read_u8_from_word(data + 1) == 1);
        interface_sniffer_configure (ctx->sniffer, read_u8_from_word (data + 2));
        break;
    default : 
        break;
    }
}

/** Receives an MME from the PWL or the HLE.
 * \param  ctx the interface context
 * \param  buffer the buffer containing the MME.
 * \param  length the MME length
 * \param  mme_data data use by the CL.
 */
void
interface_mme_recv (interface_t *ctx, u8 *buffer, uint length, 
                    cl_mme_recv_t *mme_data)
{
    u8 *copy_buffer;

    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (ETH_PACKET_MIN_SIZE <= length && length <=
                ETH_PACKET_MAX_SIZE);

    if (interface_sniffer_mme_status_rx(ctx->sniffer))
    {
        copy_buffer = interface_buffer_get (ctx);

        if (copy_buffer)
        {
            interface_sniffer_copy_mme_rx (ctx->sniffer, buffer, length,
                                       copy_buffer, false);
        }
    }

    // Call the actor callback.
    dbg_assert (ctx->mme_recv_cb);
    (*ctx->mme_recv_cb) (ctx->actor_user_data, buffer, length, mme_data);
}


/**
 * Inform the Data plane when the MME as been processed by the CP.
 * \param  ctx the interface context
 * \param  mme_recv the cl data (as a void pointer).
 */
void
interface_mme_recv_done (interface_t *ctx, void *mme_recv)
{
    dbg_assert (ctx);
    dbg_assert (mme_recv);
    dbg_assert (ctx->cl);

    cl_mme_recv_done (ctx->cl, (cl_mme_recv_t*) mme_recv);
}

/** Provides a MME to send to the CL. This MME can be send as a MME or a data.
 * \param  ctx the interface context.
 * \param  buffer the buffer containing the MME.
 * \param  length the length of the MME.
 * \param  mfs the MFS to send the MME if the mme is to be sent over the PWL, 
 * otherwise this pointer is NULL.
 */
void
interface_mme_send (interface_t *ctx, u8* buffer, uint length, mfs_tx_t *mfs)
{
    u8 *copy_buffer;

    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (length >= ETH_PACKET_MIN_SIZE && length <=
                ETH_PACKET_MAX_SIZE);

    if (interface_sniffer_mme_status_tx (ctx->sniffer))
    {
        copy_buffer = interface_buffer_get (ctx);

        if (copy_buffer)
        {
            interface_sniffer_copy_mme_tx (ctx->sniffer, buffer, length,
                                       copy_buffer, false);
        }
    }

    dbg_assert (ctx->cl);
    cl_mme_send (ctx->cl, buffer, length, mfs);
}


/** 
 * Sends a beacon, the interface will provide it to the SAR.
 * \param  ctx the interface context.
 * \param  beacon the beacon to send.
 * \param  the source mac address.
 * \param  beacon_mfs  the mfs to use to send the beacon.
 * \param  bto_bpsto  the four bto to use for the beacon and the bpsto address
 * to be stamp by the pbproc.
 */
void
interface_beacon_prepare (interface_t *ctx, pb_beacon_t *beacon, mac_t
                          mac_address, mfs_tx_t *beacon_mfs, void *bto_bpsto)
{
    u8 *buffer;

    dbg_assert (ctx);
    dbg_assert (beacon);
    dbg_assert (ctx->sar);
    dbg_assert (mac_address);

    if (interface_sniffer_beacon_status_tx (ctx->sniffer))
    {
        buffer = interface_buffer_get (ctx);
        
        if (buffer)
        {
            interface_sniffer_copy_beacon_tx (ctx->sniffer, beacon, buffer,
                                          mac_address);
        }
    }

    sar_beacon_send (ctx->sar, beacon, beacon_mfs, bto_bpsto);
}

/**
* add a beacon to the interface.
* It will provide it to the CP to process the beacon.
*
* \param  ctx  the interface context.
* \param  pb  pb containing the beacon
* \param  params  the rx params.
*/
void interface_beacon_add (interface_t *ctx, pb_beacon_t *pb,
    pbproc_rx_beacon_params_t *params)
{
    u8 *buffer;

    dbg_assert (ctx);
    dbg_assert (pb);
    dbg_assert (ctx->mac_config);

    /* Get the buffer to copy the data. */
    if (interface_sniffer_beacon_status_rx (ctx->sniffer))
    {
        buffer = interface_buffer_get (ctx);

        if (buffer)
        {
            interface_sniffer_copy_beacon_rx (ctx->sniffer, pb, buffer,
                                          ctx->mac_config->sta_mac_address);
        }
    }

    dbg_assert (ctx->beacon_add_cb);
    (*ctx->beacon_add_cb) (ctx->actor_user_data, pb); 
}


/**
 * Sends a message to the IPMbox
 * \param  ctx the interface context
 * \param  data the message to post in the ipmbox.
 * \param  length  the length of the data in bytes.
 */
void
interface_ipmbox_send (interface_t *ctx, u8 *data, uint length)
{
    uint word[length];

    dbg_assert (ctx);
    dbg_assert (data);
    dbg_assert (length);

    ipmbox_tx (ctx->ipmbox, word, length);
}

/**
 * Add a buffer to its own list. If the function returns true, the buffer has
 * been kept in the other case the buffer shall be provided to the CP.
 * \param ctx the interface context.
 * \param buffer the buffer to add.
 * \return  true if the buffer had been added, false otherwise.
 */
bool
interface_buffer_add (interface_t *ctx, u8 *buffer)
{
    bool added;
    dbg_assert (ctx);
    dbg_assert (buffer);

    dbg_assert (ctx->buffer_add_cb);
 
    /* Lock the mutex. */
    cyg_mutex_lock (&ctx->buffer_mutex);
    added = circular_buffer_add (&ctx->buffers, buffer);
    cyg_mutex_unlock (&ctx->buffer_mutex);

    if (!added)
    {
        (*ctx->buffer_add_cb) (ctx->actor_user_data, buffer);
        added = true;
    }

    return added;
}

/** 
 * Get a buffer from the list.
 * \param  ctx the interface context.
 * \return  the buffer to use, NULL if no buffer is available.
 */
u8*
interface_buffer_get (interface_t *ctx)
{
    u8 *buffer;
    dbg_assert (ctx);

    cyg_mutex_lock (&ctx->buffer_mutex);
    buffer = circular_buffer_get (&ctx->buffers);
    cyg_mutex_unlock (&ctx->buffer_mutex);

    return buffer;
}