Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame^] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright 2019 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | #include "security_manager_impl.h" |
| 19 | |
| 20 | #include <iostream> |
| 21 | #include <unordered_map> |
| 22 | |
| 23 | #include "os/log.h" |
| 24 | #include "security/pairing/classic_pairing_handler.h" |
| 25 | |
| 26 | using namespace bluetooth::security::internal; |
| 27 | using bluetooth::hci::Device; |
| 28 | using bluetooth::hci::DeviceType; |
| 29 | using bluetooth::security::pairing::PairingHandler; |
| 30 | |
| 31 | namespace { |
| 32 | std::unordered_map<std::shared_ptr<Device>, std::unique_ptr<PairingHandler>> pairing_handler_map_; |
| 33 | |
| 34 | void dispatch_new_pairing_handler(std::shared_ptr<bluetooth::security::record::SecurityRecord> record) { |
| 35 | auto entry = pairing_handler_map_.find(record->GetDevice()); |
| 36 | if (entry != pairing_handler_map_.end()) { |
| 37 | LOG_WARN("Device already has a pairing handler, and is in the middle of pairing!"); |
| 38 | return; |
| 39 | } |
| 40 | std::unique_ptr<PairingHandler> pairing_handler = nullptr; |
| 41 | switch (record->GetDevice()->GetDeviceType()) { |
| 42 | case DeviceType::CLASSIC: |
| 43 | pairing_handler = std::make_unique<bluetooth::security::pairing::ClassicPairingHandler>(record); |
| 44 | break; |
| 45 | default: |
| 46 | ASSERT_LOG(false, "Pairing type %d not implemented!", record->GetDevice()->GetDeviceType()); |
| 47 | } |
| 48 | auto new_entry = std::pair<std::shared_ptr<Device>, std::unique_ptr<PairingHandler>>(record->GetDevice(), |
| 49 | std::move(pairing_handler)); |
| 50 | pairing_handler_map_.insert(std::move(new_entry)); |
| 51 | } |
| 52 | } // namespace |
| 53 | |
| 54 | void SecurityManagerImpl::Init() { |
| 55 | // TODO(optedoblivion): Populate security record memory map from disk |
| 56 | // security_manager_channel_->SetChannelListener(this); |
| 57 | } |
| 58 | |
| 59 | void SecurityManagerImpl::CreateBond(std::shared_ptr<hci::ClassicDevice> device) { |
| 60 | std::string uuid = device->GetUuid(); |
| 61 | // Security record check |
| 62 | // if (device_database_->GetDeviceById(uuid) != nullptr) { |
| 63 | // LOG_WARN("Device already exists in the database"); |
| 64 | // TODO(optedoblivion): Check security record if device is already bonded |
| 65 | // if no security record, need to initiate bonding |
| 66 | // if security record and not bonded, need to initiate bonding |
| 67 | // if security record and is bonded, then do nothing |
| 68 | // } |
| 69 | |
| 70 | // device_database_->AddDevice(device); |
| 71 | // Create security record |
| 72 | // Pass to pairing handler |
| 73 | std::shared_ptr<record::SecurityRecord> record = std::make_shared<record::SecurityRecord>(device); |
| 74 | dispatch_new_pairing_handler(record); |
| 75 | // init the pairing handler |
| 76 | // Update bonded flag on security record |
| 77 | // Update bonded flag on device to BONDING (pairing handler does this) |
| 78 | } |
| 79 | |
| 80 | void SecurityManagerImpl::CancelBond(std::shared_ptr<hci::ClassicDevice> device) { |
| 81 | auto entry = pairing_handler_map_.find(device); |
| 82 | if (entry != pairing_handler_map_.end()) { |
| 83 | pairing_handler_map_.erase(device); |
| 84 | } |
| 85 | // Remove from DB |
| 86 | // device_database_->RemoveDevice(device); |
| 87 | // Remove from map, no longer will the event queue use it |
| 88 | // If currently bonding, cancel pairing handler job |
| 89 | // else, cancel fails |
| 90 | } |
| 91 | |
| 92 | void SecurityManagerImpl::RemoveBond(std::shared_ptr<hci::ClassicDevice> device) { |
| 93 | CancelBond(device); |
| 94 | // Update bonded flag on device to UNBONDED |
| 95 | // Signal disconnect |
| 96 | // Signal unbonding |
| 97 | // Remove security record |
| 98 | // Signal Remove from database |
| 99 | } |
| 100 | |
| 101 | void SecurityManagerImpl::RegisterCallbackListener(ISecurityManagerListener* listener) { |
| 102 | if (listeners_.size() < 1) { |
| 103 | listeners_.push_back(listener); |
| 104 | } else { |
| 105 | bool found = false; |
| 106 | for (auto it = listeners_.begin(); it != listeners_.end(); ++it) { |
| 107 | found = *it == listener; |
| 108 | if (found) break; |
| 109 | } |
| 110 | |
| 111 | if (found) { |
| 112 | LOG_ERROR("Listener has already been registered!"); |
| 113 | } else { |
| 114 | listeners_.push_back(listener); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void SecurityManagerImpl::UnregisterCallbackListener(ISecurityManagerListener* listener) { |
| 120 | if (listeners_.size() < 1) { |
| 121 | LOG_ERROR("Listener has not been registered!"); |
| 122 | } else { |
| 123 | bool found = false; |
| 124 | auto it = listeners_.begin(); |
| 125 | while (it != listeners_.end()) { |
| 126 | found = *it == listener; |
| 127 | if (found) break; |
| 128 | ++it; |
| 129 | } |
| 130 | if (found) { |
| 131 | listeners_.erase(it); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void SecurityManagerImpl::FireDeviceBondedCallbacks(std::shared_ptr<Device> device) { |
| 137 | for (auto& iter : listeners_) { |
| 138 | iter->handler_->Post(common::Bind(&ISecurityManagerListener::OnDeviceBonded, common::Unretained(iter), device)); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void SecurityManagerImpl::FireBondFailedCallbacks(std::shared_ptr<Device> device) { |
| 143 | for (auto& iter : listeners_) { |
| 144 | iter->handler_->Post(common::Bind(&ISecurityManagerListener::OnDeviceBondFailed, common::Unretained(iter), device)); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void SecurityManagerImpl::FireUnbondCallbacks(std::shared_ptr<Device> device) { |
| 149 | for (auto& iter : listeners_) { |
| 150 | iter->handler_->Post(common::Bind(&ISecurityManagerListener::OnDeviceUnbonded, common::Unretained(iter), device)); |
| 151 | } |
| 152 | } |