summaryrefslogtreecommitdiff
path: root/cesar/cp/sta/action/src/poweron.c
blob: 7352fafcb23780f008c951bd24282c5561f90336 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/sta/action/src/poweron.c
 * \brief   STA action, power on procedure related definitions.
 * \ingroup cp_sta_action
 */
#include "common/std.h"

#include "action.h"

#include "cp/defs.h"
#include "cp/msg/msg.h"
#include "cp/sta/mgr/sta_mgr.h"
#include "cp/fsm/fsm.h"
#include "cp/beacon/beacon.h"
#include "cp/beacon/discover.h"
#include "cp/cco/action/cco_action.h"
#include "bsu/bsu.h"

#include "lib/slab.h"

#include "cp/inc/context.h"
#include "cp/sta/action/inc/action.h"

#define ALL_STA_LEAVED_TIMEOUT_MS 1000

void
cp_sta_action_poweron_init (cp_t *ctx)
{
    dbg_assert (ctx);
    ctx->sta_action.poweron.enter.need_set = false;
}

void
cp_sta_action_poweron__idle__enter (cp_t *ctx)
{
    if (ctx->sta_action.poweron.enter.need_set)
        cp_fsm_trigger_new_event (ctx, bare, to_poweron);
}

/**
 * Start USTT timer.
 * \param  ctx  control plane context
 * \param  ustt_ms  USTT timer approximated value
 */
static void
cp_sta_action_poweron_ustt_start (cp_t *ctx, uint ustt_ms)
{
    cp_fsm_event_t *event;
    uint ustt_timeout_ms = ustt_ms / 2
        + lib_rnd_uniform (&ctx->rnd, ustt_ms);
    event = cp_fsm_event_bare_new (ctx, CP_FSM_EVENT_TYPE_ustt_timeout);
    cp_sta_core_gen_timed_event (ctx, &ctx->sta_action.poweron.ustt_timer,
                                 event, ustt_timeout_ms);
}

/**
 * Common handling of POWERON and USTA => USTT TIMEOUT.
 * \param  ctx  control plane context
 * \param  ustt_ms  USTT timer approximated value
 */
static void
cp_sta_action_poweron_ustt_timeout (cp_t *ctx, uint ustt_ms)
{
    /* Send an CM_UNASSOCIATED_STA.IND. */
    cp_msg_cm_unassociated_sta_ind_send (
        ctx, &CP_MME_PEER (MAC_BROADCAST, MAC_TEI_UNASSOCIATED),
        cp_sta_own_data_get_nid (ctx), CP_CCO_LEVEL);
    cp_sta_action_poweron_ustt_start (ctx, ustt_ms);
}

void
cp_sta_action_poweron_start (cp_t *ctx)
{
    cp_fsm_trigger_new_event (ctx, bare, to_poweron);
}

void
cp_sta_action_poweron_stop (cp_t *ctx)
{
    cp_fsm_trigger_new_event (ctx, bare, to_stop);
}

void
cp_sta_action_poweron__many__to_idle (cp_t *ctx)
{
    dbg_assert (ctx);
    /* Cleanup. */
    pbproc_activate (ctx->pbproc, false);
    sar_activate (ctx->sar, false);
    sar_cleanup (ctx->sar);
    bsu_activate (ctx->bsu, false);
    cp_beacon_deactivate (ctx);
    /* Signal station is stopped. */
    cp_fsm_trigger_new_event (ctx, bare, stopped);
}

void
cp_sta_action_poweron__idle__to_poweron (cp_t *ctx)
{
    cp_beacon_poweron_init (ctx);
    bsu_power_on (ctx->bsu, cp_sta_own_data_get_snid (ctx));
    /* Update beacon data program a timer is a wrong values beacon ACLF is
     * still not initialised.*/
    bsu_activate (ctx->bsu, true);
    cp_beacon_reconfigure_timer (ctx, false);
    sar_activate (ctx->sar, true);
    pbproc_activate (ctx->pbproc, true);
}

