Skip to content

feat: Implement KMP ServiceDiscovery for TCP devices#4854

Merged
jamesarich merged 7 commits intomainfrom
feat/nsd
Mar 19, 2026
Merged

feat: Implement KMP ServiceDiscovery for TCP devices#4854
jamesarich merged 7 commits intomainfrom
feat/nsd

Conversation

@jamesarich
Copy link
Copy Markdown
Collaborator

This pull request refactors the network discovery and monitoring logic to use a multiplatform architecture, introducing common interfaces and implementations for both Android and JVM (desktop) targets. It adds new abstractions for service discovery and network monitoring, and provides platform-specific implementations for each. The changes also update dependency management and documentation to reflect these architectural improvements.

Multiplatform abstractions and interface additions:

  • Introduced new common interfaces: NetworkMonitor, ServiceDiscovery, and updated NetworkRepository to depend on these abstractions, allowing for platform-specific implementations while sharing contracts across platforms. (core/network/src/commonMain/kotlin/org/meshtastic/core/network/repository/NetworkMonitor.kt [1] core/network/src/commonMain/kotlin/org/meshtastic/core/network/repository/ServiceDiscovery.kt [2] core/network/src/commonMain/kotlin/org/meshtastic/core/network/repository/NetworkRepository.kt [3]

  • Added a new multiplatform data class DiscoveredService to represent discovered network services in a platform-agnostic way. (core/network/src/commonMain/kotlin/org/meshtastic/core/network/repository/DiscoveredService.kt core/network/src/commonMain/kotlin/org/meshtastic/core/network/repository/DiscoveredService.ktR1-R24)

Platform-specific implementations:

  • Added Android implementations: AndroidNetworkMonitor and AndroidServiceDiscovery, which use ConnectivityManager and NsdManager respectively to provide the new interfaces. (core/network/src/androidMain/kotlin/org/meshtastic/core/network/repository/AndroidNetworkMonitor.kt [1] core/network/src/androidMain/kotlin/org/meshtastic/core/network/repository/AndroidServiceDiscovery.kt [2]
  • Added JVM/desktop implementations: JvmNetworkMonitor (always online) and JvmServiceDiscovery (using JmDNS for mDNS service discovery). (core/network/src/jvmMain/kotlin/org/meshtastic/core/network/repository/JvmNetworkMonitor.kt [1] core/network/src/jvmMain/kotlin/org/meshtastic/core/network/repository/JvmServiceDiscovery.kt [2]

Refactoring and dependency injection:

  • Refactored NetworkRepository into NetworkRepositoryImpl, which now depends on the new interfaces rather than platform-specific classes, and moved it to commonMain. Updated dependency injection annotations to bind the interface. (core/network/src/commonMain/kotlin/org/meshtastic/core/network/repository/NetworkRepositoryImpl.kt [1] [2] [3] [4] [5]

Android service discovery improvements:

  • Improved the NsdManager.serviceList implementation for Android, adding robust error handling, timeouts, and backoff for service resolution, and ensuring thread safety with a mutex. (core/network/src/androidMain/kotlin/org/meshtastic/core/network/repository/NsdManager.kt [1] [2]

Dependency and documentation updates:

  • Added new dependencies for lifecycle management and JmDNS to support the new architecture. (core/network/build.gradle.kts core/network/build.gradle.ktsR51-R58)
  • Updated documentation to reflect the addition of the new feature module and the multiplatform status. (docs/kmp-status.md [1] [2]

- Migrated NsdManager to use Kotlin Channels and Mutex to prevent race conditions on Android.
- Abstracted NetworkRepository and NetworkMonitor to commonMain.
- Added JmDNS dependency for Desktop mDNS TCP discovery.
- Wired up live TCP device discovery in Desktop connections screen.
@github-actions github-actions bot added the enhancement New feature or request label Mar 19, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 19, 2026

Codecov Report

❌ Patch coverage is 30.17241% with 81 lines in your changes missing coverage. Please review.
✅ Project coverage is 10.43%. Comparing base (a5d3914) to head (765662f).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...tic/core/network/repository/JvmServiceDiscovery.kt 0.00% 36 Missing ⚠️
...g/meshtastic/core/network/repository/NsdManager.kt 0.00% 18 Missing ⚠️
...omain/usecase/CommonGetDiscoveredDevicesUseCase.kt 76.31% 1 Missing and 8 partials ⚠️
...c/core/network/repository/NetworkRepositoryImpl.kt 0.00% 5 Missing ⚠️
...core/network/repository/AndroidServiceDiscovery.kt 0.00% 4 Missing ⚠️
...c/core/network/repository/AndroidNetworkMonitor.kt 0.00% 3 Missing ⚠️
...astic/core/network/repository/NetworkRepository.kt 50.00% 1 Missing and 1 partial ⚠️
...astic/core/network/repository/JvmNetworkMonitor.kt 0.00% 2 Missing ⚠️
...main/usecase/AndroidGetDiscoveredDevicesUseCase.kt 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4854      +/-   ##
==========================================
+ Coverage   10.13%   10.43%   +0.29%     
==========================================
  Files         545      551       +6     
  Lines       18095    18190      +95     
  Branches     2697     2715      +18     
==========================================
+ Hits         1834     1898      +64     
- Misses      16081    16097      +16     
- Partials      180      195      +15     
Flag Coverage Δ
host-unit 10.43% <30.17%> (+0.29%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jamesarich jamesarich added this pull request to the merge queue Mar 19, 2026
Merged via the queue into main with commit b982b14 Mar 19, 2026
8 checks passed
@jamesarich jamesarich deleted the feat/nsd branch March 19, 2026 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant