1 /* 2 * Copyright (C) 2024 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 <memory> 20 #include <vector> 21 22 #include "types.h" 23 24 // Historically, adb expects apackets to be transferred over USB with two transfers. One for the 25 // header and one for the payload. This usually translates into two Blocks. Buggy drivers and 26 // "bridges" / IO libs can lead to merged transfers (e.g.: a header and a payload, or a payload 27 // and the next header). 28 // This class is able to read inbound Blocks containing apackets chopped/merged on any boundaries. 29 class APacketReader { 30 public: 31 APacketReader(); 32 ~APacketReader() = default; 33 34 enum AddResult { OK, ERROR }; 35 AddResult add_bytes(Block&& block) noexcept; 36 37 // Returns all packets parsed so far. Upon return, the internal apacket vector is emptied. 38 std::vector<std::unique_ptr<apacket>> get_packets() noexcept; 39 40 // Clear blocks so we can start parsing the next packet. 41 void prepare_for_next_packet(); 42 43 private: 44 void add_packet(std::unique_ptr<apacket> packet); 45 Block header_{sizeof(amessage)}; 46 std::unique_ptr<apacket> packet_; 47 48 // We keep packets in this internal vector. It is empty after a `get_packets` call. 49 std::vector<std::unique_ptr<apacket>> packets_; 50 };