OSDN Git Service

service: Enable all tests for target
[android-x86/system-bt.git] / service / advertise_data.cpp
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
17 #include "service/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
26 AdvertiseData::AdvertiseData(const std::vector<uint8_t>& data)
27     : data_(data),
28       include_device_name_(false),
29       include_tx_power_level_(false) {
30 }
31
32 AdvertiseData::AdvertiseData()
33     : include_device_name_(false),
34       include_tx_power_level_(false) {
35 }
36
37 AdvertiseData::AdvertiseData(const AdvertiseData& other)
38     : data_(other.data_),
39       include_device_name_(other.include_device_name_),
40       include_tx_power_level_(other.include_tx_power_level_) {
41 }
42
43 bool AdvertiseData::IsValid() const {
44   size_t len = data_.size();
45
46   // Consider empty data as valid.
47   if (!len)
48     return true;
49
50   for (size_t i = 0, field_len = 0; i < len; i += (field_len + 1)) {
51     field_len = data_[i];
52
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";
57       return false;
58     }
59
60     // A field length of 0 would be invalid as it should at least contain the
61     // EIR field type.
62     if (field_len < 1)
63       return false;
64
65     uint8_t type = data_[i + 1];
66
67     // Clients are not allowed to set the following EIR fields as these are
68     // managed by stack.
69     switch (type) {
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;
79       return false;
80     default:
81       break;
82     }
83   }
84
85   return true;
86 }
87
88 bool AdvertiseData::operator==(const AdvertiseData& rhs) const {
89   if (include_tx_power_level_ != rhs.include_tx_power_level_)
90     return false;
91
92   if (include_device_name_ != rhs.include_device_name_)
93     return false;
94
95   return data_ == rhs.data_;
96 }
97
98 AdvertiseData& AdvertiseData::operator=(const AdvertiseData& other) {
99   if (this == &other)
100     return *this;
101
102   data_ = other.data_;
103   include_device_name_ = other.include_device_name_;
104   include_tx_power_level_ = other.include_tx_power_level_;
105
106   return *this;
107 }
108
109 }  // namespace bluetooth