mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 13:25:21 +00:00
direct peer detection by annouce TTL
This commit is contained in:
@@ -85,10 +85,6 @@ class BluetoothConnectionManager(
|
|||||||
// Public property for address-peer mapping
|
// Public property for address-peer mapping
|
||||||
val addressPeerMap get() = connectionTracker.addressPeerMap
|
val addressPeerMap get() = connectionTracker.addressPeerMap
|
||||||
|
|
||||||
// Expose first-announce helpers to higher layers
|
|
||||||
fun noteAnnounceReceived(address: String) { connectionTracker.noteAnnounceReceived(address) }
|
|
||||||
fun hasSeenFirstAnnounce(address: String): Boolean = connectionTracker.hasSeenFirstAnnounce(address)
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
powerManager.delegate = this
|
powerManager.delegate = this
|
||||||
// Observe debug settings to enforce role state while active
|
// Observe debug settings to enforce role state while active
|
||||||
|
|||||||
@@ -30,8 +30,6 @@ class BluetoothConnectionTracker(
|
|||||||
private val connectedDevices = ConcurrentHashMap<String, DeviceConnection>()
|
private val connectedDevices = ConcurrentHashMap<String, DeviceConnection>()
|
||||||
private val subscribedDevices = CopyOnWriteArrayList<BluetoothDevice>()
|
private val subscribedDevices = CopyOnWriteArrayList<BluetoothDevice>()
|
||||||
val addressPeerMap = ConcurrentHashMap<String, String>()
|
val addressPeerMap = ConcurrentHashMap<String, String>()
|
||||||
// Track whether we have seen the first ANNOUNCE on a given device connection
|
|
||||||
private val firstAnnounceSeen = ConcurrentHashMap<String, Boolean>()
|
|
||||||
|
|
||||||
// RSSI tracking from scan results (for devices we discover but may connect as servers)
|
// RSSI tracking from scan results (for devices we discover but may connect as servers)
|
||||||
private val scanRSSI = ConcurrentHashMap<String, Int>()
|
private val scanRSSI = ConcurrentHashMap<String, Int>()
|
||||||
@@ -93,8 +91,6 @@ class BluetoothConnectionTracker(
|
|||||||
Log.d(TAG, "Tracker: Adding device connection for $deviceAddress (isClient: ${deviceConn.isClient}")
|
Log.d(TAG, "Tracker: Adding device connection for $deviceAddress (isClient: ${deviceConn.isClient}")
|
||||||
connectedDevices[deviceAddress] = deviceConn
|
connectedDevices[deviceAddress] = deviceConn
|
||||||
pendingConnections.remove(deviceAddress)
|
pendingConnections.remove(deviceAddress)
|
||||||
// Mark as awaiting first ANNOUNCE on this connection
|
|
||||||
firstAnnounceSeen[deviceAddress] = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -323,7 +319,6 @@ class BluetoothConnectionTracker(
|
|||||||
subscribedDevices.removeAll { it.address == deviceAddress }
|
subscribedDevices.removeAll { it.address == deviceAddress }
|
||||||
addressPeerMap.remove(deviceAddress)
|
addressPeerMap.remove(deviceAddress)
|
||||||
}
|
}
|
||||||
firstAnnounceSeen.remove(deviceAddress)
|
|
||||||
Log.d(TAG, "Cleaned up device connection for $deviceAddress")
|
Log.d(TAG, "Cleaned up device connection for $deviceAddress")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,21 +352,6 @@ class BluetoothConnectionTracker(
|
|||||||
addressPeerMap.clear()
|
addressPeerMap.clear()
|
||||||
pendingConnections.clear()
|
pendingConnections.clear()
|
||||||
scanRSSI.clear()
|
scanRSSI.clear()
|
||||||
firstAnnounceSeen.clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mark that we have received the first ANNOUNCE over this device connection.
|
|
||||||
*/
|
|
||||||
fun noteAnnounceReceived(deviceAddress: String) {
|
|
||||||
firstAnnounceSeen[deviceAddress] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check whether the first ANNOUNCE has been seen for a device connection.
|
|
||||||
*/
|
|
||||||
fun hasSeenFirstAnnounce(deviceAddress: String): Boolean {
|
|
||||||
return firstAnnounceSeen[deviceAddress] == true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -464,21 +464,24 @@ class BluetoothMeshService(private val context: Context) {
|
|||||||
// Process the announce
|
// Process the announce
|
||||||
val isFirst = messageHandler.handleAnnounce(routed)
|
val isFirst = messageHandler.handleAnnounce(routed)
|
||||||
|
|
||||||
// Map device address -> peerID on first announce seen over this device connection
|
// Map device address -> peerID based on TTL (max TTL = direct neighbor)
|
||||||
|
// Matches iOS logic: any announce with max TTL on a link defines the direct peer
|
||||||
val deviceAddress = routed.relayAddress
|
val deviceAddress = routed.relayAddress
|
||||||
val pid = routed.peerID
|
val pid = routed.peerID
|
||||||
if (deviceAddress != null && pid != null) {
|
if (deviceAddress != null && pid != null) {
|
||||||
// First ANNOUNCE over a device connection defines a direct neighbor.
|
// Check if this is a direct connection (MAX TTL)
|
||||||
if (!connectionManager.hasSeenFirstAnnounce(deviceAddress)) {
|
// Note: packet.ttl is UByte, compare with AppConstants.MESSAGE_TTL_HOPS
|
||||||
|
val isDirect = routed.packet.ttl == com.bitchat.android.util.AppConstants.MESSAGE_TTL_HOPS
|
||||||
|
|
||||||
|
if (isDirect) {
|
||||||
// Bind or rebind this device address to the announcing peer
|
// Bind or rebind this device address to the announcing peer
|
||||||
connectionManager.addressPeerMap[deviceAddress] = pid
|
connectionManager.addressPeerMap[deviceAddress] = pid
|
||||||
connectionManager.noteAnnounceReceived(deviceAddress)
|
Log.d(TAG, "Mapped device $deviceAddress to peer $pid (TTL=${routed.packet.ttl})")
|
||||||
Log.d(TAG, "Mapped device $deviceAddress to peer $pid on FIRST-ANNOUNCE for this connection")
|
|
||||||
|
|
||||||
// Mark as directly connected (upgrades from routed if needed)
|
// Mark as directly connected
|
||||||
try { peerManager.setDirectConnection(pid, true) } catch (_: Exception) { }
|
try { peerManager.setDirectConnection(pid, true) } catch (_: Exception) { }
|
||||||
|
|
||||||
// Initial sync for this newly direct peer
|
// Initial sync for this direct peer
|
||||||
try { gossipSyncManager.scheduleInitialSyncToPeer(pid, 1_000) } catch (_: Exception) { }
|
try { gossipSyncManager.scheduleInitialSyncToPeer(pid, 1_000) } catch (_: Exception) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ Timers:
|
|||||||
- Added a `DeviceMonitoringManager` instance and provided a `disconnectCallback` that:
|
- Added a `DeviceMonitoringManager` instance and provided a `disconnectCallback` that:
|
||||||
- disconnects client GATT connections via `BluetoothConnectionTracker`.
|
- disconnects client GATT connections via `BluetoothConnectionTracker`.
|
||||||
- cancels server connections via `BluetoothGattServer.cancelConnection`.
|
- cancels server connections via `BluetoothGattServer.cancelConnection`.
|
||||||
- Exposed `noteAnnounceReceived(address)` as a small helper for higher layers.
|
|
||||||
- Updated `componentDelegate.onPacketReceived` to notify per-device activity to the monitor.
|
- Updated `componentDelegate.onPacketReceived` to notify per-device activity to the monitor.
|
||||||
|
|
||||||
2) GATT Client
|
2) GATT Client
|
||||||
@@ -59,7 +58,6 @@ Timers:
|
|||||||
|
|
||||||
4) ANNOUNCE Binding
|
4) ANNOUNCE Binding
|
||||||
- File: `BluetoothMeshService.kt` (in the ANNOUNCE handler where we first map device → peer)
|
- File: `BluetoothMeshService.kt` (in the ANNOUNCE handler where we first map device → peer)
|
||||||
- After mapping a device address to a peer on first verified ANNOUNCE, call `connectionManager.noteAnnounceReceived(address)` to cancel the 15s timer for that device.
|
|
||||||
|
|
||||||
## Behavior Summary
|
## Behavior Summary
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user