summaryrefslogtreecommitdiff
path: root/cesar/ce/src/tx.c
blob: f8dd2e65a5855729ad451921a42e67088ce0529a (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    tx.c
 * \brief   « brief description »
 * \ingroup « module »
 *
 * « long description »
 */
#include "common/std.h"
#include <stdio.h>
#include "cp/cp.h"
#include "cp/inc/context.h"
#include "ce/tx.h"
#include "ce/inc/tx.h"
#define TXCE
#include "ce/inc/cei.h"

static txce_t txce_global;

void
txce_init (cp_t *cp_ctx, mac_store_t *mac_store_ctx, mac_config_t *mac_config_ctx)
{
    dbg_assert (mac_store_ctx);
    dbg_assert (mac_config_ctx);
    txce_t *ctx = &txce_global;
    ctx->mac_store_ctx = mac_store_ctx;
    ctx->tonemask = mac_config_ctx->tonemask_info.tonemask;
    cyg_handle_t sys_clk = cyg_real_time_clock();
    cyg_handle_t counter_hdl;
    cyg_clock_to_counter (sys_clk, &counter_hdl);
    cyg_alarm_create (counter_hdl, alarm_cb, (cyg_addrword_t) cp_ctx, &ctx->alarm_hdl, &ctx->alarm_obj);
    cp_ctx->txce = ctx;
}

void
txce (cp_t *cp_ctx, uint stei, u32 mmtype, bitstream_t *stream_reader)
{
    dbg_assert (cp_ctx);
    txce_t *ctx = cp_ctx->txce;
    if (stream_reader)
    {
        dbg_assert (mmtype == CM_CHAN_EST_IND || mmtype == CM_TM_UPDATE_IND);
        dbg_assert (stei>=MAC_TEI_STA_MIN && stei<=MAC_TEI_STA_MAX);
        // Get concerned tonemaps from stei.
        sta_t *source_sta = mac_store_sta_get (ctx->mac_store_ctx, stei);
        tonemaps_t *source_tms = source_sta->tx_tonemaps;
        cei_decode (source_tms, mmtype, stream_reader, ctx->tonemask);
        blk_release (source_sta);
    }
    cyg_tick_count_t next_expiration = txce_expiration_tonemaps_management (ctx);
    cyg_alarm_initialize (ctx->alarm_hdl, next_expiration, 0);
}

cyg_tick_count_t
txce_expiration_tonemaps_management (txce_t *ctx)
{
    dbg_assert (ctx);
    uint tei;
    cyg_tick_count_t lowest_expiration_date = 0xFFFFFFFFFFFFFFFFll;
    for (tei=MAC_TEI_STA_MIN; tei<=MAC_TEI_STA_MAX; tei++)
    {
        cyg_tick_count_t current_rtc_date = cyg_current_time();
        sta_t *lsta = mac_store_sta_get (ctx->mac_store_ctx, tei);
        if (lsta)
        {
            tonemaps_t *tms = lsta->tx_tonemaps;
            if (tms)
            {
                cyg_tick_count_t expiration_date = tms->expiration_rtc_date;
                if (current_rtc_date >= expiration_date )
                {
                    int tmi;
                    for (tmi = 0; tmi<TONEMAP_INDEX_NB; tmi++)
                    {
                        if (tms->tm[tmi]) tonemap_release (tms, tmi);
                        //tms->intervals = NULL;
                        tms->expiration_rtc_date = 0xFFFFFFFFFFFFFFFFll;
                    }
                }
                else
                {
                    if (lowest_expiration_date > expiration_date)
                    {
                        lowest_expiration_date = expiration_date;
                    }
                }
            }
            blk_release (lsta);
        }
    }
    return lowest_expiration_date;
}

void
alarm_cb (cyg_handle_t alarm_hdl, cyg_addrword_t data)
{
    dbg_assert (data);
    cp_t *ctx = (cp_t *) data;
    txce (ctx, 0, 0, NULL);
}