| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2017 Edouard Griffiths, F4EXB                                   //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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 06:39:30 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <cstdio>
 | 
					
						
							|  |  |  | #include <cstring>
 | 
					
						
							|  |  |  | #include <regex>
 | 
					
						
							| 
									
										
										
										
											2021-05-30 08:23:40 +00:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  | #include <iio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-11 11:11:44 +01:00
										 |  |  | #include <QtGlobal>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-23 03:42:58 +02:00
										 |  |  | #include "deviceplutosdrbox.h"
 | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  | #include "deviceplutosdrscan.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DevicePlutoSDRScan::scan() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int i, num_contexts; | 
					
						
							|  |  |  |     struct iio_scan_context *scan_ctx; | 
					
						
							|  |  |  |     struct iio_context_info **info; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scan_ctx = iio_create_scan_context(0, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!scan_ctx) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-11-11 11:11:44 +01:00
										 |  |  |         qCritical("PlutoSDRScan::scan: could not create scan context"); | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     num_contexts = iio_scan_context_get_info_list(scan_ctx, &info); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (num_contexts < 0) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-11-11 11:11:44 +01:00
										 |  |  |         qCritical("PlutoSDRScan::scan: could not get contexts"); | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-19 23:11:03 +02:00
										 |  |  |     m_scans.clear(); | 
					
						
							| 
									
										
										
										
											2021-05-30 08:23:40 +00:00
										 |  |  |     m_scans.reserve(num_contexts); | 
					
						
							| 
									
										
										
										
											2017-09-19 23:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  |     for (i = 0; i < num_contexts; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         const char *description = iio_context_info_get_description(info[i]); | 
					
						
							|  |  |  |         const char *uri = iio_context_info_get_uri(info[i]); | 
					
						
							| 
									
										
										
										
											2017-09-23 03:42:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!DevicePlutoSDRBox::probeURI(std::string(uri))) { // continue if not accessible
 | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-11 11:11:44 +01:00
										 |  |  |         qDebug("PlutoSDRScan::scan: %d: %s [%s]", i, description, uri); | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  |         char *pch = strstr(const_cast<char*>(description), "PlutoSDR"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (pch) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2021-05-30 08:23:40 +00:00
										 |  |  |             // As device scan is used across multiple vectors it's best to use a
 | 
					
						
							|  |  |  |             // managed pointer, as to keep track of when it's safe to delete.
 | 
					
						
							|  |  |  |             std::shared_ptr<DeviceScan> dev_scan = std::make_shared<DeviceScan>( | 
					
						
							|  |  |  |                 DeviceScan({ | 
					
						
							|  |  |  |                     std::string(description), | 
					
						
							|  |  |  |                     std::string("TBD"), | 
					
						
							|  |  |  |                     std::string(uri) | 
					
						
							|  |  |  |                 })); | 
					
						
							|  |  |  |             m_scans.push_back(dev_scan); | 
					
						
							|  |  |  |             m_urilMap[m_scans.back()->m_uri] = m_scans.back(); | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             std::regex desc_regex(".*serial=(.+)"); | 
					
						
							|  |  |  |             std::smatch desc_match; | 
					
						
							| 
									
										
										
										
											2021-05-30 08:23:40 +00:00
										 |  |  |             std::regex_search(m_scans.back()->m_name, desc_match, desc_regex); | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (desc_match.size() == 2) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2021-05-30 08:23:40 +00:00
										 |  |  |                 m_scans.back()->m_serial = desc_match[1]; | 
					
						
							|  |  |  |                 m_serialMap[m_scans.back()->m_serial] = m_scans.back(); | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     iio_context_info_list_free(info); | 
					
						
							|  |  |  |     iio_scan_context_destroy(scan_ctx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const std::string* DevicePlutoSDRScan::getURIAt(unsigned int index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (index < m_scans.size()) { | 
					
						
							| 
									
										
										
										
											2021-05-30 08:23:40 +00:00
										 |  |  |         return &(m_scans[index]->m_uri); | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const std::string* DevicePlutoSDRScan::getSerialAt(unsigned int index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (index < m_scans.size()) { | 
					
						
							| 
									
										
										
										
											2021-05-30 08:23:40 +00:00
										 |  |  |         return &(m_scans[index]->m_serial); | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const std::string* DevicePlutoSDRScan::getURIFromSerial( | 
					
						
							|  |  |  |         const std::string& serial) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-30 08:23:40 +00:00
										 |  |  |     std::map<std::string, std::shared_ptr<DeviceScan>>::const_iterator it = m_serialMap.find(serial); | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  |     if (it == m_serialMap.end()) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         return &((it->second)->m_uri); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DevicePlutoSDRScan::getSerials(std::vector<std::string>& serials) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-30 08:23:40 +00:00
										 |  |  |     std::vector<std::shared_ptr<DeviceScan>>::const_iterator it = m_scans.begin(); | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  |     serials.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (; it != m_scans.end(); ++it) { | 
					
						
							| 
									
										
										
										
											2021-05-30 08:23:40 +00:00
										 |  |  |         serials.push_back((*it)->m_serial); | 
					
						
							| 
									
										
										
										
											2017-09-02 04:21:41 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-17 19:54:13 +02:00
										 |  |  | void DevicePlutoSDRScan::enumOriginDevices(const QString& hardwareId, PluginInterface::OriginDevices& originDevices) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     scan(); | 
					
						
							|  |  |  |     std::vector<std::string> serials; | 
					
						
							|  |  |  |     getSerials(serials); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::vector<std::string>::const_iterator it = serials.begin(); | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; it != serials.end(); ++it, ++i) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	    QString serial_str = QString::fromLocal8Bit(it->c_str()); | 
					
						
							|  |  |  | 	    QString displayableName(QString("PlutoSDR[%1] %2").arg(i).arg(serial_str)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         originDevices.append(PluginInterface::OriginDevice( | 
					
						
							|  |  |  |             displayableName, | 
					
						
							|  |  |  |             hardwareId, | 
					
						
							|  |  |  |             serial_str, | 
					
						
							|  |  |  |             i, // sequence
 | 
					
						
							|  |  |  |             1, // Nb Rx
 | 
					
						
							|  |  |  |             1  // Nb Tx
 | 
					
						
							|  |  |  |         )); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         qDebug("DevicePlutoSDRScan::enumOriginDevices: enumerated PlutoSDR device #%d", i); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |