summaryrefslogtreecommitdiff
path: root/cesar/cp/msg/src/msg.c
blob: b5eb37cdca24c60490211a2a97e5cb3eef59f2e2 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/msg/msg.c
 * \brief   MME message processing
 * \ingroup cp_msg
 */

#include "common/std.h"

#include "cp/beacon/inc/bentry.h"
#include "cp/beacon/inc/beacons_ctx.h"
#include "cp/beacon/forward.h"
#include "cp/beacon/inc/beacons_work.h"

#include "cp/msg/msg.h"

/*
 * this array describe messages, their prefered destination and the function
 *  which process it.
 * the array must be ordered on the first field of each line.
 */
const msg_list_mme_t msg_list_m[] = 
    { 
        { CC_CCO_APPOINT_REQ,           0 },
        { CC_CCO_APPOINT_CNF,           0 }, 
        { CC_BACKUP_APPOINT_REQ,        0 }, 
        { CC_BACKUP_APPOINT_CNF,        0 },
        { CC_LINK_INFO_REQ,             0 },
        { CC_LINK_INFO_CNF,             0 },
        { CC_LINK_INFO_IND,             0 },
        { CC_LINK_INFO_RSP,             0 },
        { CC_HANDOVER_REQ,              0 }, 
        { CC_HANDOVER_CNF,              0 }, 
        { CC_HANDOVER_INFO_IND,         0 }, 
        { CC_HANDOVER_INFO_RSP,         0 },
        // TBC ...
        {CC_ASSOC_REQ,                  RECEIVE_CC_ASSOC_REQ},
        {CC_ASSOC_CNF,                  RECEIVE_CC_ASSOC_CNF},
        {CC_SET_TEI_MAP_REQ,            RECEIVE_CC_SET_TEI_MAP_REQ},
        {CC_SET_TEI_MAP_IND,            RECEIVE_CC_SET_TEI_MAP_IND},
        
        { CM_UNASSOCIATED_STA_IND,      RECEIVE_USTA_MME},
        // TBC ...

        {DRV_STA_SET_MAC_ADDR_REQ,      RECEIVE_DRV_SET_MAC_ADDRESS},   
        {DRV_STA_SET_CCO_PREF_REQ,      RECEIVE_DRV_SET_CCO_PREFERENCE_REQ},
        {DRV_STA_SET_WAS_CCO_REQ,       RECEIVE_DRV_SET_WAS_CCO},
        {DRV_STA_SET_NPW_REQ,           RECEIVE_DRV_SET_NPW_REQ},
        {DRV_STA_SET_DPW_REQ,           RECEIVE_DRV_SET_DPW_REQ},
        {DRV_STA_SET_SL_REQ,            RECEIVE_DRV_SET_SL},
        {DRV_STA_SET_M_STA_HFID_REQ,    RECEIVE_DRV_SET_M_STA_HFID},
        {DRV_STA_SET_U_STA_HFID_REQ,    RECEIVE_DRV_SET_U_STA_HFID},
        {DRV_STA_SET_AVLN_HFID_REQ,     RECEIVE_DRV_SET_AVLN_HFID},
        {DRV_STA_SET_TONEMASK_REQ,      RECEIVE_DRV_SET_TONEMASK},
        {DRV_STA_START_REQ,             RECEIVE_DRV_MAC_START_REQ},
        {DRV_STA_STOP_REQ,              RECEIVE_DRV_MAC_STOP_IND}       
        /*
        {DRV_STA_SET_NID_REQ,           RECEIVE_DRV_SET_NID},
        {DRV_STA_SET_SNID_REQ,          RECEIVE_DRV_SET_SNID},
        */
    };

