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