summaryrefslogtreecommitdiff
path: root/cesar/cp/sta/mgr/src/net.c
blob: a2480423de54a0b70534373b82c97546021f87e1 (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/sta/mgr/src/net.c
 * \brief   Network APIs.
 * \ingroup cp_sta_mgr
 *
 */
#include "common/std.h"
#include "string.h"

#include "lib/blk.h"

#include "cl/cl_mactotei.h"
#include "mac/common/ntb.h"
#include "mac/common/timings.h"

#include "cp/defs.h"
#include "cp/fsm/fsm.h"
#include "cp/sta/mgr/net.h"
#include "cp/sta/mgr/sta.h"
#include "cp/sta/mgr/sta_own_data.h"
#include "cp/sta/mgr/sta_mgr.h"

#include "cp/inc/context.h"
#include "cp/sta/mgr/inc/net.h"
#include "cp/sta/mgr/inc/sta.h"

/**
 * Station Associated lesser.
 *
 * \param  left  left node station.
 * \param  right  right node station.
 * \return  true if left is lesser than right
 */
bool
cp_net_station_assoc_less (set_node_t *left, set_node_t *right)
{
    cp_sta_private_t *sta_left;
    cp_sta_private_t *sta_right;

    dbg_assert (left);
    dbg_assert (right);

    sta_left = (cp_sta_private_t *) PARENT_OF (cp_sta_t,
                                               node_net, left);
    sta_right = (cp_sta_private_t *) PARENT_OF (cp_sta_t,
                                               node_net, right);

    if (sta_left->tei < sta_right->tei)
        return true;
    else
        return false;
}

/**
 * Station Unassociated lesser.
 *
 * \param  left  left node station.
 * \param  right  right node station.
 * \return  true if left is lesser than right
 */
bool
cp_net_station_unassoc_less (set_node_t *left, set_node_t *right)
{
    cp_sta_private_t *sta_left;
    cp_sta_private_t *sta_right;

    dbg_assert (left);
    dbg_assert (right);

    sta_left = (cp_sta_private_t *) PARENT_OF (cp_sta_t,
                                               node_net, left);
    sta_right = (cp_sta_private_t *) PARENT_OF (cp_sta_t,
                                               node_net, right);

    if (sta_left->mac_address < sta_right->mac_address)
        return true;
    else
        return false;
}

/**
 * Garbage function for the unassociated station list and the associated
 * station list.
 *
 * \param  ctx  the module context.
 * \param  net  the network.
 * \param  date_ms  the current date in milliseconds.
 * \param  assoc  the station list to expire.
 */
void
cp_net_garbage_station_list (cp_t *ctx, cp_net_t *net, u32 date_ms,
                             cp_net_sta_status_t assoc)
{
    cp_sta_t *sta_next;
    cp_sta_t *sta;

    dbg_assert (ctx);
    dbg_assert (net);

    for (sta = cp_net_sta_get_first (ctx, net, assoc); sta; sta = sta_next)
    {
        slab_addref (sta);
        sta_next = cp_net_sta_get_next (ctx, net, sta);
        if (less_mod2p32(sta->expired_date_ms, date_ms))
            cp_sta_mgr_sta_remove (ctx, sta);
        slab_release (sta);
    }
}

void
cp_net_init (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);
    net->snid = 0;
    net->nid = 0;
    net->cco = NULL;
    net->pco = NULL;
    net->present = false;
    net->avln_slot_id = 0;
    net->avln_num_slots = 0;
    net->avln_slot_usage = 0;
    net->num_stas = 0;
    net->num_visible_stas = 0;
    net->network_mode = CP_NET_NM_CSMA_ONLY;
    net->access = HPAV_ACCESS_IN_HOME;
    net->num_associated_stas = 0;
    net->hm = MAC_COEXISTENCE_FULL_HYBRID_MODE;
    set_init (&net->associated_stas, cp_net_station_assoc_less);
    set_init (&net->unassociated_stas, cp_net_station_unassoc_less);
}

