Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -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 | // |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 16 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 17 | #include "service/gatt_server.h" |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 18 | |
Arman Uguray | 234138e | 2015-10-06 15:56:36 -0700 | [diff] [blame] | 19 | #include "service/common/bluetooth/util/address_helper.h" |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 20 | #include "service/logging_helpers.h" |
Jakub Pawlowski | a2d3cb2 | 2017-06-09 14:02:03 -0700 | [diff] [blame] | 21 | #include "stack/include/bt_types.h" |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 22 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 23 | using std::lock_guard; |
| 24 | using std::mutex; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 25 | |
| 26 | namespace bluetooth { |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 27 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 28 | // GattServer implementation |
| 29 | // ======================================================== |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 30 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 31 | GattServer::GattServer(const UUID& uuid, int server_id) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 32 | : app_identifier_(uuid), server_id_(server_id), delegate_(nullptr) {} |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 33 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 34 | GattServer::~GattServer() { |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 35 | // Automatically unregister the server. |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 36 | VLOG(1) << "GattServer unregistering: " << server_id_; |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 37 | |
| 38 | // Unregister as observer so we no longer receive any callbacks. |
| 39 | hal::BluetoothGattInterface::Get()->RemoveServerObserver(this); |
| 40 | |
| 41 | // Unregister this server, stop all services, and ignore the result. |
| 42 | // TODO(armansito): stop and remove all services here? unregister_server |
| 43 | // should really take care of that. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 44 | hal::BluetoothGattInterface::Get() |
| 45 | ->GetServerHALInterface() |
| 46 | ->unregister_server(server_id_); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 49 | void GattServer::SetDelegate(Delegate* delegate) { |
| 50 | lock_guard<mutex> lock(mutex_); |
| 51 | delegate_ = delegate; |
| 52 | } |
| 53 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 54 | const UUID& GattServer::GetAppIdentifier() const { return app_identifier_; } |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 55 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 56 | int GattServer::GetInstanceId() const { return server_id_; } |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 57 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 58 | bool GattServer::AddService(const bluetooth::Service& service, |
| 59 | const ResultCallback& callback) { |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 60 | VLOG(1) << __func__ << " server_id: " << server_id_; |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 61 | lock_guard<mutex> lock(mutex_); |
| 62 | |
| 63 | if (!callback) { |
| 64 | LOG(ERROR) << "|callback| cannot be NULL"; |
| 65 | return false; |
| 66 | } |
| 67 | |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 68 | std::vector<btgatt_db_element_t> svc; |
| 69 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 70 | svc.push_back({.type = (service.primary() ? BTGATT_DB_PRIMARY_SERVICE |
| 71 | : BTGATT_DB_SECONDARY_SERVICE), |
| 72 | .uuid = service.uuid().GetBlueDroid()}); |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 73 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 74 | for (const auto& characteristic : service.characteristics()) { |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 75 | svc.push_back({.type = BTGATT_DB_CHARACTERISTIC, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 76 | .uuid = characteristic.uuid().GetBlueDroid(), |
| 77 | .properties = characteristic.properties(), |
| 78 | .permissions = characteristic.permissions()}); |
| 79 | for (const auto& descriptor : characteristic.descriptors()) |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 80 | svc.push_back({.type = BTGATT_DB_DESCRIPTOR, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 81 | .uuid = descriptor.uuid().GetBlueDroid(), |
| 82 | .permissions = descriptor.permissions()}); |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 85 | for (const auto& incl_svc : service.included_services()) |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 86 | svc.push_back({.type = BTGATT_DB_INCLUDED_SERVICE, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 87 | .attribute_handle = incl_svc.handle()}); |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 88 | |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 89 | pending_end_decl_cb_ = callback; |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 90 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 91 | bt_status_t status = |
| 92 | hal::BluetoothGattInterface::Get()->GetServerHALInterface()->add_service( |
| 93 | server_id_, svc); |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 94 | if (status != BT_STATUS_SUCCESS) { |
| 95 | LOG(ERROR) << "Failed to initiate call to populate GATT service"; |
| 96 | CleanUpPendingData(); |
| 97 | return false; |
| 98 | } |
| 99 | |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 103 | bool GattServer::SendResponse(const std::string& device_address, int request_id, |
| 104 | GATTError error, int offset, |
| 105 | const std::vector<uint8_t>& value) { |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 106 | VLOG(1) << __func__ << " - server_id: " << server_id_ |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 107 | << " device_address: " << device_address |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 108 | << " request_id: " << request_id << " error: " << error |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 109 | << " offset: " << offset; |
| 110 | lock_guard<mutex> lock(mutex_); |
| 111 | |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 112 | RawAddress addr; |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 113 | if (!util::BdAddrFromString(device_address, &addr)) { |
| 114 | LOG(ERROR) << "Invalid device address given: " << device_address; |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | if (value.size() + offset > BTGATT_MAX_ATTR_LEN) { |
| 119 | LOG(ERROR) << "Value is too large"; |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | // Find the correct connection ID for |device_address| and |request_id|. |
| 124 | auto iter = conn_addr_map_.find(device_address); |
| 125 | if (iter == conn_addr_map_.end()) { |
| 126 | LOG(ERROR) << "No known connections for device address: " << device_address; |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | std::shared_ptr<Connection> connection; |
Chih-Hung Hsieh | 5dc0d15 | 2016-08-17 14:12:51 -0700 | [diff] [blame] | 131 | for (const auto& tmp : iter->second) { |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 132 | if (tmp->request_id_to_handle.find(request_id) == |
| 133 | tmp->request_id_to_handle.end()) |
| 134 | continue; |
| 135 | |
| 136 | connection = tmp; |
| 137 | } |
| 138 | |
| 139 | if (!connection) { |
| 140 | LOG(ERROR) << "Pending request with ID " << request_id |
| 141 | << " not found for device with BD_ADDR: " << device_address; |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | btgatt_response_t response; |
| 146 | memset(&response, 0, sizeof(response)); |
| 147 | |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 148 | // We keep -1 as the handle for "Execute Write Request". In that case, |
| 149 | // there is no need to populate the response data. Just send zeros back. |
| 150 | int handle = connection->request_id_to_handle[request_id]; |
| 151 | response.handle = handle; |
| 152 | response.attr_value.handle = handle; |
| 153 | if (handle != -1) { |
| 154 | memcpy(response.attr_value.value, value.data(), value.size()); |
| 155 | response.attr_value.offset = offset; |
| 156 | response.attr_value.len = value.size(); |
| 157 | } |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 158 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 159 | bt_status_t result = |
| 160 | hal::BluetoothGattInterface::Get() |
| 161 | ->GetServerHALInterface() |
Jakub Pawlowski | 96ac0a3 | 2017-06-21 00:00:18 -0700 | [diff] [blame] | 162 | ->send_response(connection->conn_id, request_id, error, response); |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 163 | if (result != BT_STATUS_SUCCESS) { |
| 164 | LOG(ERROR) << "Failed to initiate call to send GATT response"; |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | connection->request_id_to_handle.erase(request_id); |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 173 | bool GattServer::SendNotification(const std::string& device_address, |
| 174 | const uint16_t handle, bool confirm, |
| 175 | const std::vector<uint8_t>& value, |
| 176 | const GattCallback& callback) { |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 177 | VLOG(1) << " - server_id: " << server_id_ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 178 | << " device_address: " << device_address << " confirm: " << confirm; |
Arman Uguray | cd644e3 | 2015-10-01 16:36:38 -0700 | [diff] [blame] | 179 | lock_guard<mutex> lock(mutex_); |
| 180 | |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 181 | RawAddress addr; |
Arman Uguray | cd644e3 | 2015-10-01 16:36:38 -0700 | [diff] [blame] | 182 | if (!util::BdAddrFromString(device_address, &addr)) { |
| 183 | LOG(ERROR) << "Invalid device address given: " << device_address; |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | // Get the connection IDs for which we will send this notification. |
| 188 | auto conn_iter = conn_addr_map_.find(device_address); |
| 189 | if (conn_iter == conn_addr_map_.end()) { |
| 190 | LOG(ERROR) << "No known connections for device with address: " |
| 191 | << device_address; |
| 192 | return false; |
| 193 | } |
| 194 | |
Arman Uguray | cd644e3 | 2015-10-01 16:36:38 -0700 | [diff] [blame] | 195 | std::shared_ptr<PendingIndication> pending_ind( |
| 196 | new PendingIndication(callback)); |
| 197 | |
| 198 | // Send the notification/indication on all matching connections. |
| 199 | int send_count = 0; |
Chih-Hung Hsieh | 5dc0d15 | 2016-08-17 14:12:51 -0700 | [diff] [blame] | 200 | for (const auto& conn : conn_iter->second) { |
Arman Uguray | cd644e3 | 2015-10-01 16:36:38 -0700 | [diff] [blame] | 201 | // Make sure that one isn't already pending for this connection. |
| 202 | if (pending_indications_.find(conn->conn_id) != |
| 203 | pending_indications_.end()) { |
| 204 | VLOG(1) << "A" << (confirm ? "n indication" : " notification") |
| 205 | << " is already pending for connection: " << conn->conn_id; |
| 206 | continue; |
| 207 | } |
| 208 | |
| 209 | // The HAL API takes char* rather const char* for |value|, so we have to |
| 210 | // cast away the const. |
| 211 | // TODO(armansito): Make HAL accept const char*. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 212 | bt_status_t status = hal::BluetoothGattInterface::Get() |
| 213 | ->GetServerHALInterface() |
| 214 | ->send_indication(server_id_, handle, |
| 215 | conn->conn_id, confirm, value); |
Arman Uguray | cd644e3 | 2015-10-01 16:36:38 -0700 | [diff] [blame] | 216 | |
| 217 | // Increment the send count if this was successful. We don't immediately |
| 218 | // fail if the HAL returned an error. It's better to report success as long |
| 219 | // as we sent out at least one notification to this device as |
| 220 | // multi-transport GATT connections from the same BD_ADDR will be rare |
| 221 | // enough already. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 222 | if (status != BT_STATUS_SUCCESS) continue; |
Arman Uguray | cd644e3 | 2015-10-01 16:36:38 -0700 | [diff] [blame] | 223 | |
| 224 | send_count++; |
| 225 | pending_indications_[conn->conn_id] = pending_ind; |
| 226 | } |
| 227 | |
| 228 | if (send_count == 0) { |
| 229 | LOG(ERROR) << "Failed to send notifications/indications to device: " |
| 230 | << device_address; |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | return true; |
| 235 | } |
| 236 | |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 237 | void GattServer::ConnectionCallback( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 238 | hal::BluetoothGattInterface* /* gatt_iface */, int conn_id, int server_id, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 239 | int connected, const RawAddress& bda) { |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 240 | lock_guard<mutex> lock(mutex_); |
| 241 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 242 | if (server_id != server_id_) return; |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 243 | |
| 244 | std::string device_address = BtAddrString(&bda); |
| 245 | |
| 246 | VLOG(1) << __func__ << " conn_id: " << conn_id << " connected: " << connected |
| 247 | << " BD_ADDR: " << device_address; |
| 248 | |
| 249 | if (!connected) { |
| 250 | // Erase the entry if we were connected to it. |
| 251 | VLOG(1) << "No longer connected: " << device_address; |
| 252 | conn_id_map_.erase(conn_id); |
| 253 | auto iter = conn_addr_map_.find(device_address); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 254 | if (iter == conn_addr_map_.end()) return; |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 255 | |
| 256 | // Remove the appropriate connection objects in the address. |
| 257 | for (auto conn_iter = iter->second.begin(); conn_iter != iter->second.end(); |
| 258 | ++conn_iter) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 259 | if ((*conn_iter)->conn_id != conn_id) continue; |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 260 | |
| 261 | iter->second.erase(conn_iter); |
| 262 | break; |
| 263 | } |
| 264 | |
Jakub Pawlowski | 7932727 | 2016-07-07 16:40:11 -0700 | [diff] [blame] | 265 | if (delegate_) |
| 266 | delegate_->OnConnectionStateChanged(this, device_address, false); |
| 267 | |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 268 | return; |
| 269 | } |
| 270 | |
| 271 | if (conn_id_map_.find(conn_id) != conn_id_map_.end()) { |
| 272 | LOG(WARNING) << "Connection entry already exists; " |
| 273 | << "ignoring ConnectionCallback"; |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | LOG(INFO) << "Added connection entry for conn_id: " << conn_id |
| 278 | << " device address: " << device_address; |
| 279 | std::shared_ptr<Connection> connection(new Connection(conn_id, bda)); |
| 280 | conn_id_map_[conn_id] = connection; |
| 281 | conn_addr_map_[device_address].push_back(connection); |
Jakub Pawlowski | 7932727 | 2016-07-07 16:40:11 -0700 | [diff] [blame] | 282 | |
| 283 | if (delegate_) |
| 284 | delegate_->OnConnectionStateChanged(this, device_address, true); |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 287 | void GattServer::ServiceAddedCallback(hal::BluetoothGattInterface* gatt_iface, |
| 288 | int status, int server_id, |
Pavlin Radoslavov | b324a8d | 2016-12-09 17:50:59 -0800 | [diff] [blame] | 289 | std::vector<btgatt_db_element_t> svc) { |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 290 | lock_guard<mutex> lock(mutex_); |
| 291 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 292 | if (server_id != server_id_) return; |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 293 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 294 | VLOG(1) << __func__ << " - status: " << status << " server_id: " << server_id |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 295 | << " first handle: " << svc[0].attribute_handle |
| 296 | << " service UUID: " << UUID(svc[0].uuid).ToString() |
| 297 | << " count: " << svc.size(); |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 298 | |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 299 | Service service(svc[0].attribute_handle, true, UUID(svc[0].uuid), {}, {}); |
| 300 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 301 | for (size_t i = 1; i < svc.size(); i++) { |
| 302 | const btgatt_db_element_t& curr = svc[i]; |
| 303 | VLOG(1) << " - processing item no: " << i |
| 304 | << " handle: " << curr.attribute_handle; |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 305 | if (curr.type == BTGATT_DB_CHARACTERISTIC) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 306 | service.characteristics().push_back({curr.attribute_handle, |
| 307 | UUID(curr.uuid), |
| 308 | curr.properties, |
| 309 | curr.permissions, |
| 310 | {}}); |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 311 | } else if (curr.type == BTGATT_DB_DESCRIPTOR) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 312 | service.characteristics().back().descriptors().push_back( |
| 313 | {curr.attribute_handle, UUID(curr.uuid), curr.permissions}); |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 314 | } else if (svc[i].type == BTGATT_DB_INCLUDED_SERVICE) { |
| 315 | } |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 318 | pending_end_decl_cb_((bluetooth::BLEStatus)status, service); |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 319 | |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 320 | CleanUpPendingData(); |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void GattServer::ServiceStoppedCallback( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 324 | hal::BluetoothGattInterface* /* gatt_iface */, int /* status */, |
| 325 | int /* server_id */, int /* service_handle */) { |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 326 | // TODO(armansito): Support stopping a service. |
| 327 | } |
| 328 | |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 329 | void GattServer::RequestReadCharacteristicCallback( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 330 | hal::BluetoothGattInterface* /* gatt_iface */, int conn_id, int trans_id, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 331 | const RawAddress& bda, int attribute_handle, int offset, bool is_long) { |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 332 | lock_guard<mutex> lock(mutex_); |
| 333 | |
| 334 | // Check to see if we know about this connection. Otherwise ignore the |
| 335 | // request. |
| 336 | auto conn = GetConnection(conn_id, bda, trans_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 337 | if (!conn) return; |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 338 | |
| 339 | std::string device_address = BtAddrString(&bda); |
| 340 | |
| 341 | VLOG(1) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id |
| 342 | << " BD_ADDR: " << device_address |
| 343 | << " attribute_handle: " << attribute_handle << " offset: " << offset |
| 344 | << " is_long: " << is_long; |
| 345 | |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 346 | conn->request_id_to_handle[trans_id] = attribute_handle; |
| 347 | |
| 348 | // If there is no delegate then there is nobody to handle request. The request |
| 349 | // will eventually timeout and we should get a connection update that |
| 350 | // terminates the connection. |
| 351 | if (!delegate_) { |
| 352 | // TODO(armansito): Require a delegate at server registration so that this |
| 353 | // is never possible. |
| 354 | LOG(WARNING) << "No delegate was assigned to GattServer. Incoming request " |
| 355 | << "will time out."; |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 356 | return; |
| 357 | } |
| 358 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 359 | delegate_->OnCharacteristicReadRequest(this, device_address, trans_id, offset, |
| 360 | is_long, attribute_handle); |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 361 | } |
| 362 | void GattServer::RequestReadDescriptorCallback( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 363 | hal::BluetoothGattInterface* /* gatt_iface */, int conn_id, int trans_id, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 364 | const RawAddress& bda, int attribute_handle, int offset, bool is_long) { |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 365 | lock_guard<mutex> lock(mutex_); |
| 366 | |
| 367 | // Check to see if we know about this connection. Otherwise ignore the |
| 368 | // request. |
| 369 | auto conn = GetConnection(conn_id, bda, trans_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 370 | if (!conn) return; |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 371 | |
| 372 | std::string device_address = BtAddrString(&bda); |
| 373 | |
| 374 | VLOG(1) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id |
| 375 | << " BD_ADDR: " << device_address |
| 376 | << " attribute_handle: " << attribute_handle << " offset: " << offset |
| 377 | << " is_long: " << is_long; |
| 378 | |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 379 | conn->request_id_to_handle[trans_id] = attribute_handle; |
| 380 | |
| 381 | // If there is no delegate then there is nobody to handle request. The request |
| 382 | // will eventually timeout and we should get a connection update that |
| 383 | // terminates the connection. |
| 384 | if (!delegate_) { |
| 385 | // TODO(armansito): Require a delegate at server registration so that this |
| 386 | // is never possible. |
| 387 | LOG(WARNING) << "No delegate was assigned to GattServer. Incoming request " |
| 388 | << "will time out."; |
| 389 | return; |
| 390 | } |
| 391 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 392 | delegate_->OnDescriptorReadRequest(this, device_address, trans_id, offset, |
| 393 | is_long, attribute_handle); |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 396 | void GattServer::RequestWriteCharacteristicCallback( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 397 | hal::BluetoothGattInterface* /* gatt_iface */, int conn_id, int trans_id, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 398 | const RawAddress& bda, int attr_handle, int offset, bool need_rsp, |
Pavlin Radoslavov | b324a8d | 2016-12-09 17:50:59 -0800 | [diff] [blame] | 399 | bool is_prep, std::vector<uint8_t> value) { |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 400 | lock_guard<mutex> lock(mutex_); |
| 401 | |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 402 | // Check to see if we know about this connection. Otherwise ignore the |
| 403 | // request. |
| 404 | auto conn = GetConnection(conn_id, bda, trans_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 405 | if (!conn) return; |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 406 | |
| 407 | std::string device_address = BtAddrString(&bda); |
| 408 | |
| 409 | VLOG(1) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 410 | << " BD_ADDR: " << device_address << " attr_handle: " << attr_handle |
| 411 | << " offset: " << offset << " length: " << value.size() |
| 412 | << " need_rsp: " << need_rsp << " is_prep: " << is_prep; |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 413 | |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 414 | // Store the request ID only if this is not a write-without-response. If |
| 415 | // another request occurs after this with the same request ID, then we'll |
| 416 | // simply process it normally, though that shouldn't ever happen. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 417 | if (need_rsp) conn->request_id_to_handle[trans_id] = attr_handle; |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 418 | |
| 419 | // If there is no delegate then there is nobody to handle request. The request |
| 420 | // will eventually timeout and we should get a connection update that |
| 421 | // terminates the connection. |
| 422 | if (!delegate_) { |
| 423 | // TODO(armansito): Require a delegate at server registration so that this |
| 424 | // is never possible. |
| 425 | LOG(WARNING) << "No delegate was assigned to GattServer. Incoming request " |
| 426 | << "will time out."; |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 427 | return; |
| 428 | } |
| 429 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 430 | delegate_->OnCharacteristicWriteRequest(this, device_address, trans_id, |
| 431 | offset, is_prep, need_rsp, |
| 432 | std::move(value), attr_handle); |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void GattServer::RequestWriteDescriptorCallback( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 436 | hal::BluetoothGattInterface* /* gatt_iface */, int conn_id, int trans_id, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 437 | const RawAddress& bda, int attr_handle, int offset, bool need_rsp, |
Pavlin Radoslavov | b324a8d | 2016-12-09 17:50:59 -0800 | [diff] [blame] | 438 | bool is_prep, std::vector<uint8_t> value) { |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 439 | lock_guard<mutex> lock(mutex_); |
| 440 | |
| 441 | // Check to see if we know about this connection. Otherwise ignore the |
| 442 | // request. |
| 443 | auto conn = GetConnection(conn_id, bda, trans_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 444 | if (!conn) return; |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 445 | |
| 446 | std::string device_address = BtAddrString(&bda); |
| 447 | |
| 448 | VLOG(1) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 449 | << " BD_ADDR: " << device_address << " attr_handle: " << attr_handle |
| 450 | << " offset: " << offset << " length: " << value.size() |
| 451 | << " need_rsp: " << need_rsp << " is_prep: " << is_prep; |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 452 | |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 453 | // Store the request ID only if this is not a write-without-response. If |
| 454 | // another request occurs after this with the same request ID, then we'll |
| 455 | // simply process it normally, though that shouldn't ever happen. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 456 | if (need_rsp) conn->request_id_to_handle[trans_id] = attr_handle; |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 457 | |
| 458 | // If there is no delegate then there is nobody to handle request. The request |
| 459 | // will eventually timeout and we should get a connection update that |
| 460 | // terminates the connection. |
| 461 | if (!delegate_) { |
| 462 | // TODO(armansito): Require a delegate at server registration so that this |
| 463 | // is never possible. |
| 464 | LOG(WARNING) << "No delegate was assigned to GattServer. Incoming request " |
| 465 | << "will time out."; |
| 466 | return; |
| 467 | } |
| 468 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 469 | delegate_->OnDescriptorWriteRequest(this, device_address, trans_id, offset, |
| 470 | is_prep, need_rsp, std::move(value), |
| 471 | attr_handle); |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void GattServer::RequestExecWriteCallback( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 475 | hal::BluetoothGattInterface* /* gatt_iface */, int conn_id, int trans_id, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 476 | const RawAddress& bda, int exec_write) { |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 477 | lock_guard<mutex> lock(mutex_); |
| 478 | |
| 479 | // Check to see if we know about this connection. Otherwise ignore the |
| 480 | // request. |
| 481 | auto conn = GetConnection(conn_id, bda, trans_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 482 | if (!conn) return; |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 483 | |
| 484 | std::string device_address = BtAddrString(&bda); |
| 485 | |
| 486 | VLOG(1) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id |
| 487 | << " BD_ADDR: " << device_address << " exec_write: " << exec_write; |
| 488 | |
| 489 | // Just store a dummy invalid handle as this request doesn't apply to a |
| 490 | // specific handle. |
| 491 | conn->request_id_to_handle[trans_id] = -1; |
| 492 | |
| 493 | // If there is no delegate then there is nobody to handle request. The request |
| 494 | // will eventually timeout and we should get a connection update that |
| 495 | // terminates the connection. |
| 496 | if (!delegate_) { |
| 497 | // TODO(armansito): Require a delegate at server registration so that this |
| 498 | // is never possible. |
| 499 | LOG(WARNING) << "No delegate was assigned to GattServer. Incoming request " |
| 500 | << "will time out."; |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | delegate_->OnExecuteWriteRequest(this, device_address, trans_id, exec_write); |
| 505 | } |
| 506 | |
Arman Uguray | cd644e3 | 2015-10-01 16:36:38 -0700 | [diff] [blame] | 507 | void GattServer::IndicationSentCallback( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 508 | hal::BluetoothGattInterface* /* gatt_iface */, int conn_id, int status) { |
Arman Uguray | cd644e3 | 2015-10-01 16:36:38 -0700 | [diff] [blame] | 509 | VLOG(1) << __func__ << " conn_id: " << conn_id << " status: " << status; |
| 510 | lock_guard<mutex> lock(mutex_); |
| 511 | |
| 512 | const auto& pending_ind_iter = pending_indications_.find(conn_id); |
| 513 | if (pending_ind_iter == pending_indications_.end()) { |
| 514 | VLOG(1) << "Unknown connection: " << conn_id; |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | std::shared_ptr<PendingIndication> pending_ind = pending_ind_iter->second; |
| 519 | pending_indications_.erase(pending_ind_iter); |
| 520 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 521 | if (status == BT_STATUS_SUCCESS) pending_ind->has_success = true; |
Arman Uguray | cd644e3 | 2015-10-01 16:36:38 -0700 | [diff] [blame] | 522 | |
| 523 | // Invoke it if this was the last reference to the confirmation callback. |
| 524 | if (pending_ind.unique() && pending_ind->callback) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 525 | pending_ind->callback(pending_ind->has_success |
| 526 | ? GATT_ERROR_NONE |
| 527 | : static_cast<GATTError>(status)); |
Arman Uguray | cd644e3 | 2015-10-01 16:36:38 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 531 | void GattServer::CleanUpPendingData() { |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 532 | pending_end_decl_cb_ = ResultCallback(); |
Arman Uguray | 9161374 | 2015-09-24 22:45:16 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 535 | std::shared_ptr<GattServer::Connection> GattServer::GetConnection( |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 536 | int conn_id, const RawAddress& bda, int request_id) { |
Arman Uguray | 514bf60 | 2015-09-29 19:38:03 -0700 | [diff] [blame] | 537 | auto iter = conn_id_map_.find(conn_id); |
| 538 | if (iter == conn_id_map_.end()) { |
| 539 | VLOG(1) << "Connection doesn't belong to this server"; |
| 540 | return nullptr; |
| 541 | } |
| 542 | |
| 543 | auto conn = iter->second; |
| 544 | if (conn->bdaddr != bda) { |
| 545 | LOG(WARNING) << "BD_ADDR: " << BtAddrString(&bda) << " doesn't match " |
| 546 | << "connection ID: " << conn_id; |
| 547 | return nullptr; |
| 548 | } |
| 549 | |
| 550 | if (conn->request_id_to_handle.find(request_id) != |
| 551 | conn->request_id_to_handle.end()) { |
| 552 | VLOG(1) << "Request with ID: " << request_id << " already exists for " |
| 553 | << " connection: " << conn_id; |
| 554 | return nullptr; |
| 555 | } |
| 556 | |
| 557 | return conn; |
| 558 | } |
| 559 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 560 | // GattServerFactory implementation |
| 561 | // ======================================================== |
| 562 | |
| 563 | GattServerFactory::GattServerFactory() { |
| 564 | hal::BluetoothGattInterface::Get()->AddServerObserver(this); |
| 565 | } |
| 566 | |
| 567 | GattServerFactory::~GattServerFactory() { |
| 568 | hal::BluetoothGattInterface::Get()->RemoveServerObserver(this); |
| 569 | } |
| 570 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 571 | bool GattServerFactory::RegisterInstance(const UUID& uuid, |
| 572 | const RegisterCallback& callback) { |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 573 | VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); |
| 574 | lock_guard<mutex> lock(pending_calls_lock_); |
| 575 | |
| 576 | if (pending_calls_.find(uuid) != pending_calls_.end()) { |
| 577 | LOG(ERROR) << "GATT-server client with given UUID already being registered " |
| 578 | << " - UUID: " << uuid.ToString(); |
| 579 | return false; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 582 | const btgatt_server_interface_t* hal_iface = |
| 583 | hal::BluetoothGattInterface::Get()->GetServerHALInterface(); |
| 584 | bt_uuid_t app_uuid = uuid.GetBlueDroid(); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 585 | |
Jakub Pawlowski | 96ac0a3 | 2017-06-21 00:00:18 -0700 | [diff] [blame] | 586 | if (hal_iface->register_server(app_uuid) != BT_STATUS_SUCCESS) return false; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 587 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 588 | pending_calls_[uuid] = callback; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 589 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 590 | return true; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 593 | void GattServerFactory::RegisterServerCallback( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 594 | hal::BluetoothGattInterface* gatt_iface, int status, int server_id, |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 595 | const bt_uuid_t& app_uuid) { |
| 596 | UUID uuid(app_uuid); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 597 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 598 | VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); |
| 599 | lock_guard<mutex> lock(pending_calls_lock_); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 600 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 601 | auto iter = pending_calls_.find(uuid); |
| 602 | if (iter == pending_calls_.end()) { |
| 603 | VLOG(1) << "Ignoring callback for unknown app_id: " << uuid.ToString(); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 604 | return; |
| 605 | } |
| 606 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 607 | // No need to construct a server if the call wasn't successful. |
| 608 | std::unique_ptr<GattServer> server; |
| 609 | BLEStatus result = BLE_STATUS_FAILURE; |
| 610 | if (status == BT_STATUS_SUCCESS) { |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 611 | server.reset(new GattServer(uuid, server_id)); |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 612 | |
Jakub Pawlowski | 25689c1 | 2016-01-20 16:24:03 -0800 | [diff] [blame] | 613 | gatt_iface->AddServerObserver(server.get()); |
Arman Uguray | df0b271 | 2015-09-23 17:25:54 -0700 | [diff] [blame] | 614 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 615 | result = BLE_STATUS_SUCCESS; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 618 | // Notify the result via the result callback. |
| 619 | iter->second(result, uuid, std::move(server)); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 620 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 621 | pending_calls_.erase(iter); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 624 | } // namespace bluetooth |