1 // 2 // Copyright 2015 Google, Inc. 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 #include "bluetooth/advertise_data.h" 18 19 #include <base/logging.h> 20 21 #include "stack/include/bt_types.h" 22 #include "stack/include/hcidefs.h" 23 24 namespace bluetooth { 25 AdvertiseData(const std::vector<uint8_t> & data)26AdvertiseData::AdvertiseData(const std::vector<uint8_t>& data) : data_(data) {} 27 AdvertiseData()28AdvertiseData::AdvertiseData() {} 29 AdvertiseData(const AdvertiseData & other)30AdvertiseData::AdvertiseData(const AdvertiseData& other) : data_(other.data_) {} 31 IsValid() const32bool AdvertiseData::IsValid() const { 33 size_t len = data_.size(); 34 35 // Consider empty data as valid. 36 if (!len) return true; 37 38 for (size_t i = 0, field_len = 0; i < len; i += (field_len + 1)) { 39 field_len = data_[i]; 40 41 // If the length of the current field would exceed the total data length, 42 // then the data is badly formatted. 43 if (i + field_len >= len) { 44 VLOG(1) << "Advertising data badly formatted"; 45 return false; 46 } 47 48 // A field length of 0 would be invalid as it should at least contain the 49 // EIR field type. 50 if (field_len < 1) return false; 51 52 uint8_t type = data_[i + 1]; 53 54 // Clients are not allowed to set the following EIR fields as these are 55 // managed by stack. 56 switch (type) { 57 case HCI_EIR_FLAGS_TYPE: 58 case HCI_EIR_OOB_BD_ADDR_TYPE: 59 case HCI_EIR_OOB_COD_TYPE: 60 case HCI_EIR_OOB_SSP_HASH_C_TYPE: 61 case HCI_EIR_OOB_SSP_RAND_R_TYPE: 62 VLOG(1) << "Cannot set EIR field type: " << type; 63 return false; 64 default: 65 break; 66 } 67 } 68 69 return true; 70 } 71 operator ==(const AdvertiseData & rhs) const72bool AdvertiseData::operator==(const AdvertiseData& rhs) const { 73 return data_ == rhs.data_; 74 } 75 operator =(const AdvertiseData & other)76AdvertiseData& AdvertiseData::operator=(const AdvertiseData& other) { 77 if (this == &other) return *this; 78 79 data_ = other.data_; 80 return *this; 81 } 82 83 } // namespace bluetooth 84