void
cp_net_uninit (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);
    dbg_assert (ctx);
    /* Remove the station present in the sets. */
    if (net->present)
    {
        cp_sta_t *sta;
        uint i;
        /* For associated and non associated stations. */
        for (i = 0; i < CP_NET_STA_NB; i++)
        {
            while ((sta = cp_net_sta_get_first (ctx, net, i)))
            {
                cp_sta_mgr_sta_remove (ctx, sta);
                slab_release (sta);
            }
        }
    }
    /* By resetting the present flag, the net is considered unavailable. */
    net->present = false;
    net->pco = NULL;
    net->cco = NULL;
    net->blacklisted = false;
}

void
cp_net_garbage_stations (cp_t *ctx, cp_net_t *net, u32 date_ms)
{
    dbg_assert (ctx);
    dbg_assert (net);

    cp_net_garbage_station_list (ctx, net, date_ms, false /* Unassoc. */);

    // Does not expire the station in our AVLN.
    if (net != ctx->sta_mgr.our_avln)
        cp_net_garbage_station_list (ctx, net, date_ms, true /* Assoc. */);

    // Verify if the list not empty.
    if (set_empty (&net->associated_stas)
        && set_empty (&net->unassociated_stas)
        && net != ctx->sta_mgr.our_avln)
        cp_sta_mgr_remove_avln (ctx, net->snid, net->nid);
}

void
cp_net_set_cco (cp_t *ctx, cp_net_t *net, cp_tei_t tei)
{
    dbg_assert (ctx);
    dbg_assert (net);

    if (!net->cco
        || (net->cco && (cp_sta_get_tei (net->cco) != tei)))
    {
        // for any avln, reset the current cco.
        if (net->cco)
        {
            ((cp_sta_private_t *)net->cco)->is_cco = false;
            net->cco = NULL;
        }

        if (tei)
        {
            // if our net.
            if ((ctx->sta_mgr.our_avln == net)
                && (tei == cp_sta_own_data_get_tei (ctx)))
            {
                cp_sta_own_data_set_cco_status (ctx, true);
            }
            else
            {
                cp_sta_t *sta;

                dbg_assert (tei);
                sta = cp_sta_mgr_sta_get_assoc (ctx, net, tei);
                ((cp_sta_private_t *)sta)->is_cco = true;
                net->cco = sta;
                slab_release (sta);
            }
        }
    }
}

void
cp_net_set_pco (cp_t *ctx, cp_net_t *net, cp_tei_t tei)
{
    cp_sta_t *sta;
    dbg_assert (ctx);
    dbg_assert (net);

    // for any avln, reset the current cco.
    if (net->pco)
    {
        ((cp_sta_private_t *)net->pco)->pco_glid = 0x0;
        net->pco = NULL;
    }

    if (tei)
    {
        dbg_assert (tei);
        sta = cp_sta_mgr_sta_get_assoc (ctx, net, tei);
        ((cp_sta_private_t *)sta)->pco_glid = 0x80;
        net->pco = sta;
        slab_release (sta);
    }
}

cp_sta_t *
cp_net_get_cco (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    if (net->cco)
    {
        slab_addref (net->cco);
        return net->cco;
    }
    else
        return NULL;
}

void
cp_net_get_cco_peer (cp_t *ctx, cp_net_t *net, cp_mme_peer_t *peer)
{
    dbg_assert (net);
    dbg_assert (peer);
    dbg_assert (net->cco);
    cp_sta_get_peer (net->cco, peer);
}

cp_sta_t *
cp_net_get_pco (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    if (net->pco)
    {
        slab_addref (net->pco);
        return net->pco;
    }
    else
        return NULL;
}

