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