summaryrefslogtreecommitdiff
path: root/cesar/cp/eoc/cco/action/src/cco_action.c
blob: 4b2bc189fca392bc636d6486b15b8a08ef746221 (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
/* Cesar EOC project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/eoc/cco/action/src/cco_action.c
 * \brief   CCo Action functions.
 * \ingroup cp_eoc_cco_action
 *
 */
#include "common/std.h"

/* Private headers. */
#include "cp/inc/context.h"
#include "cp/eoc/inc/dbg_print.h"
#include "cp/sta/mgr/inc/sta.h"
#include "cp/sta/mgr/inc/net.h"
#include "mac/pbproc/inc/context.h"
#include "cp/eoc/cco/action/inc/cco_action.h"

/* Public headers. */
#include "mac/common/defs.h"
#include "cp/beacon/beacon.h"
#include "cp/eoc/cco/bw/bw.h"
#include "cp/eoc/cco/action/cco_action.h"
#include "cp/eoc/sta/mgr/sta_mgr.h"
#include "cp/eoc/sta/action/drv.h"
#include "cp/sta/mgr/sta_mgr.h"
#include "cp/sta/mgr/sta.h"
#if CONFIG_CP_EOC_IS_MASTER
#include "cp/eoc/multi_sta/action/multi_sta_action.h"
#endif

/* Config headers. */
#include "config/cp/eoc/cco/action.h"

#if CONFIG_CP_EOC_CCO_ACTION_CON_ALLOWED

#include "mac/sar/inc/context.h"

#define CONFIG_CP_EOC_CCO_CON_TIMEOUT_TCK MAC_MS_TO_TCK (950)
#define SUB_STAT_ATTR(a, b, c) ((a).c - (b).c)
#define SUB_STAT(c) SUB_STAT_ATTR (ctx->sar->stats, sar_offset, c)

static int mem = 0, auth_time = 0;
static sar_stats_t sar_offset;
#endif


void
cp_eoc_cco_action__change_nek (cp_t *ctx)
{
    /* Program the timer to periodically change the NEK. */
    cp_fsm_event_t * event = cp_fsm_event_bare_new (
        ctx, CP_FSM_EVENT_TYPE_cco__nek_change);
    cp_sta_core_gen_timed_event (ctx, &ctx->cco_action.nek_change,
                                 event, HPAV_NEK_CHANGE_MS);
}

void
cp_eoc_cco_action__power_on_no_beacons (cp_t *ctx)
{
    cp_tei_t tei;
    cp_net_t *net;
    cp_nid_t nid;
    cp_snid_t snid;
    cp_sta_own_data_t *own;
    dbg_assert (ctx);
    ctx->cco_action.last_date = phy_date ();
    tei = MAC_TEI_CCO_MIN;
    cp_sta_own_data_set_tei (ctx, tei);
    cp_sta_own_data_set_authenticated_status (ctx, true);

    cp_eoc_sta_action_drv__drv_sta_get_status_ind (ctx);

    /* Set our AVLN. */
    snid = cp_sta_own_data_get_snid (ctx);
    nid = cp_sta_own_data_get_nid (ctx);

    net = cp_sta_mgr_add_avln (ctx, snid, nid);
    cp_sta_mgr_set_our_avln (ctx, net);

    /* Set the CCo status in the station own data. */
    own = cp_sta_mgr_get_sta_own_data (ctx);
    own->nid_track = cp_sta_own_data_get_nid (ctx);
    cp_sta_own_data_set_cco_status (ctx, true);
    /* Set the NEK. */
    cp_cco_action_gen_nek (ctx);
    cp_beacon_change_nek (ctx, ctx->cco_action.new_nek.eks,
                          ctx->cco_action.new_nek.nek_enc, true /* now. */);
    /* Start  timer for periodic NEK exchange */
    cp_eoc_cco_action__change_nek (ctx);
    /* set wl status. In maximus accept all stations  */
#if MODULE_INCLUDED (hal_phy_maximus)
    ctx->cco_action.wl_accept_all = 1;
    ctx->cco_action.gm_default_output_power = 0;
    cp_key_t key = cp_secu_generate_keys (ctx, (u8 *) MASTER_GOLDEN_DPW,
            strlen (MASTER_GOLDEN_DPW), CP_SECU_SALT_KEY_DAK);
    cp_sta_own_data_set_dak (ctx, key);
#endif
}

void
cp_eoc_cco_action_poweron__idle__to_poweron (cp_t *ctx)
{
    /* simulated call to setup cp status */
    cp_eoc_cco_action__power_on_no_beacons (ctx);
    cp_eoc_cco_bw_bp_allocations (ctx);
    cp_beacon_poweron_init (ctx);
    bsu_init_scheduler_prepare (ctx->bsu, cp_eoc_scheduler_prepare, ctx);
    bsu_power_on (ctx->bsu, cp_sta_own_data_get_snid (ctx));
    bsu_activate (ctx->bsu, true);
    cp_beacon_cco_update_beacon_data (ctx);
    sar_activate (ctx->sar, true);
    pbproc_activate (ctx->pbproc, true);
    ctx->sta_action.assoc.peer = CP_MME_PEER (MAC_BROADCAST,
                                              MAC_TEI_UNASSOCIATED);
    cp_fsm_post_new_event (ctx, bare, BEACON_NOT_RECEIVED);
}

void
cp_eoc_cco_action__power_on_rx_beacon (cp_t *ctx)
{
}

void
cp_eoc_cco_action__cco_drv_mac_stop (cp_t *ctx)
{
}

void
cp_eoc_cco_action__bcco_drv_mac_stop (cp_t *ctx)
{
}

void
cp_eoc_cco_action__bcco_no_beacons (cp_t *ctx)
{
}

/**
 * bw scheduler
 * \param  user_data  cp * context provided to BSU.
 * call in DSR context on timer to assure that scheduling will be called
 * in each beacon period.
*/
void
cp_eoc_scheduler_prepare (void * user_data)
{
    /* only Master compilation */
#if CONFIG_CP_EOC_IS_MASTER
    cp_t * ctx = (cp_t *)user_data;
    dbg_assert (ctx);
    cp_eoc_cco_bw_scheduler (ctx);
#endif
}

void
cp_eoc_cco_action_send_central_beacon (cp_t *ctx)
{
    dbg_assert (ctx);
    arch_dsr_lock ();
    cp_eoc_cco_bw_bp_allocations (ctx);
    arch_dsr_unlock ();
#if CONFIG_CP_EOC_CCO_ACTION_CON_ALLOWED
    if (++mem >= 10)
    {
        auth_time++;
        mem = 0;
    }
    if (less_mod2p32 (ctx->cco_action.last_date, phy_date ()))
    {
        char temp[128];
        sprintf (temp, "cco=%d blk=%d sar[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]"
                 " pb[%d,%d]\n",
                 auth_time, blk_free_nb (),
                 SUB_STAT (rx_jobs_nb),
                 ctx->sar->stats.rx_jobs_waiting_nb,
                 SUB_STAT (rx_pb_count),
                 SUB_STAT (pb_data_before_ssn_min),
                 SUB_STAT (pb_data_duplicated),
                 SUB_STAT (pb_mme_before_ssn_min),
                 SUB_STAT (pb_mme_duplicated),
                 SUB_STAT (rx_pb_crc_error_count),
                 SUB_STAT (mfs_tx_data_pb_expired_nb),
                 SUB_STAT (mfs_rx_data_pb_expired_nb),
                 SUB_STAT (mfs_tx_mme_pb_expired_nb),
                 SUB_STAT (mfs_rx_mme_pb_expired_nb),
                 ctx->pbproc->stats.tx_data_cancel,
                 ctx->pbproc->stats.tx_data_wack_noack);
        int pos = 0, len = strlen (temp), len1;
        while (pos < len)
        {
            char * temp1 = temp + pos, tmpa[32];
            len1 = MIN (len, pos + 10);
            strncpy (tmpa, temp1, len1 - pos);
            tmpa[len1 - pos] = '\0';
            printf (tmpa);
            pos = len1;
        }
        sar_offset = ctx->sar->stats;
        ctx->cco_action.last_date = phy_date ()
            + CONFIG_CP_EOC_CCO_CON_TIMEOUT_TCK;
    }
#endif
}

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

    memset (&ctx->cco_action, 0, sizeof (cp_cco_action_t));

    ctx->cco_action.new_nek.eks = MAC_EKS_MAX; /* Initialized to MAC_EKS_MAX in
    order to have MAC_EKS_MIN as the EKS of the first generated NEK. */
}

