sense_observation_packet.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #pragma once
  2. #include <cstddef>
  3. #include "sense_update.h"
  4. namespace sunrise::middleware::bap::activity_message::sense_update {
  5. /** Complete, or partial: the envelope closed but one delimited group had an unsupported member. */
  6. [[nodiscard]] inline bool observation_status(DecodeStatus status) noexcept {
  7. return status == DecodeStatus::complete || status == DecodeStatus::partial;
  8. }
  9. /**
  10. * Tests whether every decoded object of a packet owns a bounded value range and a generation.
  11. * A complete packet may hold no undecoded object; a partial one may, and those are skipped.
  12. */
  13. [[nodiscard]] inline bool observation_packet(const DecodedPacket& packet) noexcept {
  14. if (!observation_status(packet.status) || packet.objectsTruncated || packet.valuesTruncated
  15. || packet.objectCount > packet.objects.size() || packet.valueCount > packet.values.size()) {
  16. return false;
  17. }
  18. std::size_t end = 0;
  19. std::size_t decoded = 0;
  20. for (std::size_t index = 0; index < packet.objectCount; ++index) {
  21. const DecodedObject& object = packet.objects[index];
  22. if (object.firstValue != end || object.valueCount > packet.valueCount - end
  23. || object.status == ObjectStatus::malformed) {
  24. return false;
  25. }
  26. end += object.valueCount;
  27. if (object.status == ObjectStatus::decoded) {
  28. if (!object.hasGeneration) {
  29. return false;
  30. }
  31. ++decoded;
  32. } else if (packet.status == DecodeStatus::complete) {
  33. return false;
  34. }
  35. }
  36. return end == packet.valueCount && decoded == packet.objectsDecoded;
  37. }
  38. } // namespace sunrise::middleware::bap::activity_message::sense_update