void
cp_sta_action_poweron__poweron__enter (cp_t *ctx)
{
    cp_fsm_event_t *event;
    /* BTT timer. */
    uint btt_timeout_min_ms, btt_timeout_max_ms, btt_timeout_ms;
    if (cp_sta_own_data_get_was_cco (ctx))
    {
        btt_timeout_min_ms = CP_CCO_BEACON_SCAN_MIN_MS;
        btt_timeout_max_ms = CP_CCO_BEACON_SCAN_MAX_MS;
    }
    else
    {
        btt_timeout_min_ms = CP_BEACON_SCAN_MIN_MS;
        btt_timeout_max_ms = CP_BEACON_SCAN_MAX_MS;
    }
    btt_timeout_ms = lib_rnd_uniform (
        &ctx->rnd, btt_timeout_max_ms - btt_timeout_min_ms + 1)
        + btt_timeout_min_ms;
    event = cp_fsm_event_bare_new (ctx, CP_FSM_EVENT_TYPE_btt_timeout);
    cp_sta_core_gen_timed_event (ctx, &ctx->sta_action.poweron.btt_timer,
                                 event, btt_timeout_ms);
    /* USTT timer. */
    cp_sta_action_poweron_ustt_start (ctx, CP_USTA_IND_INTERVAL_MS);

    if (ctx->sta_action.poweron.enter.need_set)
    {
        cp_sta_own_data_set_nid (ctx, ctx->sta_action.poweron.enter.nid);
        cp_sta_own_data_set_nmk (ctx, ctx->sta_action.poweron.enter.nmk,
                                 ctx->sta_action.poweron.enter.type);

        ctx->sta_action.poweron.enter.need_set = false;
    }

    bsu_beacon_t beacon;
    cp_beacon_fill (ctx, &beacon);
    /* Act as STA, for BSU corresponds to only hear the central beacon from
     * the CCo. */
    bsu_update (ctx->bsu, &beacon, BSU_UPDATE_STA_TYPE_STA);
}

void
cp_sta_action_poweron__poweron__leave (cp_t *ctx)
{
    cp_sta_core_stop_timed_or_cyclic_event (
        ctx, &ctx->sta_action.poweron.btt_timer);
    cp_sta_core_stop_timed_or_cyclic_event (
        ctx, &ctx->sta_action.poweron.ustt_timer);
}

void
cp_sta_action_poweron__poweron__ustt_timeout (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_sta_action_poweron_ustt_timeout (ctx, CP_USTA_IND_INTERVAL_MS);
}

/**
 * Search for the first USTA with the mac address lesser than our own mac
 * address.
 * \param  ctx  the module context.
 * \param  net  the AVLN to search in.
 * \return  true if a station with a bigger mac address than our own mac
 * address.
 */
static bool
cp_sta_action_btt_timeout_usta_with_mac_bigger (cp_t *ctx, cp_net_t *net)
{
    cp_sta_t *sta;
    bool sta_found = false;
    mac_t rmac = MAC_REVERSE (cp_sta_own_data_get_mac_address (ctx));
    for (sta = cp_net_sta_get_first (ctx, net, CP_NET_STA_UNASSOC);
         sta && !sta_found;
         sta = cp_net_sta_get_next (ctx, net, sta))
    {
        if (sta->cco_cap > CP_CCO_LEVEL
            || (sta->cco_cap == CP_CCO_LEVEL
                && MAC_REVERSE (cp_sta_get_mac_address (sta)) > rmac))
            sta_found = true;
    }
    return sta_found;
}

void
cp_sta_action_poweron__poweron__btt_timeout (cp_t *ctx)
{
    dbg_assert (ctx);
    bool iam_cco_on_my_nid = false, iam_cco_on_other_nid = true;
    cp_nid_t nid = cp_sta_own_data_get_nid (ctx);
    cp_net_t *net;
    for (net = cp_sta_mgr_get_first_avln (ctx);
         net;
         net = cp_sta_mgr_get_next_avln (ctx, net))
    {
        /* Search for AVLN with our NID. */
        if (cp_net_get_nid (ctx, net) == nid)
        {
            iam_cco_on_my_nid  = iam_cco_on_other_nid =
                !cp_sta_action_btt_timeout_usta_with_mac_bigger (
                    ctx, net)
                && !net->num_associated_stas;
        }
        /* Search for AVLN with another NID. */
        else if (iam_cco_on_other_nid)
        {
            iam_cco_on_other_nid = !net->num_associated_stas;
        }
    }
    if (iam_cco_on_my_nid)
    {
        cp_cco_action_cco__unassoc_start (ctx);
        cp_fsm_branch (ctx, POWERON, btt_timeout, nid_match_cco);
    }
    else if (iam_cco_on_other_nid)
    {
        cp_cco_action_ucco_start (ctx);
        cp_fsm_branch (ctx, POWERON, btt_timeout, no_avln);
    }
    else
        cp_fsm_branch (ctx, POWERON, btt_timeout, avln);
}

void
cp_sta_action_poweron__poweron__beacon (cp_t *ctx, bsu_beacon_t *beacon,
                                        cp_net_t *net, cp_sta_t *sta)
{
    dbg_assert (ctx);
    dbg_assert (beacon);
    dbg_assert (net);
    dbg_assert (sta);
    cp_sta_action_beacon_match_and_join (
        ctx, beacon, net, sta, false /* not in SC */,
        CP_FSM_BRANCH (POWERON, BEACON, nid_match),
        CP_FSM_BRANCH (POWERON, BEACON, no_nid_match));
}

void
cp_sta_action_poweron__poweron_joining__left (cp_t *ctx)
{
}

