Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f331b2b4 authored by Jakub Tyszkowski's avatar Jakub Tyszkowski
Browse files

le_audio: Improve LeAudioLtvMap

This adds the .Append(other) functionality for combining multiple LTV maps,
and another flavor of RawPacket() serialising function which does the
automatic buffer allocation.

This will be used in the following patches.

Bug: 150670922
Tag: #feature
Test: atest --host bluetooth_le_audio_test
Sponsor: jpawlowski@

Change-Id: I6e0478b9d3be130e2b6ac38a597767668f87f849
parent 95955ba7
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -315,6 +315,19 @@ uint8_t* LeAudioLtvMap::RawPacket(uint8_t* p_buf) const {
  return p_buf;
}

std::vector<uint8_t> LeAudioLtvMap::RawPacket() const {
  std::vector<uint8_t> data(RawPacketSize());
  RawPacket(data.data());
  return data;
}

void LeAudioLtvMap::Append(const LeAudioLtvMap& other) {
  /* This will override values for the already existing keys */
  for (auto& el : other.values) {
    values[el.first] = el.second;
  }
}

LeAudioLtvMap LeAudioLtvMap::Parse(const uint8_t* p_value, uint8_t len,
                                   bool& success) {
  LeAudioLtvMap ltv_map;
+2 −0
Original line number Diff line number Diff line
@@ -408,7 +408,9 @@ class LeAudioLtvMap {
  std::string ToString() const;
  size_t RawPacketSize() const;
  uint8_t* RawPacket(uint8_t* p_buf) const;
  std::vector<uint8_t> RawPacket() const;
  static LeAudioLtvMap Parse(const uint8_t* value, uint8_t len, bool& success);
  void Append(const LeAudioLtvMap& other);

 private:
  std::map<uint8_t, std::vector<uint8_t>> values;
+25 −1
Original line number Diff line number Diff line
@@ -34,6 +34,17 @@ using ::testing::SizeIs;
TEST(LeAudioLtvMapTest, test_serialization) {
  // clang-format off
  const std::vector<uint8_t> ltv_test_vec{
      0x02, 0x01, 0x0a,
      0x03, 0x02, 0xaa, 0xbb,
      0x04, 0x03, 0xde, 0xc0, 0xd0,
  };

  const std::vector<uint8_t> ltv_test_vec2{
      0x04, 0x03, 0xde, 0xc0, 0xde,
      0x05, 0x04, 0xc0, 0xde, 0xc0, 0xde,
  };

  const std::vector<uint8_t> ltv_test_vec_expected{
      0x02, 0x01, 0x0a,
      0x03, 0x02, 0xaa, 0xbb,
      0x04, 0x03, 0xde, 0xc0, 0xde,
@@ -47,6 +58,18 @@ TEST(LeAudioLtvMapTest, test_serialization) {
      LeAudioLtvMap::Parse(ltv_test_vec.data(), ltv_test_vec.size(), success);
  ASSERT_TRUE(success);
  ASSERT_FALSE(ltv_map.IsEmpty());
  ASSERT_EQ((size_t)3, ltv_map.Size());

  ASSERT_TRUE(ltv_map.Find(0x03));
  ASSERT_THAT(*(ltv_map.Find(0x03)), ElementsAre(0xde, 0xc0, 0xd0));

  LeAudioLtvMap ltv_map2 =
      LeAudioLtvMap::Parse(ltv_test_vec2.data(), ltv_test_vec2.size(), success);
  ASSERT_TRUE(success);
  ASSERT_FALSE(ltv_map2.IsEmpty());
  ASSERT_EQ((size_t)2, ltv_map2.Size());

  ltv_map.Append(ltv_map2);
  ASSERT_EQ((size_t)4, ltv_map.Size());

  ASSERT_TRUE(ltv_map.Find(0x01));
@@ -61,7 +84,8 @@ TEST(LeAudioLtvMapTest, test_serialization) {
  // RawPacket
  std::vector<uint8_t> serialized(ltv_map.RawPacketSize());
  ASSERT_TRUE(ltv_map.RawPacket(serialized.data()));
  ASSERT_THAT(serialized, ElementsAreArray(ltv_test_vec));
  ASSERT_THAT(serialized, ElementsAreArray(ltv_test_vec_expected));
  ASSERT_THAT(ltv_map2.RawPacket(), ElementsAreArray(ltv_test_vec2));
}

TEST(LeAudioLtvMapTest, test_serialization_ltv_len_is_zero) {