2024-11-26 08:38:12 -03:00
|
|
|
/*
|
2026-01-02 14:06:05 -06:00
|
|
|
* Copyright (c) 2025-2026 Meshtastic LLC
|
2024-11-26 08:38:12 -03:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2020-10-01 22:20:19 +02:00
|
|
|
package com.geeksville.mesh.service
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
2022-11-29 17:45:04 -03:00
|
|
|
import android.os.Parcelable
|
2025-12-28 08:30:15 -06:00
|
|
|
import co.touchlab.kermit.Logger
|
2025-09-25 17:22:14 -04:00
|
|
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
2025-09-24 11:43:46 -04:00
|
|
|
import org.meshtastic.core.model.DataPacket
|
|
|
|
|
import org.meshtastic.core.model.MessageStatus
|
|
|
|
|
import org.meshtastic.core.model.NodeInfo
|
2025-11-13 13:53:37 -06:00
|
|
|
import org.meshtastic.core.model.util.toPIIString
|
2025-09-30 16:55:56 -04:00
|
|
|
import org.meshtastic.core.service.ServiceRepository
|
2025-09-25 17:22:14 -04:00
|
|
|
import javax.inject.Inject
|
|
|
|
|
import javax.inject.Singleton
|
2020-10-01 22:20:19 +02:00
|
|
|
|
2025-09-25 17:22:14 -04:00
|
|
|
@Singleton
|
|
|
|
|
class MeshServiceBroadcasts
|
|
|
|
|
@Inject
|
|
|
|
|
constructor(
|
|
|
|
|
@ApplicationContext private val context: Context,
|
2026-01-02 11:13:38 -06:00
|
|
|
private val connectionStateHolder: ConnectionStateHandler,
|
2025-09-25 17:22:14 -04:00
|
|
|
private val serviceRepository: ServiceRepository,
|
2020-10-01 22:20:19 +02:00
|
|
|
) {
|
2025-09-25 17:22:14 -04:00
|
|
|
// A mapping of receiver class name to package name - used for explicit broadcasts
|
|
|
|
|
private val clientPackages = mutableMapOf<String, String>()
|
|
|
|
|
|
|
|
|
|
fun subscribeReceiver(receiverName: String, packageName: String) {
|
|
|
|
|
clientPackages[receiverName] = packageName
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 16:59:54 -05:00
|
|
|
/** Broadcast some received data Payload will be a DataPacket */
|
2020-10-01 22:20:19 +02:00
|
|
|
fun broadcastReceivedData(payload: DataPacket) {
|
2025-08-08 16:59:54 -05:00
|
|
|
explicitBroadcast(Intent(MeshService.actionReceived(payload.dataType)).putExtra(EXTRA_PAYLOAD, payload))
|
2020-10-01 22:20:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun broadcastNodeChange(info: NodeInfo) {
|
2025-12-28 08:30:15 -06:00
|
|
|
Logger.d { "Broadcasting node change ${info.user?.toPIIString()}" }
|
2026-01-02 11:13:38 -06:00
|
|
|
val intent = Intent(ACTION_NODE_CHANGE).putExtra(EXTRA_NODEINFO, info)
|
2020-10-01 22:20:19 +02:00
|
|
|
explicitBroadcast(intent)
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 19:19:13 -03:00
|
|
|
fun broadcastMessageStatus(p: DataPacket) = broadcastMessageStatus(p.id, p.status)
|
|
|
|
|
|
|
|
|
|
fun broadcastMessageStatus(id: Int, status: MessageStatus?) {
|
|
|
|
|
if (id == 0) {
|
2025-12-28 08:30:15 -06:00
|
|
|
Logger.d { "Ignoring anonymous packet status" }
|
2022-11-29 17:45:04 -03:00
|
|
|
} else {
|
|
|
|
|
// Do not log, contains PII possibly
|
2025-12-28 08:30:15 -06:00
|
|
|
// MeshService.Logger.d { "Broadcasting message status $p" }
|
2025-08-08 16:59:54 -05:00
|
|
|
val intent =
|
2026-01-02 11:13:38 -06:00
|
|
|
Intent(ACTION_MESSAGE_STATUS).apply {
|
2025-08-08 16:59:54 -05:00
|
|
|
putExtra(EXTRA_PACKET_ID, id)
|
|
|
|
|
putExtra(EXTRA_STATUS, status as Parcelable)
|
|
|
|
|
}
|
2022-11-29 17:45:04 -03:00
|
|
|
explicitBroadcast(intent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 16:59:54 -05:00
|
|
|
/** Broadcast our current connection status */
|
2020-10-01 22:20:19 +02:00
|
|
|
fun broadcastConnection() {
|
2025-11-17 15:15:22 -06:00
|
|
|
val connectionState = connectionStateHolder.connectionState.value
|
2026-01-02 11:13:38 -06:00
|
|
|
val intent = Intent(ACTION_MESH_CONNECTED).putExtra(EXTRA_CONNECTED, connectionState.toString())
|
2025-09-25 17:22:14 -04:00
|
|
|
serviceRepository.setConnectionState(connectionState)
|
2020-10-01 22:20:19 +02:00
|
|
|
explicitBroadcast(intent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* See com.geeksville.mesh broadcast intents.
|
|
|
|
|
*
|
|
|
|
|
* RECEIVED_OPAQUE for data received from other nodes
|
|
|
|
|
* NODE_CHANGE for new IDs appearing or disappearing
|
|
|
|
|
* ACTION_MESH_CONNECTED for losing/gaining connection to the packet radio
|
|
|
|
|
* Note: this is not the same as RadioInterfaceService.RADIO_CONNECTED_ACTION,
|
|
|
|
|
* because it implies we have assembled a valid node db.
|
|
|
|
|
*/
|
|
|
|
|
private fun explicitBroadcast(intent: Intent) {
|
2025-08-08 16:59:54 -05:00
|
|
|
context.sendBroadcast(
|
|
|
|
|
intent,
|
|
|
|
|
) // We also do a regular (not explicit broadcast) so any context-registered rceivers will work
|
2020-10-01 22:20:19 +02:00
|
|
|
clientPackages.forEach {
|
|
|
|
|
intent.setClassName(it.value, it.key)
|
|
|
|
|
context.sendBroadcast(intent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|