Skip to content

Commit

Permalink
sensor: fix ping360helper using the answering ip instead of ip in the…
Browse files Browse the repository at this point in the history
… answer
  • Loading branch information
Williangalvani committed Jun 10, 2023
1 parent cc70609 commit c6cd156
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/sensor/ping360asciiprotocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,36 @@ QByteArray Ping360AsciiProtocol::discoveryMessage() { return {"Discovery"}; }
Ping360DiscoveryResponse Ping360AsciiProtocol::decodeDiscoveryResponse(const QString& response)
{
auto lines = response.split(QStringLiteral("\r\n"));
QString ipAddress;

for (auto& line : lines) {
line = line.trimmed();
if (line.startsWith("IP Address:- ")) {
ipAddress = line.remove("IP Address:- ");
line = "IP Address:- " + Ping360AsciiProtocol::removeLeadingZerosFromIP(ipAddress);
}
};

if (lines.size() == Ping360DiscoveryResponse::numberOfLines) {
return {lines[0], lines[1], lines[2], lines[3]};
QStringList splitted = lines[3].split(":- ");
if (splitted.size() < 2) {
qWarning() << "Invalid format in line[3], ':- ' not found.";
return {};
}
return {lines[0], lines[1], lines[2], splitted[1]};
}

return {};
}

QString Ping360AsciiProtocol::removeLeadingZerosFromIP(const QString& line) {
QStringList ipParts = line.split(".");
for (int i = 0; i < ipParts.size(); i++) {
ipParts[i] = QString::number(ipParts[i].toInt());
}
return ipParts.join(".");
}

QByteArray Ping360AsciiProtocol::staticIpAddressMessage(const QString& address)
{
union Ipv4Union {
Expand Down
2 changes: 2 additions & 0 deletions src/sensor/ping360asciiprotocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Ping360AsciiProtocol : public QObject {
*/
static Ping360DiscoveryResponse decodeDiscoveryResponse(const QString& response);

static QString removeLeadingZerosFromIP(const QString& line);

/**
* @brief Get UDP port for this communication protocol
*
Expand Down
2 changes: 1 addition & 1 deletion src/sensor/ping360helperservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void Ping360HelperService::processBroadcastResponses()
const Ping360DiscoveryResponse decoded = Ping360AsciiProtocol::decodeDiscoveryResponse(datagram.data());
if (decoded.deviceName.contains("PING360")) {
emit availableLinkFound(
{{LinkType::Udp, {sender.toString(), "12345"}, "Ping360 Port", PingDeviceType::PING360}},
{{LinkType::Udp, {decoded.ipAddress, "12345"}, "Ping360 Port", PingDeviceType::PING360}},
QStringLiteral("Ping360 Ethernet Protocol Detector"));
} else {
qCWarning(PING360HELPERSERVICE) << "Invalid message:" << datagram.data();
Expand Down

0 comments on commit c6cd156

Please sign in to comment.