mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2025-07-05 08:28:20 +02:00
beginning of pager decoder
This commit is contained in:
32
decoder_modules/pager_decoder/src/pocsag/decoder.h
Normal file
32
decoder_modules/pager_decoder/src/pocsag/decoder.h
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include "../decoder.h"
|
||||
#include <utils/optionlist.h>
|
||||
#include <gui/widgets/symbol_diagram.h>
|
||||
#include <gui/style.h>
|
||||
|
||||
class POCSAGDecoder : public Decoder {
|
||||
public:
|
||||
POCSAGDecoder() : diag(0.6, 2400) {
|
||||
// Define baudrate options
|
||||
baudrates.define(512, "512 Baud", 512);
|
||||
baudrates.define(1200, "1200 Baud", 1200);
|
||||
baudrates.define(2400, "2400 Baud", 2400);
|
||||
}
|
||||
|
||||
void showMenu() {
|
||||
ImGui::LeftLabel("Baudrate");
|
||||
ImGui::FillWidth();
|
||||
if (ImGui::Combo(("##pager_decoder_proto_" + name).c_str(), &brId, baudrates.txt)) {
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
|
||||
ImGui::SymbolDiagram diag;
|
||||
|
||||
int brId = 2;
|
||||
|
||||
OptionList<int, int> baudrates;
|
||||
};
|
71
decoder_modules/pager_decoder/src/pocsag/dsp.h
Normal file
71
decoder_modules/pager_decoder/src/pocsag/dsp.h
Normal file
@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
#include <dsp/stream.h>
|
||||
#include <dsp/buffer/reshaper.h>
|
||||
#include <dsp/multirate/rational_resampler.h>
|
||||
#include <dsp/sink/handler_sink.h>
|
||||
#include <dsp/demod/quadrature.h>
|
||||
#include <dsp/clock_recovery/mm.h>
|
||||
#include <dsp/taps/root_raised_cosine.h>
|
||||
#include <dsp/correction/dc_blocker.h>
|
||||
#include <dsp/loop/fast_agc.h>
|
||||
#include <dsp/digital/binary_slicer.h>
|
||||
#include <dsp/routing/doubler.h>
|
||||
|
||||
class POCSAGDSP : dsp::Processor<dsp::complex_t, uint8_t> {
|
||||
using base_type = dsp::Processor<dsp::complex_t, uint8_t>;
|
||||
public:
|
||||
POCSAGDSP() {}
|
||||
POCSAGDSP(dsp::stream<dsp::complex_t>* in, double samplerate, double baudrate) { init(in, samplerate, baudrate); }
|
||||
|
||||
void init(dsp::stream<dsp::complex_t>* in, double samplerate, double baudrate) {
|
||||
// Save settings
|
||||
// TODO
|
||||
|
||||
// Configure blocks
|
||||
demod.init(NULL, -4500.0, samplerate);
|
||||
dcBlock.init(NULL, 0.001);
|
||||
float taps[] = { 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f };
|
||||
shape = dsp::taps::fromArray<float>(10, taps);
|
||||
fir.init(NULL, shape);
|
||||
recov.init(NULL, samplerate/baudrate, 1e5, 0.1, 0.05);
|
||||
|
||||
// Free useless buffers
|
||||
dcBlock.out.free();
|
||||
fir.out.free();
|
||||
recov.out.free();
|
||||
|
||||
// Init base
|
||||
base_type::init(in);
|
||||
}
|
||||
|
||||
int process(int count, dsp::complex_t* in, float* softOut, uint8_t* out) {
|
||||
count = demod.process(count, in, demod.out.readBuf);
|
||||
count = dcBlock.process(count, demod.out.readBuf, demod.out.readBuf);
|
||||
count = fir.process(count, demod.out.readBuf, demod.out.readBuf);
|
||||
count = recov.process(count, demod.out.readBuf, softOut);
|
||||
dsp::digital::BinarySlicer::process(count, softOut, out);
|
||||
return count;
|
||||
}
|
||||
|
||||
int run() {
|
||||
int count = base_type::_in->read();
|
||||
if (count < 0) { return -1; }
|
||||
|
||||
count = process(count, base_type::_in->readBuf, soft.writeBuf, base_type::out.writeBuf);
|
||||
|
||||
base_type::_in->flush();
|
||||
if (!base_type::out.swap(count)) { return -1; }
|
||||
if (!soft.swap(count)) { return -1; }
|
||||
return count;
|
||||
}
|
||||
|
||||
dsp::stream<float> soft;
|
||||
|
||||
private:
|
||||
dsp::demod::Quadrature demod;
|
||||
dsp::correction::DCBlocker<float> dcBlock;
|
||||
dsp::tap<float> shape;
|
||||
dsp::filter::FIR<float, float> fir;
|
||||
dsp::clock_recovery::MM<float> recov;
|
||||
|
||||
};
|
140
decoder_modules/pager_decoder/src/pocsag/pocsag.cpp
Normal file
140
decoder_modules/pager_decoder/src/pocsag/pocsag.cpp
Normal file
@ -0,0 +1,140 @@
|
||||
#include "pocsag.h"
|
||||
#include <string.h>
|
||||
#include <utils/flog.h>
|
||||
|
||||
#define POCSAG_FRAME_SYNC_CODEWORD ((uint32_t)(0b01111100110100100001010111011000))
|
||||
#define POCSAG_IDLE_CODEWORD_DATA ((uint32_t)(0b011110101100100111000))
|
||||
#define POCSAG_BATCH_BIT_COUNT (POCSAG_BATCH_CODEWORD_COUNT*32)
|
||||
|
||||
#define POCSAG_GEN_POLY ((uint32_t)(0b11101101001))
|
||||
|
||||
namespace pocsag {
|
||||
const char NUMERIC_CHARSET[] = {
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3',
|
||||
'4',
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
'8',
|
||||
'9',
|
||||
'*',
|
||||
'U',
|
||||
' ',
|
||||
'-',
|
||||
']',
|
||||
'['
|
||||
};
|
||||
|
||||
void Decoder::process(uint8_t* symbols, int count) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
// Get symbol
|
||||
uint32_t s = symbols[i];
|
||||
|
||||
// If not sync, try to acquire sync (TODO: sync confidence)
|
||||
if (!synced) {
|
||||
// Append new symbol to sync shift register
|
||||
syncSR = (syncSR << 1) | s;
|
||||
|
||||
// Test for sync
|
||||
synced = (distance(syncSR, POCSAG_FRAME_SYNC_CODEWORD) <= POCSAG_SYNC_DIST);
|
||||
|
||||
// Go to next symbol
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: Flush message on desync
|
||||
|
||||
// Append bit to batch
|
||||
batch[batchOffset >> 5] |= (s << (31 - (batchOffset & 0b11111)));
|
||||
batchOffset++;
|
||||
|
||||
// On end of batch, decode and reset
|
||||
if (batchOffset >= POCSAG_BATCH_BIT_COUNT) {
|
||||
decodeBatch();
|
||||
batchOffset = 0;
|
||||
synced = false;
|
||||
memset(batch, 0, sizeof(batch));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Decoder::distance(uint32_t a, uint32_t b) {
|
||||
uint32_t diff = a ^ b;
|
||||
int dist = 0;
|
||||
for (int i = 0; i < 32; i++) {
|
||||
dist += (diff >> i ) & 1;
|
||||
}
|
||||
return dist;
|
||||
}
|
||||
|
||||
bool Decoder::correctCodeword(Codeword in, Codeword& out) {
|
||||
|
||||
|
||||
return true; // TODO
|
||||
}
|
||||
|
||||
void Decoder::flushMessage() {
|
||||
if (!msg.empty()) {
|
||||
onMessage(addr, msgType, msg);
|
||||
msg.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void Decoder::decodeBatch() {
|
||||
for (int i = 0; i < POCSAG_BATCH_CODEWORD_COUNT; i++) {
|
||||
// Get codeword
|
||||
Codeword cw = batch[i];
|
||||
|
||||
// Correct errors. If corrupted, skip
|
||||
if (!correctCodeword(cw, cw)) { continue; }
|
||||
// TODO: End message if two consecutive are corrupt
|
||||
|
||||
// Get codeword type
|
||||
CodewordType type = (CodewordType)((cw >> 31) & 1);
|
||||
if (type == CODEWORD_TYPE_ADDRESS && (cw >> 11) == POCSAG_IDLE_CODEWORD_DATA) {
|
||||
type = CODEWORD_TYPE_IDLE;
|
||||
}
|
||||
|
||||
// Decode codeword
|
||||
if (type == CODEWORD_TYPE_IDLE) {
|
||||
// If a non-empty message is available, send it out and clear
|
||||
flushMessage();
|
||||
flog::debug("[{}:{}]: IDLE", (i >> 1), i&1);
|
||||
}
|
||||
else if (type == CODEWORD_TYPE_ADDRESS) {
|
||||
// If a non-empty message is available, send it out and clear
|
||||
flushMessage();
|
||||
|
||||
// Decode message type
|
||||
msgType = (MessageType)((cw >> 11) & 0b11);
|
||||
|
||||
// Decode address and append lower 8 bits from position
|
||||
addr = ((cw >> 13) & 0b111111111111111111) << 3;
|
||||
addr |= (i >> 1);
|
||||
}
|
||||
else if (type == CODEWORD_TYPE_MESSAGE) {
|
||||
// Extract the 20 data bits
|
||||
uint32_t data = (cw >> 11) & 0b11111111111111111111;
|
||||
|
||||
// Decode data depending on message type
|
||||
if (msgType == MESSAGE_TYPE_NUMERIC) {
|
||||
// Numeric messages pack 5 characters per message codeword
|
||||
msg += NUMERIC_CHARSET[(data >> 16) & 0b1111];
|
||||
msg += NUMERIC_CHARSET[(data >> 12) & 0b1111];
|
||||
msg += NUMERIC_CHARSET[(data >> 8) & 0b1111];
|
||||
msg += NUMERIC_CHARSET[(data >> 4) & 0b1111];
|
||||
msg += NUMERIC_CHARSET[data & 0b1111];
|
||||
}
|
||||
else if (msgType == MESSAGE_TYPE_ALPHANUMERIC) {
|
||||
|
||||
}
|
||||
|
||||
// Save last data
|
||||
lastMsgData = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
48
decoder_modules/pager_decoder/src/pocsag/pocsag.h
Normal file
48
decoder_modules/pager_decoder/src/pocsag/pocsag.h
Normal file
@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include <utils/new_event.h>
|
||||
|
||||
#define POCSAG_SYNC_DIST 4
|
||||
#define POCSAG_BATCH_CODEWORD_COUNT 16
|
||||
|
||||
namespace pocsag {
|
||||
enum CodewordType {
|
||||
CODEWORD_TYPE_IDLE = -1,
|
||||
CODEWORD_TYPE_ADDRESS = 0,
|
||||
CODEWORD_TYPE_MESSAGE = 1
|
||||
};
|
||||
|
||||
enum MessageType {
|
||||
MESSAGE_TYPE_NUMERIC = 0b00,
|
||||
MESSAGE_TYPE_ALPHANUMERIC = 0b11
|
||||
};
|
||||
|
||||
using Codeword = uint32_t;
|
||||
using Address = uint32_t;
|
||||
|
||||
class Decoder {
|
||||
public:
|
||||
void process(uint8_t* symbols, int count);
|
||||
|
||||
NewEvent<Address, MessageType, const std::string&> onMessage;
|
||||
|
||||
private:
|
||||
static int distance(uint32_t a, uint32_t b);
|
||||
bool correctCodeword(Codeword in, Codeword& out);
|
||||
void flushMessage();
|
||||
void decodeBatch();
|
||||
|
||||
uint32_t syncSR = 0;
|
||||
bool synced = false;
|
||||
int batchOffset = 0;
|
||||
|
||||
Codeword batch[POCSAG_BATCH_CODEWORD_COUNT];
|
||||
|
||||
Address addr;
|
||||
MessageType msgType;
|
||||
std::string msg;
|
||||
|
||||
uint32_t lastMsgData;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user