Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Cxx refactoring #5

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: |
sudo apt-get install librtlsdr-dev
sudo apt-get install librtlsdr-dev build-essential g++-11
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

- name: Build
Expand Down
6 changes: 3 additions & 3 deletions Code/Cpp rewrite/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set(CMAKE_CXX_STANDARD 20)
include_directories(${PROJECT_SOURCE_DIR}/Src)
include_directories(${PROJECT_SOURCE_DIR}/Headers)
include_directories("../Refactor/Modes Handling/Src")
include_directories("../Refactor/Modes Handling/Headers")
link_directories(${CMAKE_BINARY_DIR}/Src)
link_directories(${CMAKE_BINARY_DIR}/Headers)

Expand All @@ -19,7 +19,7 @@ add_executable(dump1090
Src/main.cpp
Src/anet.cpp Headers/anet.hpp
Src/PacketHandling.cpp Headers/PacketHandling.hpp
Src/Expanded_set.cpp Headers/Expanded_set.hpp
Headers/Expanded_set.hpp
Src/data_reader.cpp Headers/data_reader.hpp
Src/Modes.cpp Headers/Modes.hpp
Src/Utilities.cpp Headers/Utilities.hpp
Expand Down
3 changes: 2 additions & 1 deletion Code/Cpp rewrite/Headers/Decoding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "aircraft.hpp"
#include "data_reader.hpp"
#include "Modes.hpp"
#include "Expanded_set.hpp"
#include <cmath>


Expand All @@ -25,7 +26,7 @@ int detectOutOfPhase(uint16_t const *m);

void applyPhaseCorrection(uint16_t *m);

void detectModeS(uint16_t *m, uint32_t mlen);
void detectModeS(uint16_t *m, uint32_t mlen, equeue <modesMessage> &bad_queue, equeue <modesMessage> &good_queue);

int decodeAC13Field(unsigned char *msg, int *unit);

Expand Down
31 changes: 26 additions & 5 deletions Code/Cpp rewrite/Headers/Expanded_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,44 @@

#include <vector>
#include <queue>
#include <shared_mutex>
#include <mutex>
#include <thread>
#include "../Headers/PacketHandling.hpp"
#include "modesMessage.h"

template<typename T>
class equeue : public std::queue<T> {
class equeue : protected std::queue<T> {
private:
std::mutex _lock;
public:
std::shared_lock<std::mutex> lock;

void insert_packet(packet &to_store);

packet &&move_packet(int64_t id);
void insert_packet(T &to_store);

T &&move_packet(int64_t id);

equeue() = default;

~equeue() = default;
};

template<typename T>
void equeue<T>::insert_packet(T &to_store) {
std::lock_guard lk(this->_lock);
this->emplace(to_store);
}

template<typename T>
T&& equeue<T>::move_packet(int64_t id) {
T* tmp;
std::lock_guard lk(this->_lock);

auto it = this->find(id);
if (it != std::queue<T>::end()){
tmp = std::move(*it);
}

return std::move(*tmp);
}

#endif //DUMP1090_EXPANDED_SET_HPP
7 changes: 5 additions & 2 deletions Code/Cpp rewrite/Headers/modesMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ struct modesMessage {
void addRecentlySeenICAOAddr(uint32_t addr);
int ICAOAddressWasRecentlySeen(uint32_t addr);
public:
modesMessage() = default;
modesMessage();
~modesMessage() = default;
modesMessage(unsigned char *msg);
modesMessage(unsigned char *msg, uint64_t seq);
void updatePlanes();
void decodeMessage();
int errorfix(bool twobitfix);
/* Generic fields */
unsigned char msg[MODES_LONG_MSG_BYTES+1]; /* Binary bitf_message. */
int msgbits; /* Number of bits in bitf_message */
int msgtype; /* Downlink format # */
uint64_t sequence_number; /* Sequence number from where its grabbed */
int crcok; /* True if CRC was valid */
uint32_t crc; /* Message CRC */
int errorbit; /* Bit corrected. -1 if no bit corrected. */
Expand Down
39 changes: 33 additions & 6 deletions Code/Cpp rewrite/Src/Decoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../Headers/debugging.hpp"
#include "../Headers/Utilities.hpp"
#include "../Headers/modesMessage.h"
#include "../Headers/Expanded_set.hpp"

/* ===================== Mode S detection and decoding ===================== */
/* Parity table for MODE S Messages.
Expand Down Expand Up @@ -125,8 +126,15 @@ int decodeHexMessage(struct client *c) {
if (high == -1 || low == -1) return 0;
msg[j / 2] = (high << 4) | low;
}
modesMessage mm(msg);
mm.updatePlanes();
modesMessage mm(msg, -1);
if (mm.crcok == -1){
mm.errorfix(Modes.aggressive);
}
if (mm.crcok == 0){
mm.decodeMessage();
mm.updatePlanes();
}

return 0;
}

Expand Down Expand Up @@ -210,7 +218,7 @@ void applyPhaseCorrection(uint16_t *m) {
/* Detect a Mode S messages inside the magnitude buffer pointed by 'm' and of
* size 'mlen' bytes. Every detected Mode S bitf_message is convert it into a
* stream of bits and passed to the function to display it. */
void detectModeS(uint16_t *m, uint32_t mlen) {
void detectModeS(uint16_t *m, uint32_t mlen, equeue <modesMessage> &bad_queue, equeue <modesMessage> &good_queue) {
unsigned char bits[MODES_LONG_MSG_BITS];
unsigned char msg[MODES_LONG_MSG_BITS / 2];
uint16_t aux[MODES_LONG_MSG_BITS * 2];
Expand Down Expand Up @@ -241,6 +249,9 @@ void detectModeS(uint16_t *m, uint32_t mlen) {
* 9 -------------------
*/
for (j = 0; j < mlen - MODES_FULL_LEN * 2; j++) {
static uint64_t sequence_num = 0; /* Used for keeping track of where the packet was grabbed from */
sequence_num++;

int low, high, delta, i, errors;
int good_message = 0;

Expand Down Expand Up @@ -377,9 +388,22 @@ void detectModeS(uint16_t *m, uint32_t mlen) {
if (errors == 0 || (Modes.aggressive && errors < 3)) {

/* Decode the received bitf_message and update statistics */
modesMessage mm(msg);
// TODO: Move this statistics part to somewhere else so we can do errorfixes on a separate thread.
modesMessage mm(msg, sequence_num);
int crcOk = mm.crcok;
int errorbit = mm.errorbit;

if (!mm.crcok){
bad_queue.insert_packet(mm);
}
else{
/* Its good. */
good_queue.insert_packet(mm);
}

// TODO: Move this statistics part to somewhere else so we can do errorfixes on a separate thread.
// Run it in main on all packets retrieved from good_messages
mm.errorfix(Modes.aggressive);
mm.decodeMessage();
/* Update statistics. */
if (mm.crcok || use_correction) {
if (errors == 0) Modes.stat_demodulated++;
Expand Down Expand Up @@ -418,9 +442,9 @@ void detectModeS(uint16_t *m, uint32_t mlen) {
if (use_correction)
mm.phase_corrected = 1;
}

/* Pass data to the next layer */
mm.updatePlanes();

} else {
if (Modes.debug & MODES_DEBUG_DEMODERR && use_correction) {
printf("The following bitf_message has %d demod errors\n", errors);
Expand Down Expand Up @@ -571,6 +595,9 @@ int fixTwoBitsErrors(unsigned char *msg, int bits) {
* be non-zero because i starts from j+1. */
return j | (i << 8);
}
else {
aux[byte2] ^= bitmask2; /* Flip back i-th bit. */
}
}
}
return -1;
Expand Down
18 changes: 0 additions & 18 deletions Code/Cpp rewrite/Src/Expanded_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@

#include "../Headers/Expanded_set.hpp"

template<typename T>
void equeue<T>::insert_packet(packet &to_store) {
this->lock.lock();
this->insert(to_store);
this->lock.unlock();
}

template<typename T>
packet&& equeue<T>::move_packet(int64_t id) {
packet* tmp;
this->lock.lock();
auto it = this->find(id);
if (it != std::queue<T>::end()){
tmp = std::move(*it);
}
this->lock.unlock();

return std::move(*tmp);
}


3 changes: 2 additions & 1 deletion Code/Cpp rewrite/Src/data_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "rtl-sdr.h"
#include "../Headers/anet.hpp"
#include "../Headers/Modes.hpp"
#include "../../Refactor/data sources/Headers/rtlsdr.h"

#include <thread>
#include <mutex>
#include <condition_variable>
Expand Down Expand Up @@ -180,4 +182,3 @@ void *readerThreadEntryPoint(void *arg) {
return nullptr;
}


17 changes: 13 additions & 4 deletions Code/Cpp rewrite/Src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,17 @@ int main(int argc, char **argv) {
}

std::shared_ptr<uint16_t> raw_data;

equeue<modesMessage> bad_messages;
equeue<modesMessage> good_messages;

std::vector<std::thread> threads;
/* Create the thread that will read the data from the device. */
std::thread reader_thread = std::thread(readerThreadEntryPoint, nullptr);
threads.emplace_back(readerThreadEntryPoint, nullptr);
while (!Modes.exit) {
if (!Modes.data_ready) {
Modes.mtx.unlock();
Modes.data_cond.wait(*Modes.data_lock);//, eval);
Modes.data_cond.wait(*Modes.data_lock);
continue;
}
raw_data = computeMagnitudeVector(); // Calculates stuff. No copies
Expand All @@ -242,7 +247,9 @@ int main(int argc, char **argv) {
Modes.data_ready = 0;
Modes.data_cond.notify_one();

detectModeS(raw_data.get(), Modes.data_len / 2);
detectModeS(raw_data.get(), Modes.data_len / 2, bad_messages, good_messages);


/*
* modesMessage = detectModeS // Unfinished message. No error corrections and not interpreted.
*
Expand Down Expand Up @@ -273,7 +280,9 @@ int main(int argc, char **argv) {
<< Modes.stat_two_bits_fix << " two bit errors\n"
<< Modes.stat_goodcrc + Modes.stat_fixed << " total usable messages" << std::endl;
}
reader_thread.join();
// Wait for all threads to finish
for (auto &thread: threads)
thread.join();
rtlsdr_close(Modes.dev);
return 0;
}
Expand Down
Loading