blob: d9cac21b303bbf969050a52f2ec9644fbfb8b7f7 [file] [log] [blame]
Arman Uguray3d73f612015-08-31 14:31:02 -07001//
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 Pawlowski5f885332017-06-24 15:25:07 -070017#include "bluetooth/advertise_data.h"
Arman Uguray3d73f612015-08-31 14:31:02 -070018
19#include <base/logging.h>
20
21#include "stack/include/bt_types.h"
22#include "stack/include/hcidefs.h"
23
24namespace bluetooth {
25
Jakub Pawlowski0ecddf32016-10-19 14:46:09 -070026AdvertiseData::AdvertiseData(const std::vector<uint8_t>& data) : data_(data) {}
Arman Uguray3d73f612015-08-31 14:31:02 -070027
Jakub Pawlowski0ecddf32016-10-19 14:46:09 -070028AdvertiseData::AdvertiseData() {}
Arman Uguray3d73f612015-08-31 14:31:02 -070029
Jakub Pawlowski0ecddf32016-10-19 14:46:09 -070030AdvertiseData::AdvertiseData(const AdvertiseData& other) : data_(other.data_) {}
Arman Uguray3d73f612015-08-31 14:31:02 -070031
32bool AdvertiseData::IsValid() const {
33 size_t len = data_.size();
34
35 // Consider empty data as valid.
Jakub Pawlowski0ecddf32016-10-19 14:46:09 -070036 if (!len) return true;
Arman Uguray3d73f612015-08-31 14:31:02 -070037
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 Uguray12338402015-09-16 18:00:05 -070048 // A field length of 0 would be invalid as it should at least contain the
49 // EIR field type.
Jakub Pawlowski0ecddf32016-10-19 14:46:09 -070050 if (field_len < 1) return false;
Arman Uguray12338402015-09-16 18:00:05 -070051
Arman Uguray3d73f612015-08-31 14:31:02 -070052 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 Pawlowski0ecddf32016-10-19 14:46:09 -070057 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 Uguray3d73f612015-08-31 14:31:02 -070066 }
67 }
68
69 return true;
70}
71
72bool AdvertiseData::operator==(const AdvertiseData& rhs) const {
Arman Uguray3d73f612015-08-31 14:31:02 -070073 return data_ == rhs.data_;
74}
75
76AdvertiseData& AdvertiseData::operator=(const AdvertiseData& other) {
Jakub Pawlowski0ecddf32016-10-19 14:46:09 -070077 if (this == &other) return *this;
Arman Uguray3d73f612015-08-31 14:31:02 -070078
79 data_ = other.data_;
Arman Uguray3d73f612015-08-31 14:31:02 -070080 return *this;
81}
82
83} // namespace bluetooth