File: module_ax25_decoder.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (163 lines) | stat: -rw-r--r-- 5,402 bytes parent folder | download | duplicates (2)
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
#include "module_ax25_decoder.h"
#include "logger.h"
#include "imgui/imgui.h"
#include "common/utils.h"

#include "common/codings/differential/nrzi.h"
#include "common/codings/lfsr.h"
#include "common/codings/deframing/hdlc_def.h"

namespace ax25
{
    AX25DecoderModule::AX25DecoderModule(std::string input_file, std::string output_file_hint, nlohmann::json parameters) : ProcessingModule(input_file, output_file_hint, parameters)
    {
        input_buffer = new int8_t[256];

        d_nrzi = parameters.contains("nrzi") ? parameters["nrzi"].get<bool>() : false;
        d_g3ruh = parameters.contains("g3ruh") ? parameters["g3ruh"].get<bool>() : false;
    }

    AX25DecoderModule::~AX25DecoderModule()
    {
        delete[] input_buffer;
    }

    std::vector<ModuleDataType> AX25DecoderModule::getInputTypes()
    {
        return {DATA_FILE, DATA_STREAM};
    }

    std::vector<ModuleDataType> AX25DecoderModule::getOutputTypes()
    {
        return {DATA_FILE};
    }

    void AX25DecoderModule::process()
    {
        if (input_data_type == DATA_FILE)
            filesize = getFilesize(d_input_file);
        else
            filesize = 0;
        if (input_data_type == DATA_FILE)
            data_in = std::ifstream(d_input_file, std::ios::binary);

        data_out = std::ofstream(d_output_file_hint + ".frm", std::ios::binary);
        d_output_files.push_back(d_output_file_hint + ".frm");

        std::string directory = d_output_file_hint.substr(0, d_output_file_hint.rfind('/')) + "/";

        logger->info("Using input frames " + d_input_file);
        logger->info("Decoding to " + d_output_file_hint + ".frm");

        diff::NRZIDiff nrzi_decoder;
        common::lfsr g3rhu_descramble(0x21, 0x0, 16);
        HDLCDeframer hdlc_deframer(d_parameters["min_sz"].get<int>(), d_parameters["max_sz"].get<int>());

        uint8_t bits_buf[256];

        time_t lastTime = 0;
        while (input_data_type == DATA_FILE ? !data_in.eof() : input_active.load())
        {
            // Read buffer
            if (input_data_type == DATA_FILE)
                data_in.read((char *)input_buffer, 256);
            else
                input_fifo->read((uint8_t *)input_buffer, 256);

            // Slice
            for (int i = 0; i < 256; i++)
                bits_buf[i] = input_buffer[i] > 0;

            // Optional NRZ-I
            if (d_nrzi)
                nrzi_decoder.decode_bits(bits_buf, 256);

            // Optional G3RUH
            for (int i = 0; i < 256; i++)
                bits_buf[i] = g3rhu_descramble.next_bit_descramble(bits_buf[i]);

            // Deframe
            auto frames = hdlc_deframer.work(bits_buf, 256);

            for (auto frm : frames)
            {
                std::string source_callsign;
                for (int i = 0; i < 6; i++)
                    source_callsign += frm[i] >> 1;

                std::string destination_callsign;
                for (int i = 7; i < 7 + 6; i++)
                    destination_callsign += frm[i] >> 1;

                int source_ssid = frm[6] >> 1;
                int target_ssid = frm[7 + 6] >> 1;

                int ctrl = frm[14];
                int pid = frm[15];

                logger->trace("AX.25 Frame - SRC : " + source_callsign + ",%d DST : " + destination_callsign + ",%d CTRL : %d PID : %d",
                              source_ssid, target_ssid, ctrl, pid);

                // Add a custom header to ease up forwarding to the next stuff
                uint16_t pkt_sz = frm.size();

                uint8_t buffer_sz[6];
                buffer_sz[0] = 0x1a;
                buffer_sz[1] = 0xcf;
                buffer_sz[2] = 0xfc;
                buffer_sz[3] = 0x1d;
                buffer_sz[4] = pkt_sz >> 8;
                buffer_sz[5] = pkt_sz & 0xFF;

                frm.insert(frm.begin(), buffer_sz, buffer_sz + 6);
                data_out.write((char *)frm.data(), frm.size());

                frm_cnt++;
            }

            progress = data_in.tellg();

            if (time(NULL) % 10 == 0 && lastTime != time(NULL))
            {
                lastTime = time(NULL);
                logger->info("Progress " + std::to_string(round(((double)progress / (double)filesize) * 1000.0) / 10.0) + "%%, Frames " + std::to_string(frm_cnt));
            }
        }

        logger->info("Decoding finished");

        data_in.close();
    }

    void AX25DecoderModule::drawUI(bool window)
    {
        ImGui::Begin("AX-25 Decoder", NULL, window ? 0 : NOWINDOW_FLAGS);

        ImGui::Button("Deframer", {200 * ui_scale, 20 * ui_scale});
        {
            ImGui::Text("Frames : ");
            ImGui::SameLine();
            ImGui::TextColored(style::theme.green, UITO_C_STR(frm_cnt));
        }

        if (!streamingInput)
            ImGui::ProgressBar((double)progress / (double)filesize, ImVec2(ImGui::GetContentRegionAvail().x, 20 * ui_scale));

        ImGui::End();
    }

    std::string AX25DecoderModule::getID()
    {
        return "ax25_decoder";
    }

    std::vector<std::string> AX25DecoderModule::getParameters()
    {
        return {};
    }

    std::shared_ptr<ProcessingModule> AX25DecoderModule::getInstance(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
    {
        return std::make_shared<AX25DecoderModule>(input_file, output_file_hint, parameters);
    }
} // namespace noaa