const char *cst_str_drv_mme_req[] = {
    "DRV_STA_SET_MAC_ADDR.REQ",
    "DRV_STA_SET_CCO_PREF.REQ",
    "DRV_STA_SET_WAS_CCO.REQ",
    "DRV_STA_SET_NPW.REQ",
    "DRV_STA_SET_DPW.REQ",
    "DRV_STA_SET_SL.REQ",
    "DRV_STA_SET_M_STA_HFID.REQ",
    "DRV_STA_SET_U_STA_HFID.REQ",
    "DRV_STA_SET_AVLN_HFID.REQ",
    "DRV_STA_SET_TONEMASK.REQ",
    "DRV_STA_START.REQ",
    "DRV_STA_STOP.REQ"
};

const char *cst_str_drv_mme_cnf[] = {
    "DRV_STA_SET_MAC_ADDR.CNF",
    "DRV_STA_SET_CCO_PREF.CNF",
    "DRV_STA_SET_WAS_CCO.CNF",
    "DRV_STA_SET_NPW.CNF",
    "DRV_STA_SET_DPW.CNF",
    "DRV_STA_SET_SL.CNF",
    "DRV_STA_SET_M_STA_HFID.CNF",
    "DRV_STA_SET_U_STA_HFID.CNF",
    "DRV_STA_SET_AVLN_HFID.CNF",
    "DRV_STA_SET_TONEMASK.CNF",
    "DRV_STA_START.CNF",
    "DRV_STA_STOP.CNF"
};

// this is the fmsn that will be used in fragmented mme
static u8 fmsn_m;

void msg_init(void)
{
#if DEBUG == 0
    uint i;
    for(i=1 ; i<COUNT(msg_list_m) ; i++) 
        dbg_assert(msg_list_m[i].mm_type > msg_list_m[i-1].mm_type);
#endif
    fmsn_m = 0;
    msg_cm_init();
}

