summaryrefslogtreecommitdiff
path: root/cesar/cp2/conn/src/conn.c
blob: e534ce76d482e1a5f09df00fe20adec80ba9d1fe (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
 /* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    conn.c
 * \brief   conn 
 * \ingroup cp2_conn
 *
 * Control the schedule of the becon period
 */
#include "common/std.h"
#include "cp2/cp.h"
#include "cp2/inc/context.h"
#include "cp2/conn/conn.h"
#include "cp2/conn/link.h"
#include "lib/blk.h"

/**
 * function that return a specific connection
 * \param  ctx  control plane context
 * \param  cid  connection id of the target conn
 * \return  the target connection or NULL if none
 *
 * checks the list of conn until it finds the conn or that there is no more
 * conn and return it
 */
cp_conn_t*
cp_conn_get_conn(cp_t *ctx, u8 cid)
{
    bool cid_found = false;
    bool no_more_conn = false;

    cp_conn_t *conn = NULL;
    list_node_t *current_node;
    
    dbg_assert(ctx);

    current_node = list_begin(&ctx->conn_mgr.conns);

    do
    {
        conn = PARENT_OF(cp_conn_t, node,
                         current_node);

        if(conn->cid == cid)
            cid_found = true;

        else if(current_node != list_rbegin(&ctx->conn_mgr.conns))
            current_node = list_next(current_node);

        else
            no_more_conn = true;

    }
    while(!cid_found && !no_more_conn);

    return conn;
}
/**
 * init a connection 
 * \param  cid  connection identifier
 * \param  flink  forward link
 * \param  rlink  reverse link
 * \return  return the connection initialised
 *
 * Init the memory of the connection
 * fill the elements concerning the connection
 */
void
cp_conn_add_conn(cp_t *ctx, u8 cid, cp_link_t *flink, cp_link_t *rlink)
{
    cp_conn_t *new_conn;

    dbg_assert(ctx);
    dbg_assert(flink);
    dbg_assert(rlink);

    new_conn = blk_alloc();

    new_conn->cid = cid;
    new_conn->conn_state = OUT;
    new_conn->flink = flink;
    new_conn->rlink = rlink;

    list_init_node (&new_conn->node);
    list_push(&ctx->conn_mgr.conns, &new_conn->node);
    
}