summaryrefslogtreecommitdiff
path: root/cesar/cp2/conn/src/conn_mgr.c
blob: 328a9b99b8cc6df47032207c929c46dd37b35eed (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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
 /* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    conn_mgr.c
 * \brief   conn mgr
 * \ingroup cp2_conn
 *
 */
#include "common/std.h"
#include "cl/inc/cl_mactotei.h"
#include "lib/blk.h"
#include "cp2/types.h"
#include "cp2/cp.h"
#include "cp2/inc/context.h"

#include "cp2/conn/conn.h"
#include "cp2/conn/inc/conn.h"
#include "cp2/conn/link.h"
#include "cp2/conn/inc/link.h"

#include "cp2/sta/mgr/sta_own_data.h"
#include "cp2/sta/mgr/sta_mgr.h"

//#include "cp2/msg/msg.h"
/**
 * init conn manager
 * \param  ctx  Control plane context
 *
 * Initialise the conn list
 */
void
cp_conn_mgr_init(cp_t *ctx)
{
    dbg_assert(ctx);

    list_init(&ctx->conn_mgr.conns);
}

void
cp_conn_mgr_uninit(cp_t *ctx)
{
    list_node_t *del_conn_node;
    list_node_t *tmp_conn_node;

    dbg_assert(ctx);

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

    while(!list_empty(&ctx->conn_mgr.conns))
    {
        tmp_conn_node = list_next(del_conn_node);
        cp_conn_del_conn(ctx, PARENT_OF(cp_conn_t, node,
                                        del_conn_node)->conn_info->cid);
        del_conn_node = tmp_conn_node;
    }
}
/**
 * Process a CM_CONN_NEW_REQ MME
 * \param  ctx  Control Plane context
 * \param  mme  incoming MME
 *
 * « details »
 */
void
cp_conn_mgr_process_cm_conn_new_req(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_conn_t *conn;
    cp_sta_own_data_t *own_sta; 
    cp_sta_t *sta;
    cp_net_t *net;

    cp_msg_cm_conn_new_req_t received_data;
    cp_msg_apcm_conn_add_ind_t send_data;

    cp_msg_cm_conn_new_req_receive(ctx, mme, &received_data);

    own_sta = cp_sta_mgr_get_sta_own_data(ctx);
    net = cp_sta_mgr_get_our_avln(ctx);
    sta = cp_net_get_sta(net, mme->peer.tei);

    /* Init conn */
    conn = blk_alloc();
    conn->flink = cp_link_init();
    conn->rlink = cp_link_init();

    /* Init conn info */
    conn->conn_info->cid = received_data.cid;
    conn->conn_info->stei = mme->peer.tei;
    conn->conn_info->dtei = cp_sta_own_data_get_tei(own_sta);
    conn->conn_info->lid_f = (u8)received_data.cid;
    conn->conn_info->lid_r = cp_link_get_lid();
    conn->conn_info->cspec = received_data.cspec; 

    /* Conn state OUT */
    conn->conn_state = CP_CONN_STATE_STAYOUT;
    conn->type = CP_CONN_TYPE_UNICAST;

    /* Add the connection */
    cp_conn_add_conn(ctx, conn);

    /* Initiate MME */
    send_data.cid = received_data.cid; 
    send_data.oda = cp_sta_own_data_get_mac_address(own_sta);
    send_data.osa = cp_sta_get_mac_address(sta);
    send_data.cspec = received_data.cspec;

    /* Send the request to the HLE */
    if(conn->type == CP_CONN_TYPE_UNICAST)
        cp_msg_apcm_conn_add_ind_send(ctx, 
                                      CP_MME_PEER(send_data.osa),
                                      &send_data);
}

/**
 * Process a CM_CONN_NEW_CNF MME
 * \param  ctx  Control Plane context
 * \param  mme  incoming MME
 *
 */
void
cp_conn_mgr_process_cm_conn_new_cnf(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_conn_t *conn;

    cp_msg_cm_conn_new_cnf_t received_data;
    cp_msg_apcm_conn_add_cnf_t send_data;

    cp_msg_cm_conn_new_cnf_receive(ctx, 
                                   mme, 
                                   &received_data);

    /* Init the data of the MME to send to the HLE */
    send_data.cid = received_data.cid;
    send_data.oda = conn->oda; 
    send_data.osa = conn->osa;
    send_data.result = received_data.result;
    send_data.request_id = cp_conn_get_request_id(ctx, received_data.cid);
    send_data.proposed_cspec = received_data.proposed_cspec;

    conn = cp_conn_get_conn(ctx, received_data.cid);

    /* Send the MME to the HLE */
    if(conn->type == CP_CONN_TYPE_UNICAST)
        cp_msg_apcm_conn_add_cnf_send(ctx, 
                                      mme->peer, 
                                      &send_data);
    if(!received_data.result)
        conn->conn_info->lid_r = received_data.llid_r;

    else
        cp_conn_del_conn(ctx, received_data.cid);

} 

/**
 * Process a  CM_CONN_REL_IND MME
 * \param  ctx  Control Plane context
 * \param  mme  incoming MME
 *
 */
void
cp_conn_mgr_process_cm_conn_rel_ind(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_conn_t *conn;

    cp_msg_cm_conn_rel_ind_t received_data;
    cp_msg_apcm_conn_rel_ind_t send_ind_data;
    cp_msg_cm_conn_rel_rsp_t send_rsp_data;

    conn = cp_conn_get_conn(ctx, received_data.cid);
    cp_msg_cm_conn_rel_ind_receive(ctx, mme, &received_data);

    /* Init the IND to the HLE */
    send_ind_data.cid = received_data.cid;         
    send_ind_data.reason_code = received_data.reason_code;      
    send_ind_data.releasing_mac_add = mme->peer.mac;        
    send_ind_data.proposed_cspec = NULL;      
    send_ind_data.violated_cspec = received_data.violated_cspec;

    /* Send MME */
    if(conn->type == CP_CONN_TYPE_UNICAST)
        cp_msg_apcm_conn_rel_ind_send(ctx,
                                      CP_MME_PEER(mme->peer.mac),
                                      &send_ind_data);

    /* Init the RSP */
    send_rsp_data.cid = received_data.cid;

    /* Send MME */
    if(conn->type == CP_CONN_TYPE_UNICAST)
        cp_msg_cm_conn_rel_rsp_send(ctx,
                                    mme->peer,
                                    &send_rsp_data);

    /* Release conn */
    cp_conn_del_conn(ctx, received_data.cid);
}

/**
 * Process a CM_CONN_REL_RSP MME
 * \param  ctx  Control Plane context
 * \param  mme  incoming MME
 *
 * « details »
 */
void
cp_conn_mgr_process_cm_conn_rel_rsp(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_msg_cm_conn_rel_rsp_t received_data;

    cp_msg_cm_conn_rel_rsp_receive(ctx, mme, &received_data);

    
    /* TODO count the number of connection that released the connection. If
     * the all the connections didnt released the connection find a way to ask
     * them again regarding their tei.*/
    
}

/**
 * Process a CM_CONN_MOD_REQ MME
 * \param  ctx  Control Plane context
 * \param  mme  incoming MME
 *
 */
void
cp_conn_mgr_process_cm_conn_mod_req(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_conn_t *conn;

    cp_msg_cm_conn_mod_req_t received_data;
    cp_msg_apcm_conn_mod_ind_t send_data;

    cp_msg_cm_conn_mod_req_receive(ctx, mme, &received_data);

    conn = cp_conn_get_conn(ctx, received_data.cid);

    /* Init IND MME */
    send_data.cid = received_data.cid;
    send_data.modified_cspec = received_data.modified_cspec;
    send_data.cause = 0x00;

    /* Send MME */
    if(conn->type == CP_CONN_TYPE_UNICAST)
        cp_msg_apcm_conn_mod_ind_send(ctx,
                                      CP_MME_PEER(mme->peer.mac),
                                      &send_data);
    /* TODO Creation of a modified cspec to store the cspec until we received the
     * rsp from the HLE ???*/
}

/**
 * Process a CM_CONN_MOD_CNF MME
 * \param  ctx  Control Plane context
 * \param  mme  incoming MME
 *
 */
void
cp_conn_mgr_process_cm_conn_mod_cnf(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_msg_cm_conn_mod_cnf_t received_data;
    //cp_msg_apcm_conn_mod_cnf_t send_data;

    cp_msg_cm_conn_mod_cnf_receive(ctx, mme, &received_data);

    /* TODO broad/multi req*/ 
    
}

/**
 * Process a CM_CONN_INFO_REQ MME
 * \param  ctx  Control Plane context
 * \param  mme  incoming MME
 *
 */
void
cp_conn_mgr_process_cm_conn_info_req(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_msg_cm_conn_info_req_t received_data;

    cp_msg_cm_conn_info_req_receive(ctx, mme, &received_data);

    /* TODO Send the data to the requested destination */
}

/**
 * Process a CM_CONN_INFO_CNF MME
 * \param  ctx  Control Plane context
 * \param  mme  incoming MME
 *
 */
void
cp_conn_mgr_process_cm_conn_info_cnf(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_msg_cm_conn_info_cnf_t received_data;
    
    cp_msg_cm_conn_info_cnf_receive(ctx, mme, &received_data);

    /* TODO find out what to do with the data */
}

/**
 * Process a APCM_CONN_ADD.REQ
 * \param  ctx  control plane context
 * \param  mme  incoming MME
 *
 * It gets the CSPEC the destination STA and send the request to this STA
 * It stores the request ID for this connection
 * 
 */
void
cp_conn_mgr_process_apcm_conn_add_req(cp_t *ctx, cp_mme_rx_t *mme)
{
    u16 cid;

    cp_sta_own_data_t *own_sta;
    cp_net_t *net;

    cp_conn_t *conn;

    cp_msg_apcm_conn_add_req_t received_data;
    cp_msg_cm_conn_new_req_t send_data;

    net = cp_sta_mgr_get_our_avln(ctx);

    cp_msg_apcm_conn_add_req_receive(ctx, mme, &received_data);

    own_sta = cp_sta_mgr_get_sta_own_data(ctx);

    /*** Init conn ***/
    /* Initiate the new connection */
    conn = blk_alloc();
    conn->flink = cp_link_init();
    conn->rlink = cp_link_init();

    conn->conn_info = blk_alloc();
    conn->conn_info->lid_f = cp_conn_get_free_lid(ctx);
    conn->conn_info->stei = cp_sta_own_data_get_tei(own_sta);
    conn->conn_info->dtei = cl_mactotei_table_find_tei_from_mac(NULL, received_data.oda);
    conn->conn_info->cspec = received_data.cspec;

    conn->request_id = received_data.request_id;
    conn->oda = received_data.oda;
    conn->osa = received_data.osa;

    /* Get CID of the connection*/
    cid = conn->conn_info->stei;
    cid = cid << 8;
    cid = cid + conn->conn_info->lid_f;

    conn->conn_info->cid = cid;

    /* Add the connection to the list */
    cp_conn_add_conn(ctx, conn);

    /* Init MME */
    send_data.cid = cid;
    send_data.cspec = received_data.cspec;

    /* Send MME */
    if(conn->type == CP_CONN_TYPE_UNICAST)
        cp_msg_cm_conn_new_req_send(ctx, 
                                    CP_MME_PEER(received_data.oda,
                                                conn->conn_info->dtei), 
                                    &send_data);

}

/**
 * Process a APCM_CONN_ADD.RSP
 * \param  ctx  control plane context
 * \param  mme  incoming MME
 *
 * It gets the response of the connection request. It shall send the respond
 * to the originating STA with the result. 
 * 
 */
void
cp_conn_mgr_process_apcm_conn_add_rsp(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_conn_t *conn;
    cp_msg_apcm_conn_add_rsp_t received_data;
    cp_msg_cm_conn_new_cnf_t send_data;

    cp_msg_apcm_conn_add_rsp_receive(ctx, mme, &received_data);

    conn = cp_conn_get_conn(ctx, received_data.cid);

    /* Init RSP MME */
    send_data.cid = received_data.cid;
    send_data.llid_r = conn->conn_info->lid_r;
    send_data.result = received_data.result;
    send_data.proposed_cspec = received_data.cspec;

    /* Send MME */
    if(conn->type == CP_CONN_TYPE_UNICAST)
        cp_msg_cm_conn_new_cnf_send(ctx,
                                    CP_MME_PEER(conn->osa, conn->conn_info->stei),
                                    &send_data);

    /* Study the result */
    if (received_data.result != 0x00)
        cp_conn_del_conn(ctx, received_data.cid);

}

/**
 * Process a APCM_CONN_MOD.REQ
 * \param  ctx  control plane context
 * \param  mme  incoming MME
 *
 */
void
cp_conn_mgr_process_apcm_conn_mod_req(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_conn_t *conn;
    cp_msg_apcm_conn_mod_req_t received_data;
    cp_msg_cm_conn_mod_req_t send_data;

    cp_msg_apcm_conn_mod_req_receive(ctx, mme, &received_data);

    conn = cp_conn_get_conn(ctx, received_data.cid);

    /* Modify the CSPEC */
    conn->conn_info->cspec = received_data.cspec;

    /* Init rep MME */
    send_data.cid = received_data.cid;
    send_data.modified_cspec = received_data.cspec;

    /* Send MME */
    if(conn->type == CP_CONN_TYPE_UNICAST)
        cp_msg_cm_conn_mod_req_send(ctx,
                                    CP_MME_PEER(conn->oda, conn->conn_info->dtei),
                                    &send_data);
}

/**
 * Process a APCM_CONN_MOD.RSP
 * \param  ctx  control plane context
 * \param  mme  incoming MME
 *
 */
void
cp_conn_mgr_process_apcm_conn_mod_rsp(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_conn_t *conn;
    cp_msg_apcm_conn_mod_rsp_t received_data;
    cp_msg_cm_conn_mod_cnf_t send_data;

    cp_msg_apcm_conn_mod_rsp_receive(ctx, mme, &received_data);

    conn = cp_conn_get_conn(ctx,received_data.cid);

    /* Init rsp MME */
    send_data.cid = received_data.cid;
    send_data.result = received_data.result;
    send_data.proposed_cspec = received_data.proposed_cspec;

    /* Send MME */
    if(conn->type == CP_CONN_TYPE_UNICAST)
        cp_msg_cm_conn_mod_cnf_send(ctx,
                                    CP_MME_PEER(conn->osa, conn->conn_info->stei),
                                    &send_data);
}

/**
 * Process a APCM_CONN_REL.REQ
 * \param  ctx  control plane context
 * \param  mme  incoming MME
 *
 */
void
cp_conn_mgr_process_apcm_conn_rel_req(cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_conn_t *conn;
    cp_msg_apcm_conn_rel_req_t received_data;
    cp_msg_cm_conn_rel_ind_t send_data;

    cp_msg_apcm_conn_rel_req_receive(ctx, mme, &received_data);

    /* Init the release req MME*/
    send_data.cid = received_data.cid;
    send_data.reason_code = 0x00;
    send_data.violated_cspec = NULL;

    conn = cp_conn_get_conn(ctx, received_data.cid);

    /* Send MME */
    if(conn->type == CP_CONN_TYPE_UNICAST)
        cp_msg_cm_conn_rel_ind_send(ctx,
                                    CP_MME_PEER(conn->oda,conn->conn_info->dtei),
                                    &send_data);

}