OSDN Git Service

Merge "Run code coverage only on host target"
[android-x86/system-bt.git] / vendor_libs / test_vendor_lib / include / l2cap_packet.h
1 /*
2  * Copyright 2017 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 <cmath>
20 #include <cstdint>
21 #include <iterator>
22 #include <memory>
23 #include <vector>
24
25 #include "base/macros.h"
26 #include "hci_packet.h"
27 #include "l2cap_sdu.h"
28
29 namespace test_vendor_lib {
30
31 const int kSduHeaderLength = 4;
32
33 class L2capPacket : public HciPacket {
34  public:
35   // Returns an assembled L2cap object if successful, nullptr if failure.
36   static std::shared_ptr<L2capPacket> assemble(
37       const std::vector<std::shared_ptr<L2capSdu> >& sdu_packet);
38
39   // Returns a fragmented vector of L2capSdu objects if successful
40   // Returns an empty vector of L2capSdu objects if unsuccessful
41   std::vector<std::shared_ptr<L2capSdu> > fragment(uint16_t maximum_sdu_size,
42                                                    uint8_t txseq,
43                                                    uint8_t reqseq);
44
45   uint16_t get_l2cap_cid() const;
46
47   // HciPacket Functions
48   size_t get_length();
49   uint8_t& get_at_index(size_t index);
50
51  private:
52   L2capPacket() = default;
53
54   // Entire L2CAP packet: length, CID, and payload in that order.
55   std::vector<uint8_t> l2cap_packet_;
56
57   DISALLOW_COPY_AND_ASSIGN(L2capPacket);
58
59   // Helper functions for fragmenting.
60   static void set_sdu_header_length(std::vector<uint8_t>& sdu, uint16_t length);
61
62   static void set_total_sdu_length(std::vector<uint8_t>& sdu,
63                                    uint16_t total_sdu_length);
64
65   static void set_sdu_cid(std::vector<uint8_t>& sdu, uint16_t cid);
66
67   static void set_sdu_control_bytes(std::vector<uint8_t>& sdu, uint8_t txseq,
68                                     uint8_t reqseq);
69
70   bool check_l2cap_packet() const;
71
72 };  // L2capPacket
73
74 }  // namespace test_vendor_lib