damage_monitor_auth.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #pragma once
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <span>
  5. #include "../../encoding/bit_writer.h"
  6. #include "auth_fields.h"
  7. // Type-20 damage monitor. The body binds the monitor to one type-4 object and carries a revision
  8. // the client echoes with the object's health and shield fractions.
  9. namespace sunrise::middleware::bap::activity_message::damage_monitor {
  10. namespace fields = auth_fields;
  11. inline constexpr std::uint8_t kSlotType = 20;
  12. inline constexpr std::uint32_t kComponentClass = 0x80809560U;
  13. inline constexpr std::uint32_t kAuthSchema = 0x80809563U;
  14. inline constexpr std::uint32_t kSenseSchema = 0x80809562U;
  15. /** The watched object is a type-4 object slot. */
  16. inline constexpr std::uint32_t kTargetSlotType = 4;
  17. inline constexpr std::size_t kBits = fields::kClientRefBits + 32;
  18. inline constexpr std::size_t kBytes = (kBits + 7) / 8;
  19. /**
  20. * Encodes the monitor body.
  21. * @param revision Positive revision; a change re-binds the monitor.
  22. * @param written Receives kBytes.
  23. */
  24. [[nodiscard]] inline bool encode(std::uint32_t registryKey,
  25. std::uint16_t slotIndex,
  26. std::int32_t revision,
  27. std::span<std::byte> output,
  28. std::size_t& written) noexcept {
  29. written = 0;
  30. if (registryKey == 0 || revision <= 0 || slotIndex > fields::kMaximumClientRefIndex
  31. || output.size() < kBytes) {
  32. return false;
  33. }
  34. encoding::bits::Writer writer(output.first(kBytes));
  35. return fields::write_client_ref(writer, registryKey, kTargetSlotType, slotIndex)
  36. && writer.write(static_cast<std::uint32_t>(revision) + fields::kSigned32Bias, 32)
  37. && fields::finish_exact(writer, kBits, kBytes, written);
  38. }
  39. } // namespace sunrise::middleware::bap::activity_message::damage_monitor