Arman Uguray | 3d73f61 | 2015-08-31 14:31:02 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 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 | |
Jakub Pawlowski | 5f88533 | 2017-06-24 15:25:07 -0700 | [diff] [blame^] | 17 | #include "bluetooth/advertise_data.h" |
Arman Uguray | 3d73f61 | 2015-08-31 14:31:02 -0700 | [diff] [blame] | 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 | |
Jakub Pawlowski | 0ecddf3 | 2016-10-19 14:46:09 -0700 | [diff] [blame] | 26 | AdvertiseData::AdvertiseData(const std::vector<uint8_t>& data) : data_(data) {} |
Arman Uguray | 3d73f61 | 2015-08-31 14:31:02 -0700 | [diff] [blame] | 27 | |
Jakub Pawlowski | 0ecddf3 | 2016-10-19 14:46:09 -0700 | [diff] [blame] | 28 | AdvertiseData::AdvertiseData() {} |
Arman Uguray | 3d73f61 | 2015-08-31 14:31:02 -0700 | [diff] [blame] | 29 | |
Jakub Pawlowski | 0ecddf3 | 2016-10-19 14:46:09 -0700 | [diff] [blame] | 30 | AdvertiseData::AdvertiseData(const AdvertiseData& other) : data_(other.data_) {} |
Arman Uguray | 3d73f61 | 2015-08-31 14:31:02 -0700 | [diff] [blame] | 31 | |
| 32 | bool AdvertiseData::IsValid() const { |
| 33 | size_t len = data_.size(); |
| 34 | |
| 35 | // Consider empty data as valid. |
Jakub Pawlowski | 0ecddf3 | 2016-10-19 14:46:09 -0700 | [diff] [blame] | 36 | if (!len) return true; |
Arman Uguray | 3d73f61 | 2015-08-31 14:31:02 -0700 | [diff] [blame] | 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 | |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 48 | // A field length of 0 would be invalid as it should at least contain the |
| 49 | // EIR field type. |
Jakub Pawlowski | 0ecddf3 | 2016-10-19 14:46:09 -0700 | [diff] [blame] | 50 | if (field_len < 1) return false; |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 51 | |
Arman Uguray | 3d73f61 | 2015-08-31 14:31:02 -0700 | [diff] [blame] | 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) { |
Jakub Pawlowski | 0ecddf3 | 2016-10-19 14:46:09 -0700 | [diff] [blame] | 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; |
Arman Uguray | 3d73f61 | 2015-08-31 14:31:02 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool AdvertiseData::operator==(const AdvertiseData& rhs) const { |
Arman Uguray | 3d73f61 | 2015-08-31 14:31:02 -0700 | [diff] [blame] | 73 | return data_ == rhs.data_; |
| 74 | } |
| 75 | |
| 76 | AdvertiseData& AdvertiseData::operator=(const AdvertiseData& other) { |
Jakub Pawlowski | 0ecddf3 | 2016-10-19 14:46:09 -0700 | [diff] [blame] | 77 | if (this == &other) return *this; |
Arman Uguray | 3d73f61 | 2015-08-31 14:31:02 -0700 | [diff] [blame] | 78 | |
| 79 | data_ = other.data_; |
Arman Uguray | 3d73f61 | 2015-08-31 14:31:02 -0700 | [diff] [blame] | 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | } // namespace bluetooth |