summaryrefslogtreecommitdiff
path: root/test_general/integration/station-cp-dp/src/station-cp-dp.c
blob: 2bc708a0ecd5eecccdd4bf6a3199fed20cf95c18 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    station-cp-dp.c
 * \brief   « brief description »
 * \ingroup « module »
 *
 * « long description »
 */

#include "common/std.h"
#include "host/station.h"

#include "lib/test.h"
#include "lib/trace.h"
#include "lib/read_word.h"
#include "lib/restrack.h"

#include "mac/ca/ca.h"
#include "mac/sar/sar.h"
#include "cl/cl.h"
#include "hle/hle.h"

#include "mac/common/ntb.h"
#include "cp/beacon/beacons.h"
#include "cp/beacon/inc/beacons_ctx.h"
#include "cp/beacon/forward.h"

#include "cp/secu/secu.h"
#include "cp/interf/interf.h"
#include "cp/msg/inc/msg_drv.h"
#include "cp/station/station.h"

#include "hal/hle/ipmbox.h"

/*#include "test_general/station/int_station.h"*/
#include "test_general/station/int_station_fc.h"

#include "cp/station/maximus/inc/maximus_cp_station.h"

/*#include "cp/test/inc/test_secu.h"
#include "cp/test/inc/test_msg.h"
#include "cp/test/inc/test_interf.h"
#include "cp/test/inc/test_station.h"
#include "cp/beacon/inc/beacon_types.h"*/

/*int_station_t int_sta_global;*/
cp_sta_t cp_sta_global;

cp_secu_t sec_m;
mac_config_t mac_config;

#define CP_THREAD_PRIORITY 30
static unsigned char cp_thread_stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
static cyg_handle_t cp_thread_handle;
static cyg_thread cp_thread;

#define CE_THREAD_PRIORITY 29
static unsigned char ce_thread_stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
static cyg_handle_t ce_thread_handle;
static cyg_thread ce_thread;

extern station_ctx_t my_station;

/* Use to keep a list of rx params in the CE */
list_t ce_rx_params_list;

struct ce_rx_params_node_t
{
    list_node_t node;
    pbproc_rx_params_t *rx_params;
};
typedef struct ce_rx_params_node_t ce_rx_params_node_t;

/****************** VARIABLES DEFINITION *****************/


/******************* CALLBACK FUNCTIONS ******************/


/**
 * Cp beacon call back when the leon timer expires.
 * It shall call the add event function of the Visual state FSM.
 *
 * \param user_data  the user_data provided on the cp_beacon_init function.
 */
void
cp_beacon_timer_expires (void *user_data)
{
#if DEBUG
    printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
#endif
}

/*********************** FUNCTIONS ***********************/

/**
 * \brief Rx-CE thread body.
 * 
 */
void 
ce_thread_main (cyg_addrword_t data)
{
#if DEBUG
    printf("%u %s() : Rx-CE thread ready...\n",cyg_hal_sys_getpid(),__FUNCTION__);
#endif
    while(1)
    {
        printf("%u %s() : Rx-CE thread loop...\n",cyg_hal_sys_getpid(),__FUNCTION__);
        cyg_thread_delay (300);
    }
}

/**
 * \brief CE init and Rx-CE thread launch.
 * 
 * \return 0
 * 
 */
int
ce_init (void)
{
    // TODO : CE init

    // ...
    
    // creation of the Rx-CE thread
    cyg_thread_create (
                        CE_THREAD_PRIORITY,
                        &ce_thread_main,
                        0,
                        "CE-Rx",
                        ce_thread_stack,
                        sizeof(ce_thread_stack),
                        &ce_thread_handle,
                        &ce_thread);
    cyg_thread_resume (ce_thread_handle);

    return 0;
}

/**
 * \brief CP init and thread launch.
 * 
 * \param  mac_store_ctx  the pointer to MAC store context
 * \param  mac_config  the pointer to MAC configuration
 * \param  interf_cl_ctx  the pointer to CL context
 * \param  pbproc  the pointer to PBProc context
 * \param  ca_ctx  the pointer to CA context
 * \param  hal_timer  the pointer to HAL timer context
 * 
 * \return 0
 * 
 */
