Android-cuttlefish cvd tool
apacket_reader.h
Go to the documentation of this file.
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.
30 public:
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.
42
43 private:
44 void add_packet(std::unique_ptr<apacket> packet);
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};
Definition: apacket_reader.h:29
void add_packet(std::unique_ptr< apacket > packet)
Definition: apacket_reader.cpp:26
~APacketReader()=default
AddResult
Definition: apacket_reader.h:34
@ ERROR
Definition: apacket_reader.h:34
@ OK
Definition: apacket_reader.h:34
Block header_
Definition: apacket_reader.h:45
std::unique_ptr< apacket > packet_
Definition: apacket_reader.h:46
AddResult add_bytes(Block &&block) noexcept
Definition: apacket_reader.cpp:33
std::vector< std::unique_ptr< apacket > > packets_
Definition: apacket_reader.h:49
APacketReader()
Definition: apacket_reader.cpp:22
std::vector< std::unique_ptr< apacket > > get_packets() noexcept
Definition: apacket_reader.cpp:115
void prepare_for_next_packet()
Definition: apacket_reader.cpp:122
Definition: logging.h:464
Definition: types.h:35
Definition: types.h:148
Definition: types.h:157