mirror of https://github.com/nostar/urfd.git
Merge 1230625601 into bd0c114e43
commit
4edc2c60cb
@ -0,0 +1,160 @@
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
#include "M17Parrot.h"
|
||||
#include "Global.h"
|
||||
#include "M17Protocol.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Stream Parrot
|
||||
|
||||
CM17StreamParrot::CM17StreamParrot(const CCallsign &src_addr, std::shared_ptr<CM17Client> spc, uint16_t ft, CM17Protocol *proto)
|
||||
: CParrot(src_addr, spc, ft, proto), m_streamId(0)
|
||||
{
|
||||
m_is3200 = (0x4U == (0x4U & ft));
|
||||
m_lastHeard.start();
|
||||
}
|
||||
|
||||
void CM17StreamParrot::Add(const CBuffer &Buffer, uint16_t streamId, uint16_t frameNumber)
|
||||
{
|
||||
(void)frameNumber; // We generate our own sequence on playback
|
||||
if (m_data.size() < 500u)
|
||||
{
|
||||
m_streamId = streamId;
|
||||
size_t length = m_is3200 ? 16 : 8;
|
||||
|
||||
bool isStandard = false;
|
||||
if (Buffer.size() == 56) isStandard = true;
|
||||
|
||||
// Use parser to get payload pointer safely
|
||||
CM17Packet parser(Buffer.data(), isStandard);
|
||||
const uint8_t *payload = parser.GetPayload();
|
||||
|
||||
m_data.emplace_back(payload, payload + length);
|
||||
}
|
||||
m_lastHeard.start();
|
||||
}
|
||||
|
||||
void CM17StreamParrot::Play()
|
||||
{
|
||||
m_fut = std::async(std::launch::async, &CM17StreamParrot::playThread, this);
|
||||
}
|
||||
|
||||
bool CM17StreamParrot::IsExpired() const
|
||||
{
|
||||
return m_lastHeard.time() > 1.6; // 1.6s timeout like mrefd
|
||||
}
|
||||
|
||||
void CM17StreamParrot::playThread()
|
||||
{
|
||||
m_state = EParrotState::play;
|
||||
|
||||
// Determine format to send
|
||||
bool useLegacy = g_Configure.GetBoolean(g_Keys.m17.compat);
|
||||
|
||||
uint8_t buffer[60];
|
||||
CM17Packet pkt(buffer, !useLegacy);
|
||||
memset(buffer, 0, 60); // clear buffer
|
||||
|
||||
pkt.SetMagic();
|
||||
pkt.SetStreamId(m_streamId);
|
||||
|
||||
// I will add `SetDestBytes` to CM17Packet? Or just use explicit CCallsign.
|
||||
// I will try to use the `m_src` as dest? No, that's what `CodeOut` does.
|
||||
// I will use `pkt.SetDestCallsign` with a dummy, and then manually overwrite if needed?
|
||||
// Better: `CM17Packet` exposes `GetLichPointer()`. I can write to it manually!
|
||||
|
||||
// Set Source
|
||||
pkt.SetSourceCallsign(m_src);
|
||||
pkt.SetFrameType(m_frameType);
|
||||
|
||||
// Set Dest to FF
|
||||
uint8_t *lich = pkt.GetLICHPointer();
|
||||
memset(lich, 0xFF, 6); // Dest is at offset 0 of LICH
|
||||
|
||||
auto clock = std::chrono::steady_clock::now();
|
||||
size_t size = m_data.size();
|
||||
|
||||
for (size_t n = 0; n < size; n++)
|
||||
{
|
||||
size_t length = m_is3200 ? 16 : 8;
|
||||
pkt.SetPayload(m_data[n].data());
|
||||
|
||||
uint16_t fn = (uint16_t)n;
|
||||
if (n == size - 1)
|
||||
fn |= 0x8000u;
|
||||
pkt.SetFrameNumber(fn);
|
||||
|
||||
// CRC
|
||||
CM17CRC m17crc_inst;
|
||||
if (!useLegacy) {
|
||||
// Standard LICH CRC
|
||||
uint16_t l_crc = m17crc_inst.CalcCRC(lich, 28);
|
||||
((SM17LichStandard*)lich)->crc = htons(l_crc);
|
||||
}
|
||||
|
||||
uint16_t p_crc = m17crc_inst.CalcCRC(pkt.GetBuffer(), pkt.GetSize()-2);
|
||||
pkt.SetCRC(p_crc);
|
||||
|
||||
clock = clock + std::chrono::milliseconds(40);
|
||||
std::this_thread::sleep_until(clock);
|
||||
|
||||
if (m_proto) {
|
||||
CBuffer sendBuf;
|
||||
sendBuf.Append(pkt.GetBuffer(), pkt.GetSize());
|
||||
m_proto->Send(sendBuf, m_client->GetIp());
|
||||
}
|
||||
m_data[n].clear();
|
||||
}
|
||||
m_data.clear();
|
||||
m_state = EParrotState::done;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Packet Parrot
|
||||
|
||||
CM17PacketParrot::CM17PacketParrot(const CCallsign &src_addr, std::shared_ptr<CM17Client> spc, uint16_t ft, CM17Protocol *proto)
|
||||
: CParrot(src_addr, spc, ft, proto)
|
||||
{
|
||||
}
|
||||
|
||||
void CM17PacketParrot::AddPacket(const CBuffer &Buffer)
|
||||
{
|
||||
m_packet = Buffer;
|
||||
}
|
||||
|
||||
void CM17PacketParrot::Play()
|
||||
{
|
||||
m_fut = std::async(std::launch::async, &CM17PacketParrot::playThread, this);
|
||||
}
|
||||
|
||||
void CM17PacketParrot::playThread()
|
||||
{
|
||||
m_state = EParrotState::play;
|
||||
|
||||
// 100ms delay like mrefd
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
// Change destination to ALL (broadcast back to sender) or specific?
|
||||
// M17P is usually 4 magic + 6 dst + 6 src + ...
|
||||
if (m_packet.size() >= 10)
|
||||
{
|
||||
memset(m_packet.data() + 4, 0xFF, 6); // Set dst to @ALL
|
||||
|
||||
// Recalculate CRC
|
||||
CM17CRC m17crc;
|
||||
// M17P packets also have a CRC at the end.
|
||||
// CRC is usually last 2 bytes.
|
||||
size_t len = m_packet.size();
|
||||
if (len >= 2)
|
||||
{
|
||||
uint16_t crc = htons(m17crc.CalcCRC(m_packet.data(), len - 2));
|
||||
memcpy(m_packet.data() + len - 2, &crc, 2);
|
||||
}
|
||||
|
||||
if (m_proto)
|
||||
m_proto->Send(m_packet, m_client->GetIp());
|
||||
}
|
||||
|
||||
m_state = EParrotState::done;
|
||||
}
|
||||
@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <atomic>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
|
||||
#include "Callsign.h"
|
||||
#include "Timer.h"
|
||||
#include "M17Client.h"
|
||||
#include "M17Packet.h"
|
||||
|
||||
enum class EParrotState { record, play, done };
|
||||
|
||||
class CM17Protocol;
|
||||
|
||||
class CParrot
|
||||
{
|
||||
public:
|
||||
CParrot(const CCallsign &src_addr, std::shared_ptr<CM17Client> spc, uint16_t ft, CM17Protocol *proto)
|
||||
: m_src(src_addr), m_client(spc), m_frameType(ft), m_state(EParrotState::record), m_proto(proto) {}
|
||||
virtual ~CParrot() { Quit(); }
|
||||
virtual void Add(const CBuffer &Buffer, uint16_t streamId, uint16_t frameNumber) = 0;
|
||||
virtual void AddPacket(const CBuffer &Buffer) = 0;
|
||||
virtual bool IsExpired() const = 0;
|
||||
virtual void Play() = 0;
|
||||
virtual bool IsStream() const = 0;
|
||||
EParrotState GetState() const { return m_state; }
|
||||
const CCallsign &GetSRC() const { return m_src; }
|
||||
void Quit() { if (m_fut.valid()) m_fut.get(); }
|
||||
|
||||
protected:
|
||||
const CCallsign m_src;
|
||||
std::shared_ptr<CM17Client> m_client;
|
||||
const uint16_t m_frameType;
|
||||
std::atomic<EParrotState> m_state;
|
||||
std::future<void> m_fut;
|
||||
CM17Protocol *m_proto;
|
||||
};
|
||||
|
||||
class CM17StreamParrot : public CParrot
|
||||
{
|
||||
public:
|
||||
CM17StreamParrot(const CCallsign &src_addr, std::shared_ptr<CM17Client> spc, uint16_t ft, CM17Protocol *proto);
|
||||
void Add(const CBuffer &Buffer, uint16_t streamId, uint16_t frameNumber) override;
|
||||
void AddPacket(const CBuffer &Buffer) override { (void)Buffer; }
|
||||
void Play() override;
|
||||
bool IsExpired() const override;
|
||||
bool IsStream() const override { return true; }
|
||||
|
||||
private:
|
||||
void playThread();
|
||||
|
||||
std::vector<std::vector<uint8_t>> m_data;
|
||||
bool m_is3200;
|
||||
CTimer m_lastHeard;
|
||||
uint16_t m_streamId;
|
||||
};
|
||||
|
||||
class CM17PacketParrot : public CParrot
|
||||
{
|
||||
public:
|
||||
CM17PacketParrot(const CCallsign &src_addr, std::shared_ptr<CM17Client> spc, uint16_t ft, CM17Protocol *proto);
|
||||
void Add(const CBuffer &Buffer, uint16_t streamId, uint16_t frameNumber) override { (void)Buffer; (void)streamId; (void)frameNumber; }
|
||||
void AddPacket(const CBuffer &Buffer) override;
|
||||
void Play() override;
|
||||
bool IsExpired() const override { return false; }
|
||||
bool IsStream() const override { return false; }
|
||||
|
||||
private:
|
||||
void playThread();
|
||||
|
||||
CBuffer m_packet;
|
||||
};
|
||||
Loading…
Reference in new issue