1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2026-06-01 21:54:55 -04:00

Add UDP port for packet forwarding in ChirpChat mod.

This commit is contained in:
Jon Beniston
2021-04-07 21:13:10 +01:00
parent 960af9157e
commit 2aab4cc2cb
10 changed files with 341 additions and 6 deletions
@@ -20,6 +20,8 @@
#include <QMutexLocker>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QUdpSocket>
#include <QNetworkDatagram>
#include <QBuffer>
#include <QThread>
@@ -52,7 +54,8 @@ ChirpChatMod::ChirpChatMod(DeviceAPI *deviceAPI) :
m_deviceAPI(deviceAPI),
m_currentPayloadTime(0.0),
m_settingsMutex(QMutex::Recursive),
m_sampleRate(48000)
m_sampleRate(48000),
m_udpSocket(nullptr)
{
setObjectName(m_channelId);
@@ -327,6 +330,27 @@ void ChirpChatMod::applySettings(const ChirpChatModSettings& settings, bool forc
}
}
if ((settings.m_udpEnabled != m_settings.m_udpEnabled) || force) {
reverseAPIKeys.append("udpEnabled");
}
if ((settings.m_udpAddress != m_settings.m_udpAddress) || force) {
reverseAPIKeys.append("udpAddress");
}
if ((settings.m_udpPort != m_settings.m_udpPort) || force) {
reverseAPIKeys.append("udpPort");
}
if ( (settings.m_udpEnabled != m_settings.m_udpEnabled)
|| (settings.m_udpAddress != m_settings.m_udpAddress)
|| (settings.m_udpPort != m_settings.m_udpPort)
|| force)
{
if (settings.m_udpEnabled)
openUDP(settings);
else
closeUDP();
}
if (m_settings.m_streamIndex != settings.m_streamIndex)
{
if (m_deviceAPI->getSampleMIMO()) // change of stream is possible for MIMO devices only
@@ -524,6 +548,15 @@ void ChirpChatMod::webapiUpdateChannelSettings(
if (channelSettingsKeys.contains("messageRepeat")) {
settings.m_messageRepeat = response.getChirpChatModSettings()->getMessageRepeat();
}
if (channelSettingsKeys.contains("udpEnabled")) {
settings.m_udpEnabled = response.getPacketDemodSettings()->getUdpEnabled();
}
if (channelSettingsKeys.contains("udpAddress")) {
settings.m_udpAddress = *response.getPacketDemodSettings()->getUdpAddress();
}
if (channelSettingsKeys.contains("udpPort")) {
settings.m_udpPort = response.getPacketDemodSettings()->getUdpPort();
}
if (channelSettingsKeys.contains("rgbColor")) {
settings.m_rgbColor = response.getChirpChatModSettings()->getRgbColor();
}
@@ -665,6 +698,10 @@ void ChirpChatMod::webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings&
bytesStr->push_back(new QString(tr("%1").arg(b, 2, 16, QChar('0'))));
}
response.getChirpChatModSettings()->setUdpEnabled(settings.m_udpEnabled);
response.getChirpChatModSettings()->setUdpAddress(new QString(settings.m_udpAddress));
response.getChirpChatModSettings()->setUdpPort(settings.m_udpPort);
response.getChirpChatModSettings()->setRgbColor(settings.m_rgbColor);
if (response.getChirpChatModSettings()->getTitle()) {
@@ -855,6 +892,16 @@ void ChirpChatMod::webapiFormatChannelSettings(
swgChirpChatModSettings->setMessageRepeat(settings.m_messageRepeat);
}
if (channelSettingsKeys.contains("udpEnabled") || force) {
swgChirpChatModSettings->setUdpEnabled(settings.m_udpEnabled);
}
if (channelSettingsKeys.contains("udpAddress") || force) {
swgChirpChatModSettings->setUdpAddress(new QString(settings.m_udpAddress));
}
if (channelSettingsKeys.contains("udpPort") || force) {
swgChirpChatModSettings->setUdpPort(settings.m_udpPort);
}
if (channelSettingsKeys.contains("rgbColor") || force) {
swgChirpChatModSettings->setRgbColor(settings.m_rgbColor);
}
@@ -903,3 +950,49 @@ bool ChirpChatMod::getModulatorActive() const
{
return m_basebandSource->getActive();
}
void ChirpChatMod::openUDP(const ChirpChatModSettings& settings)
{
closeUDP();
m_udpSocket = new QUdpSocket();
if (!m_udpSocket->bind(QHostAddress(settings.m_udpAddress), settings.m_udpPort))
qCritical() << "ChirpChatMod::openUDP: Failed to bind to port " << settings.m_udpAddress << ":" << settings.m_udpPort << ". Error: " << m_udpSocket->error();
else
qDebug() << "ChirpChatMod::openUDP: Listening for packets on " << settings.m_udpAddress << ":" << settings.m_udpPort;
connect(m_udpSocket, &QUdpSocket::readyRead, this, &ChirpChatMod::udpRx);
}
void ChirpChatMod::closeUDP()
{
if (m_udpSocket != nullptr)
{
disconnect(m_udpSocket, &QUdpSocket::readyRead, this, &ChirpChatMod::udpRx);
delete m_udpSocket;
m_udpSocket = nullptr;
}
}
void ChirpChatMod::udpRx()
{
while (m_udpSocket->hasPendingDatagrams())
{
QNetworkDatagram datagram = m_udpSocket->receiveDatagram();
ChirpChatModBaseband::MsgConfigureChirpChatModPayload *payloadMsg = nullptr;
std::vector<unsigned short> symbols;
m_encoder.encodeBytes(datagram.data(), symbols);
payloadMsg = ChirpChatModBaseband::MsgConfigureChirpChatModPayload::create(symbols);
if (payloadMsg)
{
m_basebandSource->getInputMessageQueue()->push(payloadMsg);
m_currentPayloadTime = (symbols.size()*(1<<m_settings.m_spreadFactor)*1000.0) / ChirpChatModSettings::bandwidths[m_settings.m_bandwidthIndex];
if (getMessageQueueToGUI())
{
MsgReportPayloadTime *rpt = MsgReportPayloadTime::create(m_currentPayloadTime);
getMessageQueueToGUI()->push(rpt);
}
}
}
}
@@ -35,6 +35,7 @@
class QNetworkAccessManager;
class QNetworkReply;
class QThread;
class QUdpSocket;
class DeviceAPI;
class CWKeyer;
class ChirpChatModBaseband;
@@ -159,6 +160,7 @@ private:
QNetworkAccessManager *m_networkManager;
QNetworkRequest m_networkRequest;
QUdpSocket *m_udpSocket;
void applySettings(const ChirpChatModSettings& settings, bool force = false);
void webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response);
@@ -175,9 +177,12 @@ private:
const ChirpChatModSettings& settings,
bool force
);
void openUDP(const ChirpChatModSettings& settings);
void closeUDP();
private slots:
void networkManagerFinished(QNetworkReply *reply);
void udpRx();
};
@@ -333,6 +333,24 @@ void ChirpChatModGUI::on_hexText_editingFinished()
applySettings();
}
void ChirpChatModGUI::on_udpEnabled_clicked(bool checked)
{
m_settings.m_udpEnabled = checked;
applySettings();
}
void ChirpChatModGUI::on_udpAddress_editingFinished()
{
m_settings.m_udpAddress = ui->udpAddress->text();
applySettings();
}
void ChirpChatModGUI::on_udpPort_editingFinished()
{
m_settings.m_udpPort = ui->udpPort->text().toInt();
applySettings();
}
void ChirpChatModGUI::onWidgetRolled(QWidget* widget, bool rollDown)
{
(void) widget;
@@ -498,6 +516,9 @@ void ChirpChatModGUI::displaySettings()
ui->repeatMessage->setValue(m_settings.m_messageRepeat);
ui->repeatText->setText(tr("%1").arg(m_settings.m_messageRepeat));
ui->msgType->setCurrentIndex((int) m_settings.m_messageType);
ui->udpEnabled->setChecked(m_settings.m_udpEnabled);
ui->udpAddress->setText(m_settings.m_udpAddress);
ui->udpPort->setText(QString::number(m_settings.m_udpPort));
blockApplySettings(false);
}
@@ -104,6 +104,9 @@ private slots:
void on_generateMessages_clicked(bool checked);
void on_messageText_editingFinished();
void on_hexText_editingFinished();
void on_udpEnabled_clicked(bool checked);
void on_udpAddress_editingFinished();
void on_udpPort_editingFinished();
void onWidgetRolled(QWidget* widget, bool rollDown);
void onMenuDialogCalled(const QPoint& p);
void tick();
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>402</width>
<height>373</height>
<height>461</height>
</rect>
</property>
<property name="minimumSize">
@@ -497,16 +497,16 @@
<widget class="QWidget" name="messageContainer" native="true">
<property name="geometry">
<rect>
<x>1</x>
<y>150</y>
<x>0</x>
<y>160</y>
<width>400</width>
<height>221</height>
<height>250</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>220</height>
<height>250</height>
</size>
</property>
<property name="windowTitle">
@@ -1168,6 +1168,103 @@
<string>Sync</string>
</property>
</widget>
<widget class="QCheckBox" name="udpEnabled">
<property name="geometry">
<rect>
<x>2</x>
<y>230</y>
<width>50</width>
<height>16</height>
</rect>
</property>
<property name="toolTip">
<string>Forward messages received via UDP</string>
</property>
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string>UDP</string>
</property>
</widget>
<widget class="QLineEdit" name="udpPort">
<property name="geometry">
<rect>
<x>199</x>
<y>230</y>
<width>50</width>
<height>16</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>50</width>
<height>16777215</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>UDP port to listen for messages to forward on</string>
</property>
<property name="inputMask">
<string>00000</string>
</property>
<property name="text">
<string>9997</string>
</property>
</widget>
<widget class="QLabel" name="udpSeparator">
<property name="geometry">
<rect>
<x>184</x>
<y>230</y>
<width>10</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>:</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="udpAddress">
<property name="geometry">
<rect>
<x>60</x>
<y>230</y>
<width>120</width>
<height>16</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>120</width>
<height>0</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>UDP address to listen for messages to forward on</string>
</property>
<property name="inputMask">
<string>000.000.000.000</string>
</property>
<property name="text">
<string>127.0.0.1</string>
</property>
</widget>
</widget>
</widget>
<customwidgets>
@@ -81,6 +81,9 @@ void ChirpChatModSettings::resetToDefaults()
m_syncWord = 0x34;
m_channelMute = false;
m_messageRepeat = 1;
m_udpEnabled = false;
m_udpAddress = "127.0.0.1";
m_udpPort = 9998;
m_rgbColor = QColor(255, 0, 255).rgb();
m_title = "ChirpChat Modulator";
m_streamIndex = 0;
@@ -189,6 +192,9 @@ QByteArray ChirpChatModSettings::serialize() const
s.writeU32(53, m_reverseAPIDeviceIndex);
s.writeU32(54, m_reverseAPIChannelIndex);
s.writeS32(55, m_streamIndex);
s.writeBool(56, m_udpEnabled);
s.writeString(57, m_udpAddress);
s.writeU32(58, m_udpPort);
return s.final();
}
@@ -279,6 +285,15 @@ bool ChirpChatModSettings::deserialize(const QByteArray& data)
m_reverseAPIChannelIndex = utmp > 99 ? 99 : utmp;
d.readS32(55, &m_streamIndex, 0);
d.readBool(56, &m_udpEnabled);
d.readString(57, &m_udpAddress, "127.0.0.1");
d.readU32(58, &utmp);
if ((utmp > 1023) && (utmp < 65535)) {
m_udpPort = utmp;
} else {
m_udpPort = 9998;
}
return true;
}
else
@@ -77,6 +77,9 @@ struct ChirpChatModSettings
QString m_textMessage;
QByteArray m_bytesMessage;
int m_messageRepeat;
bool m_udpEnabled;
QString m_udpAddress;
uint16_t m_udpPort;
uint32_t m_rgbColor;
QString m_title;
int m_streamIndex;