void
cp_sta_action_poweron__joining__to_stop (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_sta_action_assoc_leave (ctx);
}

static inline void
cp_sta_action_poweron__usta_usta_joining__enter (cp_t *ctx)
{
    bsu_beacon_t beacon;
    if (MAC_TEI_IS_STA (cp_sta_own_data_get_tei (ctx)))
        cp_cco_action_cco__unassoc_stop (ctx);
    cp_beacon_fill (ctx, &beacon);
    bsu_update (ctx->bsu, &beacon, BSU_UPDATE_STA_TYPE_STA);
}

void
cp_sta_action_poweron__usta__enter (cp_t *ctx)
{
    cp_sta_action_poweron__usta_usta_joining__enter (ctx);
    cp_sta_action_poweron_ustt_start (ctx, CP_DISCOVER_PERIOD_MAX_MS);
}

void
cp_sta_action_poweron__usta_joining__enter (cp_t *ctx)
{
    cp_sta_action_poweron__usta_usta_joining__enter (ctx);
}

void
cp_sta_action_poweron__usta__leave (cp_t *ctx)
{
    cp_sta_core_stop_timed_or_cyclic_event (
        ctx, &ctx->sta_action.poweron.ustt_timer);
}

void
cp_sta_action_poweron__usta__ustt_timeout (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_sta_action_poweron_ustt_timeout (ctx, CP_DISCOVER_PERIOD_MAX_MS);
}

void
cp_sta_action_poweron__usta__beacon (cp_t *ctx, bsu_beacon_t *beacon,
                                     cp_net_t *net, cp_sta_t *sta)
{
    dbg_assert (ctx);
    dbg_assert (beacon);
    dbg_assert (net);
    dbg_assert (sta);
    cp_sta_action_beacon_match_and_join (
        ctx, beacon, net, sta, false /* not in SC */,
        CP_FSM_BRANCH (USTA, BEACON, nid_match),
        CP_FSM_BRANCH (USTA, BEACON, no_nid_match));
}

void
cp_sta_action_poweron__usta__usta_ind (cp_t *ctx, cp_net_t *net,
                                       cp_sta_t *sta)
{
    dbg_assert (ctx);
    dbg_assert (net);
    dbg_assert (sta);
    if (cp_net_get_nid (ctx, net) != cp_sta_own_data_get_nid (ctx))
    {
        cp_fsm_branch (ctx, USTA, usta_ind, else);
    }
    else
    {
        if (sta->cco_cap > CP_CCO_LEVEL
            || (sta->cco_cap == CP_CCO_LEVEL
                && (MAC_REVERSE (cp_sta_get_mac_address (sta))
                    > MAC_REVERSE (cp_sta_own_data_get_mac_address (ctx)))))
            cp_fsm_branch (ctx, USTA, usta_ind, else);
        else
        {
            cp_cco_action_cco__unassoc_start (ctx);
            cp_fsm_branch (ctx, USTA, usta_ind, nid_match_cco);
        }
    }
}

void
cp_sta_action_poweron__usta_joining__left (cp_t *ctx)
{
}

void
cp_sta_action_poweron__ucco__beacon (cp_t *ctx, bsu_beacon_t *beacon,
                                     cp_net_t *net, cp_sta_t *sta)
{
    dbg_assert (ctx);
    dbg_assert (beacon);
    dbg_assert (net);
    dbg_assert (sta);
    /* Stop UCCo behavior. */
    cp_cco_action_ucco_stop (ctx);
    /* Handle incoming beacon. */
    cp_sta_action_beacon_match_and_join (
        ctx, beacon, net, sta, false /* not in SC */,
        CP_FSM_BRANCH (UCCO, BEACON, nid_match),
        CP_FSM_BRANCH (UCCO, BEACON, no_nid_match));
}

void
cp_sta_action_poweron__sta__to_stop (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_sta_action_assoc_leave (ctx);
}

void
cp_sta_action_poweron_cco__enter (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_fsm_event_t *event;
    event = cp_fsm_event_bare_new (ctx, CP_FSM_EVENT_TYPE_join_timeout);
    cp_sta_core_gen_timed_event (ctx, &ctx->sta_action.poweron.join_timer,
                                 event, CP_DISCOVER_PERIOD_MAX_MS);
}

void
cp_sta_action_poweron_cco__leave (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_sta_core_stop_timed_or_cyclic_event (
        ctx, &ctx->sta_action.poweron.join_timer);
}

void
cp_sta_action_poweron__cco__join_timeout (cp_t *ctx)
{
    cp_sta_action_poweron__cco__join_timeout_sc_failed (
        ctx,
        CP_FSM_BRANCH (CCO, join_timeout, no_sta_no_avln),
        CP_FSM_BRANCH (CCO, join_timeout, no_sta_avln),
        CP_FSM_BRANCH (CCO, join_timeout, sta)
        );
}