summaryrefslogtreecommitdiffstats
path: root/src/TimestampDecoder.cpp
blob: 5044366e98cd8750824fc2101a0c895efb0d6999 (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
/*
   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the
   Queen in Right of Canada (Communications Research Center Canada)

   Copyright (C) 2014, 2015
   Matthias P. Braendli, matthias.braendli@mpb.li

    http://opendigitalradio.org
 */
/*
   This file is part of ODR-DabMod.

   ODR-DabMod is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   ODR-DabMod is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with ODR-DabMod.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <queue>
#include <iostream>
#include <fstream>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/make_shared.hpp>
#include <sys/types.h>
#include "PcDebug.h"
#include "TimestampDecoder.h"
#include "Eti.h"
#include "Log.h"

//#define MDEBUG(fmt, args...) fprintf (LOG, "*****" fmt , ## args) 
#define MDEBUG(fmt, args...) PDEBUG(fmt, ## args) 


void TimestampDecoder::calculateTimestamp(struct frame_timestamp& ts)
{
    boost::shared_ptr<struct frame_timestamp> ts_queued =
        boost::make_shared<struct frame_timestamp>();

    /* Push new timestamp into queue */
    ts_queued->timestamp_valid = full_timestamp_received_mnsc;
    ts_queued->timestamp_sec = time_secs;
    ts_queued->timestamp_pps_offset = time_pps;
    ts_queued->fct = latestFCT;

    ts_queued->timestamp_refresh = offset_changed;
    offset_changed = false;

    MDEBUG("time_secs=%d, time_pps=%f\n", time_secs, time_pps);
    *ts_queued += timestamp_offset;

    queue_timestamps.push(ts_queued);

    /* Here, the queue size is one more than the pipeline delay, because
     * we've just added a new element in the queue.
     *
     * Therefore, use <= and not < for comparison
     */
    if (queue_timestamps.size() <= m_tist_delay_stages) {
        //fprintf(stderr, "* %zu %u ", queue_timestamps.size(), m_tist_delay_stages);
        /* Return invalid timestamp until the queue is full */
        ts.timestamp_valid = false;
        ts.timestamp_sec = 0;
        ts.timestamp_pps_offset = 0;
        ts.timestamp_refresh = false;
        ts.fct = -1;
    }
    else {
        //fprintf(stderr, ". %zu ", queue_timestamps.size());
        /* Return timestamp from queue */
        ts_queued = queue_timestamps.front();
        queue_timestamps.pop();
        /*fprintf(stderr, "ts_queued v:%d, sec:%d, pps:%f, ref:%d\n",
                ts_queued->timestamp_valid,
                ts_queued->timestamp_sec,
                ts_queued->timestamp_pps_offset,
                ts_queued->timestamp_refresh);*/
        ts = *ts_queued;
        /*fprintf(stderr, "ts v:%d, sec:%d, pps:%f, ref:%d\n\n",
                ts.timestamp_valid,
                ts.timestamp_sec,
                ts.timestamp_pps_offset,
                ts.timestamp_refresh);*/
    }

    MDEBUG("Timestamp queue size %zu, delay_calc %u\n",
            queue_timestamps.size(),
            m_tist_delay_stages);

    if (queue_timestamps.size() > m_tist_delay_stages) {
        etiLog.level(error) << "Error: Timestamp queue is too large : size " <<
            queue_timestamps.size() << "! This should not happen !";
    }

    //ts.print("calc2 ");
}

void TimestampDecoder::pushMNSCData(int framephase, uint16_t mnsc)
{
    struct eti_MNSC_TIME_0 *mnsc0;
    struct eti_MNSC_TIME_1 *mnsc1;
    struct eti_MNSC_TIME_2 *mnsc2;
    struct eti_MNSC_TIME_3 *mnsc3;

    switch (framephase)
    {
        case 0:
            mnsc0 = (struct eti_MNSC_TIME_0*)&mnsc;
            enableDecode = (mnsc0->type == 0) &&
                (mnsc0->identifier == 0);
            gmtime_r(0, &temp_time);
            break;

        case 1:
            mnsc1 = (struct eti_MNSC_TIME_1*)&mnsc;
            temp_time.tm_sec = mnsc1->second_tens * 10 + mnsc1->second_unit;
            temp_time.tm_min = mnsc1->minute_tens * 10 + mnsc1->minute_unit;

            if (!mnsc1->sync_to_frame)
            {
                enableDecode = false;
                PDEBUG("TimestampDecoder: MNSC time info is not synchronised to frame\n");
            }

            break;

        case 2:
            mnsc2 = (struct eti_MNSC_TIME_2*)&mnsc;
            temp_time.tm_hour = mnsc2->hour_tens * 10 + mnsc2->hour_unit;
            temp_time.tm_mday = mnsc2->day_tens * 10 + mnsc2->day_unit;
            break;

        case 3:
            mnsc3 = (struct eti_MNSC_TIME_3*)&mnsc;
            temp_time.tm_mon = (mnsc3->month_tens * 10 + mnsc3->month_unit) - 1;
            temp_time.tm_year = (mnsc3->year_tens * 10 + mnsc3->year_unit) + 100;

            if (enableDecode)
            {
                full_timestamp_received_mnsc = true;
                updateTimestampSeconds(mktime(&temp_time));
            }
            break;
    }

    MDEBUG("TimestampDecoder::pushMNSCData(%d, 0x%x)\n", framephase, mnsc);
    MDEBUG("                            -> %s\n", asctime(&temp_time));
    MDEBUG("                            -> %zu\n", mktime(&temp_time));
}

void TimestampDecoder::updateTimestampSeconds(uint32_t secs)
{
    if (inhibit_second_update > 0)
    {
        MDEBUG("TimestampDecoder::updateTimestampSeconds(%d) inhibit\n", secs);
        inhibit_second_update--;
    }
    else
    {
        MDEBUG("TimestampDecoder::updateTimestampSeconds(%d) apply\n", secs);
        time_secs = secs;
    }
}

void TimestampDecoder::updateTimestampPPS(double pps)
{
    MDEBUG("TimestampDecoder::updateTimestampPPS(%f)\n", pps);

    if (time_pps > pps) // Second boundary crossed
    {
        MDEBUG("TimestampDecoder::updateTimestampPPS crossed second\n");

        // The second for the next eight frames will not
        // be defined by the MNSC
        inhibit_second_update = 2;
        time_secs += 1;
    }

    time_pps = pps;
}

void TimestampDecoder::updateTimestampEti(
        int framephase,
        uint16_t mnsc,
        double pps,
        int32_t fct)
{
    updateTimestampPPS(pps);
    pushMNSCData(framephase, mnsc);
    latestFCT = fct;
}

void TimestampDecoder::set_parameter(
        const std::string& parameter,
        const std::string& value)
{
    using namespace std;

    stringstream ss(value);
    ss.exceptions ( stringstream::failbit | stringstream::badbit );

    if (parameter == "offset") {
        ss >> timestamp_offset;
        offset_changed = true;
    }
    else {
        stringstream ss;
        ss << "Parameter '" << parameter
            << "' is not exported by controllable " << get_rc_name();
        throw ParameterError(ss.str());
    }
}

const std::string TimestampDecoder::get_parameter(
        const std::string& parameter) const
{
    using namespace std;

    stringstream ss;
    if (parameter == "offset") {
        ss << timestamp_offset;
    }
    else {
        ss << "Parameter '" << parameter <<
            "' is not exported by controllable " << get_rc_name();
        throw ParameterError(ss.str());
    }
    return ss.str();
}