Home | History | Annotate | Download | only in avrcp
      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 
     17 #include "change_path.h"
     18 
     19 namespace bluetooth {
     20 namespace avrcp {
     21 
     22 std::unique_ptr<ChangePathResponseBuilder>
     23 ChangePathResponseBuilder::MakeBuilder(Status status,
     24                                        uint32_t num_items_in_folder) {
     25   std::unique_ptr<ChangePathResponseBuilder> builder(
     26       new ChangePathResponseBuilder(status, num_items_in_folder));
     27 
     28   return builder;
     29 }
     30 
     31 size_t ChangePathResponseBuilder::size() const {
     32   size_t len = BrowsePacket::kMinSize();
     33   len += 1;  // Status
     34   if (status_ != Status::NO_ERROR) return len;
     35 
     36   len += 4;  // Number of items in folder
     37   return len;
     38 }
     39 
     40 bool ChangePathResponseBuilder::Serialize(
     41     const std::shared_ptr<::bluetooth::Packet>& pkt) {
     42   ReserveSpace(pkt, size());
     43 
     44   BrowsePacketBuilder::PushHeader(pkt, size() - BrowsePacket::kMinSize());
     45 
     46   AddPayloadOctets1(pkt, (uint8_t)status_);
     47   if (status_ != Status::NO_ERROR) return true;
     48 
     49   AddPayloadOctets4(pkt, base::ByteSwap(num_items_in_folder_));
     50   return true;
     51 }
     52 
     53 uint16_t ChangePathRequest::GetUidCounter() const {
     54   auto it = begin() + BrowsePacket::kMinSize();
     55   return base::ByteSwap(it.extract<uint16_t>());
     56 }
     57 
     58 Direction ChangePathRequest::GetDirection() const {
     59   auto it = begin() + BrowsePacket::kMinSize() + static_cast<size_t>(2);
     60   return static_cast<Direction>(*it);
     61 }
     62 
     63 uint64_t ChangePathRequest::GetUid() const {
     64   auto it = begin() + BrowsePacket::kMinSize() + static_cast<size_t>(3);
     65   return base::ByteSwap(it.extract<uint64_t>());
     66 }
     67 
     68 bool ChangePathRequest::IsValid() const {
     69   if (!BrowsePacket::IsValid()) return false;
     70   // Change path request packets are always the same size
     71   return size() == kMinSize();
     72 }
     73 
     74 std::string ChangePathRequest::ToString() const {
     75   std::stringstream ss;
     76   ss << "ChangePathRequestPacket: " << std::endl;
     77   ss << "   PDU = " << GetPdu() << std::endl;
     78   ss << "   Length = " << GetLength() << std::endl;
     79   ss << "   UID Counter = " << loghex(GetUidCounter()) << std::endl;
     80   ss << "   Direction = " << GetDirection() << std::endl;
     81   ss << "   UID Requested = " << loghex(GetUid()) << std::endl;
     82   ss << std::endl;
     83 
     84   return ss.str();
     85 }
     86 
     87 std::unique_ptr<ChangePathRequestBuilder> ChangePathRequestBuilder::MakeBuilder(
     88     uint16_t uid_counter, Direction direction, uint64_t folder_uid) {
     89   std::unique_ptr<ChangePathRequestBuilder> builder(
     90       new ChangePathRequestBuilder(uid_counter, direction, folder_uid));
     91 
     92   return builder;
     93 }
     94 
     95 size_t ChangePathRequestBuilder::size() const {
     96   return ChangePathRequest::kMinSize();
     97 }
     98 
     99 bool ChangePathRequestBuilder::Serialize(
    100     const std::shared_ptr<::bluetooth::Packet>& pkt) {
    101   ReserveSpace(pkt, size());
    102 
    103   BrowsePacketBuilder::PushHeader(pkt, size() - BrowsePacket::kMinSize());
    104 
    105   AddPayloadOctets2(pkt, base::ByteSwap(uid_counter_));
    106   AddPayloadOctets1(pkt, static_cast<uint8_t>(direction_));
    107   AddPayloadOctets8(pkt, base::ByteSwap(folder_uid_));
    108   return true;
    109 }
    110 
    111 }  // namespace avrcp
    112 }  // namespace bluetooth