• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "hal/facade.h"
18 
19 #include <memory>
20 #include <mutex>
21 
22 #include "grpc/grpc_event_queue.h"
23 #include "hal/facade.grpc.pb.h"
24 #include "hal/hci_hal.h"
25 
26 using ::grpc::ServerAsyncResponseWriter;
27 using ::grpc::ServerAsyncWriter;
28 using ::grpc::ServerContext;
29 
30 namespace bluetooth {
31 namespace hal {
32 
33 class HciHalFacadeService : public HciHalFacade::Service, public ::bluetooth::hal::HciHalCallbacks {
34  public:
HciHalFacadeService(HciHal * hal)35   explicit HciHalFacadeService(HciHal* hal) : hal_(hal) {
36     hal->registerIncomingPacketCallback(this);
37   }
38 
~HciHalFacadeService()39   ~HciHalFacadeService() override {
40     hal_->unregisterIncomingPacketCallback();
41   }
42 
SendHciCommand(::grpc::ServerContext * context,const::bluetooth::hal::HciCommandPacket * request,::google::protobuf::Empty * response)43   ::grpc::Status SendHciCommand(::grpc::ServerContext* context, const ::bluetooth::hal::HciCommandPacket* request,
44                                 ::google::protobuf::Empty* response) override {
45     std::unique_lock<std::mutex> lock(mutex_);
46     can_send_hci_command_ = false;
47     std::string req_string = request->payload();
48     hal_->sendHciCommand(std::vector<uint8_t>(req_string.begin(), req_string.end()));
49     while (!can_send_hci_command_) {
50       cv_.wait(lock);
51     }
52     return ::grpc::Status::OK;
53   }
54 
SendHciAcl(::grpc::ServerContext * context,const::bluetooth::hal::HciAclPacket * request,::google::protobuf::Empty * response)55   ::grpc::Status SendHciAcl(::grpc::ServerContext* context, const ::bluetooth::hal::HciAclPacket* request,
56                             ::google::protobuf::Empty* response) override {
57     std::string req_string = request->payload();
58     hal_->sendAclData(std::vector<uint8_t>(req_string.begin(), req_string.end()));
59     return ::grpc::Status::OK;
60   }
61 
SendHciSco(::grpc::ServerContext * context,const::bluetooth::hal::HciScoPacket * request,::google::protobuf::Empty * response)62   ::grpc::Status SendHciSco(::grpc::ServerContext* context, const ::bluetooth::hal::HciScoPacket* request,
63                             ::google::protobuf::Empty* response) override {
64     std::string req_string = request->payload();
65     hal_->sendScoData(std::vector<uint8_t>(req_string.begin(), req_string.end()));
66     return ::grpc::Status::OK;
67   }
68 
FetchHciEvent(::grpc::ServerContext * context,const::google::protobuf::Empty * request,::grpc::ServerWriter<HciEventPacket> * writer)69   ::grpc::Status FetchHciEvent(::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
70                                ::grpc::ServerWriter<HciEventPacket>* writer) override {
71     return pending_hci_events_.RunLoop(context, writer);
72   };
73 
FetchHciAcl(::grpc::ServerContext * context,const::google::protobuf::Empty * request,::grpc::ServerWriter<HciAclPacket> * writer)74   ::grpc::Status FetchHciAcl(::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
75                              ::grpc::ServerWriter<HciAclPacket>* writer) override {
76     return pending_acl_events_.RunLoop(context, writer);
77   };
78 
FetchHciSco(::grpc::ServerContext * context,const::google::protobuf::Empty * request,::grpc::ServerWriter<HciScoPacket> * writer)79   ::grpc::Status FetchHciSco(::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
80                              ::grpc::ServerWriter<HciScoPacket>* writer) override {
81     return pending_sco_events_.RunLoop(context, writer);
82   };
83 
hciEventReceived(bluetooth::hal::HciPacket event)84   void hciEventReceived(bluetooth::hal::HciPacket event) override {
85     {
86       HciEventPacket response;
87       response.set_payload(std::string(event.begin(), event.end()));
88       pending_hci_events_.OnIncomingEvent(std::move(response));
89     }
90     can_send_hci_command_ = true;
91     cv_.notify_one();
92   }
93 
aclDataReceived(bluetooth::hal::HciPacket data)94   void aclDataReceived(bluetooth::hal::HciPacket data) override {
95     HciAclPacket response;
96     response.set_payload(std::string(data.begin(), data.end()));
97     pending_acl_events_.OnIncomingEvent(std::move(response));
98   }
99 
scoDataReceived(bluetooth::hal::HciPacket data)100   void scoDataReceived(bluetooth::hal::HciPacket data) override {
101     HciScoPacket response;
102     response.set_payload(std::string(data.begin(), data.end()));
103     pending_sco_events_.OnIncomingEvent(std::move(response));
104   }
105 
106  private:
107   HciHal* hal_;
108   bool can_send_hci_command_ = true;
109   mutable std::mutex mutex_;
110   std::condition_variable cv_;
111   ::bluetooth::grpc::GrpcEventQueue<HciEventPacket> pending_hci_events_{"FetchHciEvent"};
112   ::bluetooth::grpc::GrpcEventQueue<HciAclPacket> pending_acl_events_{"FetchHciAcl"};
113   ::bluetooth::grpc::GrpcEventQueue<HciScoPacket> pending_sco_events_{"FetchHciSco"};
114 };
115 
ListDependencies(ModuleList * list)116 void HciHalFacadeModule::ListDependencies(ModuleList* list) {
117   ::bluetooth::grpc::GrpcFacadeModule::ListDependencies(list);
118   list->add<HciHal>();
119 }
120 
Start()121 void HciHalFacadeModule::Start() {
122   ::bluetooth::grpc::GrpcFacadeModule::Start();
123   service_ = new HciHalFacadeService(GetDependency<HciHal>());
124 }
125 
Stop()126 void HciHalFacadeModule::Stop() {
127   delete service_;
128   ::bluetooth::grpc::GrpcFacadeModule::Stop();
129 }
130 
GetService() const131 ::grpc::Service* HciHalFacadeModule::GetService() const {
132   return service_;
133 }
134 
__anond72826490102() 135 const ModuleFactory HciHalFacadeModule::Factory = ::bluetooth::ModuleFactory([]() { return new HciHalFacadeModule(); });
136 
137 }  // namespace hal
138 }  // namespace bluetooth
139