OSDN Git Service

9642cc7f5f36082978b6e60b50c5e0259d48eb9d
[android-x86/system-bt.git] / service / adapter.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/adapter.h"
18
19 #include <base/logging.h>
20
21 #include "service/logging_helpers.h"
22
23 namespace bluetooth {
24
25 // static
26 const char Adapter::kDefaultAddress[] = "00:00:00:00:00:00";
27 // static
28 const char Adapter::kDefaultName[] = "not-initialized";
29
30 // The minimum number of advertising instances required for multi-advertisement
31 // support.
32 //
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;
38
39 void Adapter::Observer::OnAdapterStateChanged(Adapter* adapter,
40                                               AdapterState prev_state,
41                                               AdapterState new_state) {
42   // Default implementation does nothing
43 }
44
45 Adapter::Adapter()
46     : state_(ADAPTER_STATE_OFF),
47       address_(kDefaultAddress),
48       name_(kDefaultName) {
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();
54 }
55
56 Adapter::~Adapter() {
57   hal::BluetoothInterface::Get()->RemoveObserver(this);
58 }
59
60 void Adapter::AddObserver(Observer* observer) {
61   std::lock_guard<std::mutex> lock(observers_lock_);
62   observers_.AddObserver(observer);
63 }
64
65 void Adapter::RemoveObserver(Observer* observer) {
66   std::lock_guard<std::mutex> lock(observers_lock_);
67   observers_.RemoveObserver(observer);
68 }
69
70 AdapterState Adapter::GetState() const {
71   return state_.load();
72 }
73
74 bool Adapter::IsEnabled() const {
75   return state_.load() == ADAPTER_STATE_ON;
76 }
77
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);
83     return false;
84   }
85
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_);
90
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_);
97     return false;
98   }
99
100   return true;
101 }
102
103 bool Adapter::Disable() {
104   if (!IsEnabled()) {
105     LOG(INFO) << "Adapter is not enabled";
106     return false;
107   }
108
109   AdapterState current_state = GetState();
110
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_);
115
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_);
122     return false;
123   }
124
125   return true;
126 }
127
128 std::string Adapter::GetName() const {
129   return name_.Get();
130 }
131
132 bool Adapter::SetName(const std::string& name) {
133   bt_bdname_t hal_name;
134   size_t max_name_len = sizeof(hal_name.name);
135
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;
140     return false;
141   }
142
143   strncpy(reinterpret_cast<char*>(hal_name.name), name.c_str(),
144           name.length() + 1);
145
146   VLOG(1) << "Setting adapter name: " << name;
147
148   if (!SetAdapterProperty(BT_PROPERTY_BDNAME, &hal_name, sizeof(hal_name))) {
149     LOG(ERROR) << "Failed to set adapter name: " << name;
150     return false;
151   }
152
153   return true;
154 }
155
156 std::string Adapter::GetAddress() const {
157   return address_.Get();
158 }
159
160 bool Adapter::IsMultiAdvertisementSupported() const {
161   return local_le_features_.max_adv_instance >= kMinAdvInstancesForMultiAdv;
162 }
163
164 LowEnergyClientFactory* Adapter::GetLowEnergyClientFactory() const {
165   return ble_client_factory_.get();
166 }
167
168 GattServerFactory* Adapter::GetGattServerFactory() const {
169   return gatt_server_factory_.get();
170 }
171
172 void Adapter::AdapterStateChangedCallback(bt_state_t state) {
173   LOG(INFO) << "Adapter state changed: " << BtStateText(state);
174
175   AdapterState prev_state = GetState();
176
177   switch (state) {
178   case BT_STATE_OFF:
179     state_ = ADAPTER_STATE_OFF;
180     break;
181
182   case BT_STATE_ON:
183     state_ = ADAPTER_STATE_ON;
184     break;
185
186   default:
187     NOTREACHED();
188   }
189
190   NotifyAdapterStateChanged(prev_state, GetState());
191 }
192
193 void Adapter::AdapterPropertiesCallback(bt_status_t status,
194                                         int num_properties,
195                                         bt_property_t* properties) {
196   LOG(INFO) << "Adapter properties changed";
197
198   if (status != BT_STATUS_SUCCESS) {
199     LOG(ERROR) << "status: " << BtStatusText(status);
200     return;
201   }
202
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*>(
208             property->val));
209         LOG(INFO) << "Adapter address changed: " << address;
210         address_.Set(address);
211         break;
212       }
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;
217         name_.Set(name);
218         break;
219       }
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";
224           break;
225         }
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";
230         break;
231       }
232       default:
233         VLOG(1) << "Unhandled adapter property: "
234                 << BtPropertyText(property->type);
235         break;
236     }
237
238     // TODO(armansito): notify others of the updated properties
239   }
240 }
241
242 bool Adapter::SetAdapterProperty(bt_property_type_t type,
243                                  void* value, int length) {
244   CHECK(length > 0);
245   CHECK(value);
246
247   bt_property_t property;
248   property.len = length;
249   property.val = value;
250   property.type = type;
251
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";
256     return false;
257   }
258
259   return true;
260 }
261
262 void Adapter::NotifyAdapterStateChanged(AdapterState prev_state,
263                                         AdapterState new_state) {
264   if (prev_state == new_state)
265     return;
266
267   std::lock_guard<std::mutex> lock(observers_lock_);
268   FOR_EACH_OBSERVER(Observer, observers_,
269                     OnAdapterStateChanged(this, prev_state, new_state));
270 }
271
272 }  // namespace bluetooth