| 
									
										
										
										
											2020-09-18 16:36:27 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2023-11-18 06:36:53 +01:00
										 |  |  | // Copyright (C) 2019-2021 Edouard Griffiths, F4EXB <f4exb06@gmail.com>          //
 | 
					
						
							|  |  |  | // Copyright (C) 2020-2021 Jon Beniston, M7RCE <jon@beniston.com>                //
 | 
					
						
							|  |  |  | // Copyright (C) 2022 Jiří Pinkava <jiri.pinkava@rossum.ai>                      //
 | 
					
						
							| 
									
										
										
										
											2020-09-18 16:36:27 +01:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is free software; you can redistribute it and/or modify          //
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by          //
 | 
					
						
							|  |  |  | // the Free Software Foundation as version 3 of the License, or                  //
 | 
					
						
							|  |  |  | // (at your option) any later version.                                           //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,               //
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of                //
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
 | 
					
						
							|  |  |  | // GNU General Public License V3 for more details.                               //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License             //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "dsp/upchannelizer.h"
 | 
					
						
							|  |  |  | #include "dsp/dspcommands.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "packetmodbaseband.h"
 | 
					
						
							|  |  |  | #include "packetmod.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MESSAGE_CLASS_DEFINITION(PacketModBaseband::MsgConfigurePacketModBaseband, Message) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-15 21:59:42 +02:00
										 |  |  | PacketModBaseband::PacketModBaseband() | 
					
						
							| 
									
										
										
										
											2020-09-18 16:36:27 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_sampleFifo.resize(SampleSourceFifo::getSizePolicy(48000)); | 
					
						
							|  |  |  |     m_channelizer = new UpChannelizer(&m_source); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     qDebug("PacketModBaseband::PacketModBaseband"); | 
					
						
							|  |  |  |     QObject::connect( | 
					
						
							|  |  |  |         &m_sampleFifo, | 
					
						
							|  |  |  |         &SampleSourceFifo::dataRead, | 
					
						
							|  |  |  |         this, | 
					
						
							|  |  |  |         &PacketModBaseband::handleData, | 
					
						
							|  |  |  |         Qt::QueuedConnection | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PacketModBaseband::~PacketModBaseband() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     delete m_channelizer; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PacketModBaseband::reset() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  |     m_sampleFifo.reset(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-21 02:30:29 +01:00
										 |  |  | void PacketModBaseband::setChannel(ChannelAPI *channel) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_source.setChannel(channel); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 16:36:27 +01:00
										 |  |  | void PacketModBaseband::pull(const SampleVector::iterator& begin, unsigned int nbSamples) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     unsigned int part1Begin, part1End, part2Begin, part2End; | 
					
						
							|  |  |  |     m_sampleFifo.read(nbSamples, part1Begin, part1End, part2Begin, part2End); | 
					
						
							|  |  |  |     SampleVector& data = m_sampleFifo.getData(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (part1Begin != part1End) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         std::copy( | 
					
						
							|  |  |  |             data.begin() + part1Begin, | 
					
						
							|  |  |  |             data.begin() + part1End, | 
					
						
							|  |  |  |             begin | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unsigned int shift = part1End - part1Begin; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (part2Begin != part2End) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         std::copy( | 
					
						
							|  |  |  |             data.begin() + part2Begin, | 
					
						
							|  |  |  |             data.begin() + part2End, | 
					
						
							|  |  |  |             begin + shift | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PacketModBaseband::handleData() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  |     SampleVector& data = m_sampleFifo.getData(); | 
					
						
							|  |  |  |     unsigned int ipart1begin; | 
					
						
							|  |  |  |     unsigned int ipart1end; | 
					
						
							|  |  |  |     unsigned int ipart2begin; | 
					
						
							|  |  |  |     unsigned int ipart2end; | 
					
						
							|  |  |  |     qreal rmsLevel, peakLevel; | 
					
						
							|  |  |  |     int numSamples; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unsigned int remainder = m_sampleFifo.remainder(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while ((remainder > 0) && (m_inputMessageQueue.size() == 0)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_sampleFifo.write(remainder, ipart1begin, ipart1end, ipart2begin, ipart2end); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (ipart1begin != ipart1end) { // first part of FIFO data
 | 
					
						
							|  |  |  |             processFifo(data, ipart1begin, ipart1end); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (ipart2begin != ipart2end) { // second part of FIFO data (used when block wraps around)
 | 
					
						
							|  |  |  |             processFifo(data, ipart2begin, ipart2end); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         remainder = m_sampleFifo.remainder(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_source.getLevels(rmsLevel, peakLevel, numSamples); | 
					
						
							|  |  |  |     emit levelChanged(rmsLevel, peakLevel, numSamples); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PacketModBaseband::processFifo(SampleVector& data, unsigned int iBegin, unsigned int iEnd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_channelizer->prefetch(iEnd - iBegin); | 
					
						
							|  |  |  |     m_channelizer->pull(data.begin() + iBegin, iEnd - iBegin); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PacketModBaseband::handleInputMessages() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Message* message; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while ((message = m_inputMessageQueue.pop()) != nullptr) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (handleMessage(*message)) { | 
					
						
							|  |  |  |             delete message; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool PacketModBaseband::handleMessage(const Message& cmd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (MsgConfigurePacketModBaseband::match(cmd)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  |         MsgConfigurePacketModBaseband& cfg = (MsgConfigurePacketModBaseband&) cmd; | 
					
						
							|  |  |  |         qDebug() << "PacketModBaseband::handleMessage: MsgConfigurePacketModBaseband"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         applySettings(cfg.getSettings(), cfg.getForce()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-14 22:26:02 +01:00
										 |  |  |     else if (PacketMod::MsgTx::match(cmd)) | 
					
						
							| 
									
										
										
										
											2020-09-18 16:36:27 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-14 22:26:02 +01:00
										 |  |  |         qDebug() << "PacketModBaseband::handleMessage: MsgTx"; | 
					
						
							|  |  |  |         m_source.addTXPacket(m_settings.m_callsign, m_settings.m_to, m_settings.m_via, m_settings.m_data); | 
					
						
							| 
									
										
										
										
											2020-09-18 16:36:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-07 21:06:00 +01:00
										 |  |  |     else if (PacketMod::MsgTXPacketBytes::match(cmd)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         PacketMod::MsgTXPacketBytes& tx = (PacketMod::MsgTXPacketBytes&) cmd; | 
					
						
							|  |  |  |         m_source.addTXPacket(tx.m_data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-21 07:48:39 +01:00
										 |  |  |     else if (PacketMod::MsgTXPacketData::match(cmd)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         PacketMod::MsgTXPacketData& tx = (PacketMod::MsgTXPacketData&) cmd; | 
					
						
							|  |  |  |         m_source.addTXPacket(tx.m_callsign, tx.m_to, tx.m_via, tx.m_data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-09-18 16:36:27 +01:00
										 |  |  |     else if (DSPSignalNotification::match(cmd)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  |         DSPSignalNotification& notif = (DSPSignalNotification&) cmd; | 
					
						
							|  |  |  |         qDebug() << "PacketModBaseband::handleMessage: DSPSignalNotification: basebandSampleRate: " << notif.getSampleRate(); | 
					
						
							|  |  |  |         m_sampleFifo.resize(SampleSourceFifo::getSizePolicy(notif.getSampleRate())); | 
					
						
							|  |  |  |         m_channelizer->setBasebandSampleRate(notif.getSampleRate()); | 
					
						
							|  |  |  |         m_source.applyChannelSettings(m_channelizer->getChannelSampleRate(), m_channelizer->getChannelFrequencyOffset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         qDebug() << "PacketModBaseband - Baseband got unknown message"; | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PacketModBaseband::applySettings(const PacketModSettings& settings, bool force) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if ((settings.m_inputFrequencyOffset != m_settings.m_inputFrequencyOffset) || force) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_channelizer->setChannelization(m_channelizer->getChannelSampleRate(), settings.m_inputFrequencyOffset); | 
					
						
							|  |  |  |         m_source.applyChannelSettings(m_channelizer->getChannelSampleRate(), m_channelizer->getChannelFrequencyOffset()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_source.applySettings(settings, force); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_settings = settings; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int PacketModBaseband::getChannelSampleRate() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_channelizer->getChannelSampleRate(); | 
					
						
							|  |  |  | } |