get_item_attributes.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "get_item_attributes.h"
  17. namespace bluetooth {
  18. namespace avrcp {
  19. std::unique_ptr<GetItemAttributesResponseBuilder>
  20. GetItemAttributesResponseBuilder::MakeBuilder(Status status, size_t mtu) {
  21. std::unique_ptr<GetItemAttributesResponseBuilder> builder(
  22. new GetItemAttributesResponseBuilder(status, mtu));
  23. return builder;
  24. }
  25. bool GetItemAttributesResponseBuilder::AddAttributeEntry(AttributeEntry entry) {
  26. CHECK(entries_.size() < 0xFF);
  27. size_t remaining_space = mtu_ - size();
  28. if (entry.size() > remaining_space) {
  29. entry.resize(remaining_space);
  30. }
  31. if (entry.empty()) {
  32. return false;
  33. }
  34. entries_.insert(entry);
  35. return true;
  36. }
  37. bool GetItemAttributesResponseBuilder::AddAttributeEntry(Attribute attribute,
  38. std::string value) {
  39. return AddAttributeEntry(AttributeEntry(attribute, value));
  40. }
  41. size_t GetItemAttributesResponseBuilder::size() const {
  42. size_t len = BrowsePacket::kMinSize();
  43. len += 1; // Status
  44. if (status_ != Status::NO_ERROR) return len;
  45. len += 1; // Number of attributes
  46. for (const auto& entry : entries_) {
  47. len += entry.size();
  48. }
  49. return len;
  50. }
  51. bool GetItemAttributesResponseBuilder::Serialize(
  52. const std::shared_ptr<::bluetooth::Packet>& pkt) {
  53. ReserveSpace(pkt, size());
  54. BrowsePacketBuilder::PushHeader(pkt, size() - BrowsePacket::kMinSize());
  55. AddPayloadOctets1(pkt, (uint8_t)status_);
  56. if (status_ != Status::NO_ERROR) return true;
  57. AddPayloadOctets1(pkt, entries_.size());
  58. for (const auto& entry : entries_) {
  59. AddPayloadOctets4(pkt, base::ByteSwap((uint32_t)entry.attribute()));
  60. uint16_t character_set = 0x006a; // UTF-8
  61. AddPayloadOctets2(pkt, base::ByteSwap(character_set));
  62. uint16_t value_length = entry.value().length();
  63. AddPayloadOctets2(pkt, base::ByteSwap(value_length));
  64. for (const uint8_t& byte : entry.value()) {
  65. AddPayloadOctets1(pkt, byte);
  66. }
  67. }
  68. return true;
  69. }
  70. Scope GetItemAttributesRequest::GetScope() const {
  71. auto it = begin() + BrowsePacket::kMinSize();
  72. return static_cast<Scope>(*it);
  73. }
  74. uint64_t GetItemAttributesRequest::GetUid() const {
  75. auto it = begin() + BrowsePacket::kMinSize() + static_cast<size_t>(1);
  76. return it.extractBE<uint64_t>();
  77. }
  78. uint16_t GetItemAttributesRequest::GetUidCounter() const {
  79. auto it = begin() + BrowsePacket::kMinSize() + static_cast<size_t>(9);
  80. return it.extractBE<uint16_t>();
  81. }
  82. uint8_t GetItemAttributesRequest::GetNumAttributes() const {
  83. auto it = begin() + BrowsePacket::kMinSize() + static_cast<size_t>(11);
  84. return *it;
  85. }
  86. std::vector<Attribute> GetItemAttributesRequest::GetAttributesRequested()
  87. const {
  88. auto it = begin() + BrowsePacket::kMinSize() + static_cast<size_t>(11);
  89. size_t number_of_attributes = it.extract<uint8_t>();
  90. std::vector<Attribute> attribute_list;
  91. for (size_t i = 0; i < number_of_attributes; i++) {
  92. attribute_list.push_back((Attribute)it.extractBE<uint32_t>());
  93. }
  94. return attribute_list;
  95. }
  96. bool GetItemAttributesRequest::IsValid() const {
  97. if (!BrowsePacket::IsValid()) return false;
  98. if (size() < kMinSize()) return false;
  99. // Casting the int returned from end - attr_start should be fine. If an
  100. // overflow occurs we can definitly say the packet is invalid
  101. return (GetNumAttributes() * sizeof(Attribute)) == (size() - kMinSize());
  102. }
  103. std::string GetItemAttributesRequest::ToString() const {
  104. std::stringstream ss;
  105. ss << "GetItemAttributesRequestPacket: " << std::endl;
  106. ss << " └ PDU = " << GetPdu() << std::endl;
  107. ss << " └ Length = " << GetLength() << std::endl;
  108. ss << " └ Scope = " << GetScope() << std::endl;
  109. ss << " └ UID Requested = " << loghex(GetUid()) << std::endl;
  110. ss << " └ UID Counter = " << loghex(GetUidCounter()) << std::endl;
  111. ss << " └ Num Attributes = " << loghex(GetNumAttributes()) << std::endl;
  112. auto attr_list = GetAttributesRequested();
  113. ss << " └ Attribute List: Size: " << attr_list.size() << std::endl;
  114. for (auto it = attr_list.begin(); it != attr_list.end(); it++) {
  115. ss << " └ " << loghex((uint32_t)(*it)) << std::endl;
  116. }
  117. ss << std::endl;
  118. return ss.str();
  119. }
  120. } // namespace avrcp
  121. } // namespace bluetooth