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/adapter.h"
19 #include <base/logging.h>
21 #include "service/logging_helpers.h"
26 const char Adapter::kDefaultAddress[] = "00:00:00:00:00:00";
28 const char Adapter::kDefaultName[] = "not-initialized";
30 // The minimum number of advertising instances required for multi-advertisement
33 // TODO(armansito): This number comes straight from
34 // packages/apps/Bluetooth/src/c/a/b/btservice/AdapterService.java. It would be
35 // nice to know if there were a way to obtain this number from the stack instead
36 // of hardcoding it here.
37 const char kMinAdvInstancesForMultiAdv = 5;
39 void Adapter::Observer::OnAdapterStateChanged(Adapter* adapter,
40 AdapterState prev_state,
41 AdapterState new_state) {
42 // Default implementation does nothing
46 : state_(ADAPTER_STATE_OFF),
47 address_(kDefaultAddress),
49 memset(&local_le_features_, 0, sizeof(local_le_features_));
50 hal::BluetoothInterface::Get()->AddObserver(this);
51 ble_client_factory_.reset(new LowEnergyClientFactory());
52 gatt_server_factory_.reset(new GattServerFactory());
53 hal::BluetoothInterface::Get()->GetHALInterface()->get_adapter_properties();
57 hal::BluetoothInterface::Get()->RemoveObserver(this);
60 void Adapter::AddObserver(Observer* observer) {
61 std::lock_guard<std::mutex> lock(observers_lock_);
62 observers_.AddObserver(observer);
65 void Adapter::RemoveObserver(Observer* observer) {
66 std::lock_guard<std::mutex> lock(observers_lock_);
67 observers_.RemoveObserver(observer);
70 AdapterState Adapter::GetState() const {
74 bool Adapter::IsEnabled() const {
75 return state_.load() == ADAPTER_STATE_ON;
78 bool Adapter::Enable() {
79 AdapterState current_state = GetState();
80 if (current_state != ADAPTER_STATE_OFF) {
81 LOG(INFO) << "Adapter not disabled - state: "
82 << AdapterStateToString(current_state);
86 // Set the state before calling enable() as there might be a race between here
87 // and the AdapterStateChangedCallback.
88 state_ = ADAPTER_STATE_TURNING_ON;
89 NotifyAdapterStateChanged(current_state, state_);
91 int status = hal::BluetoothInterface::Get()->GetHALInterface()->enable();
92 if (status != BT_STATUS_SUCCESS) {
93 LOG(ERROR) << "Failed to enable Bluetooth - status: "
94 << BtStatusText((const bt_status_t)status);
95 state_ = ADAPTER_STATE_OFF;
96 NotifyAdapterStateChanged(ADAPTER_STATE_TURNING_ON, state_);
103 bool Adapter::Disable() {
105 LOG(INFO) << "Adapter is not enabled";
109 AdapterState current_state = GetState();
111 // Set the state before calling enable() as there might be a race between here
112 // and the AdapterStateChangedCallback.
113 state_ = ADAPTER_STATE_TURNING_OFF;
114 NotifyAdapterStateChanged(current_state, state_);
116 int status = hal::BluetoothInterface::Get()->GetHALInterface()->disable();
117 if (status != BT_STATUS_SUCCESS) {
118 LOG(ERROR) << "Failed to disable Bluetooth - status: "
119 << BtStatusText((const bt_status_t)status);
120 state_ = current_state;
121 NotifyAdapterStateChanged(ADAPTER_STATE_TURNING_OFF, state_);
128 std::string Adapter::GetName() const {
132 bool Adapter::SetName(const std::string& name) {
133 bt_bdname_t hal_name;
134 size_t max_name_len = sizeof(hal_name.name);
136 // Include the \0 byte in size measurement.
137 if (name.length() >= max_name_len) {
138 LOG(ERROR) << "Given name \"" << name << "\" is larger than maximum allowed"
139 << " size: " << max_name_len;
143 strncpy(reinterpret_cast<char*>(hal_name.name), name.c_str(),
146 VLOG(1) << "Setting adapter name: " << name;
148 if (!SetAdapterProperty(BT_PROPERTY_BDNAME, &hal_name, sizeof(hal_name))) {
149 LOG(ERROR) << "Failed to set adapter name: " << name;
156 std::string Adapter::GetAddress() const {
157 return address_.Get();
160 bool Adapter::IsMultiAdvertisementSupported() const {
161 return local_le_features_.max_adv_instance >= kMinAdvInstancesForMultiAdv;
164 LowEnergyClientFactory* Adapter::GetLowEnergyClientFactory() const {
165 return ble_client_factory_.get();
168 GattServerFactory* Adapter::GetGattServerFactory() const {
169 return gatt_server_factory_.get();
172 void Adapter::AdapterStateChangedCallback(bt_state_t state) {
173 LOG(INFO) << "Adapter state changed: " << BtStateText(state);
175 AdapterState prev_state = GetState();
179 state_ = ADAPTER_STATE_OFF;
183 state_ = ADAPTER_STATE_ON;
190 NotifyAdapterStateChanged(prev_state, GetState());
193 void Adapter::AdapterPropertiesCallback(bt_status_t status,
195 bt_property_t* properties) {
196 LOG(INFO) << "Adapter properties changed";
198 if (status != BT_STATUS_SUCCESS) {
199 LOG(ERROR) << "status: " << BtStatusText(status);
203 for (int i = 0; i < num_properties; i++) {
204 bt_property_t* property = properties + i;
205 switch (property->type) {
206 case BT_PROPERTY_BDADDR: {
207 std::string address = BtAddrString(reinterpret_cast<bt_bdaddr_t*>(
209 LOG(INFO) << "Adapter address changed: " << address;
210 address_.Set(address);
213 case BT_PROPERTY_BDNAME: {
214 bt_bdname_t* hal_name = reinterpret_cast<bt_bdname_t*>(property->val);
215 std::string name = reinterpret_cast<char*>(hal_name->name);
216 LOG(INFO) << "Adapter name changed: " << name;
220 case BT_PROPERTY_LOCAL_LE_FEATURES: {
221 if (property->len != sizeof(bt_local_le_features_t)) {
222 LOG(WARNING) << "Malformed value received for property: "
223 << "BT_PROPERTY_LOCAL_LE_FEATURES";
226 bt_local_le_features_t* features =
227 reinterpret_cast<bt_local_le_features_t*>(property->val);
228 memcpy(&local_le_features_, features, sizeof(*features));
229 LOG(INFO) << "Supported LE features updated";
233 VLOG(1) << "Unhandled adapter property: "
234 << BtPropertyText(property->type);
238 // TODO(armansito): notify others of the updated properties
242 bool Adapter::SetAdapterProperty(bt_property_type_t type,
243 void* value, int length) {
247 bt_property_t property;
248 property.len = length;
249 property.val = value;
250 property.type = type;
252 int status = hal::BluetoothInterface::Get()->GetHALInterface()->
253 set_adapter_property(&property);
254 if (status != BT_STATUS_SUCCESS) {
255 VLOG(1) << "Failed to set property";
262 void Adapter::NotifyAdapterStateChanged(AdapterState prev_state,
263 AdapterState new_state) {
264 if (prev_state == new_state)
267 std::lock_guard<std::mutex> lock(observers_lock_);
268 FOR_EACH_OBSERVER(Observer, observers_,
269 OnAdapterStateChanged(this, prev_state, new_state));
272 } // namespace bluetooth