| 
									
										
										
										
											2017-11-18 05:06:43 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2017 F4EXB                                                      //
 | 
					
						
							|  |  |  | // written by Edouard Griffiths                                                  //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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.                                           //
 | 
					
						
							| 
									
										
										
										
											2017-11-18 05:06:43 +01: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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QCommandLineOption>
 | 
					
						
							|  |  |  | #include <QRegExpValidator>
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "mainparser.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MainParser::MainParser() : | 
					
						
							|  |  |  |     m_serverAddressOption(QStringList() << "a" << "api-address", | 
					
						
							|  |  |  |         "Web API server address.", | 
					
						
							|  |  |  |         "address", | 
					
						
							| 
									
										
										
										
											2021-11-01 09:51:50 +00:00
										 |  |  |         ""), | 
					
						
							| 
									
										
										
										
											2017-11-18 05:06:43 +01:00
										 |  |  |     m_serverPortOption(QStringList() << "p" << "api-port", | 
					
						
							|  |  |  |         "Web API server port.", | 
					
						
							|  |  |  |         "port", | 
					
						
							| 
									
										
										
										
											2020-03-11 18:19:56 +01:00
										 |  |  |         "8091"), | 
					
						
							|  |  |  |     m_fftwfWisdomOption(QStringList() << "w" << "fftwf-wisdom", | 
					
						
							|  |  |  |         "FFTW Wisdom file.", | 
					
						
							|  |  |  |         "file", | 
					
						
							|  |  |  |         "") | 
					
						
							| 
									
										
										
										
											2017-11-18 05:06:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-01 09:51:50 +00:00
										 |  |  |     m_serverAddress = "";   // Bind to any address
 | 
					
						
							| 
									
										
										
										
											2017-11-19 23:58:49 +01:00
										 |  |  |     m_serverPort = 8091; | 
					
						
							| 
									
										
										
										
											2019-06-14 16:58:09 +02:00
										 |  |  |     m_mimoSupport = false; | 
					
						
							| 
									
										
										
										
											2020-03-11 18:19:56 +01:00
										 |  |  |     m_fftwfWindowFileName = ""; | 
					
						
							| 
									
										
										
										
											2017-11-18 05:06:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_parser.setApplicationDescription("Software Defined Radio application"); | 
					
						
							|  |  |  |     m_parser.addHelpOption(); | 
					
						
							|  |  |  |     m_parser.addVersionOption(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_parser.addOption(m_serverAddressOption); | 
					
						
							|  |  |  |     m_parser.addOption(m_serverPortOption); | 
					
						
							| 
									
										
										
										
											2020-03-11 18:19:56 +01:00
										 |  |  |     m_parser.addOption(m_fftwfWisdomOption); | 
					
						
							| 
									
										
										
										
											2017-11-18 05:06:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MainParser::~MainParser() | 
					
						
							|  |  |  | { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MainParser::parse(const QCoreApplication& app) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_parser.process(app); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int pos; | 
					
						
							|  |  |  |     bool ok; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // server address
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QString serverAddress = m_parser.value(m_serverAddressOption); | 
					
						
							| 
									
										
										
										
											2021-11-01 09:51:50 +00:00
										 |  |  |     if (!serverAddress.isEmpty()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         QString ipRange = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])"; | 
					
						
							|  |  |  |         QRegExp ipRegex ("^" + ipRange | 
					
						
							|  |  |  |                          + "\\." + ipRange | 
					
						
							|  |  |  |                          + "\\." + ipRange | 
					
						
							|  |  |  |                          + "\\." + ipRange + "$"); | 
					
						
							|  |  |  |         QRegExpValidator ipValidator(ipRegex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (ipValidator.validate(serverAddress, pos) == QValidator::Acceptable) { | 
					
						
							|  |  |  |             m_serverAddress = serverAddress; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             qWarning() << "MainParser::parse: server address invalid. Defaulting to any address."; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-18 05:06:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // server port
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QString serverPortStr = m_parser.value(m_serverPortOption); | 
					
						
							|  |  |  |     int serverPort = serverPortStr.toInt(&ok); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ok && (serverPort > 1023) && (serverPort < 65536)) { | 
					
						
							|  |  |  |         m_serverPort = serverPort; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         qWarning() << "MainParser::parse: server port invalid. Defaulting to " << m_serverPort; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-12 10:25:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-14 06:40:38 +01:00
										 |  |  |     // FFTWF wisdom file
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_fftwfWindowFileName = m_parser.value(m_fftwfWisdomOption); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-21 23:19:17 +01:00
										 |  |  |     // MIMO - from version
 | 
					
						
							| 
									
										
										
										
											2019-05-12 10:25:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-21 23:19:17 +01:00
										 |  |  |     QStringList versionParts = app.applicationVersion().split("."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (versionParts.size() > 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         bool ok; | 
					
						
							|  |  |  |         int maj = versionParts.at(0).toInt(&ok); | 
					
						
							| 
									
										
										
										
											2020-11-12 22:45:20 +01:00
										 |  |  |         m_mimoSupport = ok && (maj > 4); | 
					
						
							| 
									
										
										
										
											2019-12-21 23:19:17 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_mimoSupport = false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-18 05:06:43 +01:00
										 |  |  | } |