summaryrefslogtreecommitdiff
path: root/hal/hle/maximus/src/maximus_ipmbox.c
blob: 6f1cbd71b30a1a491992b575b7c215ab1aaa2eca (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    hal/hle/maximus/src/maximus_ipmbox.c
 * \brief   HAL HLE functions for Maximus.
 * \ingroup hal_hle_maximus
 */

#include "common/std.h"
#include "hal/hle/ipmbox.h"
#include "hal/hle/defs.h" // for 'HLE_MSG_TYPE_...' and 'ipmbox_msg_hdr_t'
#include "hal/hle/maximus/inc/maximus_ipmbox_ctx.h"
#include "hal/hle/maximus/inc/maximus_ether.h" // for 'maximus_ether_recv()'
#include "hal/hle/maximus/inc/maximus_defs.h" // for 'MAXIMUS_MAILBOX_MAX_SIZE'
#include "hal/hle/maximus/inc/maximus_interrupts.h" // for 'HAL_HLE_INTERRUPT_IPMBOX' and 'HAL_HLE_INTERRUPT_PRIORITY'
#include "maximus/common/types/ethernet_types.h" // for 'ETHERNET_TYPE_...'
#include "host/station.h" // for 'station_log()'
#include "ecos/packages/hal/maximus/arch/current/include/hal_host_intr.h"
#include <string.h> // for 'memset'
#include <stdlib.h> // for 'free'
#include <errno.h>

#ifdef ECOS
static cyg_uint32 _ipmbox_ecos_isr(cyg_vector_t vector, cyg_addrword_t data)
{
    cyg_drv_interrupt_mask(HAL_HLE_INTERRUPT_IPMBOX);
    cyg_drv_interrupt_acknowledge(HAL_HLE_INTERRUPT_IPMBOX);
    return CYG_ISR_CALL_DSR; // cause DSR to be run
}

static void _ipmbox_ecos_dsr(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
{
    /* nothing to do except calling the hle dsr */
    ipmbox_t *ctx = (ipmbox_t *)data;
    dbg_assert_ptr(ctx);
    dbg_assert_ptr(ctx->rx_cb);
    dbg_assert_ptr(ctx->user_data);
    if ((NULL != ctx)
        && (NULL != ctx->rx_cb)
        && (NULL != ctx->user_data))
    {
        (*ctx->rx_cb)(ctx->user_data, ctx->rx.mailbox, ctx->rx.length);
        // reset total length of messages
        ctx->rx.length = 0;
    }
    cyg_drv_interrupt_unmask(HAL_HLE_INTERRUPT_IPMBOX);
    return;
}
#endif /* ECOS */

/**
 * Initialise the HAL HLE.
 * \param  user_data  user data passed to any callback
 * \param  rx_cb  RX DSR callback, callback used when an Ethernet packet is received
 * \return  the newly created context
 * set errno to:
 * - EINVAL if user_data or rx_cb are null
 */
ipmbox_t *
ipmbox_init (void *user_data, ipmbox_rx_cb_t rx_cb)
{
    static ipmbox_t ctx;
    static maximus_hle_buffer_t first_buffer;

    dbg_assert_ptr(user_data);
    dbg_assert_ptr(rx_cb);
    if ((NULL == user_data)
        || (NULL == rx_cb))
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        memset(&ctx, '\0', sizeof(ipmbox_t));
        ctx.user_data = user_data;
        ctx.rx_cb = rx_cb;

        // initialize linked list of allocated buffers
        ctx.first_buffer = &first_buffer;
        ctx.first_buffer->next = NULL;
        ctx.first_buffer->id = 0;
        ctx.first_buffer->data = NULL;
        ctx.last_buffer = ctx.first_buffer;

        // register ether_recv to the sci layer
        sci_register_callback(my_station.sci, SCI_MSG_TYPE_ETHERNET, maximus_ether_recv, &ctx);

#ifdef ECOS
        // register the ipmbox ISR and DSR into eCos
        cyg_drv_interrupt_create(HAL_HLE_INTERRUPT_IPMBOX,
                                 HAL_HLE_INTERRUPT_PRIORITY,
                                 (cyg_addrword_t)&ctx,
                                 _ipmbox_ecos_isr,
                                 _ipmbox_ecos_dsr,
                                 &ctx.ipmbox_interrupt_handle,
                                 &ctx.ipmbox_interrupt);
        cyg_drv_interrupt_attach(ctx.ipmbox_interrupt_handle);
        cyg_drv_interrupt_mask(HAL_HLE_INTERRUPT_IPMBOX);
#endif /* ECOS */
    }

    return &ctx;
}

/**
 * Activate ipmbox interruptions.
 * \param  ctx  ipmbox context
 * \param  activation  indicates if interruptions are activated or deactivated
 * set errno to:
 * - EINVAL if ctx is NULL
 */
void
ipmbox_activate (ipmbox_t *ctx, bool activation)
{
    dbg_assert_ptr(ctx);
    if (NULL == ctx)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
#ifdef ECOS
        if (activation)
        {
            cyg_drv_interrupt_unmask(HAL_HLE_INTERRUPT_IPMBOX);
        }
        else
        {
            cyg_drv_interrupt_mask(HAL_HLE_INTERRUPT_IPMBOX);
        }
#endif /* ECOS */
    }
}

/**
 * Uninitialise the HAL HLE.
 * \param  ctx  ipmbox context
 * set errno to:
 * - EINVAL if ctx is NULL
 */
