incident.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include <array>
  3. #include <cstddef>
  4. #include <cstdint>
  5. #include <span>
  6. namespace sunrise::middleware::bap::activity_message::incident {
  7. /** Activity message type 19 carries one incident. Both sides can send it. */
  8. inline constexpr std::uint32_t kMessageType = 19;
  9. inline constexpr std::uint8_t kTargetWidth = 13;
  10. /** The highest valid target index. Above it the Client indexes handler tables unbounded. */
  11. inline constexpr std::uint32_t kTargetMaximum = 7'762;
  12. /** These three rows carry type code -1 and are a crash risk, so they never pass. */
  13. inline constexpr std::array<std::uint32_t, 3> kPoisonTargets{795, 4'690, 5'375};
  14. inline constexpr std::uint8_t kExtraCountWidth = 5;
  15. inline constexpr std::uint32_t kExtraTargetMaximum = 25;
  16. inline constexpr std::uint8_t kSelectorPresenceWidth = 1;
  17. inline constexpr std::uint8_t kSelectorLengthWidth = 9;
  18. inline constexpr std::uint32_t kSelectorMaximum = 260;
  19. inline constexpr std::uint8_t kOptionalPresenceWidth = 1;
  20. inline constexpr std::uint8_t kOptionalWordWidth = 32;
  21. inline constexpr std::uint8_t kPayloadLengthWidth = 9;
  22. inline constexpr std::uint32_t kPayloadMaximum = 500;
  23. /** Why one incident did not pass validation. */
  24. enum class Verdict : std::uint8_t {
  25. accepted,
  26. truncated,
  27. targetOutOfRange,
  28. targetPoisoned,
  29. tooManyTargets,
  30. payloadTooLong,
  31. selectorTooLong,
  32. };
  33. /** One validated incident, framed to the end of its payload. */
  34. struct Incident {
  35. std::uint32_t primaryTarget{};
  36. std::array<std::uint16_t, kExtraTargetMaximum> extraTargets{};
  37. std::uint32_t extraTargetCount{};
  38. std::uint32_t selectorLength{};
  39. std::uint32_t payloadLength{};
  40. /** Bits the body used. Below the payload bit count means trailing padding. */
  41. std::uint32_t consumedBits{};
  42. /** Set when the two optional words are present. */
  43. bool hasOptionalBlock{};
  44. };
  45. /** @return A short stable name for one verdict, for the log line. */
  46. [[nodiscard]] const char* verdict_name(Verdict verdict) noexcept;
  47. /** Validates framing and every target before the Client can consume the incident. */
  48. [[nodiscard]] Verdict validate(std::span<const std::byte> payload, Incident& parsed) noexcept;
  49. } // namespace sunrise::middleware::bap::activity_message::incident