mock interface now pretty completely simulates a real device

This commit is contained in:
Kevin Hester 2021-02-01 10:56:47 +08:00
parent a0160cadf9
commit 7d846461e4
7 changed files with 103 additions and 43 deletions

View file

@ -2,38 +2,109 @@ package com.geeksville.mesh.service
import com.geeksville.android.Logging
import com.geeksville.mesh.MeshProtos
import com.geeksville.mesh.Portnums
import com.geeksville.mesh.Position
import com.geeksville.mesh.R
import com.geeksville.mesh.model.getInitials
import com.google.protobuf.ByteString
import okhttp3.internal.toHexString
/** A simulated interface that is used for testing in the simulator */
class MockInterface(private val service: RadioInterfaceService) : Logging, IRadioInterface {
companion object : Logging {
val interfaceName = "m"
const val interfaceName = "m"
}
private var messageCount = 50
// an infinite sequence of ints
private val messageNumSequence = generateSequence { messageCount++ }.iterator()
init {
info("Starting the mock interface")
service.onConnect() // Tell clients they can use the API
}
override fun handleSendToRadio(b: ByteArray) {
val p = MeshProtos.ToRadio.parseFrom(b)
override fun handleSendToRadio(p: ByteArray) {
val pr = MeshProtos.ToRadio.parseFrom(p)
if (p.wantConfigId != 0)
sendConfigResponse(p.wantConfigId)
else
info("Ignoring data sent to mock interface $p")
when {
pr.wantConfigId != 0 -> sendConfigResponse(pr.wantConfigId)
pr.hasPacket() && pr.packet.wantAck -> sendFakeAck(pr)
else -> info("Ignoring data sent to mock interface $pr")
}
}
override fun close() {
info("Closing the mock interface")
}
/// Generate a fake text message from a node
private fun makeTextMessage(numIn: Int) =
MeshProtos.FromRadio.newBuilder().apply {
packet = MeshProtos.MeshPacket.newBuilder().apply {
id = messageNumSequence.next()
from = numIn
to = 0xffffffff.toInt() // ugly way of saying broadcast
rxTime = (System.currentTimeMillis() / 1000).toInt()
rxSnr = 1.5f
decoded = MeshProtos.SubPacket.newBuilder().apply {
data = MeshProtos.Data.newBuilder().apply {
portnum = Portnums.PortNum.TEXT_MESSAGE_APP
payload = ByteString.copyFromUtf8("This simulated node sends Hi!")
}.build()
}.build()
}.build()
}
private fun makeAck(fromIn: Int, toIn: Int, msgId: Int) =
MeshProtos.FromRadio.newBuilder().apply {
packet = MeshProtos.MeshPacket.newBuilder().apply {
id = messageNumSequence.next()
from = fromIn
to = toIn
rxTime = (System.currentTimeMillis() / 1000).toInt()
rxSnr = 1.5f
decoded = MeshProtos.SubPacket.newBuilder().apply {
data = MeshProtos.Data.newBuilder().apply {
successId = msgId
}.build()
}.build()
}.build()
}
/// Send a fake ack packet back if the sender asked for want_ack
private fun sendFakeAck(pr: MeshProtos.ToRadio) {
service.handleFromRadio(makeAck(pr.packet.to, pr.packet.from, pr.packet.id).build().toByteArray())
}
private fun sendConfigResponse(configId: Int) {
debug("Sending mock config response")
/// Generate a fake node info entry
fun makeNodeInfo(numIn: Int, lat: Double, lon: Double) =
MeshProtos.FromRadio.newBuilder().apply {
nodeInfo = MeshProtos.NodeInfo.newBuilder().apply {
num = numIn
user = MeshProtos.User.newBuilder().apply {
id = "!0x" + num.toHexString()
longName = "Sim " + num.toHexString()
shortName = getInitials(longName)
}.build()
position = MeshProtos.Position.newBuilder().apply {
latitudeI = Position.degI(lat)
longitudeI = Position.degI(lon)
batteryLevel = 42
altitude = 35
time = (System.currentTimeMillis() / 1000).toInt()
}.build()
}.build()
}
// Simulated network data to feed to our app
val MY_NODE = 0x42424242
val MY_NODE = 0x42424242
val packets = arrayOf(
// MyNodeInfo
MeshProtos.FromRadio.newBuilder().apply {
@ -56,40 +127,30 @@ class MockInterface(private val service: RadioInterfaceService) : Logging, IRadi
preferences = MeshProtos.RadioConfig.UserPreferences.newBuilder().apply {
region = MeshProtos.RegionCode.TW
// FIXME set critical times
// FIXME set critical times?
}.build()
channel = MeshProtos.ChannelSettings.newBuilder().apply {
// fixme() // fix channel display
// fix testlab // (application as GeeksvilleApplication).isInTestLab
// we just have an empty listing so that the default channel works
}.build()
}.build()
},
// Fake NodeDB
MeshProtos.FromRadio.newBuilder().apply {
nodeInfo = MeshProtos.NodeInfo.newBuilder().apply {
num = MY_NODE
user = MeshProtos.User.newBuilder().apply {
id = "!0x42424242"
longName = "Sim User"
shortName = "SU"
}.build()
position = MeshProtos.Position.newBuilder().apply {
latitudeI = 42
longitudeI = 42 // FIXME
time = 42 // FIXME
}.build()
}.build()
},
makeNodeInfo(MY_NODE, 32.776665, -96.796989), // dallas
makeNodeInfo(MY_NODE + 1, 32.960758, -96.733521), // richardson
MeshProtos.FromRadio.newBuilder().apply {
configCompleteId = configId
}
},
// Done with config response, now pretend to receive some text messages
makeTextMessage(MY_NODE + 1)
)
packets.forEach { p ->
service.handleFromRadio(p.build().toByteArray())
}
}
}
}

View file

@ -86,12 +86,16 @@ class RadioInterfaceService : Service(), Logging {
}
// If we are running on the emulator we default to the mock interface, so we can have some data to show to the user
if(address == null && isEmulator)
if(address == null && isMockInterfaceAvailable(context))
address = MockInterface.interfaceName
return address
}
/** return true if we should show the mock interface on this device
* (ie are we in an emulator or in testlab
*/
fun isMockInterfaceAvailable(context: Context) = isEmulator || ((context.applicationContext as GeeksvilleApplication).isInTestLab)
/** Like getDeviceAddress, but filtered to return only devices we are currently bonded with
*