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