2020-03-24 13:48:22 -07:00
|
|
|
package com.geeksville.mesh.model
|
|
|
|
|
|
|
|
|
|
import android.graphics.Bitmap
|
|
|
|
|
import android.net.Uri
|
|
|
|
|
import android.util.Base64
|
|
|
|
|
import com.geeksville.mesh.MeshProtos
|
2020-12-14 21:30:00 +08:00
|
|
|
import com.google.protobuf.ByteString
|
2020-03-24 13:48:22 -07:00
|
|
|
import com.google.zxing.BarcodeFormat
|
|
|
|
|
import com.google.zxing.MultiFormatWriter
|
|
|
|
|
import com.journeyapps.barcodescanner.BarcodeEncoder
|
|
|
|
|
import java.net.MalformedURLException
|
|
|
|
|
|
2020-06-12 20:38:43 -07:00
|
|
|
/** Utility function to make it easy to declare byte arrays - FIXME move someplace better */
|
|
|
|
|
fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) { pos -> ints[pos].toByte() }
|
|
|
|
|
|
2020-03-24 13:48:22 -07:00
|
|
|
|
|
|
|
|
data class Channel(
|
2020-04-09 16:33:42 -07:00
|
|
|
val settings: MeshProtos.ChannelSettings = MeshProtos.ChannelSettings.getDefaultInstance()
|
2020-03-24 13:48:22 -07:00
|
|
|
) {
|
|
|
|
|
companion object {
|
2020-06-12 20:26:10 -07:00
|
|
|
// Note: this string _SHOULD NOT BE LOCALIZED_ because it directly hashes to values used on the device for the default channel name.
|
|
|
|
|
val defaultChannelName = "Default"
|
|
|
|
|
|
2020-08-10 21:20:55 -04:00
|
|
|
// These bytes must match the well known and not secret bytes used the default channel AES128 key device code
|
2020-06-12 20:38:43 -07:00
|
|
|
val channelDefaultKey = byteArrayOfInts(
|
|
|
|
|
0xd4, 0xf1, 0xbb, 0x3a, 0x20, 0x29, 0x07, 0x59,
|
|
|
|
|
0xf0, 0xbc, 0xff, 0xab, 0xcf, 0x4e, 0x69, 0xbf
|
|
|
|
|
)
|
|
|
|
|
|
2020-03-24 13:48:22 -07:00
|
|
|
// Placeholder when emulating
|
|
|
|
|
val emulated = Channel(
|
2020-06-12 20:26:10 -07:00
|
|
|
MeshProtos.ChannelSettings.newBuilder().setName(defaultChannelName)
|
2020-03-24 13:48:22 -07:00
|
|
|
.setModemConfig(MeshProtos.ChannelSettings.ModemConfig.Bw125Cr45Sf128).build()
|
|
|
|
|
)
|
2020-11-17 19:54:48 +08:00
|
|
|
|
|
|
|
|
const val prefix = "https://www.meshtastic.org/c/#"
|
2020-03-24 13:48:22 -07:00
|
|
|
|
|
|
|
|
private const val base64Flags = Base64.URL_SAFE + Base64.NO_WRAP
|
|
|
|
|
|
|
|
|
|
private fun urlToSettings(url: Uri): MeshProtos.ChannelSettings {
|
|
|
|
|
val urlStr = url.toString()
|
2020-08-29 19:07:29 -07:00
|
|
|
|
2020-11-17 19:54:48 +08:00
|
|
|
// We no longer support the super old (about 0.8ish? verison of the URLs that don't use the # separator - I doubt
|
|
|
|
|
// anyone is still using that old format
|
|
|
|
|
val pathRegex = Regex("$prefix(.*)")
|
2020-03-24 13:48:22 -07:00
|
|
|
val (base64) = pathRegex.find(urlStr)?.destructured
|
2020-09-08 12:05:19 -07:00
|
|
|
?: throw MalformedURLException("Not a meshtastic URL: ${urlStr.take(40)}")
|
2020-03-24 13:48:22 -07:00
|
|
|
val bytes = Base64.decode(base64, base64Flags)
|
|
|
|
|
|
|
|
|
|
return MeshProtos.ChannelSettings.parseFrom(bytes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(url: Uri) : this(urlToSettings(url))
|
|
|
|
|
|
2020-12-14 21:30:00 +08:00
|
|
|
/// Return the name of our channel as a human readable string. If empty string, assume "Default" per mesh.proto spec
|
|
|
|
|
val name: String get() = if(settings.name.isEmpty()) defaultChannelName else settings.name
|
2020-04-09 16:33:42 -07:00
|
|
|
val modemConfig: MeshProtos.ChannelSettings.ModemConfig get() = settings.modemConfig
|
|
|
|
|
|
2020-12-14 21:30:00 +08:00
|
|
|
val psk get() = if(settings.psk.size() != 1)
|
|
|
|
|
settings.psk // A standard PSK
|
|
|
|
|
else {
|
|
|
|
|
// One of our special 1 byte PSKs, see mesh.proto for docs.
|
|
|
|
|
val pskIndex = settings.psk.byteAt(0).toInt()
|
|
|
|
|
|
|
|
|
|
if(pskIndex == 0)
|
|
|
|
|
ByteString.EMPTY // Treat as an empty PSK (no encryption)
|
|
|
|
|
else {
|
|
|
|
|
// Treat an index of 1 as the old channelDefaultKey and work up from there
|
|
|
|
|
val bytes = channelDefaultKey.clone()
|
|
|
|
|
bytes[bytes.size - 1] = (0xff and (bytes[bytes.size - 1] + pskIndex - 1)).toByte()
|
|
|
|
|
ByteString.copyFrom(bytes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 12:31:37 -07:00
|
|
|
/**
|
|
|
|
|
* Return a name that is formatted as #channename-suffix
|
|
|
|
|
*
|
|
|
|
|
* Where suffix indicates the hash of the PSK
|
|
|
|
|
*/
|
|
|
|
|
val humanName: String
|
|
|
|
|
get() {
|
2020-08-13 15:24:36 -07:00
|
|
|
val code = settings.psk.fold(0, { acc, x -> acc xor (x.toInt() and 0xff) })
|
2020-08-12 12:31:37 -07:00
|
|
|
return "#${settings.name}-${'A' + (code % 26)}"
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 13:48:22 -07:00
|
|
|
/// Can this channel be changed right now?
|
|
|
|
|
var editable = false
|
|
|
|
|
|
|
|
|
|
/// Return an URL that represents the current channel values
|
|
|
|
|
fun getChannelUrl(): Uri {
|
|
|
|
|
// If we have a valid radio config use it, othterwise use whatever we have saved in the prefs
|
|
|
|
|
|
2020-04-19 16:24:47 -07:00
|
|
|
val channelBytes = settings.toByteArray() ?: ByteArray(0) // if unset just use empty
|
2020-03-24 13:48:22 -07:00
|
|
|
val enc = Base64.encodeToString(channelBytes, base64Flags)
|
|
|
|
|
|
|
|
|
|
return Uri.parse("$prefix$enc")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun getChannelQR(): Bitmap {
|
|
|
|
|
val multiFormatWriter = MultiFormatWriter()
|
|
|
|
|
|
|
|
|
|
val bitMatrix =
|
|
|
|
|
multiFormatWriter.encode(getChannelUrl().toString(), BarcodeFormat.QR_CODE, 192, 192);
|
|
|
|
|
val barcodeEncoder = BarcodeEncoder()
|
|
|
|
|
return barcodeEncoder.createBitmap(bitMatrix)
|
|
|
|
|
}
|
|
|
|
|
}
|