aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.cpp
blob: f69b013ee795b683e7dabdb78b9a1b4020a50ec0 (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
/*
    Copyright (C) 2014 CSP Innovazione nelle ICT s.c.a r.l. (http://www.csp.it/)
    Copyright (C) 2018 Matthias P. Braendli (http://www.opendigitalradio.org)
    Copyright (C) 2015 Data Path

    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 3 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, see <http://www.gnu.org/licenses/>.

    etisnoop.cpp
          Parse ETI NI G.703 file

    Authors:
         Sergio Sagliocco <sergio.sagliocco@csp.it>
         Matthias P. Braendli <matthias@mpb.li>
                   / |  |-  ')|)  |-|_ _ (|,_   .|  _  ,_ \
         Data Path \(|(||_(|/_| (||_||(a)_||||(|||.(_()|||/

*/

#include "utils.hpp"
#include <cstring>
#include <sstream>
#include <cmath>
#include <limits>
#include <stdarg.h>

using namespace std;

static int verbosity = 0;

void set_verbosity(int v)
{
    verbosity = v;
}

int get_verbosity()
{
    return verbosity;
}

display_settings_t display_settings_t::operator+(int indent_offset) const
{
    return display_settings_t(print, indent+indent_offset);
}

std::string strprintf(const char* fmt, ...)
{
    int size = 512;
    std::string str;
    va_list ap;
    while (1) {
        str.resize(size);
        va_start(ap, fmt);
        int n = vsnprintf((char *)str.c_str(), size, fmt, ap);
        va_end(ap);
        if (n > -1 && n < size) {
            str.resize(n);
            break;
        }
        if (n > -1)
            size = n + 1;
        else
            size *= 2;
    }

    return str;
}

static void printyaml(const string& header,
        const display_settings_t &disp,
        uint8_t* buffer = nullptr,
        size_t size = 0,
        const std::string& desc = "",
        const std::string& value = "")
{
    if (disp.print) {
        stringstream ss;
        for (int i = 0; i < disp.indent; i++) {
            ss << " ";
        }

        ss << header;
        ss << ":";

        if (not value.empty() and desc.empty() and not buffer) {
            ss << " " << value;
        }
        else {
            if (not value.empty()) {
                ss << "\n";
                for (int i = 0; i < disp.indent + 1; i++) {
                    ss << " ";
                }
                ss << strprintf("value: %s", value.c_str());
            }

            if (not desc.empty()) {
                ss << "\n";
                for (int i = 0; i < disp.indent + 1; i++) {
                    ss << " ";
                }
                ss << strprintf("desc: %s", desc.c_str());
            }

            if (buffer and verbosity > 0) {
                if (size != 0) {
                    ss << "\n";
                    for (int i = 0; i < disp.indent + 1; i++) {
                        ss << " ";
                    }
                    ss << "data: [";

                    size_t num_printed = 0;

                    for (size_t i = 0; i < size; i++) {
                        if (i > 0) {
                            ss << ",";
                            num_printed++;
                        }

                        if (num_printed + disp.indent + 1 + 7 > 60 ) {
                            ss << "\n";
                            for (int i = 0; i < disp.indent + 8; i++) {
                                ss << " ";
                            }
                            num_printed = 2;
                        }
                        else if (i > 0) {
                            ss << " ";
                            num_printed++;
                        }

                        ss << strprintf("0x%02x", buffer[i]);
                        num_printed += 3;
                    }
                    ss << "]";
                }
            }
        }

        ss << "\n";
        printf("%s", ss.str().c_str());
    }
}

void printbuf(const std::string& header,
        int indent,
        uint8_t* buffer,
        size_t size,
        const std::string& desc,
        const std::string& value)
{
    display_settings_t disp(true, indent);
    printyaml(header, disp, buffer, size, desc, value);
}

void printbuf(const string& header,
        const display_settings_t &disp,
        uint8_t* buffer,
        size_t size,
        const std::string& desc,
        const std::string& value)
{
    display_settings_t d = disp;
    d.print = (verbosity > 0);
    printyaml(header, d, buffer, size, desc, value);
}

void printbuf(const std::string& header, int indent)
{
    display_settings_t disp(true, indent);
    return printyaml(header, disp);
}

void printfig(const string& header,
        const display_settings_t &disp,
        uint8_t* buffer,
        size_t size,
        const std::string& desc,
        const std::string& value)
{
    printyaml(header, disp, buffer, size, desc, value);
}

void printvalue(const std::string& header,
        const display_settings_t &disp,
        const std::string& desc,
        const std::string& value)
{
    return printyaml(header, disp, nullptr, 0, desc, value);
}

void printvalue(const std::string& header,
        int indent,
        const std::string& desc,
        const std::string& value)
{
    display_settings_t disp(true, indent);
    return printyaml(header, disp, nullptr, 0, desc, value);
}

void printinfo(const string &header,
        const display_settings_t &disp,
        int min_verb)
{
    if (verbosity >= min_verb) {
        for (int i = 0; i < disp.indent; i++) {
            printf(" ");
        }
        printf("info: %s\n", header.c_str());
    }
}

void printinfo(const std::string &header, int min_verb)
{
    const display_settings_t disp(true, 0);
    printinfo(header, min_verb);
}

void printsequencestart(int indent)
{
    for (int i = 0; i < indent; i++) {
        printf(" ");
    }
    printf("-\n");
}

int sprintfMJD(char *dst, int mjd) {
    // EN 62106 Annex G
    // These formulas are applicable between the inclusive dates: 1st March 1900 to 28th February 2100
    int y, m, k;
    struct tm timeDate;

    memset(&timeDate, 0, sizeof(struct tm));

    // find Y, M, D from MJD
    y = (int)(((double)mjd - 15078.2) / 365.25);
    m = (int)(((double)mjd - 14956.1 - (int)((double)y * 365.25)) / 30.6001);
    timeDate.tm_mday = mjd - 14956 - (int)((double)y * 365.25) - (int)((double)m * 30.6001);
    if ((m == 14) || (m == 15)) {
        k = 1;
    }
    else {
        k = 0;
    }
    timeDate.tm_year = y + k;
    timeDate.tm_mon = (m - 1 - (k * 12)) - 1;

    // find WD from MJD
    timeDate.tm_wday = (((mjd + 2) % 7) + 1) % 7;

    //timeDate.tm_yday = 0; // Number of days since the first day of January not calculated
    timeDate.tm_isdst = -1; // No time print then information not available

    // print date string
    if ((timeDate.tm_mday < 0) || (timeDate.tm_mon < 0) || (timeDate.tm_year < 0)) {
        return sprintf(dst, "invalid MJD mday=%d mon=%d year=%d", timeDate.tm_mday, timeDate.tm_mon, timeDate.tm_year);
    }
    return strftime(dst, 256, "%a %b %d %Y", &timeDate);
}

std::string pnum_to_str(uint16_t Programme_Number)
{
    uint8_t day, hour, minute;

    minute = (uint8_t)(Programme_Number & 0x003F);
    hour = (uint8_t)((Programme_Number >> 6) & 0x001F);
    day = (uint8_t)((Programme_Number >> 11) & 0x001F);
    if (day != 0) {
        return strprintf("day of month=%d time=%02d:%02d", day, hour, minute);
    }
    else {  // day == 0
        // Special codes are allowed when the date part of the PNum field
        // signals date = "0". In this case, the hours and minutes part of
        // the field shall contain a special code, as follows
        if ((hour == 0) && (minute == 0)) {
            return "Status code: no meaningful PNum is currently provided";
        }
        else if ((hour == 0) && (minute == 1)) {
            return "Blank code: the current programme is not worth recording";
        }
        else if ((hour == 0) && (minute == 2)) {
            return "Interrupt code: the interrupt is unplanned "
                "(for example a traffic announcement)";
        }
        else {
            return "invalid value";
        }
    }
}

int absolute_to_dB(int16_t value)
{
    const int16_t int16_max = std::numeric_limits<int16_t>::max();
    return value ? round(20*log10((double)value / int16_max)) : -90;
}