set_browsed_player.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "set_browsed_player.h"
  17. namespace bluetooth {
  18. namespace avrcp {
  19. std::unique_ptr<SetBrowsedPlayerResponseBuilder>
  20. SetBrowsedPlayerResponseBuilder::MakeBuilder(Status status,
  21. uint16_t uid_counter,
  22. uint32_t num_items_in_folder,
  23. uint8_t folder_depth,
  24. std::string folder_name) {
  25. std::unique_ptr<SetBrowsedPlayerResponseBuilder> builder(
  26. new SetBrowsedPlayerResponseBuilder(
  27. status, uid_counter, num_items_in_folder, folder_depth, folder_name));
  28. return builder;
  29. }
  30. size_t SetBrowsedPlayerResponseBuilder::size() const {
  31. size_t len = BrowsePacket::kMinSize();
  32. len += 1; // Status
  33. // If the status isn't success the rest of the fields are ommited
  34. if (status_ != Status::NO_ERROR) return len;
  35. len += 2; // UID Counter
  36. len += 4; // Number of items in folder
  37. len += 2; // UTF-8 Character Set
  38. len += 1; // Folder Depth
  39. // This is only included if the folder returned isn't the root folder
  40. if (folder_depth_ != 0) {
  41. len += 2; // Folder Name Size;
  42. len += folder_name_.size(); // Folder Name
  43. }
  44. return len;
  45. }
  46. bool SetBrowsedPlayerResponseBuilder::Serialize(
  47. const std::shared_ptr<::bluetooth::Packet>& pkt) {
  48. ReserveSpace(pkt, size());
  49. BrowsePacketBuilder::PushHeader(pkt, size() - BrowsePacket::kMinSize());
  50. AddPayloadOctets1(pkt, (uint8_t)status_);
  51. if (status_ != Status::NO_ERROR) return true;
  52. AddPayloadOctets2(pkt, base::ByteSwap(uid_counter_));
  53. AddPayloadOctets4(pkt, base::ByteSwap(num_items_in_folder_));
  54. AddPayloadOctets2(pkt, base::ByteSwap((uint16_t)0x006a)); // UTF-8
  55. AddPayloadOctets1(pkt, folder_depth_);
  56. // Skip adding the folder name if the folder depth is 0
  57. if (folder_depth_ == 0) return true;
  58. uint16_t folder_name_len = folder_name_.size();
  59. AddPayloadOctets2(pkt, base::ByteSwap(folder_name_len));
  60. for (auto it = folder_name_.begin(); it != folder_name_.end(); it++) {
  61. AddPayloadOctets1(pkt, *it);
  62. }
  63. return true;
  64. }
  65. uint16_t SetBrowsedPlayerRequest::GetPlayerId() const {
  66. auto it = begin() + BrowsePacket::kMinSize();
  67. return it.extractBE<uint16_t>();
  68. }
  69. bool SetBrowsedPlayerRequest::IsValid() const {
  70. if (!BrowsePacket::IsValid()) return false;
  71. return size() == kMinSize();
  72. }
  73. std::string SetBrowsedPlayerRequest::ToString() const {
  74. std::stringstream ss;
  75. ss << "SetBrowsedPlayerRequestPacket: " << std::endl;
  76. ss << " └ PDU = " << GetPdu() << std::endl;
  77. ss << " └ Length = " << GetLength() << std::endl;
  78. ss << " └ Player ID = " << loghex(GetPlayerId()) << std::endl;
  79. ss << std::endl;
  80. return ss.str();
  81. }
  82. } // namespace avrcp
  83. } // namespace bluetooth