script_messages.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include <array>
  3. #include <cstddef>
  4. #include <cstdint>
  5. #include <span>
  6. namespace sunrise::middleware::bap::activity_message::script_messages {
  7. /** The ten-bit record count caps the table, and the blob keeps the server-side ceiling. */
  8. inline constexpr std::size_t kStateMaximumRecords = 512;
  9. inline constexpr std::size_t kStateMaximumBlobBytes = 8'000;
  10. inline constexpr std::size_t kStateCountBits = 10;
  11. inline constexpr std::size_t kStateRecordBits = 128;
  12. inline constexpr std::size_t kStateBlobLengthBits = 32;
  13. /** Body sizes the encoder buffer must cover, empty and full. */
  14. inline constexpr std::size_t kStateMinimumBits = kStateCountBits + kStateBlobLengthBits;
  15. inline constexpr std::size_t kStateMaximumBits =
  16. kStateCountBits + kStateMaximumRecords * kStateRecordBits + kStateBlobLengthBits
  17. + kStateMaximumBlobBytes * 8U;
  18. /** Same size padded up to whole bytes. */
  19. inline constexpr std::size_t kStateMaximumBytes = (kStateMaximumBits + 7U) / 8U;
  20. /** One fixed script-state record. Hash identities and offset target semantics remain unnamed. */
  21. struct StateRecord final {
  22. std::uint32_t hashA{};
  23. std::uint32_t hashB{};
  24. std::uint32_t hashC{};
  25. std::int32_t stringOffset{};
  26. };
  27. /** Complete bounded message-40 body, including its raw tail outside reflection. */
  28. struct StateBody final {
  29. std::array<StateRecord, kStateMaximumRecords> records{};
  30. std::array<std::byte, kStateMaximumBlobBytes> blob{};
  31. std::uint16_t recordCount{};
  32. std::uint32_t blobLength{};
  33. };
  34. /** Computes the meaningful bit count for one bounded script-state body. */
  35. [[nodiscard]] bool
  36. state_bit_count(std::size_t records, std::size_t blobBytes, std::size_t& bits) noexcept;
  37. /** Computes the padded byte extent for one bounded script-state body. */
  38. [[nodiscard]] bool
  39. state_byte_count(std::size_t records, std::size_t blobBytes, std::size_t& bytes) noexcept;
  40. /** Encodes one exact message-40 reflected table and raw blob. */
  41. [[nodiscard]] bool
  42. encode_state(const StateBody& body, std::span<std::byte> output, std::size_t& written) noexcept;
  43. /** Decodes one exact message-40 body with server-side count clamps. */
  44. [[nodiscard]] bool decode_state(std::span<const std::byte> input, StateBody& body) noexcept;
  45. /** Checks one exact message-40 body without retaining decoded state. */
  46. [[nodiscard]] bool validate_state(std::span<const std::byte> input) noexcept;
  47. /** The nine-bit payload length caps the event tail, and the header is fixed width. */
  48. inline constexpr std::size_t kEventMaximumPayloadBytes = 256;
  49. inline constexpr std::size_t kEventHeaderBits = 107;
  50. /** Body size the encoder buffer must cover at the full payload length. */
  51. inline constexpr std::size_t kEventMaximumBits = kEventHeaderBits + kEventMaximumPayloadBytes * 8U;
  52. inline constexpr std::size_t kEventMaximumBytes = (kEventMaximumBits + 7U) / 8U;
  53. /** Complete bounded message-41 event record. Hash and kind meanings remain unnamed. */
  54. struct EventBody final {
  55. std::uint32_t hashA{};
  56. std::uint32_t hashB{};
  57. std::uint32_t hashC{};
  58. std::int8_t kind{-1};
  59. std::array<std::int8_t, kEventMaximumPayloadBytes> payload{};
  60. std::uint16_t payloadLength{};
  61. };
  62. /** Computes the meaningful bit count for one bounded script-event body. */
  63. [[nodiscard]] bool event_bit_count(std::size_t payloadBytes, std::size_t& bits) noexcept;
  64. /** Computes the padded byte extent for one bounded script-event body. */
  65. [[nodiscard]] bool event_byte_count(std::size_t payloadBytes, std::size_t& bytes) noexcept;
  66. /** Encodes one exact message-41 event record. */
  67. [[nodiscard]] bool
  68. encode_event(const EventBody& body, std::span<std::byte> output, std::size_t& written) noexcept;
  69. /** Decodes one exact message-41 body with the declared array clamp. */
  70. [[nodiscard]] bool decode_event(std::span<const std::byte> input, EventBody& body) noexcept;
  71. } // namespace sunrise::middleware::bap::activity_message::script_messages