| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2020 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                  //
 | 
					
						
							|  |  |  | // (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 <QEventLoop>
 | 
					
						
							|  |  |  | #include <QTimer>
 | 
					
						
							| 
									
										
										
										
											2021-08-08 13:19:39 +02:00
										 |  |  | #include <QNetworkInterface>
 | 
					
						
							|  |  |  | #include <QSet>
 | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "devicemetisscan.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DeviceMetisScan::scan() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_scans.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  |     if (m_udpSocket.bind(QHostAddress::AnyIPv4, 10001, QUdpSocket::ShareAddress)) | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         connect(&m_udpSocket, SIGNAL(readyRead()), this, SLOT(readyRead())); | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         qDebug() << "DeviceMetisScan::scan: cannot bind socket"; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unsigned char buffer[63]; | 
					
						
							|  |  |  |     buffer[0] = (unsigned char) 0xEF; | 
					
						
							|  |  |  |     buffer[1] = (unsigned char) 0XFE; | 
					
						
							|  |  |  |     buffer[2] = (unsigned char) 0x02; | 
					
						
							|  |  |  |     std::fill(&buffer[3], &buffer[63], 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-08 13:19:39 +02:00
										 |  |  |     QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); | 
					
						
							|  |  |  |     QSet<QHostAddress> broadcastAddrs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < ifaces.size(); i++) | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-08-08 13:19:39 +02:00
										 |  |  |         QList<QNetworkAddressEntry> addrs = ifaces[i].addressEntries(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (int j = 0; j < addrs.size(); j++) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if ((addrs[j].ip().protocol() == QAbstractSocket::IPv4Protocol) && (addrs[j].broadcast().toString() != "")) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 QHostAddress br = addrs[j].broadcast(); | 
					
						
							|  |  |  |                 broadcastAddrs.insert(br); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-08-08 13:19:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     QSet<QHostAddress>::const_iterator brIt = broadcastAddrs.begin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (; brIt != broadcastAddrs.end(); ++brIt) | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-08-08 13:19:39 +02:00
										 |  |  |         if (m_udpSocket.writeDatagram((const char*) buffer, sizeof(buffer), *brIt, 1024) < 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             qDebug("DeviceMetisScan::scan: discovery writeDatagram to %s failed: %s ", | 
					
						
							|  |  |  |                 qPrintable(brIt->toString()), qPrintable(m_udpSocket.errorString())); | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             qDebug("DeviceMetisScan::scan: discovery writeDatagram sent to %s", qPrintable(brIt->toString())); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-08 13:19:39 +02:00
										 |  |  |     // wait for timeout before returning
 | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     QEventLoop loop; | 
					
						
							|  |  |  |     QTimer *timer = new QTimer(this); | 
					
						
							|  |  |  |     connect(timer, SIGNAL(timeout()), &loop, SLOT(quit())); | 
					
						
							| 
									
										
										
										
											2021-08-08 13:19:39 +02:00
										 |  |  |     timer->start(500); // 500 ms timeout
 | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     qDebug() << "DeviceMetisScan::scan: start 0.5 second timeout loop"; | 
					
						
							|  |  |  |     // Execute the event loop here and wait for the timeout to trigger
 | 
					
						
							|  |  |  |     // which in turn will trigger event loop quit.
 | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  |     loop.exec(); | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     disconnect(&m_udpSocket, SIGNAL(readyRead()), this, SLOT(readyRead())); | 
					
						
							|  |  |  |     m_udpSocket.close(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DeviceMetisScan::enumOriginDevices(const QString& hardwareId, PluginInterface::OriginDevices& originDevices) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     scan(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < m_scans.size(); i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         const DeviceScan& deviceInfo = m_scans.at(i); | 
					
						
							|  |  |  |         QString serial = QString("%1:%2_%3").arg(deviceInfo.m_address.toString()).arg(deviceInfo.m_port).arg(deviceInfo.m_serial); | 
					
						
							|  |  |  |         QString displayableName(QString("Metis[%1] %2").arg(i).arg(serial)); | 
					
						
							|  |  |  |         originDevices.append(PluginInterface::OriginDevice( | 
					
						
							|  |  |  |             displayableName, | 
					
						
							|  |  |  |             hardwareId, | 
					
						
							|  |  |  |             serial, | 
					
						
							|  |  |  |             i, // sequence
 | 
					
						
							|  |  |  |             8, // Nb Rx
 | 
					
						
							|  |  |  |             1  // Nb Tx
 | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  |         )); | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  | const DeviceMetisScan::DeviceScan* DeviceMetisScan::getDeviceAt(int index) const | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (index < m_scans.size()) { | 
					
						
							|  |  |  |         return &m_scans.at(index); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DeviceMetisScan::getSerials(QList<QString>& serials) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (int i = 0; i < m_scans.size(); i++) { | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  |         serials.append(m_scans.at(i).m_serial); | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DeviceMetisScan::readyRead() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QHostAddress metisAddress; | 
					
						
							|  |  |  |     quint16 metisPort; | 
					
						
							|  |  |  |     unsigned char buffer[1024]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  |     if (m_udpSocket.readDatagram((char*) &buffer, (qint64) sizeof(buffer), &metisAddress, &metisPort) < 0) | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         qDebug() << "DeviceMetisScan::readyRead: readDatagram failed " << m_udpSocket.errorString(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  |     QString metisIP = QString("%1:%2").arg(metisAddress.toString()).arg(metisPort); | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  |     if (buffer[0] == 0xEF && buffer[1] == 0xFE) | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  |         switch(buffer[2]) | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |         { | 
					
						
							|  |  |  |             case 3:  // reply
 | 
					
						
							|  |  |  |                 // should not happen on this port
 | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 2:  // response to a discovery packet
 | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 QByteArray array((char *) &buffer[3], 6); | 
					
						
							|  |  |  |                 QString serial = QString(array.toHex()); | 
					
						
							|  |  |  |                 m_scans.append(DeviceScan( | 
					
						
							|  |  |  |                     serial, | 
					
						
							|  |  |  |                     metisAddress, | 
					
						
							|  |  |  |                     metisPort | 
					
						
							|  |  |  |                 )); | 
					
						
							|  |  |  |                 m_serialMap.insert(serial, &m_scans.back()); | 
					
						
							|  |  |  |                 qDebug() << "DeviceMetisScan::readyRead: found Metis at:" << metisIP << "MAC:" << serial; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 1:  // a data packet
 | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-11-11 10:10:04 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         qDebug() << "DeviceMetisScan::readyRead: received invalid response to discovery"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-14 22:08:06 +01:00
										 |  |  | } |