summaryrefslogtreecommitdiff
path: root/cesar/interface/fcall/src/interface_fcall.c
blob: e92837c8f1b5d2ad49c11547f25d267508f2f163 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
 
/**
 * \file    interface_fcall.c
 * \brief   The public interface fcall functions
 * \ingroup interface/fcall
 *
 * \todo   
 */
#include "common/std.h"
#include "lib/dbg.h"
#include "interface/fcall/inc/context.h"
#include "interface/fcall/inc/interface_fcall.h"
#include "interface/interface_module.h" // for 'INTERFACE_MODULE_FCALL'
#include "common/defs/ethernet.h" // for 'ETH_PACKET_MIN_SIZE' and 'ETH_PACKET_MAX_SIZE'
#include "common/defs/homeplugAV.h" // for 'HPAV_MTYPE_MME' and 'HPAV_MMV'
#include "hal/hle/defs.h"
#include "lib/read_word.h"
#include "lib/utils.h"
#include <errno.h>

/**
 * initialise the interface fcall module and the callback functions
 * \param  send_cb  the function to call when the interface fcall needs to send an INTERFACE message
 * \param  send_done_cb  the function to call when the interface fcall needs to send a SEND DONE message
 * \param  user_data  the data to provide on callback function
 */
interface_fcall_t*
interface_fcall_init (interface_fcall_send_message_cb_t send_cb,
                      interface_fcall_send_done_message_cb_t send_done_cb,
                      void *user_data)
{
    static interface_fcall_t ctx;
    static sci_ctx_t sci_ctx;
    static fcall_ctx_t fcall_ctx;
    static probe_ctx_t probe_ctx;

    dbg_assert_ptr(send_cb);
    dbg_assert_ptr(send_done_cb);
    dbg_assert_ptr(user_data);
    if ((NULL == send_cb)
        || (NULL == send_done_cb)
        || (NULL == user_data))
    {
        errno = EINVAL;
        return NULL;
    }

    memset(&ctx, '\0', sizeof(interface_fcall_t));
    ctx.fcall_ctx = &fcall_ctx;
    ctx.probe_ctx = &probe_ctx;
    ctx.send_cb = send_cb;
    ctx.send_done_cb = send_done_cb;
    ctx.user_data = user_data;
    if (sci_init(&sci_ctx, NULL) < 0)
    {
        dbg_assert_print(false, "sci init failed with errno = %d", errno);
        return NULL;
    }
    if (sci_register_send_callback(&sci_ctx, &interface_fcall_mme_send, (void*)&ctx) < 0)
    {
        dbg_assert_print(false, "sci register send callback failed with errno = %d", errno);
        return NULL;
    }
    if (fcall_init(&fcall_ctx, &sci_ctx) < 0)
    {
        dbg_assert_print(false, "fcall init failed with errno = %d", errno);
        return NULL;
    }
    if (probe_init(&probe_ctx, &fcall_ctx) < 0)
    {
        dbg_assert_print(false, "probe init failed with errno = %d", errno);
        return NULL;
    }

    return &ctx;
}

/**
 * uninitalise the interface fcall module
 * \param  ctx  the interface fcall context
 */
void
interface_fcall_uninit (interface_fcall_t *ctx)
{
    dbg_assert_ptr(ctx);
    if (NULL == ctx)
    {
        errno = EINVAL;
        return;
    }

    memset(ctx, '\0', sizeof(interface_fcall_t)); 
}

/**
 * process a received MME and request the interface module to send the MME response
 * \param  ctx  the interface fcall context
 * \param  mme  the MME buffer
 * \param  length  the MME length
 * \param  buffer  an empty allocated buffer for fcall response
 */