u8
msg_get_fmsn(const bool new)
{
    printf ("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    // TODO the question is : is it possible to have 2 thread accessing this 
    // function at the same time ?
    if(new) fmsn_m++;
    return fmsn_m; 
}

u16 
msg_get_number (msg_mme_t *msg)
{
    msg_mme_b_t * msg_b;
    u16 mme_num;
    u16 ret = MSG_UNKNOW;
      
    printf ("%u %s() : DEBUT...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert ( !msg_check_wrong_mme_const_values (msg));
    msg_b = (msg_mme_b_t *)msg; 
    printf ("%u %s() : VERIF... msg->m_type = 0x%04x ; msg_b->m_type = 0x%04x\n",cyg_hal_sys_getpid(),__FUNCTION__,big_to_cpuhost_endian_u16(msg->m_type),big_to_cpuhost_endian_u16(msg_b->m_type));
    //TODO this is maybe not very efficient, to check with more messages ...
    if (big_to_cpuhost_endian_u16(msg->m_type) == MSG_MTYPE_IEEE_ETHERTYPE)
    {
        printf ("%u %s() : DICHOTOMY sur msg->mm_type 0x%04x...\n",cyg_hal_sys_getpid(),__FUNCTION__,msg->mm_type);
        DICHOTOMY_SEARCH(0, COUNT(msg_list_m), mme_num, msg->mm_type <= msg_list_m[mme_num].mm_type);
        printf("%u requested msg : 0x%04x, index found : %i\n",cyg_hal_sys_getpid(), msg->mm_type, mme_num);
        if(msg->mm_type != msg_list_m[mme_num].mm_type)
        {
            printf ("%u %s() : FIN. ; MSG_UNKNOW !\n",cyg_hal_sys_getpid(),__FUNCTION__);
            ret = MSG_UNKNOW;
        }
        else
        {
            printf ("%u %s() : FIN. ; mme_num = %i (0x%04x)\n",cyg_hal_sys_getpid(),__FUNCTION__,mme_num,mme_num);
            ret = mme_num;
        }
    }
    else if ((big_to_cpuhost_endian_u16(msg_b->m_type) == MSG_MTYPE_IEEE_ETHERTYPE))
    {
        printf ("%u %s() : DICHOTOMY sur msg_b->mm_type 0x%04x...\n",cyg_hal_sys_getpid(),__FUNCTION__,msg_b->mm_type);
        DICHOTOMY_SEARCH(0, COUNT(msg_list_m), mme_num, msg_b->mm_type <= msg_list_m[mme_num].mm_type);
        printf ("%u requested msg : 0x%04x, index found : %i\n",cyg_hal_sys_getpid(), msg_b->mm_type, mme_num);
        if(msg_b->mm_type != msg_list_m[mme_num].mm_type)
        {
            printf ("%u %s() : FIN. ; MSG_UNKNOW !\n",cyg_hal_sys_getpid(),__FUNCTION__);
            ret = MSG_UNKNOW;
        }
        else
        {
            printf ("%u %s() : FIN. ; mme_num = %i (0x%04x)\n",cyg_hal_sys_getpid(),__FUNCTION__,mme_num,mme_num);
            ret = mme_num;
        }
    }
    else
    {
        printf ("%u %s() : FIN. ; ERREUR (mme_num = %i (0x%04x) !!!\n",cyg_hal_sys_getpid(),__FUNCTION__,mme_num,mme_num);
    }
    return ret;
}

//void
//msg_dispatch (msg_mme_t *msg)
void
msg_dispatch (msg_ctx_t *msg_ctx)
{
    msg_mme_t *msg;
    msg_mme_b_t *msg_b;
    u16 mme_num;

    printf ("%u %s() : DEBUT...\n",cyg_hal_sys_getpid(),__FUNCTION__);

    dbg_assert(msg_ctx);
    dbg_assert(msg_ctx->buffer);
    msg   = (msg_mme_t *)msg_ctx->buffer;
    msg_b = (msg_mme_b_t *)msg;

    dbg_assert ( !msg_check_wrong_mme_const_values (msg));
    mme_num = msg_get_number (msg);
    printf ("%u %s() : suite... ; mme_num = %i (0x%04x)\n",cyg_hal_sys_getpid(),__FUNCTION__,mme_num,mme_num);
    if (mme_num != MSG_UNKNOW)
    {
        dbg_assert(msg_list_m[mme_num].msg_num);
        cp_station_add_event(msg_list_m[mme_num].msg_num, (void *) msg_ctx);
    }
    // release the message's buffer(s)
//    interf_release_buf(msg);
    printf ("%u %s() : FIN.\n",cyg_hal_sys_getpid(),__FUNCTION__);
}

void 
msg_set_mme_const_values (msg_mme_t *msg)
{
    dbg_assert (msg);
    
    msg->m_type = cpuhost_to_big_endian_u16(MSG_MTYPE);    // 11.1.4
    msg->mmv = MSG_MM_VERSION;
    #if CHECK_BUFFER_OVERFLOW
    interf_buffer_t *buffer = NULL;
    buffer = PARENT_OF(interf_buffer_t, msg, msg);
    buffer->signature = BUFFER_SIGNATURE;
    #endif
}

bool 
msg_check_wrong_mme_const_values (const msg_mme_t *msg)
{int i; u8 *p = (u8*)msg;
    msg_mme_b_t *msg_b;

    printf ("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    for (i=0;i<1;i++)printf("msg = 0x%08lx ",(unsigned long)msg);printf("\n");
    dbg_assert (msg);

    msg_b = (msg_mme_b_t *)msg;

    #if CHECK_BUFFER_OVERFLOW
    interf_buffer_t *buffer = NULL;
    buffer = PARENT_OF(interf_buffer_t, msg, msg);
    dbg_assert(buffer->signature == BUFFER_SIGNATURE);
    #endif

    for (i= 0;i<16;i++)printf("%02x.",p[i]);printf("\n");
    for (i=16;i<32;i++)printf("%02x.",p[i]);printf("\n");
    for (i=32;i<48;i++)printf("%02x.",p[i]);printf("\n");
    for (i=48;i<64;i++)printf("%02x.",p[i]);printf("\n");
    printf("%u %s() : msg->m_type   = 0x%04x\n",cyg_hal_sys_getpid(),__FUNCTION__,big_to_cpuhost_endian_u16(msg->m_type));
    printf("%u %s() : msg_b->m_type = 0x%04x\n",cyg_hal_sys_getpid(),__FUNCTION__,big_to_cpuhost_endian_u16(msg_b->m_type));
    if (big_to_cpuhost_endian_u16(msg->m_type) == MSG_MTYPE)
        for (i=0;i<1;i++)printf("msg->m_type = 0x%04x ; msg->mm_type = 0x%04x",big_to_cpuhost_endian_u16(msg->m_type),msg->mm_type);
    else if (big_to_cpuhost_endian_u16(msg_b->m_type) == MSG_MTYPE)
        for (i=0;i<1;i++)printf("msg->m_type = 0x%04x ; msg->mm_type = 0x%04x",big_to_cpuhost_endian_u16(msg_b->m_type),msg_b->mm_type);
    else
        for (i=0;i<1;i++)printf("msg->m_type = 0x???? ; msg->mm_type = 0x????");
    printf("\n");

    return ((big_to_cpuhost_endian_u16(msg->m_type) != MSG_MTYPE) && (big_to_cpuhost_endian_u16(msg_b->m_type) != MSG_MTYPE));
}

msg_mme_t *
msg_sending_common_part(msg_mme_t *msg, const mac_address_t oda, msg_param_t *msg_param)
{   
    dbg_assert(msg);
    dbg_assert(msg_param);
    
    // ask for a buffer
    //msg = interf_give_buf ();
    //dbg_assert ( !msg_check_wrong_mme_const_values (msg));
    // set the header values of msg
    memcpy(msg->oda, oda, sizeof(mac_address_t));
    //memcpy(msg->osa, station_get_mac_address(), sizeof(mac_address_t));
    cp_station_get_mac_address(NULL,&msg->osa); //TODO : pass CP ctx as first argument instead of NULL pointer...
    // vlan is optional and not used
    msg->m_type = cpuhost_to_big_endian_u16(MSG_MTYPE);    // 11.1.4
    msg->mmv = MSG_MM_VERSION;
    // set fragmentation default values
    msg->fmi.nf_mi = 0;
    msg->fmi.fn_mi = 0;
    msg->fmi.fmsn  = 0;
    // set the default message parameters
    memset(msg_param, 0, sizeof(msg_param_t));
    msg_param->encryption = HARDWARE_ENCRYPTED;
    msg_param->peks = NOT_ENCRYPTED;
    return msg;
}

void
msg_send(msg_mme_t *msg, u16 msg_size, msg_param_t msg_param)
{
    tei_t tei;
    
    dbg_assert ( !msg_check_wrong_mme_const_values (msg));
    dbg_assert(msg_size <= ETH_PACKET_MAX_SIZE);

    // find the tei of the ODA
    tei = cp_station_find_tei_from_mac(msg->oda);
    // check if the message is software encrypted
    if(msg_param.encryption == SOFTWARE_ENCRYPTED)
    {
        
    }
    else
    {
        // and then, send the message
        interf_send (msg, msg_size, msg_param, tei);
    }
}

/**
 * \brief Get DRV-MME .REQ message name
 *  
 * \param  cnf  the REQ message type identifier 
 * \return the .REQ message name string constant
 */
char *
get_str_drv_mme_req(int req)
{
    return (char*)cst_str_drv_mme_req[req];
}

/**
 * \brief Get DRV-MME .CNF message name
 *  
 * \param  cnf  the CNF message type identifier 
 * \return the .CNF message name string constant
 */
char *
get_str_drv_mme_cnf(int cnf)
{
    return (char*)cst_str_drv_mme_cnf[cnf];
}

#if 0
/**
 * \brief Get a message type name string
 *  
 * \param  mmtype  the message type identifier 
 * \return the message type name string constant
 */
char *
get_str_mmtype(u16 mmtype)
{
    return (char*)cst_str_drv_mme_cnf[cnf];
}
#endif