int
cp_init (mac_store_t *mac_store_ctx, mac_config_t *mac_config, cl_t *interf_cl_ctx, pbproc_t *pbproc, ca_t *ca_ctx,hal_timer_t *hal_timer)
{
    ntb_t *my_ntb_ctx;
    phy_t *my_phy_ctx;

    dbg_assert(mac_store_ctx);
    dbg_assert(mac_config);
    dbg_assert(interf_cl_ctx);
    dbg_assert(pbproc);
    dbg_assert(ca_ctx);

    /*
     * reset structures containing CP params :
     * 
     *  - struct to store CP-STATION params (obtained by receiving them as DRV-MME messages)
     *  - struct to store CP-STATION security params computed from NPW (NPW is a param stored in flash)
     */
    memset(&cp_sta_global.cp_sta_flash_params,0x00,sizeof(cp_sta_global.cp_sta_flash_params));
    memset(&cp_sta_global.cp_sta_secu_params,0x00,sizeof(cp_sta_global.cp_sta_secu_params));

    /*
     * get phy context before calling ntb_init() because ca_ctx pointer
     * is an opaque context pointer which does not permit to dereference
     * the phy context pointer...
     */
    my_phy_ctx = pbproc_get_phy (pbproc);

    /* init the NTB clock synchronization management */
    my_ntb_ctx = ntb_init(my_phy_ctx, mac_config);

    /*
     * init MME message interface with CL
     * (interfacing CP with CL is done through CP-DP dedicated interface & sniffer module)
     */
    interf_init (interf_cl_ctx);

    /* init message management module */
    msg_init ();

    /* init the security management module */
    cp_sta_global.sec = NULL;
    secu_init (&sec_m);
    cp_sta_global.sec = &sec_m;

    /* init the station management */
    cp_station_init (mac_store_ctx, interf_cl_ctx, pbproc, ca_ctx);

    /* init CCo context */
    cp_sta_global.cco = cp_cco_init();

    /* init the beacon management */
    cp_sta_global.hal_timer = hal_timer_init (my_phy_ctx);
#if 1
    cp_sta_global.cp_beacon = cp_beacon_init(cp_sta_global.interface,            
                                             pbproc_get_phy(cp_sta_global.pbproc),
                                             &cp_sta_global,
                                             cp_sta_global.sec,
                                             pbproc_get_ca(cp_sta_global.pbproc),
                                             cp_sta_global.pbproc,
                                             cp_sta_global.mac_store,
                                             cp_sta_global.cco,
                                             cp_sta_global.hal_timer,
                                             NULL, // ??? (void *user_data)
                                             cp_beacon_timer_expires);
    cp_sta_global.cp_beacon->sta = &cp_sta_global;
#endif
    // creation of the CP thread
    printf("%u %s() : AVANT cyg_thread_create()...",cyg_hal_sys_getpid(),__FUNCTION__);
    cyg_thread_create (
                        CP_THREAD_PRIORITY,
                        &cp_station_wait_event,
                        0,
                        "CP",
                        cp_thread_stack,
                        sizeof(cp_thread_stack),
                        &cp_thread_handle,
                        &cp_thread);
    cyg_thread_resume (cp_thread_handle);
    printf("%u %s() : APRES cyg_thread_resume()...",cyg_hal_sys_getpid(),__FUNCTION__);

    // CE init and Rx-CE thread launch...
    ce_init ();

    return 0;
}

#if 0
/**
 * \brief mac config initialisation.
 * 
 * \return mac_config (pointer to mac config)
 * 
 */
mac_config_t * my_mac_config_init(void)
{
    mac_config_t *mac_config;

    mac_config = blk_alloc ();
    mac_config->rts_broadcast = false;
    mac_config->fc_symbols_nb = 1;
    mac_config->ntb_offset_tck = 0;
    tonemask_default ((u8*) &mac_config->tonemask_info);
    tonemask_update (&mac_config->tonemask_info);

    return mac_config;
}
#endif

/**
 * \brief CESAR application software entry-point function.
 * 
 * \return true
 * 
 */
