blob: 2f772fa2cec182c8f4fd0330e9137674fb8744f8 [file] [log] [blame]
Martin Brabham7fd25ca2019-12-03 20:13:05 -08001/*
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
24namespace bluetooth {
25namespace security {
26
Martin Brabham560fe3d2019-12-03 21:21:06 -080027class SecurityModuleFacadeService : public SecurityModuleFacade::Service, public ISecurityManagerListener {
Martin Brabham7fd25ca2019-12-03 20:13:05 -080028 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 Brabham560fe3d2019-12-03 21:21:06 -080034 security_module_->GetSecurityManager()->RegisterCallbackListener(this, security_handler_);
Martin Brabham7fd25ca2019-12-03 20:13:05 -080035 }
36
Martin Brabham560fe3d2019-12-03 21:21:06 -080037 ::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 Brabham7fd25ca2019-12-03 20:13:05 -080070 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
77void 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
85void 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
92void SecurityModuleFacadeModule::Stop() {
93 delete service_;
94 ::bluetooth::grpc::GrpcFacadeModule::Stop();
95}
96
97::grpc::Service* SecurityModuleFacadeModule::GetService() const {
98 return service_;
99}
100
101const ModuleFactory SecurityModuleFacadeModule::Factory =
102 ::bluetooth::ModuleFactory([]() { return new SecurityModuleFacadeModule(); });
103
104} // namespace security
105} // namespace bluetooth