SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
kiwisdrworker.cpp
Go to the documentation of this file.
1 // Copyright (C) 2019 Vort //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include <boost/endian/conversion.hpp>
19 #include "kiwisdrworker.h"
20 
22  : QObject(),
23  m_timer(this),
24  m_sampleFifo(sampleFifo),
25  m_samplesBuf(),
26  m_centerFrequency(1450000),
27  m_gain(20),
28  m_useAGC(true),
29  m_status(0)
30 {
31  connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
32 
33  m_webSocket.setParent(this);
34  connect(&m_webSocket, &QWebSocket::connected,
36  connect(&m_webSocket, &QWebSocket::binaryMessageReceived,
38  connect(&m_webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
40  connect(&m_webSocket, &QWebSocket::disconnected,
42 }
43 
45 {
46  m_webSocket.sendTextMessage("SET auth t=kiwi p=#");
47 }
48 
50 {
51  qDebug("KiwiSDRWorker::onDisconnected");
52  m_status = 4;
53  emit updateStatus(4);
54 }
55 
56 void KiwiSDRWorker::onSocketError(QAbstractSocket::SocketError error)
57 {
58  m_status = 3;
59  emit updateStatus(3);
60 }
61 
63 {
64  if (!m_webSocket.isValid())
65  return;
66 
67  QString freq = QString::number(m_centerFrequency / 1000.0, 'f', 3);
68  QString msg = "SET mod=iq low_cut=-5980 high_cut=5980 freq=" + freq;
69  m_webSocket.sendTextMessage(msg);
70 }
71 
73 {
74  if (!m_webSocket.isValid())
75  return;
76 
77  QString msg("SET agc=");
78  msg.append(m_useAGC ? "1" : "0");
79  msg.append(" hang=0 thresh=-130 slope=6 decay=1000 manGain=");
80  msg.append(QString::number(m_gain));
81  m_webSocket.sendTextMessage(msg);
82 }
83 
84 void KiwiSDRWorker::onBinaryMessageReceived(const QByteArray &message)
85 {
86  if (message[0] == 'M' && message[1] == 'S' && message[2] == 'G')
87  {
88  QStringList al = QString::fromUtf8(message).split(' ');
89  if (al[1] == "audio_init=0" &&
90  al[2] == "audio_rate=12000")
91  {
92  m_webSocket.sendTextMessage("SET AR OK in=12000 out=48000");
93  m_webSocket.sendTextMessage("SERVER DE CLIENT KiwiAngel SND");
94  sendGain();
96  m_timer.start(5000);
97  m_status = 2;
98  emit updateStatus(2);
99  }
100  }
101  else if (message[0] == 'S' && message[1] == 'N' && message[2] == 'D')
102  {
103  int dataOffset = 20;
104  int sampleCount = 512;
105  const int16_t* messageSamples = (const int16_t*)(message.constData() + dataOffset);
106 
107  m_samplesBuf.clear();
108  for (int i = 0; i < sampleCount; i++)
109  {
110  m_samplesBuf.push_back(Sample(
111  boost::endian::endian_reverse(messageSamples[i * 2]) << (SDR_RX_SAMP_SZ - 16),
112  boost::endian::endian_reverse(messageSamples[i * 2 + 1]) << (SDR_RX_SAMP_SZ - 16)
113  ));
114  }
115 
116  m_sampleFifo->write(m_samplesBuf.begin(), m_samplesBuf.end());
117  }
118 }
119 
120 void KiwiSDRWorker::onCenterFrequencyChanged(quint64 centerFrequency)
121 {
122  if (m_centerFrequency == centerFrequency)
123  return;
124 
125  m_centerFrequency = centerFrequency;
127 }
128 
129 void KiwiSDRWorker::onGainChanged(quint32 gain, bool useAGC)
130 {
131  if (m_gain == gain && m_useAGC == useAGC)
132  return;
133 
134  m_gain = gain;
135  m_useAGC = useAGC;
136 
137  sendGain();
138 }
139 
140 void KiwiSDRWorker::onServerAddressChanged(QString serverAddress)
141 {
142  if (m_serverAddress == serverAddress)
143  return;
144  m_serverAddress = serverAddress;
145 
146  m_status = 1;
147  emit updateStatus(1);
148 
149  QString url("ws://");
150  url.append(m_serverAddress);
151  url.append("/kiwi/");
152  url.append(QString::number(QDateTime::currentMSecsSinceEpoch()));
153  url.append("/SND");
154  m_webSocket.open(QUrl(url));
155 }
156 
158 {
159  m_webSocket.sendTextMessage("SET keepalive");
160 }
short int16_t
Definition: rtptypes_win.h:43
void onCenterFrequencyChanged(quint64 centerFrequency)
QString m_serverAddress
Definition: kiwisdrworker.h:40
SampleVector m_samplesBuf
Definition: kiwisdrworker.h:37
void sendCenterFrequency()
uint write(const quint8 *data, uint count)
QWebSocket m_webSocket
Definition: kiwisdrworker.h:35
KiwiSDRWorker(SampleSinkFifo *sampleFifo)
uint32_t m_gain
Definition: kiwisdrworker.h:43
void onDisconnected()
#define SDR_RX_SAMP_SZ
Definition: dsptypes.h:32
uint64_t m_centerFrequency
Definition: kiwisdrworker.h:41
SampleSinkFifo * m_sampleFifo
Definition: kiwisdrworker.h:38
void onBinaryMessageReceived(const QByteArray &message)
void updateStatus(int status)
int32_t i
Definition: decimators.h:244
void onServerAddressChanged(QString serverAddress)
void onSocketError(QAbstractSocket::SocketError error)
void onGainChanged(quint32 gain, bool useAGC)
int m_status
See GUI for status number detail.
Definition: kiwisdrworker.h:46