-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
server/core/src/main/java/dev/slimevr/tracking/trackers/udp/FeatureFlags.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package dev.slimevr.tracking.trackers.udp | ||
|
||
import java.nio.ByteBuffer | ||
|
||
/** | ||
* Bit packed flags, enum values start with 0 and indicate which bit it is. | ||
* | ||
* Change the enums and `flagsEnabled` inside to extend. | ||
*/ | ||
class FirmwareFeatures { | ||
enum class FirmwareFeatureFlags { | ||
// EXAMPLE_FEATURE, | ||
|
||
// Add new flags here | ||
|
||
BITS_TOTAL, ; | ||
} | ||
|
||
fun has(flag: FirmwareFeatureFlags): Boolean { | ||
val bit = flag.ordinal | ||
return (flags[bit / 8].toInt() and (1 shl (bit % 8))) != 0 | ||
} | ||
|
||
/** | ||
* Whether the firmware supports the "feature flags" feature, | ||
* set to true when we've received flags packet from the firmware. | ||
*/ | ||
var available = false | ||
private set | ||
|
||
companion object { | ||
fun from(received: ByteBuffer, length: Int): FirmwareFeatures { | ||
val res = FirmwareFeatures() | ||
res.available = true | ||
received.get(res.flags, 0, Math.min(res.flags.size, length)) | ||
return res | ||
} | ||
} | ||
|
||
private val flags = ByteArray(FirmwareFeatureFlags.BITS_TOTAL.ordinal / 8 + 1) | ||
} | ||
|
||
enum class ServerFeatureFlags { | ||
/** Server can parse bundle packets: `PACKET_BUNDLE` = 100 (0x64). */ | ||
PROTOCOL_BUNDLE_SUPPORT, | ||
|
||
// Add new flags here | ||
|
||
BITS_TOTAL, ; | ||
|
||
companion object { | ||
val flagsEnabled: Set<ServerFeatureFlags> = setOf( | ||
PROTOCOL_BUNDLE_SUPPORT | ||
|
||
// Add enabled flags here | ||
) | ||
|
||
val packed = run { | ||
val byteLength = BITS_TOTAL.ordinal / 8 + 1 | ||
val tempPacked = ByteArray(byteLength) | ||
|
||
for (flag in flagsEnabled) { | ||
val bit = flag.ordinal | ||
tempPacked[bit / 8] = | ||
(tempPacked[bit / 8].toInt() or (1 shl (bit % 8))).toByte() | ||
} | ||
|
||
tempPacked | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters