Skip to content

Conversation

@X9X0
Copy link
Owner

@X9X0 X9X0 commented Nov 21, 2025

Overview
Complete refactoring of the Pi Discovery feature to run client-side with improved reliability, debugging capabilities, and network compatibility.

Major Changes
🔄 Architecture Changes

Client-side scanning: Pi Discovery now runs from the client machine instead of requiring connection to a Pi server
UI reorganization: Moved from main window tab to Tools menu dialog for better UX
🎛️ Enhanced Functionality

Configurable scan parameters: UI controls for timeout (1-10s) and batch size (1-50 hosts)
Verbose debug logging: Comprehensive per-host logging showing ping, MAC lookup, hostname resolution, and LabLink probing
Command-line debug flag: --debug flag for both client and server
🎉 Easter Egg Feature

Hidden debug mode: 7-click activation on launcher header
Funny discovery message with visual feedback
Automatically passes --debug to both server and client processes
🐛 Bug Fixes

MAC lookup reliability: Fixed arp command path issues by using ip neigh with fallback to full paths (/usr/sbin/arp, /sbin/arp)
MAC masquerading support: Fallback LabLink probing for hosts where routers hide real Pi MAC addresses in ARP cache
Now discovers Pis even when gateway presents its own MAC instead of the device's actual MAC
Technical Details
Async network scanning using asyncio
Cross-platform support (Windows/Linux/Mac)
Identifies Pis by MAC vendor prefixes: B8:27:EB, DC:A6:32, E4:5F:01, D8:3A:DD, 28:CD:C1
LabLink detection via HTTP probing on port 8000 (/api, /health, /api/system/version)
Batch processing to avoid overwhelming networks
Testing
Tested on network with MAC masquerading (WiFi router hiding device MACs) - successfully discovers all Pis including those with obscured MAC addresses.

Major changes:
- Move Pi Discovery scanning logic from server to client-side
- Create new client/utils/pi_discovery.py module for local network scanning
- Convert Pi Discovery from a tab to a Tools menu dialog
- Remove dependency on server connection for Pi discovery

Benefits:
- Pi Discovery now runs on the client machine's network
- No longer requires connection to a Pi to discover other Pis
- Accessible via Tools > Discover Raspberry Pis menu item
- More intuitive workflow for initial setup

Technical details:
- New PiDiscovery class in client/utils/pi_discovery.py handles async scanning
- Cross-platform support (Windows/Linux/Mac) for ping and arp commands
- Updated PiDiscoveryDialog (formerly PiDiscoveryPanel) to use local scanning
- Removed Pi Discovery tab from main window
- Added menu item under Tools menu
Changes:
- Add detailed debug logging throughout Pi discovery process
  - Log each step: ping, MAC lookup, hostname lookup, Pi identification
  - Log batch progress during scanning
  - Use warning level for errors with stack traces
- Add configurable scan parameters in UI
  - Timeout setting (1-10 seconds, default 3s)
  - Batch size setting (1-50 hosts, default 10)
  - Input validation with user-friendly error messages
- Reduce default batch size from 20 to 10 for more reliable scanning
- Increase default timeout from 2s to 3s for slower networks

These changes will help diagnose why some Pis are missed during scans
and allow users to tune parameters for their network conditions.
Add command-line argument support to enable DEBUG level logging:
- New --debug flag enables detailed logging output
- Helps diagnose Pi discovery issues by showing per-host scan details
- Debug logs include: ping results, MAC lookups, hostname resolution,
  Pi identification method, and LabLink probe results

Usage: python3 -m client.main --debug
Easter egg features:
- Click the header "LabLink System Launcher" 7 times rapidly (within 2 seconds)
- Unlocks debug mode with a funny congratulatory message 🎉
- Header changes to red background and shows "[DEBUG MODE]"
- Server and client will launch with --debug flag for verbose logging
- Click 7 more times to disable debug mode

Changes:
- lablink.py: Add click tracking and Easter egg handler to header
  - _header_clicked() method detects 7 rapid clicks
  - Shows humorous message: "Well well well... looks like someone found the secret button! 🕵️"
  - Visual feedback: header turns red when debug mode enabled
  - Passes --debug flag to server and client launches
- server/main.py: Add --debug command-line argument support
  - Enables debug logging when --debug flag is provided
  - Overrides settings.debug configuration
- client/main.py: Already has --debug flag support

This provides a fun, discoverable way to enable verbose logging without
cluttering the UI with debug checkboxes.
Problem: Pi discovery was failing to identify Pis because MAC address
lookups were failing with "No such file or directory: 'arp'"

Root cause: The 'arp' command is in /usr/sbin/arp on Ubuntu/Debian,
which is not in the regular user PATH.

Solution:
- Try modern 'ip neigh' command first (located at /usr/sbin/ip)
  - Parse output format: "IP dev DEVICE lladdr MAC STATE"
- Fall back to arp command with multiple path attempts:
  - /usr/sbin/arp (Ubuntu/Debian)
  - /sbin/arp (some systems)
  - arp (if it's in PATH)

This fix allows Pi discovery to properly identify Raspberry Pis by their
MAC addresses, which was previously failing for all hosts.

Debug logs showed:
- 10.10.0.42 was being pinged successfully
- But MAC lookup failed with FileNotFoundError
- No hostname was available either
- Therefore couldn't identify as Pi and was skipped

With this fix, MAC addresses should be retrieved successfully and Pis
will be properly identified.
Some routers/gateways use MAC masquerading for WiFi clients, presenting
their own MAC address in the ARP table instead of the real device MAC.
This caused Pis to be missed during discovery even when reachable.

Changes:
- Now probe ALL reachable hosts for LabLink, not just those with Pi MACs
- Only skip hosts if they're not identified as Pi AND not running LabLink
- Log "MAC masquerading detected" when LabLink found on unidentified hosts

This ensures Pis running LabLink are always discovered, regardless of
whether the router hides their real MAC address in the ARP cache.

Fixes issue where Pi at 10.10.0.42 (real MAC: E4:5F:01:3B:89:75) was
missed because ARP showed router MAC (78:9A:18:6B:15:A9) instead.
@X9X0 X9X0 changed the title feat: Refactor Pi Discovery to run on client machine Refactor Pi Discovery to client-side with enhanced debugging and MAC masquerading support Nov 21, 2025
@X9X0 X9X0 merged commit 7f80035 into main Nov 21, 2025
25 checks passed
@X9X0 X9X0 deleted the claude/change-pi-discovery-011KLMo5Vpt3mvfqzwTnhzZ4 branch November 21, 2025 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants