1 /*
2 * Copyright 2020 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
17 #include "hci/facade/le_initiator_address_facade.h"
18
19 #include "blueberry/facade/hci/le_initiator_address_facade.grpc.pb.h"
20 #include "blueberry/facade/hci/le_initiator_address_facade.pb.h"
21 #include "hci/acl_manager.h"
22 #include "hci/hci_packets.h"
23 #include "hci/octets.h"
24 #include "packet/raw_builder.h"
25
26 using ::grpc::ServerAsyncResponseWriter;
27 using ::grpc::ServerAsyncWriter;
28 using ::grpc::ServerContext;
29
30 using ::bluetooth::packet::RawBuilder;
31
32 namespace bluetooth {
33 namespace hci {
34 namespace facade {
35
36 using namespace blueberry::facade::hci;
37
38 class LeInitiatorAddressFacadeService : public LeInitiatorAddressFacade::Service {
39 public:
LeInitiatorAddressFacadeService(AclManager * acl_manager,::bluetooth::os::Handler * facade_handler)40 LeInitiatorAddressFacadeService(AclManager* acl_manager, ::bluetooth::os::Handler* facade_handler)
41 : acl_manager_(acl_manager),
42 address_manager_(acl_manager_->GetLeAddressManager()),
43 facade_handler_(facade_handler) {
44 log::assert_that(facade_handler_ != nullptr, "assert failed: facade_handler_ != nullptr");
45 }
46
SetPrivacyPolicyForInitiatorAddress(::grpc::ServerContext *,const PrivacyPolicy * request,::google::protobuf::Empty *)47 ::grpc::Status SetPrivacyPolicyForInitiatorAddress(
48 ::grpc::ServerContext* /* context */,
49 const PrivacyPolicy* request,
50 ::google::protobuf::Empty* /* writer */) override {
51 Address address = Address::kEmpty;
52 LeAddressManager::AddressPolicy address_policy =
53 static_cast<LeAddressManager::AddressPolicy>(request->address_policy());
54 if (address_policy == LeAddressManager::AddressPolicy::USE_STATIC_ADDRESS) {
55 log::assert_that(
56 Address::FromString(request->address_with_type().address().address(), address),
57 "assert failed: Address::FromString(request->address_with_type().address().address(), "
58 "address)");
59 }
60 AddressWithType address_with_type(address, static_cast<AddressType>(request->address_with_type().type()));
61 auto minimum_rotation_time = std::chrono::milliseconds(request->minimum_rotation_time());
62 auto maximum_rotation_time = std::chrono::milliseconds(request->maximum_rotation_time());
63 Octet16 irk = {};
64 auto request_irk_length = request->rotation_irk().end() - request->rotation_irk().begin();
65 if (request_irk_length == kOctet16Length) {
66 std::vector<uint8_t> irk_data(request->rotation_irk().begin(), request->rotation_irk().end());
67 std::copy_n(irk_data.begin(), kOctet16Length, irk.begin());
68 acl_manager_->SetPrivacyPolicyForInitiatorAddressForTest(
69 address_policy, address_with_type, irk, minimum_rotation_time, maximum_rotation_time);
70 } else {
71 acl_manager_->SetPrivacyPolicyForInitiatorAddress(
72 address_policy, address_with_type, minimum_rotation_time, maximum_rotation_time);
73 log::assert_that(request_irk_length == 0, "assert failed: request_irk_length == 0");
74 }
75 return ::grpc::Status::OK;
76 }
77
GetCurrentInitiatorAddress(::grpc::ServerContext *,const::google::protobuf::Empty *,::blueberry::facade::BluetoothAddressWithType * response)78 ::grpc::Status GetCurrentInitiatorAddress(
79 ::grpc::ServerContext* /* context */,
80 const ::google::protobuf::Empty* /* request */,
81 ::blueberry::facade::BluetoothAddressWithType* response) override {
82 AddressWithType current = address_manager_->GetInitiatorAddress();
83 auto bluetooth_address = new ::blueberry::facade::BluetoothAddress();
84 bluetooth_address->set_address(current.GetAddress().ToString());
85 response->set_type(static_cast<::blueberry::facade::BluetoothAddressTypeEnum>(current.GetAddressType()));
86 response->set_allocated_address(bluetooth_address);
87 return ::grpc::Status::OK;
88 }
89
NewResolvableAddress(::grpc::ServerContext *,const::google::protobuf::Empty *,::blueberry::facade::BluetoothAddressWithType * response)90 ::grpc::Status NewResolvableAddress(
91 ::grpc::ServerContext* /* context */,
92 const ::google::protobuf::Empty* /* request */,
93 ::blueberry::facade::BluetoothAddressWithType* response) override {
94 AddressWithType another = address_manager_->NewResolvableAddress();
95 auto bluetooth_address = new ::blueberry::facade::BluetoothAddress();
96 bluetooth_address->set_address(another.GetAddress().ToString());
97 response->set_type(static_cast<::blueberry::facade::BluetoothAddressTypeEnum>(another.GetAddressType()));
98 response->set_allocated_address(bluetooth_address);
99 return ::grpc::Status::OK;
100 }
101
102 private:
103 AclManager* acl_manager_;
104 LeAddressManager* address_manager_;
105 ::bluetooth::os::Handler* facade_handler_;
106 };
107
ListDependencies(ModuleList * list) const108 void LeInitiatorAddressFacadeModule::ListDependencies(ModuleList* list) const {
109 ::bluetooth::grpc::GrpcFacadeModule::ListDependencies(list);
110 list->add<AclManager>();
111 }
112
Start()113 void LeInitiatorAddressFacadeModule::Start() {
114 ::bluetooth::grpc::GrpcFacadeModule::Start();
115 service_ = new LeInitiatorAddressFacadeService(GetDependency<AclManager>(), GetHandler());
116 }
117
Stop()118 void LeInitiatorAddressFacadeModule::Stop() {
119 delete service_;
120 ::bluetooth::grpc::GrpcFacadeModule::Stop();
121 }
122
GetService() const123 ::grpc::Service* LeInitiatorAddressFacadeModule::GetService() const {
124 return service_;
125 }
126
127 const ModuleFactory LeInitiatorAddressFacadeModule::Factory =
__anon72e2c09d0102() 128 ::bluetooth::ModuleFactory([]() { return new LeInitiatorAddressFacadeModule(); });
129
130 } // namespace facade
131 } // namespace hci
132 } // namespace bluetooth
133