void
cp_net_get_pco_peer (cp_t *ctx, cp_net_t *net, cp_mme_peer_t *peer)
{
    dbg_assert (net);
    dbg_assert (peer);
    dbg_assert (net->pco);
    cp_sta_get_peer (net->pco, peer);
}

cp_sta_t *
cp_net_sta_get_first (cp_t *ctx, cp_net_t *net, cp_net_sta_status_t assoc)
{
    set_t *set;
    cp_sta_t *sta;

    dbg_assert (net);
    dbg_assert (ctx);

    set = (assoc == CP_NET_STA_UNASSOC) ? &net->unassociated_stas:
        &net->associated_stas;

    // Verify if the set contains a sta.
    if (set_empty (set))
        return NULL;

    sta = PARENT_OF (cp_sta_t, node_net, set_begin (set));
    slab_addref (sta);

    return sta;
}

cp_sta_t *
cp_net_sta_get_next (cp_t *ctx, cp_net_t *net, cp_sta_t *prev_sta)
{
    set_t *set;
    set_node_t *node;
    cp_sta_t *sta;

    dbg_assert (ctx);
    dbg_assert (prev_sta);

    // Verify if the station is associated or not
    if (!cp_sta_get_tei (prev_sta))
        set = &net->unassociated_stas;
    else
        set = &net->associated_stas;

    // Get the next node.
    node = set_next (set, &prev_sta->node_net);
    slab_release (prev_sta);

    if (node)
    {
        sta = PARENT_OF (cp_sta_t, node_net, node);
        slab_addref (sta);
        return sta;
    }
    else
    {
        return NULL;
    }
}

cp_snid_t
cp_net_get_snid (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->snid;
}

cp_nid_t
cp_net_get_nid (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->nid;
}

void
cp_net_set_slot_id_and_usage (cp_t *ctx, cp_net_t *net,
                              u8 slot_id, u8 slot_usage)
{
    dbg_assert (net);

    net->avln_slot_id = slot_id;
    net->avln_slot_usage = slot_usage;
}

u8
cp_net_get_slot_id (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->avln_slot_id;
}

u8
cp_net_get_slot_usage_mask (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->avln_slot_usage;
}

u8
cp_net_get_num_discovered_stas (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->num_visible_stas;
}

u8
cp_net_get_num_stas (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->num_stas;
}

void
cp_net_set_nm (cp_t *ctx, cp_net_t *net, cp_net_network_mode_t mode)
{
    dbg_assert (net);
    dbg_assert (mode < CP_NET_NM_ASSERT);

    net->network_mode = mode;
}

cp_net_network_mode_t
cp_net_get_nm (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->network_mode;
}

bool
cp_net_is_empty (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (ctx);
    dbg_assert (net);

    // Check the associated list.
    if (set_empty (&net->associated_stas))
    {
        return true;
    }
    return false;
}

void
cp_net_set_access (cp_t *ctx, cp_net_t *net, hpav_access_t access)
{
    dbg_assert (ctx);
    dbg_assert (net);

    dbg_assert (access < HPAV_ACCESS_NB);

    net->access = access;
}

hpav_access_t
cp_net_get_access (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (ctx);
    dbg_assert (net);

    return net->access;
}

cp_net_t *
cp_sta_get_net (cp_sta_t *ctx)
{
    /* Check parameter. */
    dbg_assert (ctx);

    return ((cp_sta_private_t *)ctx)->net;
}

uint
cp_net_num_sta_associated (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (ctx);
    dbg_assert (net);

    return net->num_associated_stas;
}

void
cp_net_blacklisted_status_set (cp_t *ctx, cp_net_t *net, bool status)
{
    dbg_assert (net);
    net->blacklisted_reset_date = phy_date () +
        MAC_MS_TO_TCK (CP_STA_MGR_NET_RESET_BLACK_LISTS_MS);
    net->blacklisted = status;
}

bool
cp_net_blacklisted_status_get (cp_t *ctx, cp_net_t *net)
{
    return net->blacklisted;
}