2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "service/advertise_data.h"
19 #include <base/logging.h>
21 #include "stack/include/bt_types.h"
22 #include "stack/include/hcidefs.h"
26 AdvertiseData::AdvertiseData(const std::vector<uint8_t>& data)
28 include_device_name_(false),
29 include_tx_power_level_(false) {
32 AdvertiseData::AdvertiseData()
33 : include_device_name_(false),
34 include_tx_power_level_(false) {
37 AdvertiseData::AdvertiseData(const AdvertiseData& other)
39 include_device_name_(other.include_device_name_),
40 include_tx_power_level_(other.include_tx_power_level_) {
43 bool AdvertiseData::IsValid() const {
44 size_t len = data_.size();
46 // Consider empty data as valid.
50 for (size_t i = 0, field_len = 0; i < len; i += (field_len + 1)) {
53 // If the length of the current field would exceed the total data length,
54 // then the data is badly formatted.
55 if (i + field_len >= len) {
56 VLOG(1) << "Advertising data badly formatted";
60 // A field length of 0 would be invalid as it should at least contain the
65 uint8_t type = data_[i + 1];
67 // Clients are not allowed to set the following EIR fields as these are
70 case HCI_EIR_FLAGS_TYPE:
71 case HCI_EIR_TX_POWER_LEVEL_TYPE:
72 case HCI_EIR_SHORTENED_LOCAL_NAME_TYPE:
73 case HCI_EIR_COMPLETE_LOCAL_NAME_TYPE:
74 case HCI_EIR_OOB_BD_ADDR_TYPE:
75 case HCI_EIR_OOB_COD_TYPE:
76 case HCI_EIR_OOB_SSP_HASH_C_TYPE:
77 case HCI_EIR_OOB_SSP_RAND_R_TYPE:
78 VLOG(1) << "Cannot set EIR field type: " << type;
88 bool AdvertiseData::operator==(const AdvertiseData& rhs) const {
89 if (include_tx_power_level_ != rhs.include_tx_power_level_)
92 if (include_device_name_ != rhs.include_device_name_)
95 return data_ == rhs.data_;
98 AdvertiseData& AdvertiseData::operator=(const AdvertiseData& other) {
103 include_device_name_ = other.include_device_name_;
104 include_tx_power_level_ = other.include_tx_power_level_;
109 } // namespace bluetooth