int
cesar_init (void)
{
    trace_init ();

    cp_sta_global.mac_store  = mac_store_init ();
    //cp_sta_global.mac_config = my_mac_config_init ();
    mac_config_init (&mac_config);
    cp_sta_global.mac_config = &mac_config;
    cp_sta_global.pbproc     = pbproc_init (cp_sta_global.mac_config,
                                            cp_sta_global.mac_store);

    mac_ntb_init (pbproc_get_phy(cp_sta_global.pbproc),
                                 cp_sta_global.mac_config);

    cp_sta_global.sar = sar_init (cp_sta_global.mac_store,
            cp_sta_global.pbproc, pbproc_get_ca (cp_sta_global.pbproc));

    cp_sta_global.cl = cl_init (cp_sta_global.mac_store, cp_sta_global.sar,
                                cp_sta_global.mac_config);

    cp_sta_global.hle = hle_init (cp_sta_global.cl);

    cp_sta_global.interface = interface_init(cp_sta_global.hle, cp_sta_global.cl, cp_sta_global.sar, cp_sta_global.mac_config);

    /*---------------------------------------------------------------------*/
#if 0
    my_sta = int_station_init ();

    cp_init (my_sta->mac_store, my_sta->cl, my_sta->pbproc, NULL);
#else
    // CP init and thread launch...
    cp_init (cp_sta_global.mac_store,
             cp_sta_global.mac_config,
             cp_sta_global.cl,
             cp_sta_global.pbproc,
             pbproc_get_ca(cp_sta_global.pbproc),
             cp_sta_global.hal_timer
            );

    // start HLE...
    hle_activate(cp_sta_global.hle, true);

#endif

#if 0
    list_init (&ce_rx_params_list);

    sar_init_measure_context (my_sta->sar, my_sta->sar);
    sar_init_measurement_cb (my_sta->sar, ce_measurements);

    sar_init_measure_context (my_sta->sar, my_sta->sar);
    sar_init_measurement_cb (my_sta->sar, ce_measurements);
/*
    fcall_register (my_station.fcall, "fc_station_link_add",
            &fc_station_link_add, NULL);

    fcall_register (my_station.fcall, "fc_station_link_remove",
            &fc_station_link_remove, NULL);

    fcall_register (my_station.fcall, "fc_station_init_config",
            &fc_station_init_config, NULL);

    fcall_register (my_station.fcall, "fc_station_discover",
            &fc_station_discover, NULL);    memset(&cp_sta_flash_params,0x00,sizeof(cp_sta_flash_params));
            

    fcall_register (my_station.fcall, "fc_station_uninit",
            &fc_station_uninit, NULL);

    fcall_register (my_station.fcall, "fc_cp_mme_send_as_data",
            &fc_cp_mme_send_as_data, NULL);

    fcall_register (my_station.fcall, "fc_cp_mme_send_as_mme",
            &fc_cp_mme_send_as_mme, NULL);
*/
    fcall_register (my_station.fcall, "fc_hle_print_trace",
            &fc_hle_print_trace, NULL);

    fcall_register (my_station.fcall, "fc_cl_print_trace",
            &fc_cl_print_trace, NULL);

    fcall_register (my_station.fcall, "fc_sar_print_trace",
            &fc_sar_print_trace, NULL);


    list_init (&ce_rx_params_list);
#endif
    return true;
}

#if 0
/**
 * simulates the Leon timer to awake the beacon module.
 *
 */
void
phy_leon_timer (cyg_addrword_t int_sta)
{
    cyg_thread_yield();

    cyg_thread_delay(cp_sta_global.cp_beacon->awake_time);
}
#endif

/**
 * \brief Entry-point function, main() or cyg_user_start().
 * - main() or cyg_user_start() called directly by maximus core in case of maximus simulation environment, 
 * - cyg_user_start() called by eCos on true target...
 * 
 * \return cesar initialisation return code
 * 
 */
int
main (void)
{
    /* inits specifiques pour environnement de simulation Maximus */
#define SIMU_WITH_MAXIMUS
#ifdef SIMU_WITH_MAXIMUS
    maximus_cp_station_init(&my_station);
    station_log_set_level(&my_station, STATION_LOG_WARNING);
    station_log_set_mask(&my_station, STATION_LOGTYPE_ALL);
    my_station.pipe_log_fd = 1;
#endif

    return cesar_init();
}