summaryrefslogtreecommitdiff
path: root/cesar/cp/sta/action/src/poweron.c
blob: a3169ae1b82cb94e7416d0c0ee0c4940258530e0 (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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* 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/cco/action/cco_action.h"

#include "lib/slab.h"

#include "cp/inc/context.h"

#define ALL_STA_LEAVED_TIMEOUT_MS 1000


/**
 * 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)
{
    if (!cp_sta_mgr_net_list_is_empty (ctx))
    {
        /* 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);
}

/**
 * Common handling of POWERON and USTA => BEACON.
 * \param  ctx  control plane context
 * \param  beacon  received beacon
 * \param  net  STA net
 * \param  sta  STA emitting the beacon
 * \param  branch_match  branch taken if NID match
 * \param  branch_no_match  branch taken in other cases
 */
static void
cp_sta_action_poweron_beacon_match_and_join (cp_t *ctx,
                                             cp_beacon_desc_t *beacon,
                                             cp_net_t *net, cp_sta_t *sta,
                                             cp_fsm_branch_t branch_match,
                                             cp_fsm_branch_t branch_no_match)
{
    cp_sta_own_data_t *own_data = cp_sta_mgr_get_sta_own_data (ctx);
    cp_nid_t our_nid = cp_sta_own_data_get_nid (ctx);
    cp_nid_t its_nid = cp_net_get_nid (ctx, net);
    u8 its_snid = cp_net_get_snid (ctx, net);
    u8 its_tei = cp_sta_get_tei (sta);
    /* Does not support proxy networking for the moment, only handle CCo
     * beacon. */
    if (cp_sta_get_cco_status (sta))
    {
        /* Should we track this beacon? */
        /* TODO: better synchronisation status. */
        /* TODO: non black-listed. */
        if (!own_data->nid_track /* No AVLN tracked. */
            || (our_nid != own_data->nid_track
                && our_nid == its_nid) /* Better NID match. */
            || (its_tei != MAC_TEI_UNASSOCIATED
                && its_tei != own_data->tei_track
                && our_nid == its_nid)
           )
        {
            own_data->nid_track = its_nid;
            own_data->tei_track = its_tei;
            cp_sta_own_data_set_snid (ctx, its_snid);
            cp_beacon_process_tracked_avln (ctx, beacon, net);
        }
        /* Should we associate? */
        /* TODO: is synchronised?. */
        /* TODO: is non black-listed?. */
        if (its_nid == our_nid)
        {
            cp_sta_action_assoc_start (ctx, net, sta);
            cp_fsm_branch_ (ctx, branch_match);
        }
        else
        {
            cp_fsm_branch_ (ctx, branch_no_match);
        }
    }
    else
    {
        cp_fsm_branch_ (ctx, branch_no_match);
    }
}

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);
    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_create_default_schedules (ctx);
    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);
}

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);
}

void
cp_sta_action_poweron__poweron__btt_timeout (cp_t *ctx)
{
    dbg_assert (ctx);
    if (cp_sta_mgr_net_list_is_empty (ctx))
    {
        cp_fsm_branch (ctx, POWERON, btt_timeout, no_avln);
    }
    else
    {
        /* I can become CCo only if at least one unassociated STA is seen. */
        bool iam_cco = true, sta_seen = false;
        mac_t rmac = MAC_REVERSE (cp_sta_own_data_get_mac_address (ctx));
        /* Look for a net with a matching NID (XXX: sta_mgr interface could be
         * improved on this point). */
        cp_snid_t snid;
        cp_nid_t nid = cp_sta_own_data_get_nid (ctx);
        for (snid = 0; iam_cco && snid < 16; snid++)
        {
            /* For each matching net, look at unassociated STA. */
            cp_net_t *net = cp_sta_mgr_get_avln (ctx, snid, nid);
            if (!net)
                continue;
            cp_sta_t *sta = cp_net_sta_get_first (ctx, net, CP_NET_STA_UNASSOC);
            if (sta)
                sta_seen = true;
            for (; sta; 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))
                {
                    iam_cco = false;
                    /* Early stop, should release the STA. */
                    slab_release (sta);
                    break;
                }
            }
        }
        if (iam_cco && sta_seen)
        {
            cp_cco_action_snid_change_now (ctx);
            cp_cco_action__unassoc__cco_start (ctx);
            cp_fsm_branch (ctx, POWERON, btt_timeout, nid_match_cco);
        }
        else
            cp_fsm_branch (ctx, POWERON, btt_timeout, avln);
    }
}

void
cp_sta_action_poweron__poweron__beacon (cp_t *ctx, cp_beacon_desc_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_poweron_beacon_match_and_join (
        ctx, beacon, net, sta,
        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);
}

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

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, cp_beacon_desc_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_poweron_beacon_match_and_join (
        ctx, beacon, net, sta,
        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_snid_change_now (ctx);
            cp_cco_action__unassoc__cco_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__enter (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_cco_action_snid_change_now (ctx);
    cp_beacon_cco_send_discover_beacon (ctx);
}

void
cp_sta_action_poweron__ucco__beacon (cp_t *ctx, cp_beacon_desc_t *beacon,
                                     cp_net_t *net, cp_sta_t *sta)
{
    dbg_assert (ctx);
    dbg_assert (beacon);
    dbg_assert (net);
    dbg_assert (sta);
    /* Change to STA behaviour. */
    cp_beacon_reconfigure_timer (ctx);
    /* Handle incoming beacon. */
    cp_sta_action_poweron_beacon_match_and_join (
        ctx, beacon, net, sta,
        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)
{
    dbg_assert (ctx);
    cp_net_t *our_net = cp_sta_mgr_get_our_avln (ctx);
    if (cp_net_is_empty (ctx, our_net))
    {
        /* Leave CCo role. */
        cp_cco_action__cco__unassoc_stop (ctx);
        if (cp_sta_mgr_net_list_is_empty (ctx))
            cp_fsm_branch (ctx, CCO, join_timeout, no_sta_no_avln);
        else
            cp_fsm_branch (ctx, CCO, join_timeout, no_sta_avln);
    }
    else
    {
        /* Ok, stay CCo. */
        cp_fsm_branch (ctx, CCO, join_timeout, sta);
    }
}

void
cp_sta_action_poweron__cco__all_sta_leaved (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_sta_core_stop_timed_or_cyclic_event (
        ctx, &ctx->sta_action.poweron.join_timer);
    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, ALL_STA_LEAVED_TIMEOUT_MS);
}