Home | History | Annotate | Download | only in src
      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 "base/logging.h"
     22 
     23 #include <algorithm>
     24 
     25 extern "C" {
     26 #include "osi/include/log.h"
     27 }  // extern "C"
     28 
     29 namespace test_vendor_lib {
     30 
     31 Packet::Packet(serial_data_type_t type) : type_(type) {}
     32 
     33 bool Packet::Encode(const std::vector<uint8_t>& header,
     34                     const std::vector<uint8_t>& payload) {
     35   if (header.back() != payload.size())
     36     return false;
     37   header_ = header;
     38   payload_ = payload;
     39   return true;
     40 }
     41 
     42 const std::vector<uint8_t>& Packet::GetHeader() const {
     43   // Every packet must have a header.
     44   CHECK(GetHeaderSize() > 0);
     45   return header_;
     46 }
     47 
     48 uint8_t Packet::GetHeaderSize() const {
     49   return header_.size();
     50 }
     51 
     52 size_t Packet::GetPacketSize() const {
     53   // Add one for the type octet.
     54   return 1 + header_.size() + payload_.size();
     55 }
     56 
     57 const std::vector<uint8_t>& Packet::GetPayload() const {
     58   return payload_;
     59 }
     60 
     61 uint8_t Packet::GetPayloadSize() const {
     62   return payload_.size();
     63 }
     64 
     65 serial_data_type_t Packet::GetType() const {
     66   return type_;
     67 }
     68 
     69 }  // namespace test_vendor_lib
     70