| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2023-11-19 06:43:20 +01:00
										 |  |  | // Copyright (C) 2018-2019, 2021 Edouard Griffiths, F4EXB <f4exb06@gmail.com>    //
 | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | // Remote sink channel (Rx) data blocks to read queue                            //
 | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | // SDRangel can serve as a remote SDR front end that handles the interface       //
 | 
					
						
							|  |  |  | // with a physical device and sends or receives the I/Q samples stream via UDP   //
 | 
					
						
							|  |  |  | // to or from another SDRangel instance or any program implementing the same     //
 | 
					
						
							|  |  |  | // protocol. The remote SDRangel is controlled via its Web REST API.             //
 | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02: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                  //
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:32:15 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | #include <channel/remotedatablock.h>
 | 
					
						
							|  |  |  | #include <channel/remotedatareadqueue.h>
 | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | const uint32_t RemoteDataReadQueue::MinimumMaxSize = 10; | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | RemoteDataReadQueue::RemoteDataReadQueue() : | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |         m_dataFrame(nullptr), | 
					
						
							| 
									
										
										
										
											2018-08-29 08:48:57 +02:00
										 |  |  |         m_maxSize(MinimumMaxSize), | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |         m_blockIndex(1), | 
					
						
							| 
									
										
										
										
											2018-08-28 14:04:30 +02:00
										 |  |  |         m_sampleIndex(0), | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |         m_sampleCount(0) | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | RemoteDataReadQueue::~RemoteDataReadQueue() | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |     RemoteDataFrame* data; | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-10 23:40:28 +01:00
										 |  |  |     while ((data = pop()) != nullptr) | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-02-03 01:52:11 +01:00
										 |  |  |         qDebug("RemoteDataReadQueue::~RemoteDataReadQueue: data block was still in queue"); | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |         delete data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  | void RemoteDataReadQueue::push(RemoteDataFrame* dataFrame) | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |     if (length() < m_maxSize) { | 
					
						
							|  |  |  |         m_dataReadQueue.enqueue(dataFrame); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-02-03 01:52:11 +01:00
										 |  |  |         qWarning("RemoteDataReadQueue::push: queue is full"); | 
					
						
							| 
									
										
										
										
											2018-08-28 14:04:30 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  | RemoteDataFrame* RemoteDataReadQueue::pop() | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |     if (m_dataReadQueue.isEmpty()) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-10 23:40:28 +01:00
										 |  |  |         return nullptr; | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_blockIndex = 1; | 
					
						
							|  |  |  |         m_sampleIndex = 0; | 
					
						
							| 
									
										
										
										
											2021-12-10 23:40:28 +01:00
										 |  |  |         return m_dataReadQueue.dequeue(); | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | void RemoteDataReadQueue::setSize(uint32_t size) | 
					
						
							| 
									
										
										
										
											2018-08-29 08:48:57 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (size != m_maxSize) { | 
					
						
							|  |  |  |         m_maxSize = size < MinimumMaxSize ? MinimumMaxSize : size; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  | void RemoteDataReadQueue::readSample(Sample& s, bool isTx) | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-28 14:04:30 +02:00
										 |  |  |     // depletion/repletion state
 | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |     if (m_dataFrame == nullptr) | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |         m_dataFrame = pop(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (m_dataFrame) | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |             qDebug("RemoteDataReadQueue::readSample: initial pop new frame: queue size: %u", length()); | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |             m_blockIndex = 1; | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |             m_sampleIndex = 0; | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  |             convertDataToSample(s, m_blockIndex, m_sampleIndex, isTx); | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |             m_sampleIndex++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             s = Sample{0, 0}; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 07:56:02 +01:00
										 |  |  |         m_sampleCount++; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |     int sampleSize = m_dataFrame->m_superBlocks[m_blockIndex].m_header.m_sampleBytes * 2; | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |     uint32_t samplesPerBlock = RemoteNbBytesPerBlock / sampleSize; | 
					
						
							| 
									
										
										
										
											2018-09-10 18:52:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_sampleIndex < samplesPerBlock) | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  |         convertDataToSample(s, m_blockIndex, m_sampleIndex, isTx); | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |         m_sampleIndex++; | 
					
						
							| 
									
										
										
										
											2018-08-30 01:56:53 +02:00
										 |  |  |         m_sampleCount++; | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_sampleIndex = 0; | 
					
						
							|  |  |  |         m_blockIndex++; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |         if (m_blockIndex < RemoteNbOrginalBlocks) | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  |             convertDataToSample(s, m_blockIndex, m_sampleIndex, isTx); | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |             m_sampleIndex++; | 
					
						
							| 
									
										
										
										
											2018-08-30 01:56:53 +02:00
										 |  |  |             m_sampleCount++; | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |             delete m_dataFrame; | 
					
						
							|  |  |  |             m_dataFrame = nullptr; | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |             m_dataFrame = pop(); | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |             if (m_dataFrame) | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |             { | 
					
						
							|  |  |  |                 m_blockIndex = 1; | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |                 m_sampleIndex = 0; | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  |                 convertDataToSample(s, m_blockIndex, m_sampleIndex, isTx); | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |                 m_sampleIndex++; | 
					
						
							| 
									
										
										
										
											2018-08-30 01:56:53 +02:00
										 |  |  |                 m_sampleCount++; | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |                 qWarning("RemoteDataReadQueue::readSample: try to pop new block but queue is empty"); | 
					
						
							| 
									
										
										
										
											2018-08-28 06:33:15 +02:00
										 |  |  |                 s = Sample{0, 0}; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |