Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scanning for elrs network devices with crsf msp frame capability #2922

Merged
merged 3 commits into from Sep 17, 2022

Conversation

freasy
Copy link
Member

@freasy freasy commented May 13, 2022

In future ELRS there is CRSF MSP support.

This contains 2 parts because a elrs rx can connect to home wifi or start a access point mode

  1. home wifi
    in this mode the elrs rx responds to mdns requests and sends 3 responses (A, TXT, SRV), with those i can make a valid tcp port
  2. ap mode
    in the latest ELRS master - the mdns is fixed, so i rely on that there too

To check if the connection is still valid we have a "down" event from the mdns browser, but thats not realy reliable, so i poll if a http connection can be made every 5 seconds, this check is skipped, when the GUI is connected to a device.

@CapnBry
Copy link

CapnBry commented May 13, 2022

I've mentioned this in Discord but I'll drop it here for the Betaflight folks' info. This should not be using the hostname as the key (which can change). It should use the mDNS TXT records returned from the query vendor = elrs and type = rx to determine if the target is suitable.

@freasy
Copy link
Member Author

freasy commented May 14, 2022

I've mentioned this in Discord but I'll drop it here for the Betaflight folks' info. This should not be using the hostname as the key (which can change). It should use the mDNS TXT records returned from the query vendor = elrs and type = rx to determine if the target is suitable.

Yes, I will implement that once we sorted AP mode out 👍

@freasy freasy marked this pull request as ready for review May 14, 2022 22:47
self.elrs_lock = false;
}
});
}, 2000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems rather high. This function is already called every 500ms. So it has to fit into this time window.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to flood the network. My second idea was to enable AP mode scanning my hand and don't have it on all the time. How do you think about that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be a better approach.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping is removed

self.elrs_lock = true;

setTimeout(() => {
ping.sys.probe('10.0.0.1', function (isAlive) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should define a constant for 10.0.0.1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping is removed

@blckmn
Copy link
Member

blckmn commented May 15, 2022

AUTOMERGE: (FAIL)

  • github identifies PR as mergeable -> FAIL
  • assigned to a milestone -> PASS
  • cooling off period lapsed -> PASS
  • commit count less or equal to three -> PASS
  • Don't merge label NOT found -> PASS
  • at least one RN: label found -> PASS
  • Tested label found -> PASS
  • assigned to an approver -> PASS
  • approver count at least three -> FAIL

@CapnBry
Copy link

CapnBry commented May 15, 2022

While I can't vouch for anything regarding the appropriateness of how this fits into the configurator's scanning pattern, I can say that the ELRS detection from the mDNS responses gets my full approval. This old incorrect comment should be removed though since it no longer does this

// check if any of the answers on the response have a name that starts with
// 'elrs_', which indicates that it is an elers device

@freasy
Copy link
Member Author

freasy commented May 16, 2022

i refactored to another package which is now event based, so no timer anymore.

With the current ELRS master, the need of pinging a constant is obsolete.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@sonarcloud
Copy link

sonarcloud bot commented May 19, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@github-actions

This comment has been minimized.

Copy link
Member

@limonspb limonspb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package-lock.json probably should not be added to the PR?
Please also make sure you have only 1 commit per PR (squash, amend etc)

@howels
Copy link

howels commented Jun 25, 2022

expressLRS v3 RC is approaching rapidly, would be good to get this included in BF configurator so WiFi connection can be tested easily.

@haslinghuis
Copy link
Member

@freasy please rebase and squash

@freasy freasy force-pushed the feature/mdns_check_and_resolve branch 3 times, most recently from 425f98f to 66521c3 Compare June 27, 2022 06:42
@freasy
Copy link
Member Author

freasy commented Jun 27, 2022

package-lock.json probably should not be added to the PR? Please also make sure you have only 1 commit per PR (squash, amend etc)

i removed the lock file

@freasy please rebase and squash

i rebased current master and squashed to 1 commit

the android part still needs testing - i couldnt do the test, because remote chrome debug wasnt catching the bf app's chromium

@github-actions

This comment has been minimized.

@sugaarK
Copy link
Member

sugaarK commented Jun 27, 2022

@KarateBrot @daleckystepan wana has a look at this

@haslinghuis
Copy link
Member

haslinghuis commented Jun 27, 2022

Checked out the branch but it's not working (Hangs at loading as PortHandler can't be initialized)
Second just running in development it produces changes to yarn.lock which should be included in the PR.

@@ -44,6 +142,10 @@ PortHandler.check = function () {
self.check_serial_devices();
}

self.check_elrs_devices();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this function defined?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this changes restores PortHandler to normal operation. Did not test ELRS functionality:

diff --git a/src/js/port_handler.js b/src/js/port_handler.js
index a9230135..dab965bb 100644
--- a/src/js/port_handler.js
+++ b/src/js/port_handler.js
@@ -1,40 +1,5 @@
 'use strict';
 
-const http = require("http");
-const bonjour = require('bonjour')();
-
-const MDNS_INTERVAL = 10000;
-const TCP_CHECK_INTERVAL = 5000;
-const TCP_TIMEOUT = 2000;
-
-let mdnsBrowser = null;
-if (GUI.isCordova()) {
-    mdnsBrowser = {
-        services: [],
-    };
-
-    const zeroconf = cordova.plugins.zeroconf;
-    zeroconf.registerAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
-    zeroconf.watchAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
-    zeroconf.watch("_http._tcp.", "local.").subscribe(result => {
-        const action = result.action;
-        const service = result.service;
-        if (['added', 'resolved'].includes(action)) {
-            console.log("Zeroconf Service Changed", service);
-            mdnsBrowser.services.push({
-                addresses: service.ipv4Addresses,
-                txt: service.txtRecord,
-                fqdn: service.hostname,
-            });
-        } else {
-            console.log("Zeroconf Service Removed", service);
-            mdnsBrowser.services = mdnsBrowser.services.filter(s => s.fqdn !== service.hostname);
-        }
-    });
-} else {
-    bonjour.find({ type: 'http' });
-}
-
 const TIMEOUT_CHECK = 500 ; // With 250 it seems that it produces a memory leak and slowdown in some versions, reason unknown
 
 const usbDevices = { filters: [
@@ -50,6 +15,7 @@ const PortHandler = new function () {
     this.port_available = false;
     this.showAllSerialDevices = false;
     this.showVirtualMode = false;
+    this.mdnsBrowser = null;
 };
 
 PortHandler.initialize = function () {
@@ -66,7 +32,41 @@ PortHandler.initialize = function () {
 
     const self = this;
 
-    mdnsBrowser?.on('down', function (service) {
+    const http = require("http");
+    const bonjour = require('bonjour')();
+    
+    const MDNS_INTERVAL = 10000;
+    const TCP_CHECK_INTERVAL = 5000;
+    const TCP_TIMEOUT = 2000;
+    
+    if (GUI.isCordova()) {
+        self.mdnsBrowser = {
+            services: [],
+        };
+    
+        const zeroconf = cordova.plugins.zeroconf;
+        zeroconf.registerAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
+        zeroconf.watchAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
+        zeroconf.watch("_http._tcp.", "local.").subscribe(result => {
+            const action = result.action;
+            const service = result.service;
+            if (['added', 'resolved'].includes(action)) {
+                console.log("Zeroconf Service Changed", service);
+                self.mdnsBrowser.services.push({
+                    addresses: service.ipv4Addresses,
+                    txt: service.txtRecord,
+                    fqdn: service.hostname,
+                });
+            } else {
+                console.log("Zeroconf Service Removed", service);
+                self.mdnsBrowser.services = self.mdnsBrowser.services.filter(s => s.fqdn !== service.hostname);
+            }
+        });
+    } else {
+        bonjour.find({ type: 'http' });
+    }
+
+    self.mdnsBrowser?.on('down', function (service) {
         console.log('Lost an HTTP server:', service);
         let currentPorts = [];
         if (self.initialPorts && self.initialPorts.length > 0) {
@@ -86,10 +86,10 @@ PortHandler.initialize = function () {
                 const tcpPorts = self.initialPorts.filter(p => p.path.startsWith('tcp://'));
                 tcpPorts.forEach(function (port) {
                     const removePort = () => {
-                        mdnsBrowser?._removeService(port.fqdn);
+                        self.mdnsBrowser?._removeService(port.fqdn);
 
                         if (GUI.isCordova()) {
-                            mdnsBrowser.services = mdnsBrowser.services.filter(s => s.fqdn !== port.fqdn);
+                            self.mdnsBrowser.services = self.mdnsBrowser.services.filter(s => s.fqdn !== port.fqdn);
                         }
                     };
                     http.get({
@@ -123,7 +123,7 @@ PortHandler.initialize = function () {
 
     self.mdns_timer = setInterval(() => {
         if (!GUI.connected_to) {
-            mdnsBrowser?.update();
+            self.mdnsBrowser?.update();
         }
     }, MDNS_INTERVAL);
 
@@ -158,7 +158,7 @@ PortHandler.check_serial_devices = function () {
 
         let currentPorts = [
             ...cp,
-            ...(mdnsBrowser?.services?.filter(s => s.txt.vendor === 'elrs' && s.txt.type === 'rx')
+            ...(self.mdnsBrowser?.services?.filter(s => s.txt.vendor === 'elrs' && s.txt.type === 'rx')
                 .map(s => s.addresses.map(a => ({
                     path: `tcp://${a}`,
                     displayName: `${s.txt.target} - ${s.txt.version}`,

@freasy freasy requested a review from limonspb September 9, 2022 13:24
@github-actions

This comment has been minimized.

src/js/serial_backend.js Outdated Show resolved Hide resolved
As suggested

Co-authored-by: haslinghuis <mark@numloq.nl>
@freasy freasy requested review from haslinghuis and chmelevskij and removed request for limonspb and haslinghuis September 10, 2022 11:25
@sonarcloud
Copy link

sonarcloud bot commented Sep 10, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@github-actions
Copy link
Contributor

Do you want to test this code? Here you have an automated build:
Betaflight-Configurator-Android
Betaflight-Configurator-Linux
Betaflight-Configurator-macOS
Betaflight-Configurator-Windows
WARNING: It may be unstable and result in corrupted configurations or data loss. Use only for testing!

@freasy freasy requested review from limonspb and removed request for chmelevskij September 11, 2022 06:58
const action = result.action;
const service = result.service;
if (['added', 'resolved'].includes(action)) {
console.log("Zeroconf Service Changed", service);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totaly, i will change it, so that those logs go to the GUI as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change in follow up patch.

Copy link
Member

@chmelevskij chmelevskij left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the question about the GUI.logs.

@haslinghuis haslinghuis moved this from Configurator to Ready to merge in Finalizing Firmware 4.4 Release Sep 14, 2022
@howels
Copy link

howels commented Sep 14, 2022

Tested on Android and it works. For some reason it wouldn't show the FC on the first launch but after Force-Stopping and restarting the app it's working so perhaps it just takes a second to do the mDNS lookup.

@freasy
Copy link
Member Author

freasy commented Sep 15, 2022

@howels

Tested on Android and it works. For some reason it wouldn't show the FC on the first launch but after Force-Stopping and restarting the app it's working so perhaps it just takes a second to do the mDNS lookup.

yes mDNS is a b**** specially on android, so if it works sometimes, thats already a big win! I am glad, that it works, since i could not test the code myself.

@CapnBry
Copy link

CapnBry commented Sep 15, 2022

Don't know if you're looking for more "Me too"s, but I just installed the artifact on my Samsung Galaxy S10e and it discovered my ELRS RX no problem on my home wifi network, then switched over to put the RX into AP mode and it discovered properly on that network as well.

My crap eyes thank you for not making me mistype tcp://10.0.01 by mistake 100x before realizing the gray on gray text is missing a pixel!

@haslinghuis haslinghuis moved this from Ready to merge to Configurator in Finalizing Firmware 4.4 Release Sep 15, 2022
@haslinghuis haslinghuis moved this from Configurator to Ready to merge in Finalizing Firmware 4.4 Release Sep 17, 2022
@blckmn blckmn merged commit 8813538 into betaflight:master Sep 17, 2022
Finalizing Firmware 4.4 Release automation moved this from Ready to merge to Closed Sep 17, 2022
@ASDosjani
Copy link
Contributor

ASDosjani commented Oct 15, 2022

@freasy I tested it ony lastest nightlies and the autobuild from this PR, it's working on my phone but not on my pc. I'm using win11. The tcp port is showing up for 1-2 sec but after it dissapears. I can connect to it manually but the auto detection is broken.

This is the error message:
image

It happens after the TCP_CHECK_INTERVAL expired and it checks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

10 participants