Martin Brabham | 7fd25ca | 2019-12-03 20:13:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 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 | #include "security/facade.h" |
| 17 | #include "hci/hci_layer.h" |
| 18 | #include "l2cap/classic/l2cap_classic_module.h" |
| 19 | #include "l2cap/le/l2cap_le_module.h" |
| 20 | #include "os/handler.h" |
| 21 | #include "security/facade.grpc.pb.h" |
| 22 | #include "security/security_module.h" |
| 23 | |
| 24 | namespace bluetooth { |
| 25 | namespace security { |
| 26 | |
Martin Brabham | 560fe3d | 2019-12-03 21:21:06 -0800 | [diff] [blame] | 27 | class SecurityModuleFacadeService : public SecurityModuleFacade::Service, public ISecurityManagerListener { |
Martin Brabham | 7fd25ca | 2019-12-03 20:13:05 -0800 | [diff] [blame] | 28 | public: |
| 29 | SecurityModuleFacadeService(SecurityModule* security_module, l2cap::le::L2capLeModule* l2cap_le_module, |
| 30 | l2cap::classic::L2capClassicModule* l2cap_classic_module, hci::HciLayer* hci_layer, |
| 31 | ::bluetooth::os::Handler* security_handler) |
| 32 | : security_module_(security_module), l2cap_le_module_(l2cap_le_module), |
| 33 | l2cap_classic_module_(l2cap_classic_module), security_handler_(security_handler) { |
Martin Brabham | 560fe3d | 2019-12-03 21:21:06 -0800 | [diff] [blame] | 34 | security_module_->GetSecurityManager()->RegisterCallbackListener(this, security_handler_); |
Martin Brabham | 7fd25ca | 2019-12-03 20:13:05 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Martin Brabham | 560fe3d | 2019-12-03 21:21:06 -0800 | [diff] [blame] | 37 | ::grpc::Status CreateBond(::grpc::ServerContext* context, const facade::BluetoothAddressWithType* request, |
| 38 | ::google::protobuf::Empty* response) override { |
| 39 | hci::Address peer; |
| 40 | ASSERT(hci::Address::FromString(request->address().address(), peer)); |
| 41 | hci::AddressType peer_type = hci::AddressType::PUBLIC_DEVICE_ADDRESS; |
| 42 | security_module_->GetSecurityManager()->CreateBond(hci::AddressWithType(peer, peer_type)); |
| 43 | return ::grpc::Status::OK; |
| 44 | } |
| 45 | |
| 46 | ::grpc::Status CancelBond(::grpc::ServerContext* context, const facade::BluetoothAddressWithType* request, |
| 47 | ::google::protobuf::Empty* response) override { |
| 48 | hci::Address peer; |
| 49 | ASSERT(hci::Address::FromString(request->address().address(), peer)); |
| 50 | hci::AddressType peer_type = hci::AddressType::PUBLIC_DEVICE_ADDRESS; |
| 51 | security_module_->GetSecurityManager()->CancelBond(hci::AddressWithType(peer, peer_type)); |
| 52 | return ::grpc::Status::OK; |
| 53 | } |
| 54 | |
| 55 | ::grpc::Status RemoveBond(::grpc::ServerContext* context, const facade::BluetoothAddressWithType* request, |
| 56 | ::google::protobuf::Empty* response) override { |
| 57 | hci::Address peer; |
| 58 | ASSERT(hci::Address::FromString(request->address().address(), peer)); |
| 59 | hci::AddressType peer_type = hci::AddressType::PUBLIC_DEVICE_ADDRESS; |
| 60 | security_module_->GetSecurityManager()->RemoveBond(hci::AddressWithType(peer, peer_type)); |
| 61 | return ::grpc::Status::OK; |
| 62 | } |
| 63 | |
| 64 | void OnDeviceBonded(hci::AddressWithType device) {} |
| 65 | |
| 66 | void OnDeviceUnbonded(hci::AddressWithType device) {} |
| 67 | |
| 68 | void OnDeviceBondFailed(hci::AddressWithType device) {} |
| 69 | |
Martin Brabham | 7fd25ca | 2019-12-03 20:13:05 -0800 | [diff] [blame] | 70 | private: |
| 71 | SecurityModule* security_module_ __attribute__((unused)); |
| 72 | l2cap::le::L2capLeModule* l2cap_le_module_ __attribute__((unused)); |
| 73 | l2cap::classic::L2capClassicModule* l2cap_classic_module_ __attribute__((unused)); |
| 74 | ::bluetooth::os::Handler* security_handler_ __attribute__((unused)); |
| 75 | }; |
| 76 | |
| 77 | void SecurityModuleFacadeModule::ListDependencies(ModuleList* list) { |
| 78 | ::bluetooth::grpc::GrpcFacadeModule::ListDependencies(list); |
| 79 | list->add<SecurityModule>(); |
| 80 | list->add<l2cap::le::L2capLeModule>(); |
| 81 | list->add<l2cap::classic::L2capClassicModule>(); |
| 82 | list->add<hci::HciLayer>(); |
| 83 | } |
| 84 | |
| 85 | void SecurityModuleFacadeModule::Start() { |
| 86 | ::bluetooth::grpc::GrpcFacadeModule::Start(); |
| 87 | service_ = new SecurityModuleFacadeService(GetDependency<SecurityModule>(), GetDependency<l2cap::le::L2capLeModule>(), |
| 88 | GetDependency<l2cap::classic::L2capClassicModule>(), |
| 89 | GetDependency<hci::HciLayer>(), GetHandler()); |
| 90 | } |
| 91 | |
| 92 | void SecurityModuleFacadeModule::Stop() { |
| 93 | delete service_; |
| 94 | ::bluetooth::grpc::GrpcFacadeModule::Stop(); |
| 95 | } |
| 96 | |
| 97 | ::grpc::Service* SecurityModuleFacadeModule::GetService() const { |
| 98 | return service_; |
| 99 | } |
| 100 | |
| 101 | const ModuleFactory SecurityModuleFacadeModule::Factory = |
| 102 | ::bluetooth::ModuleFactory([]() { return new SecurityModuleFacadeModule(); }); |
| 103 | |
| 104 | } // namespace security |
| 105 | } // namespace bluetooth |