void
ipmbox_uninit (ipmbox_t *ctx)
{
    dbg_assert_ptr(ctx);
    if (NULL == ctx)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        // release allocated buffers
        maximus_hle_buffer_t *current_buffer = ctx->first_buffer->next;
        maximus_hle_buffer_t *next_buffer = NULL;
        while (NULL != current_buffer)
        {
            next_buffer = current_buffer->next;
            free(current_buffer->data);
            free(current_buffer);
            current_buffer = next_buffer;
        }

        // reset ipmbox context
        memset(ctx, '\0', sizeof(ipmbox_t));
    }
}

/**
 * Transmit an Ethernet packet.
 * \param  ctx  ipmbox context
 * \param  first_msg  pointer to the first received message header
 * \param  length  total length (in word) of messages to transmit
 * set errno to:
 * - EINVAL if ctx or first_msg are null, if length is incorrect, or if first_msg values are incorrect
 */
void
ipmbox_tx (ipmbox_t *ctx, u32 *first_msg, uint length)
{
    dbg_assert_ptr(ctx);
    dbg_assert_ptr(first_msg);
    dbg_assert(2 == length);
    if ((NULL == ctx)
        || (NULL == first_msg)
        || (2 != length))
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        ipmbox_msg_hdr_t hdr = *(ipmbox_msg_hdr_t *)first_msg;
        dbg_assert(HLE_MSG_TYPE_NB > hdr.type);
        dbg_assert(1 == hdr.length);
        if ((HLE_MSG_TYPE_NB <= hdr.type) || (1 != hdr.length))
        {
            errno = EINVAL;
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                        "%s: errno = %d", __FUNCTION__, errno);
        }
        else
        {
            if (HLE_MSG_TYPE_DATA == hdr.type)
            {
                // to release buffer
                bool found = false;
                maximus_hle_buffer_t *current_buffer = ctx->first_buffer;
                maximus_hle_buffer_t *previous_buffer = current_buffer;

                // get DATA or MME length (11 bits): length of data into DATA or MME buffer
                uint data_length = (uint)((hdr.param & 0xFFE) >> 1);

                // get type (1 bit): DATA or MME type
                // 0: DATA type
                // 1: MME type
                u8 type = (u8)(hdr.param & 0x001);
                if (0 == type)
                {
                    type = ETHERNET_TYPE_DATA;
                }
                else
                {
                    type = ETHERNET_TYPE_MME;
                }

                // get buffer pointer (32 bits): pointer to DATA or MME buffer
                u32 data = *(first_msg + 1);

                // check that this pointer is still allocated
                while ((NULL != current_buffer->next) && !found)
                {
                    previous_buffer = current_buffer;
                    current_buffer = current_buffer->next;
                    found = (data == (u32)current_buffer->data); // pointer to buffer to release
                }
                dbg_assert(found);
                if (!found)
                {
                    errno = EINVAL;
                    station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                                "%s: errno = %d", __FUNCTION__, errno);
                }
                else
                {
                    // for BUFFER RELEASED message
                    u32 id = current_buffer->id;

                    // send DATA or MME to Maximus
                    if (0 != maximus_ether_send(ctx, type, 0 /* sniffer_type */, ETHERNET_FLAG_NONE, data_length, (u32 *)data))
                    {
                        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                                    "%s: errno = %d", __FUNCTION__, errno);
                        dbg_assert_print(false, "errno = %d when transmitting an Ethernet packet", errno);
                    }

                    // before deallocating buffer, link previous to next,
                    // and update last buffer if needed
                    previous_buffer->next = current_buffer->next;
                    if (current_buffer == ctx->last_buffer)
                    {
                        ctx->last_buffer = previous_buffer;
                    }
                    free(current_buffer->data);
                    free(current_buffer);

                    // send BUFFER RELEASED to Maximus
                    if (0 != maximus_ether_send(ctx, ETHERNET_TYPE_BUFFER_RELEASED, 0 /* sniffer_type */, ETHERNET_FLAG_NONE, sizeof(u32), &id))
                    {
                        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                                    "%s: errno = %d", __FUNCTION__, errno);
                        dbg_assert_print(false, "errno = %d when transmitting an Ethernet packet", errno);
                    }
                }
            }
            else if (HLE_MSG_TYPE_SEND_DONE == hdr.type)
            {
                /* Nothing to do:
                 * buffers are released when a TX is done
                 * (messages of type 'HLE_MSG_TYPE_DATA'). */
            }
            else if (HLE_MSG_TYPE_BUFFER_ADD == hdr.type)
            {
                errno = EINVAL;
                station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                            "%s: errno = %d", __FUNCTION__, errno);
                dbg_assert_print(false, "errno = %d because cannot transmit an Ethernet packet of type BUFFER_ADD", errno);
            }
            else // HLE_MSG_TYPE_INTERFACE_SNIFFER
            {
                // get data length (11 bits): length of data into buffer
                uint data_length = (uint)((hdr.param & 0xFFE0) >> 5);

                // get data type (3 bits): Beacon or MME type
                // 0: Beacon type
                // 1: MME type
                // 2-7: unused
                u8 sniffer_type = (u8)((hdr.param & 0x001C) >> 2);

                // get encryption (1 bit): encrypted or not
                // 0: false
                // 1: true
                // and get way (1 bit): Tx or Rx
                // 0: Tx
                // 1: Rx
                u8 flags = (u8)(hdr.param & 0x0003);

                // get buffer pointer (32 bits): pointer to buffer
                u32 data = *(first_msg + 1);

                // send sniffed Beacon or MME to Maximus
                if (0 != maximus_ether_send(ctx, ETHERNET_TYPE_SNIFFER, sniffer_type, flags, data_length, (u32 *)data))
                {
                    station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                                "%s: errno = %d", __FUNCTION__, errno);
                    dbg_assert_print(false, "errno = %d when transmitting a sniffed packet", errno);
                }
            }
        }
    }
}