summaryrefslogtreecommitdiff
path: root/cesar/maximus/interface/msg/src/MsgMpdu.cpp
blob: ea4d57766231d2e23d54ecec7df1820243146a27 (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
/* Maximus project {{{
 *
 * Copyright (C) 2012 MStar Semiconductor
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    maximus/interface/msg/src/MsgMpdu.cpp
 * \ingroup maximus_interface_msg
 *
 */
#include "maximus/interface/msg/inc/MsgMpdu.h"
#include "maximus/utils/inc/Error.h"
#include "maximus/utils/inc/Logger.h"

MsgMpdu::MsgMpdu (PhyProcessor &ref):
    phy_proc (ref),
    SciMsg_pre (NULL),
    SciMsg_fc (NULL),
    SciMsg_payload (NULL),
    mpdu_pbsize (0),
    is_sent (0)
{
    logFunction ();
}

MsgMpdu::~MsgMpdu ( )
{
    logFunction ();
}

bool
MsgMpdu::setup_tx (
    Phy_Fc_Mode mpdu_mode,
    uint32_t mpdu_fc_10,
    uint32_t *mpdu_fc_av,
    std::string &mpdu_payload,
    Phy_Short_Ppdu mpdu_shortppdu,
    Phy_Mod mpdu_mod)
{
    logFunction ();

    if (mpdu_fc_av == NULL)
        return false;


    Phy_Mpdu_Format mpdu_format;
    Phy_Gil mpdu_gil;
    Phy_Fecrate mpdu_fecrate;
    Phy_Flags mpdu_flags = PHY_FLAG_CRC_OK;
    Phy_Nb_Of_Pbs nb_of_pbs;

    /* Step 0°) Pre-requested. */
    {
        /* Set MPDU format
         * HomePlug AV specs => delimiter type (DT_AV) is described by
         * bits 0-2 of first fc_av octet.
         * +1 to have the correspondance with our own MPDU format
         * definition (of 'phy_types.h'). */
        mpdu_format = (Phy_Mpdu_Format) ((mpdu_fc_av[0] & 0x07) + 1);

        if (!mpdu_shortppdu)
        {
            mpdu_fecrate = PHY_FEC_RATE_1_2;

            if (mpdu_mod == PHY_MOD_MINI_ROBO)
            {
                mpdu_gil = PHY_GIL_567;
                mpdu_pbsize = MAC_PB136_BYTES;
            }
            else
            {
                mpdu_gil = PHY_GIL_417;
                mpdu_pbsize = MAC_PB520_BYTES;
                mpdu_flags |= PHY_FLAG_PB512;
            }
        }
        else
        {
            mpdu_fecrate = PHY_FEC_RATE_NONE;
            mpdu_gil = PHY_GIL_NONE;
            mpdu_pbsize = MAC_PB520_BYTES;
            mpdu_flags |= PHY_FLAG_PB512;
        }

        nb_of_pbs = ((mpdu_payload.size () + 1 + mpdu_pbsize - 1)
                     / mpdu_pbsize);
    }

    /* Step 1°) PhySciMsg Preamble. */
    SciMsg_pre = new PhySciMsg (0);
    SciMsg_pre->set_phy_type (PHY_TYPE_PREAMBLE);

    /* Step 2°) PhySciMsg Frame Control. */
    {
        size_t mpdu_fc_size = 0;
        Phy_Type mpdu_fc_type;
        unsigned char *p1 = NULL;

        if (mpdu_fc_10 != 0)
        {
            mpdu_fc_type = PHY_TYPE_FC_HYBRID_MODE;
            mpdu_fc_size = 5 * sizeof (uint32_t);
        }
        else
        {
            mpdu_fc_type = PHY_TYPE_FC_AV_ONLY_MODE;
            mpdu_fc_size = 4 * sizeof (uint32_t);
        }

        SciMsg_fc = new PhySciMsg (mpdu_fc_size);
        SciMsg_fc->set_phy_type (mpdu_fc_type);
        p1 = SciMsg_fc->get_phy_data_addr ();

        if (mpdu_fc_type == PHY_TYPE_FC_HYBRID_MODE)
        {
            memcpy (p1, &mpdu_fc_10, sizeof (uint32_t));
            p1 += sizeof (uint32_t);
        }

        memcpy (p1, mpdu_fc_av, 4 * sizeof (uint32_t));
    }

    /* Step 3°) PhySciMsg Payload. */
    {
        unsigned char *p2;

        size_t length = nb_of_pbs * mpdu_pbsize;
        SciMsg_payload = new PhySciMsg (length);
        SciMsg_payload->set_phy_type (PHY_TYPE_MPDU_PAYLOAD);
        p2 = SciMsg_payload->get_phy_data_addr ();
        memcpy (p2, mpdu_payload.c_str (), mpdu_payload.size ());
        memset (p2 + mpdu_payload.size (),
                0, length - mpdu_payload.size ());
    }

    /* Step 4°) common fields. */
    {

        SciMsg_pre->set_phy_FecRate (mpdu_fecrate);
        SciMsg_fc->set_phy_FecRate (mpdu_fecrate);
        SciMsg_payload->set_phy_FecRate (mpdu_fecrate);

        SciMsg_pre->set_phy_gil (mpdu_gil);
        SciMsg_fc->set_phy_gil (mpdu_gil);
        SciMsg_payload->set_phy_gil (mpdu_gil);

        /* mpdu_pbsize is not used in SCI. */
        SciMsg_pre->set_phy_shortppdu (mpdu_shortppdu);
        SciMsg_fc->set_phy_shortppdu (mpdu_shortppdu);
        SciMsg_payload->set_phy_shortppdu (mpdu_shortppdu);

        SciMsg_pre->set_phy_mod (mpdu_mod);
        SciMsg_fc->set_phy_mod (mpdu_mod);
        SciMsg_payload->set_phy_mod (mpdu_mod);

        SciMsg_pre->set_phy_flags (mpdu_flags);
        SciMsg_fc->set_phy_flags (mpdu_flags);
        SciMsg_payload->set_phy_flags (mpdu_flags);

        SciMsg_payload->set_phy_MpduFormat (mpdu_format);
    }

    maximus_log (LOG_ERROR, "mpdu_payload.size = " << std::dec << mpdu_payload.size ());
    maximus_log (LOG_ERROR, "mpdu_pbsize = " << std::dec << mpdu_pbsize);

    SciMsg_payload->set_phy_NbOfPbs (nb_of_pbs);

    return true;
}