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 #pragma once 18 19 #include <iostream> 20 #include <vector> 21 22 #include "packet/avrcp/avrcp_packet.h" 23 24 // These classes are temporary placeholders to easily switch between BT_HDR and 25 // packets. 26 class VectorPacket : public ::bluetooth::Packet { 27 public: 28 using Packet::Packet; // Inherit constructors 29 Make()30 static std::shared_ptr<VectorPacket> Make() { 31 return std::shared_ptr<VectorPacket>(new VectorPacket()); 32 }; 33 Make(std::vector<uint8_t> payload)34 static std::shared_ptr<VectorPacket> Make(std::vector<uint8_t> payload) { 35 auto pkt = VectorPacket::Make(); 36 pkt->packet_start_index_ = 0; 37 pkt->packet_end_index_ = payload.size(); 38 pkt->data_ = std::make_shared<std::vector<uint8_t>>(std::move(payload)); 39 return pkt; 40 }; 41 GetData()42 const std::vector<uint8_t>& GetData() { return *data_; }; 43 ToString()44 virtual std::string ToString() const override { 45 std::stringstream ss; 46 ss << "VectorPacket:" << std::endl; 47 ss << " └ Payload ="; 48 for (auto it = begin(); it != end(); it++) { 49 ss << " " << loghex(*it); 50 } 51 ss << std::endl; 52 53 return ss.str(); 54 }; 55 GetPayloadIndecies()56 virtual std::pair<size_t, size_t> GetPayloadIndecies() const override { 57 return std::pair<size_t, size_t>(packet_start_index_, packet_end_index_); 58 } 59 IsValid()60 virtual bool IsValid() const override { return true; } 61 }; 62 63 // TODO (apanicke): When deleting the old AVRCP Stack, remove this class and 64 // instead create a BT_HDR Parsing packet. 65 class AvrcpMessageConverter { 66 public: Parse(tAVRC_MSG * m)67 static std::shared_ptr<::bluetooth::Packet> Parse(tAVRC_MSG* m) { 68 std::vector<uint8_t> data; 69 70 switch (m->hdr.opcode) { 71 case AVRC_OP_VENDOR: { 72 tAVRC_MSG_VENDOR* msg = (tAVRC_MSG_VENDOR*)m; 73 data.push_back(m->hdr.ctype); 74 data.push_back((m->hdr.subunit_type << 3) | m->hdr.subunit_id); 75 data.push_back(m->hdr.opcode); 76 for (int i = 2; i >= 0; i--) { 77 data.push_back((uint8_t)((msg->company_id >> i * 8) & 0xff)); 78 } 79 for (int i = 0; i < msg->vendor_len; i++) { 80 data.push_back(msg->p_vendor_data[i]); 81 } 82 } break; 83 case AVRC_OP_PASS_THRU: { 84 tAVRC_MSG_PASS* msg = (tAVRC_MSG_PASS*)m; 85 data.push_back(m->hdr.ctype); 86 data.push_back((m->hdr.subunit_type << 3) | m->hdr.subunit_id); 87 data.push_back(m->hdr.opcode); 88 data.push_back((msg->state << 7) | msg->op_id); 89 data.push_back(0x00); 90 } break; 91 case AVRC_OP_BROWSE: { 92 tAVRC_MSG_BROWSE* msg = (tAVRC_MSG_BROWSE*)m; 93 // The first 3 bytes are header bytes that aren't actually in AVRCP 94 // packets 95 for (int i = 0; i < msg->browse_len; i++) { 96 data.push_back(msg->p_browse_data[i]); 97 } 98 } break; 99 default: 100 LOG(ERROR) << "Unknown opcode for AVRCP message"; 101 break; 102 } 103 104 return VectorPacket::Make(data); 105 } 106 }; 107