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