void
interface_fcall_mme_recv (void *data, u8 *mme, u8 *buffer)
{
    interface_fcall_t *ctx = NULL;
    static sci_msg_t msg;
    uint length = 0;

    dbg_assert_ptr(data);
    dbg_assert_ptr(mme);
    dbg_assert_ptr(buffer);
    if ( (NULL == data)
         || (NULL == mme)
         || (NULL == buffer) )
    {
        errno = EINVAL;
        return;
    }

    ctx = (interface_fcall_t*)data;

    /** Read ODA and OSA. */
    bitstream_init (&ctx->bitstream_ctx, mme, INTERFACE_FCALL_PAYLOAD_OFFSET, BITSTREAM_READ);
    bitstream_access (&ctx->bitstream_ctx, &ctx->oda, 48);
    bitstream_access (&ctx->bitstream_ctx, &ctx->osa, 48);
    bitstream_finalise (&ctx->bitstream_ctx);

    /** Read payload length. */
    length = read_u16_from_word(mme + INTERFACE_FCALL_PAYLOAD_OFFSET - sizeof(u16)) + INTERFACE_FCALL_PAYLOAD_OFFSET;

    if (sci_msg_init(&msg, buffer, SCI_MSG_MAX_SIZE) < 0)
    {
        dbg_assert_print(false, "sci msg init failed with errno = %d", errno);
        return;
    }
    if ((int)length != sci_msg_push(&msg, length))
    {
        dbg_assert_print(false, "sci msg push failed with errno = %d", errno);
        return;
    }
    bitstream_memcpy(msg.data_begin, mme, length);
    if (INTERFACE_FCALL_PAYLOAD_OFFSET != sci_msg_pop(&msg, INTERFACE_FCALL_PAYLOAD_OFFSET))
    {
        dbg_assert_print(false, "sci msg pop failed with errno = %d", errno);
        return;
    }
    dbg_assert_ptr(ctx->fcall_ctx);
    if ( (NULL != ctx->fcall_ctx)
         && (sci_recv_msg(ctx->fcall_ctx->sci, &msg) < 0) )
    {
        dbg_assert_print(false, "sci recv msg failed with errno = %d", errno);
        return;
    }

    /** To free buffer allocated by the driver. */
    dbg_assert_ptr(ctx->send_done_cb);
    if (NULL != ctx->send_done_cb)
    {
        (*ctx->send_done_cb)(ctx->user_data, mme);
    }
}

/**
 * request the interface module to send a MME
 * \param  data  pointer to the interface fcall context
 * \param  msg  the SCI message to send encapsulated into an MME
 */
void
interface_fcall_mme_send (void *data, sci_msg_t *msg)
{
    interface_fcall_t *ctx = NULL;
    uint field = 0, length = 0;
    uint word[2];

    dbg_assert_ptr(data);
    dbg_assert_ptr(msg);
    if ((NULL == data)
        || (NULL == msg))
    {
        errno = EINVAL;
        return;
    }

    ctx = (interface_fcall_t*)data;

    if (INTERFACE_FCALL_PAYLOAD_OFFSET != sci_msg_push(msg, INTERFACE_FCALL_PAYLOAD_OFFSET))
    {
        dbg_assert_print(false, "sci msg push failed with errno = %d", errno);
        return;
    }

    // Fill the MME Header.
    bitstream_init (&ctx->bitstream_ctx, msg->data_begin, INTERFACE_FCALL_PAYLOAD_OFFSET, BITSTREAM_WRITE);

    /* Inserting the ODA and the OSA. */
    bitstream_access (&ctx->bitstream_ctx, &ctx->osa, 48);
    bitstream_access (&ctx->bitstream_ctx, &ctx->oda, 48);

    /* Inserting the MTYPE. */
    field = htons(HPAV_MTYPE_MME);
    bitstream_access (&ctx->bitstream_ctx, &field, 16);

    /* Inserting the MMV. */
    field = HPAV_MMV;
    bitstream_access (&ctx->bitstream_ctx, &field, 8);

    /* Inserting the MMTYPE. */
    field = INTERFACE_FCALL_MMTYPE;
    bitstream_access (&ctx->bitstream_ctx, &field, 16);

    /* Inserting the FMI. */
    field = 0;
    bitstream_access (&ctx->bitstream_ctx, &field, 16);

    /* Inserting the module type. */
    field = INTERFACE_MODULE_FCALL;
    bitstream_access (&ctx->bitstream_ctx, &field, 8);

    /* Inserting the payload length. */
    length = msg->length - INTERFACE_FCALL_PAYLOAD_OFFSET;
    bitstream_access (&ctx->bitstream_ctx, &length, 16);

    bitstream_finalise (&ctx->bitstream_ctx);

    word[0] = BF_FILL (IPMBOX_REG, (MSG_TYPE, HLE_MSG_TYPE_INTERFACE),
                       (MSG_LENGTH, 1),
                       (PARAM_INTERFACE_TYPE, INTERFACE_MODULE_FCALL),
                       (PARAM_INTERFACE_LENGTH, msg->length));
    word[1] = (uint)msg->data_begin;

    /** Request the interface to send the message to the linux driver. */
    dbg_assert_ptr(ctx->send_cb);
    if (NULL != ctx->send_cb)
    {
        (*ctx->send_cb)(ctx->user_data, word, 2);
    }
}