blob: 3f41d62340263cca9814cf905626a542f106961a [file] [log] [blame]
Hansong Zhang29f99002019-04-24 17:25:42 +00001/*
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
Hansong Zhang90ec7402019-04-29 16:04:07 -070017#include "hal/facade.h"
Hansong Zhang29f99002019-04-24 17:25:42 +000018
19#include <memory>
20#include <mutex>
Hansong Zhang29f99002019-04-24 17:25:42 +000021
Jack He0bea5c72019-12-06 17:13:27 -080022#include "grpc/grpc_event_queue.h"
Zach Johnson34703602019-04-29 16:45:21 -070023#include "hal/facade.grpc.pb.h"
Hansong Zhang29f99002019-04-24 17:25:42 +000024#include "hal/hci_hal.h"
Hansong Zhang29f99002019-04-24 17:25:42 +000025
26using ::grpc::ServerAsyncResponseWriter;
27using ::grpc::ServerAsyncWriter;
Hansong Zhang29f99002019-04-24 17:25:42 +000028using ::grpc::ServerContext;
29
30namespace bluetooth {
31namespace hal {
Hansong Zhang29f99002019-04-24 17:25:42 +000032
Jack He0bea5c72019-12-06 17:13:27 -080033class HciHalFacadeService : public HciHalFacade::Service, public ::bluetooth::hal::HciHalCallbacks {
Hansong Zhang29f99002019-04-24 17:25:42 +000034 public:
Jack He0bea5c72019-12-06 17:13:27 -080035 explicit HciHalFacadeService(HciHal* hal) : hal_(hal) {
Zach Johnson34703602019-04-29 16:45:21 -070036 hal->registerIncomingPacketCallback(this);
37 }
Zach Johnsone0e158c2019-04-26 11:57:05 -070038
Jack He0bea5c72019-12-06 17:13:27 -080039 ~HciHalFacadeService() override {
Hansong Zhang116cb562019-07-24 15:33:35 -070040 hal_->unregisterIncomingPacketCallback();
41 }
42
Zach Johnson34703602019-04-29 16:45:21 -070043 ::grpc::Status SendHciCommand(::grpc::ServerContext* context, const ::bluetooth::hal::HciCommandPacket* request,
Chienyuan4adb0422019-04-25 15:11:54 -070044 ::google::protobuf::Empty* response) override {
Hansong Zhang90ec7402019-04-29 16:04:07 -070045 std::unique_lock<std::mutex> lock(mutex_);
46 can_send_hci_command_ = false;
Hansong Zhang29f99002019-04-24 17:25:42 +000047 std::string req_string = request->payload();
Zach Johnsone0e158c2019-04-26 11:57:05 -070048 hal_->sendHciCommand(std::vector<uint8_t>(req_string.begin(), req_string.end()));
Hansong Zhang90ec7402019-04-29 16:04:07 -070049 while (!can_send_hci_command_) {
50 cv_.wait(lock);
51 }
Hansong Zhang29f99002019-04-24 17:25:42 +000052 return ::grpc::Status::OK;
53 }
54
Zach Johnson34703602019-04-29 16:45:21 -070055 ::grpc::Status SendHciAcl(::grpc::ServerContext* context, const ::bluetooth::hal::HciAclPacket* request,
Hansong Zhang29f99002019-04-24 17:25:42 +000056 ::google::protobuf::Empty* response) override {
57 std::string req_string = request->payload();
Zach Johnsone0e158c2019-04-26 11:57:05 -070058 hal_->sendAclData(std::vector<uint8_t>(req_string.begin(), req_string.end()));
Hansong Zhang29f99002019-04-24 17:25:42 +000059 return ::grpc::Status::OK;
60 }
61
Zach Johnson34703602019-04-29 16:45:21 -070062 ::grpc::Status SendHciSco(::grpc::ServerContext* context, const ::bluetooth::hal::HciScoPacket* request,
Hansong Zhang29f99002019-04-24 17:25:42 +000063 ::google::protobuf::Empty* response) override {
64 std::string req_string = request->payload();
Zach Johnsone0e158c2019-04-26 11:57:05 -070065 hal_->sendScoData(std::vector<uint8_t>(req_string.begin(), req_string.end()));
Hansong Zhang29f99002019-04-24 17:25:42 +000066 return ::grpc::Status::OK;
67 }
68
Jack He0bea5c72019-12-06 17:13:27 -080069 ::grpc::Status FetchHciEvent(::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
Zach Johnson34703602019-04-29 16:45:21 -070070 ::grpc::ServerWriter<HciEventPacket>* writer) override {
Jack He0bea5c72019-12-06 17:13:27 -080071 return pending_hci_events_.RunLoop(context, writer);
Chienyuan4adb0422019-04-25 15:11:54 -070072 };
73
Jack He0bea5c72019-12-06 17:13:27 -080074 ::grpc::Status FetchHciAcl(::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
Chienyuan4adb0422019-04-25 15:11:54 -070075 ::grpc::ServerWriter<HciAclPacket>* writer) override {
Jack He0bea5c72019-12-06 17:13:27 -080076 return pending_acl_events_.RunLoop(context, writer);
Chienyuan4adb0422019-04-25 15:11:54 -070077 };
78
Jack He0bea5c72019-12-06 17:13:27 -080079 ::grpc::Status FetchHciSco(::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
Chienyuan4adb0422019-04-25 15:11:54 -070080 ::grpc::ServerWriter<HciScoPacket>* writer) override {
Jack He0bea5c72019-12-06 17:13:27 -080081 return pending_sco_events_.RunLoop(context, writer);
Chienyuan4adb0422019-04-25 15:11:54 -070082 };
83
Hansong Zhang29f99002019-04-24 17:25:42 +000084 void hciEventReceived(bluetooth::hal::HciPacket event) override {
Jack He0bea5c72019-12-06 17:13:27 -080085 {
86 HciEventPacket response;
87 response.set_payload(std::string(event.begin(), event.end()));
88 pending_hci_events_.OnIncomingEvent(std::move(response));
89 }
Hansong Zhang90ec7402019-04-29 16:04:07 -070090 can_send_hci_command_ = true;
91 cv_.notify_one();
Hansong Zhang29f99002019-04-24 17:25:42 +000092 }
93
94 void aclDataReceived(bluetooth::hal::HciPacket data) override {
Jack He0bea5c72019-12-06 17:13:27 -080095 HciAclPacket response;
96 response.set_payload(std::string(data.begin(), data.end()));
97 pending_acl_events_.OnIncomingEvent(std::move(response));
Hansong Zhang29f99002019-04-24 17:25:42 +000098 }
99
100 void scoDataReceived(bluetooth::hal::HciPacket data) override {
Jack He0bea5c72019-12-06 17:13:27 -0800101 HciScoPacket response;
102 response.set_payload(std::string(data.begin(), data.end()));
103 pending_sco_events_.OnIncomingEvent(std::move(response));
Hansong Zhang29f99002019-04-24 17:25:42 +0000104 }
Chienyuan4adb0422019-04-25 15:11:54 -0700105
Hansong Zhang29f99002019-04-24 17:25:42 +0000106 private:
Zach Johnsonb45ecd22019-04-29 12:07:23 -0700107 HciHal* hal_;
Hansong Zhang90ec7402019-04-29 16:04:07 -0700108 bool can_send_hci_command_ = true;
109 mutable std::mutex mutex_;
110 std::condition_variable cv_;
Jack He0bea5c72019-12-06 17:13:27 -0800111 ::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"};
Hansong Zhang29f99002019-04-24 17:25:42 +0000114};
115
Zach Johnson34703602019-04-29 16:45:21 -0700116void HciHalFacadeModule::ListDependencies(ModuleList* list) {
Zach Johnsone0e158c2019-04-26 11:57:05 -0700117 ::bluetooth::grpc::GrpcFacadeModule::ListDependencies(list);
Zach Johnsonb45ecd22019-04-29 12:07:23 -0700118 list->add<HciHal>();
Hansong Zhang29f99002019-04-24 17:25:42 +0000119}
120
Zach Johnson34703602019-04-29 16:45:21 -0700121void HciHalFacadeModule::Start() {
Zach Johnson84b448b2019-04-29 15:34:55 -0700122 ::bluetooth::grpc::GrpcFacadeModule::Start();
Zach Johnson34703602019-04-29 16:45:21 -0700123 service_ = new HciHalFacadeService(GetDependency<HciHal>());
Hansong Zhang29f99002019-04-24 17:25:42 +0000124}
125
Zach Johnson34703602019-04-29 16:45:21 -0700126void HciHalFacadeModule::Stop() {
Zach Johnsone0e158c2019-04-26 11:57:05 -0700127 delete service_;
Hansong Zhang90ec7402019-04-29 16:04:07 -0700128 ::bluetooth::grpc::GrpcFacadeModule::Stop();
Hansong Zhang29f99002019-04-24 17:25:42 +0000129}
130
Zach Johnson34703602019-04-29 16:45:21 -0700131::grpc::Service* HciHalFacadeModule::GetService() const {
Zach Johnsone0e158c2019-04-26 11:57:05 -0700132 return service_;
Hansong Zhang29f99002019-04-24 17:25:42 +0000133}
134
Jack He0bea5c72019-12-06 17:13:27 -0800135const ModuleFactory HciHalFacadeModule::Factory = ::bluetooth::ModuleFactory([]() { return new HciHalFacadeModule(); });
Hansong Zhang29f99002019-04-24 17:25:42 +0000136
Hansong Zhang29f99002019-04-24 17:25:42 +0000137} // namespace hal
138} // namespace bluetooth