summaryrefslogtreecommitdiff
path: root/cesar/cp/src/cp.c
blob: 3d0923b96fda60bc0213da2268171bbfd3a182a8 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/src/cp.c
 * \brief   CP functions.
 * \ingroup cp
 *
 */
#include "common/std.h"

#include "cp/cp.h"

#include "cp/inc/context.h"

/** Static data. */
static cp_t cp_global;

/**
 * Initialise the Control plane.
 * \param  mac_config  Mac configuration context.
 * \param  interface  Interface context.
 * \param  hal_timer  the HAL timer context.
 * \param  pbproc  the PBProcessing context.
 * \param  mac_store  the mac store context.
 * \param  sar  the SAR context.
 * \param  cl  the CL context.
 * \param  seed  the seed to initialise the random generator.
 * \return  The control plane context.
 *
 */
cp_t *
cp_init (mac_config_t * mac_config, interface_t * interface,
         hal_timer_t *hal_timer, pbproc_t *pbproc,
         mac_store_t *mac_store, sar_t *sar, cl_t *cl,
         u32 seed)
{
    dbg_assert (mac_config);
    dbg_assert (interface);
    dbg_assert (hal_timer);
    dbg_assert (pbproc);
    dbg_assert (sar);
    dbg_assert (cl);

    cp_global.mac_config = mac_config;
    cp_global.interface = interface;
    cp_global.hal_timer = hal_timer;
    cp_global.pbproc = pbproc;
    cp_global.phy = pbproc_get_phy (pbproc);
    cp_global.ca = pbproc_get_ca (pbproc);
    cp_global.mac_store = mac_store;
    cp_global.sar = sar;
    cp_global.cl = cl;

    /* Initialise traces. */
    cp_trace_init (&cp_global);

    /* Initialise the beacon module. */
    cp_beacon_init (&cp_global);

    /* Initialise the CCo module. */
    cp_cco_action_init (&cp_global);
    cp_cco_bw_init (&cp_global);
    cp_cco_region_init (&cp_global);

    /* Initialise the Control plane. */
    cp_cl_interf_init (&cp_global);

    /* Initialise the Connection manager. */
    //cp_conn_mgr_init (&cp_global);

    /* Initialise the MSG module. */
    cp_msg_init (&cp_global);

    /* Initialise the PWL module. */
    cp_pwl_init (&cp_global);

    /* Initialise the Station. */
    cp_sta_action_init (&cp_global);

    /* Initialise the station core. */
    cp_sta_core_init (&cp_global);

    /* Initialise the station manager.  */
    cp_sta_mgr_init (&cp_global);

    /* Initialise the FSM. */
    cp_fsm_init (&cp_global);

    /* Init the random generator. */
    lib_rnd_init (&cp_global.rnd, seed ^ 0x87543571);

    /* Init TXCE context. */
    txce_init (&cp_global, mac_store, mac_config);

    return &cp_global;
}

void
cp_uninit (cp_t *ctx)
{
    dbg_assert (ctx);

    cp_fsm_uninit (ctx);
    cp_sta_core_uninit (ctx);
    cp_sta_mgr_uninit (ctx);
    cp_beacon_uninit (ctx);
    cp_cco_action_uninit (ctx);
    cp_cco_bw_uninit (ctx);
    cp_cco_region_uninit (ctx);
    cp_cl_interf_uninit (ctx);
    cp_msg_uninit (ctx);
    cp_pwl_uninit (ctx);
    cp_trace_uninit (ctx);
}

/**
 * Compute the NID and the NMK from the network password.
 * \param  ctx  the CP context.
 * \param  npw  the network password.
 * \param  sl the security level of the station.
 *
 * This function will generate the NID and the NMK from the NPW and store it
 * in the station own data.
 */
void
cp_compute_nmk_and_nid_from_npw (cp_t *ctx, const char *npw,
                                 cp_security_level_t sl)
{
    u64 nid;
    cp_key_t nmk;
    uint length;

    dbg_assert (ctx);
    dbg_assert (npw);

    length = strlen (npw);

    nmk = cp_secu_npw2nmk ((u8*) npw, length);
    nid = cp_secu_nmk2nid (nmk, sl);

    /* Store the nid and the nmk to the station own data. */
    cp_sta_own_data_set_nmk (ctx, nmk);
    cp_sta_own_data_set_nid (ctx, nid);
}