From f32266083dcf9126e07854f14864fb6bbc51064d Mon Sep 17 00:00:00 2001 From: James Rich <2199651+jamesarich@users.noreply.github.com> Date: Fri, 12 Dec 2025 08:46:43 -0600 Subject: [PATCH] refactor: Move byte utilities to core:common module (#3977) Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com> --- .../org/meshtastic/core/common/ByteUtils.kt | 25 +++++++++++++++++++ core/model/build.gradle.kts | 1 + .../org/meshtastic/core/model/Channel.kt | 7 ++---- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 core/common/src/main/kotlin/org/meshtastic/core/common/ByteUtils.kt diff --git a/core/common/src/main/kotlin/org/meshtastic/core/common/ByteUtils.kt b/core/common/src/main/kotlin/org/meshtastic/core/common/ByteUtils.kt new file mode 100644 index 000000000..c27040e73 --- /dev/null +++ b/core/common/src/main/kotlin/org/meshtastic/core/common/ByteUtils.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Meshtastic LLC + * + * 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 . + */ + +package org.meshtastic.core.common + +/** Utility function to make it easy to declare byte arrays */ +fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) { pos -> ints[pos].toByte() } + +fun xorHash(b: ByteArray) = b.fold(0) { acc, x -> acc xor (x.toInt() and BYTE_MASK) } + +private const val BYTE_MASK = 0xff diff --git a/core/model/build.gradle.kts b/core/model/build.gradle.kts index d18352a4f..6171ec8cb 100644 --- a/core/model/build.gradle.kts +++ b/core/model/build.gradle.kts @@ -31,6 +31,7 @@ android { } dependencies { + implementation(projects.core.common) implementation(projects.core.proto) implementation(projects.core.strings) diff --git a/core/model/src/main/kotlin/org/meshtastic/core/model/Channel.kt b/core/model/src/main/kotlin/org/meshtastic/core/model/Channel.kt index 5f8ea392b..5bf9cdb7a 100644 --- a/core/model/src/main/kotlin/org/meshtastic/core/model/Channel.kt +++ b/core/model/src/main/kotlin/org/meshtastic/core/model/Channel.kt @@ -18,6 +18,8 @@ package org.meshtastic.core.model import com.google.protobuf.ByteString +import org.meshtastic.core.common.byteArrayOfInts +import org.meshtastic.core.common.xorHash import org.meshtastic.proto.ChannelProtos import org.meshtastic.proto.ConfigKt.loRaConfig import org.meshtastic.proto.ConfigProtos @@ -25,11 +27,6 @@ import org.meshtastic.proto.ConfigProtos.Config.LoRaConfig.ModemPreset import org.meshtastic.proto.channelSettings import java.security.SecureRandom -/** 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() } - -fun xorHash(b: ByteArray) = b.fold(0) { acc, x -> acc xor (x.toInt() and 0xff) } - data class Channel( val settings: ChannelProtos.ChannelSettings = default.settings, val loraConfig: ConfigProtos.Config.LoRaConfig = default.loraConfig,