OSDN Git Service

test_vendor_lib: Initial commit
[android-x86/system-bt.git] / vendor_libs / test_vendor_lib / src / packet.cc
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 #define LOG_TAG "packet"
18
19 #include "vendor_libs/test_vendor_lib/include/packet.h"
20
21 #include <algorithm>
22
23 extern "C" {
24 #include "osi/include/log.h"
25 }  // extern "C"
26
27 namespace test_vendor_lib {
28
29 Packet::Packet(serial_data_type_t type) : type_(type) {}
30
31 bool Packet::Encode(const std::vector<uint8_t>& header,
32                     const std::vector<uint8_t>& payload) {
33   if (header.back() != payload.size()) {
34     return false;
35   }
36   header_ = header;
37   payload_ = payload;
38   return true;
39 }
40
41 const std::vector<uint8_t>& Packet::GetHeader() const {
42   // Every packet must have a header.
43   assert(GetHeaderSize() > 0);
44   return header_;
45 }
46
47 uint8_t Packet::GetHeaderSize() const {
48   return header_.size();
49 }
50
51 size_t Packet::GetPacketSize() const {
52   return header_.size() + payload_.size() + sizeof(type_);
53 }
54
55 const std::vector<uint8_t>& Packet::GetPayload() const {
56   return payload_;
57 }
58
59 uint8_t Packet::GetPayloadSize() const {
60   return payload_.size();
61 }
62
63 serial_data_type_t Packet::GetType() const {
64   return type_;
65 }
66
67 }  // namespace test_vendor_lib