summaryrefslogtreecommitdiff
path: root/system/packet/avrcp/get_element_attributes_packet.cc
blob: 8634ce0792ecf0419e21829cddf16b77748d26ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <algorithm>

#include "get_element_attributes_packet.h"

namespace bluetooth {
namespace avrcp {

uint64_t GetElementAttributesRequest::GetIdentifier() const {
  auto it = begin() + VendorPacket::kMinSize();
  return it.extract<uint64_t>();
}

uint8_t GetElementAttributesRequest::GetNumAttributes() const {
  auto it = begin() + VendorPacket::kMinSize() + static_cast<size_t>(8);
  return it.extract<uint8_t>();
}

std::vector<Attribute> GetElementAttributesRequest::GetAttributesRequested()
    const {
  auto it = begin() + VendorPacket::kMinSize() + static_cast<size_t>(8);

  size_t number_of_attributes = it.extract<uint8_t>();

  std::vector<Attribute> attribute_list;

  for (size_t i = 0; i < number_of_attributes; i++) {
    attribute_list.push_back((Attribute)it.extractBE<uint32_t>());
  }

  return attribute_list;
}

bool GetElementAttributesRequest::IsValid() const {
  if (!VendorPacket::IsValid()) return false;
  if (size() < kMinSize()) return false;

  size_t num_attributes = GetNumAttributes();
  auto attr_start = begin() + VendorPacket::kMinSize() + static_cast<size_t>(9);

  // Casting the int returned from end - attr_start should be fine. If an
  // overflow occurs we can definitly say the packet is invalid
  return (num_attributes * sizeof(Attribute)) == (size_t)(end() - attr_start);
}

std::string GetElementAttributesRequest::ToString() const {
  std::stringstream ss;
  ss << "RegisterNotificationPacket: " << std::endl;
  ss << "  └ cType = " << GetCType() << std::endl;
  ss << "  └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
  ss << "  └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
  ss << "  └ OpCode = " << GetOpcode() << std::endl;
  ss << "  └ Company ID = " << loghex(GetCompanyId()) << std::endl;
  ss << "  └ Command PDU = " << GetCommandPdu() << std::endl;
  ss << "  └ PacketType = " << GetPacketType() << std::endl;
  ss << "  └ Parameter Length = " << GetParameterLength() << std::endl;
  ss << "  └ Identifier = " << loghex(GetIdentifier()) << std::endl;

  auto attr_list = GetAttributesRequested();

  ss << "  └ Attribute List: Size: " << attr_list.size() << std::endl;
  for (auto it = attr_list.begin(); it != attr_list.end(); it++) {
    ss << "      └ " << loghex((uint32_t)(*it)) << std::endl;
  }
  ss << std::endl;

  return ss.str();
}

std::unique_ptr<GetElementAttributesResponseBuilder>
GetElementAttributesResponseBuilder::MakeBuilder(size_t mtu) {
  std::unique_ptr<GetElementAttributesResponseBuilder> builder(
      new GetElementAttributesResponseBuilder(mtu));

  return builder;
}

bool GetElementAttributesResponseBuilder::AddAttributeEntry(
    AttributeEntry entry) {
  CHECK_LT(entries_.size(), size_t(0xFF))
      << __func__ << ": attribute entry overflow";

  size_t remaining_space = mtu_ - size();
  if (entry.size() > remaining_space) {
    entry.resize(remaining_space);
  }

  if (entry.empty()) {
    return false;
  }

  entries_.insert(entry);
  return true;
}

bool GetElementAttributesResponseBuilder::AddAttributeEntry(Attribute attribute,
                                                            std::string value) {
  return AddAttributeEntry(AttributeEntry(attribute, value));
}

size_t GetElementAttributesResponseBuilder::size() const {
  size_t attr_list_size = 0;

  for (auto& attribute_entry : entries_) {
    attr_list_size += attribute_entry.size();
  }

  return VendorPacket::kMinSize() + 1 + attr_list_size;
}

bool GetElementAttributesResponseBuilder::Serialize(
    const std::shared_ptr<::bluetooth::Packet>& pkt) {
  ReserveSpace(pkt, size());

  PacketBuilder::PushHeader(pkt);

  VendorPacketBuilder::PushHeader(pkt, size() - VendorPacket::kMinSize());

  AddPayloadOctets1(pkt, entries_.size());
  for (const auto& attribute_entry : entries_) {
    PushAttributeValue(pkt, attribute_entry);
  }

  return true;
}

}  // namespace avrcp
}  // namespace bluetooth