void
cp_eoc_cco_action_uninit (cp_t *ctx)
{
    dbg_assert (ctx);
}

void
cp_eoc_cco_action_garbage (cp_t *ctx)
{
    int i, released_sta_nb = 0, released_sta_tei[CCO_STA_CON];
    dbg_assert (ctx);
    cp_net_t *net = cp_sta_mgr_get_our_avln (ctx);

    /* Check if associated sta got disconnected */
    for (i = MAC_TEI_STA_MIN_EOC; i < MAC_TEI_STA_MAX; i++)
    {
        cp_sta_t * station = cp_sta_mgr_sta_get_assoc (ctx, net, i);
        if (!station)
            continue;
        sta_t *sta = mac_store_sta_get (ctx->mac_store, i);
        /* cp sta is assoc but no mac store sta? Really wrong. */
        dbg_assert (sta);
        /* station is declared dead by lower layers? */
        if(!sta->tdma_poll && sta->authenticated)
            station->authenticated_to_unassociated = true;
        /* release station, no answers + to long in assoc. state */
        if ((!sta->tdma_poll && sta->authenticated)
              || (!sta->authenticated
                  && (station->associated_date_ms
                      + CP_EOC_STA_ASSOCIATED_TIMEOUT_MS
                      < cp_sta_core_get_date_ms (ctx))))
        {
#if CONFIG_CP_EOC_IS_MASTER
            /* only Master compilation */
            cp_eoc_multi_sta_action_put_sta_unassociated (ctx, station);
#endif
            /* Start release procedure */
            cp_sta_mgr_release_station (ctx, i);

            if (sta->authenticated)
            {
                if (released_sta_nb < CCO_STA_CON)
                    released_sta_tei[released_sta_nb] = i;
                released_sta_nb ++;
                DBG_PRINT_2 ("sta: rel, tei=%d", i);
            }
            else
                DBG_PRINT ("sta: auth fail, tei=%d", i);
        }
        slab_release (station);
        blk_release (sta);
    }
    /* Check if unassociated sta is still in the network */
    cp_net_garbage_station_list (ctx, cp_sta_mgr_get_our_avln (ctx),
                                 cp_sta_core_get_date_ms (ctx),
                                 CP_NET_STA_UNASSOC);

    if (released_sta_nb)
    {
        DBG_PRINTC ("rel: %d; TEI:", released_sta_nb);
        for (i = 0; ((i < released_sta_nb) && (i < CCO_STA_CON)); i++)
            DBG_PRINTC (" %d", released_sta_tei[i]);
        DBG_PRINTC (released_sta_nb > CCO_STA_CON ? " ...\n" : "\n");
    }
}