aboutsummaryrefslogtreecommitdiffstats
path: root/lib/edi/STIWriter.cpp
blob: 29f0124c937dfe30cdda2c103c2636c319ca4a86 (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
/*
   Copyright (C) 2019
   Matthias P. Braendli, matthias.braendli@mpb.li

   http://opendigitalradio.org

    This program 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 2 of the License, or
    (at your option) any later version.

    This program 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 this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include "STIWriter.hpp"
#include "crc.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
#include <stdexcept>
#include <sstream>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <sstream>

namespace EdiDecoder {

using namespace std;

STIWriter::STIWriter(std::function<void(sti_frame_t&&)>&& frame_callback) :
    m_frame_callback(move(frame_callback))
{
}

void STIWriter::update_protocol(
        const std::string& proto,
        uint16_t major,
        uint16_t minor)
{
    m_proto_valid = (proto == "DSTI" and major == 0 and minor == 0);

    if (not m_proto_valid) {
        throw std::invalid_argument("Wrong EDI protocol");
    }
}


void STIWriter::update_stat(uint8_t stat, uint16_t spid)
{
    m_stat = stat;
    m_spid = spid;
    m_stat_valid = true;

    if (m_stat != 0xFF) {
        etiLog.log(warn, "STI errorlevel %02x", m_stat);
    }
}

void STIWriter::update_rfad(std::array<uint8_t, 9> rfad)
{
    (void)rfad;
}

void STIWriter::update_sti_management(const sti_management_data& data)
{
    m_management_data = data;
    m_management_data_valid = true;
}

void STIWriter::add_payload(sti_payload_data&& payload)
{
    m_payload = move(payload);
    m_payload_valid = true;
}

void STIWriter::update_audio_levels(const audio_level_data& data)
{
    m_audio_levels = data;
}

void STIWriter::update_odr_version(const odr_version_data& data)
{
    m_version_data = data;
}

void STIWriter::update_edi_time(
        uint32_t utco,
        uint32_t seconds)
{
    if (not m_proto_valid) {
        throw std::runtime_error("Cannot update time before protocol");
    }

    m_utco = utco;
    m_seconds = seconds;

    // TODO check validity
    m_time_valid = true;
}


void STIWriter::assemble()
{
    if (not m_proto_valid) {
        throw std::runtime_error("Cannot assemble STI before protocol");
    }

    if (not m_management_data_valid) {
        throw std::runtime_error("Cannot assemble STI before management data");
    }

    if (not m_payload_valid) {
        throw std::runtime_error("Cannot assemble STI without frame data");
    }

    // TODO check time validity

    sti_frame_t stiFrame;
    stiFrame.dlfc = m_management_data.dlfc;
    stiFrame.frame = move(m_payload.istd);
    stiFrame.timestamp.seconds = m_seconds;
    stiFrame.timestamp.utco = m_utco;
    stiFrame.timestamp.tsta = m_management_data.tsta;
    stiFrame.audio_levels = m_audio_levels;
    stiFrame.version_data = m_version_data;

    m_frame_callback(move(stiFrame));

    m_proto_valid = false;
    m_management_data_valid = false;
    m_stat_valid = false;
    m_time_valid = false;
    m_payload_valid = false;
}

}