• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 
22 #include "base/logging.h"
23 
24 #include "include/inquiry.h"
25 #include "packets/packet_builder.h"
26 #include "types/class_of_device.h"
27 
28 namespace test_vendor_lib {
29 namespace packets {
30 
31 class InquiryResponseBuilder : public PacketBuilder<true> {
32  public:
33   virtual ~InquiryResponseBuilder() = default;
34 
CreateStandard(uint8_t page_scan_repetition_mode,const ClassOfDevice & class_of_device,uint16_t clock_offset)35   static std::unique_ptr<InquiryResponseBuilder> CreateStandard(uint8_t page_scan_repetition_mode,
36                                                                 const ClassOfDevice& class_of_device,
37                                                                 uint16_t clock_offset) {
38     return std::unique_ptr<InquiryResponseBuilder>(new InquiryResponseBuilder(
39         Inquiry::InquiryType::STANDARD, page_scan_repetition_mode, class_of_device, clock_offset));
40   }
CreateRssi(uint8_t page_scan_repetition_mode,const ClassOfDevice & class_of_device,uint16_t clock_offset,uint8_t rssi)41   static std::unique_ptr<InquiryResponseBuilder> CreateRssi(uint8_t page_scan_repetition_mode,
42                                                             const ClassOfDevice& class_of_device, uint16_t clock_offset,
43                                                             uint8_t rssi) {
44     return std::unique_ptr<InquiryResponseBuilder>(new InquiryResponseBuilder(
45         Inquiry::InquiryType::RSSI, page_scan_repetition_mode, class_of_device, clock_offset, rssi));
46   }
CreateExtended(uint8_t page_scan_repetition_mode,const ClassOfDevice & class_of_device,uint16_t clock_offset,uint8_t rssi,const std::vector<uint8_t> & extended_data)47   static std::unique_ptr<InquiryResponseBuilder> CreateExtended(uint8_t page_scan_repetition_mode,
48                                                                 const ClassOfDevice& class_of_device,
49                                                                 uint16_t clock_offset, uint8_t rssi,
50                                                                 const std::vector<uint8_t>& extended_data) {
51     return std::unique_ptr<InquiryResponseBuilder>(new InquiryResponseBuilder(
52         Inquiry::InquiryType::EXTENDED, page_scan_repetition_mode, class_of_device, clock_offset, rssi, extended_data));
53   }
54 
size()55   virtual size_t size() const override {
56     size_t inquiry_size =
57         sizeof(inquiry_type_) + sizeof(page_scan_repetition_mode_) + sizeof(class_of_device_) + sizeof(clock_offset_);
58     if (inquiry_type_ == Inquiry::InquiryType::STANDARD) {
59       return inquiry_size;
60     }
61     inquiry_size += sizeof(rssi_);
62     if (inquiry_type_ == Inquiry::InquiryType::RSSI) {
63       return inquiry_size;
64     }
65 
66     return inquiry_size + extended_data_.size();
67   }
68 
69  protected:
Serialize(std::back_insert_iterator<std::vector<uint8_t>> it)70   virtual void Serialize(std::back_insert_iterator<std::vector<uint8_t>> it) const override {
71     insert(static_cast<uint8_t>(inquiry_type_), it);
72     insert(page_scan_repetition_mode_, it);
73     insert_class_of_device(class_of_device_, it);
74     insert(clock_offset_, it);
75     if (inquiry_type_ == Inquiry::InquiryType::STANDARD) {
76       return;
77     }
78     insert(rssi_, it);
79     if (inquiry_type_ == Inquiry::InquiryType::RSSI) {
80       return;
81     }
82     insert_vector(extended_data_, it);
83   }
84 
85  private:
86   Inquiry::InquiryType inquiry_type_;
87   uint8_t page_scan_repetition_mode_;
88   ClassOfDevice class_of_device_;
89   uint16_t clock_offset_;
90   uint8_t rssi_{0xff};
91   std::vector<uint8_t> extended_data_;
InquiryResponseBuilder(Inquiry::InquiryType inquiry_type,uint8_t page_scan_repetition_mode,const ClassOfDevice & class_of_device,uint16_t clock_offset)92   explicit InquiryResponseBuilder(Inquiry::InquiryType inquiry_type, uint8_t page_scan_repetition_mode,
93                                   const ClassOfDevice& class_of_device, uint16_t clock_offset)
94       : inquiry_type_(inquiry_type), page_scan_repetition_mode_(page_scan_repetition_mode),
95         class_of_device_(class_of_device), clock_offset_(clock_offset) {}
InquiryResponseBuilder(Inquiry::InquiryType inquiry_type,uint8_t page_scan_repetition_mode,const ClassOfDevice & class_of_device,uint16_t clock_offset,uint8_t rssi)96   explicit InquiryResponseBuilder(Inquiry::InquiryType inquiry_type, uint8_t page_scan_repetition_mode,
97                                   const ClassOfDevice& class_of_device, uint16_t clock_offset, uint8_t rssi)
98       : inquiry_type_(inquiry_type), page_scan_repetition_mode_(page_scan_repetition_mode),
99         class_of_device_(class_of_device), clock_offset_(clock_offset), rssi_(rssi) {}
InquiryResponseBuilder(Inquiry::InquiryType inquiry_type,uint8_t page_scan_repetition_mode,const ClassOfDevice & class_of_device,uint16_t clock_offset,uint8_t rssi,const std::vector<uint8_t> & extended_data)100   explicit InquiryResponseBuilder(Inquiry::InquiryType inquiry_type, uint8_t page_scan_repetition_mode,
101                                   const ClassOfDevice& class_of_device, uint16_t clock_offset, uint8_t rssi,
102                                   const std::vector<uint8_t>& extended_data)
103       : inquiry_type_(inquiry_type), page_scan_repetition_mode_(page_scan_repetition_mode),
104         class_of_device_(class_of_device), clock_offset_(clock_offset), rssi_(rssi), extended_data_(extended_data) {}
105 };
106 
107 }  // namespace packets
108 }  // namespace test_vendor_lib
109