@autorunify/capacitor-nrfmesh 0.0.18 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -311,8 +311,8 @@ class FormatManager {
311
311
  val pub = msg.heartbeatPublication
312
312
  return JSObject().apply {
313
313
  put("address", pub.dst)
314
- put("count", Utils.decodeHeartbeatCount(pub.countLog.toInt()))
315
- put("period", Utils.decodeHeartbeatPeriod(pub.periodLog.toInt()))
314
+ put("count", Utils.Pwr2(pub.countLog.toInt()))
315
+ put("period", Utils.Pwr2(pub.periodLog.toInt()))
316
316
  put("ttl", pub.ttl)
317
317
  put("netkeyIndex", pub.netKeyIndex)
318
318
  put("features", JSObject().apply {
@@ -329,8 +329,8 @@ class FormatManager {
329
329
  return JSObject().apply {
330
330
  put("srcAddress", sub.src)
331
331
  put("dstAddress", sub.dst)
332
- put("period", Utils.decodeHeartbeatPeriod(sub.periodLog.toInt()))
333
- put("count", Utils.decodeHeartbeatCount(sub.countLog.toInt()))
332
+ put("period", Utils.Pwr2(sub.periodLog.toInt()))
333
+ put("count", Utils.Pwr2(sub.countLog.toInt()))
334
334
  put("minHops", sub.minHops)
335
335
  put("maxHops", sub.maxHops)
336
336
  }
@@ -739,8 +739,8 @@ class NrfMeshPlugin : Plugin {
739
739
 
740
740
  val message = ConfigHeartbeatPublicationSet(
741
741
  address,
742
- Utils.encodeHeartbeatCount(count).toByte(),
743
- Utils.encodeHeartbeatPeriod(period).toByte(),
742
+ Utils.Log2(count.toLong()).toByte(),
743
+ Utils.Log2(period.toLong()).toByte(),
744
744
  ttl,
745
745
  features,
746
746
  netkeyIndex
@@ -765,7 +765,7 @@ class NrfMeshPlugin : Plugin {
765
765
  val message = ConfigHeartbeatSubscriptionSet(
766
766
  srcAddress,
767
767
  dstAddress,
768
- Utils.encodeHeartbeatPeriod(period).toByte()
768
+ Utils.Log2(period.toLong()).toByte()
769
769
  )
770
770
 
771
771
  pairs.add(MessagePair(CONFIG_HEARTBEAT_SUBSCRIPTION_SET, message))
@@ -5,31 +5,26 @@ import kotlin.math.pow
5
5
 
6
6
  class Utils {
7
7
  companion object {
8
- fun decodeHeartbeatPeriod(periodLog: Int): Int {
9
- if (periodLog == 0x00) return 0x0000
10
- if (periodLog == 0x11 || periodLog == -1) return 0xFFFF
11
-
12
- return (2.0).pow(periodLog.toDouble() - 1).toInt()
13
- }
14
-
15
- fun encodeHeartbeatPeriod(period: Int): Int {
16
- if (period == 0x0000) return 0x00
17
- if (period == 0xFFFF) return 0x11
18
-
19
- return ((ln(period.toDouble()) / ln(2.0)) + 1).toInt()
20
- }
21
-
22
- fun decodeHeartbeatCount(countLog: Int): Int {
23
- if (countLog == 0x00) return 0x0000
24
- if (countLog == -1 || countLog == 0x11) return 0xFFFF
25
- return ((2.0).pow(countLog.toDouble() - 1)).toInt()
8
+ fun Pwr2(value: Int): Int {
9
+ val maskedVal =
10
+ value and 0xFF // Ensure we are only treating it as an 8-bit unsigned value
11
+ return when (maskedVal) {
12
+ 0x00 -> 0x0000
13
+ 0xFF -> 0xFFFF
14
+ else -> 1 shl (maskedVal - 1)
15
+ }
26
16
  }
27
17
 
28
- fun encodeHeartbeatCount(count: Int): Int {
29
- if (count == 0x0000) return 0x00
30
- if (count == 0xFFFF) return 0x11
18
+ fun Log2(value: Long): Int {
19
+ val maskedVal = value and 0xFFFFFFFFL // Ensure we treat it as a 32-bit unsigned value
20
+ if (maskedVal == 0L) {
21
+ return 0x00
22
+ }
31
23
 
32
- return ((ln(count.toDouble()) / ln(2.0)) + 1).toInt()
24
+ // Integer.numberOfLeadingZeros expects a 32-bit Int.
25
+ // We convert our masked value to Int to get the accurate leading zero count.
26
+ val leadingZeros = java.lang.Integer.numberOfLeadingZeros(maskedVal.toInt())
27
+ return 32 - leadingZeros
33
28
  }
34
29
  }
35
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autorunify/capacitor-nrfmesh",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "nrf mesh plugin",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",