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