OSDN Git Service

Merge "Run code coverage only on host target"
[android-x86/system-bt.git] / vendor_libs / test_vendor_lib / include / packet_stream.h
1 //
2 // Copyright 2015 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 <cstdint>
20 #include <memory>
21 #include <vector>
22
23 #include "command_packet.h"
24 #include "event_packet.h"
25 #include "packet.h"
26
27 namespace test_vendor_lib {
28
29 // Provides abstractions for IO with Packet objects. Used to receive commands
30 // and data from the HCI and to send controller events back to the host.
31 class PacketStream {
32  public:
33   PacketStream() = default;
34
35   virtual ~PacketStream() = default;
36
37   // Reads a command packet from the file descriptor at |fd| and returns the
38   // packet back to the caller, along with the responsibility of managing the
39   // packet.
40   std::unique_ptr<CommandPacket> ReceiveCommand(int fd) const;
41
42   // Reads a single octet from |fd| and interprets it as a packet type octet.
43   // Validates the type octet for correctness.
44   serial_data_type_t ReceivePacketType(int fd) const;
45
46   // Sends an event to file descriptor |fd|. The ownership of the event is left
47   // with the caller.
48   bool SendEvent(std::unique_ptr<EventPacket> event, int fd) const;
49
50  private:
51   // Checks if |type| is in the valid range from DATA_TYPE_COMMAND to
52   // DATA_TYPE_SCO.
53   bool ValidateTypeOctet(serial_data_type_t type) const;
54
55   // Attempts to receive |num_octets_to_receive| into |destination| from |fd|,
56   // returning false if an error occurs.
57   bool ReceiveAll(std::vector<uint8_t>& destination,
58                   size_t num_octets_to_receive, int fd) const;
59
60   // Attempts to send |num_octets_to_send| from |source| to |fd|, returning
61   // false if an error occurs.
62   bool SendAll(const std::vector<uint8_t>& source, size_t num_octets_to_send,
63                int fd) const;
64 };
65
66 }  // namespace test_vendor_lib