1 //! BT HCI HAL facade
2 
3 use crate::{AclHal, ControlHal, IsoHal};
4 use bt_common::GrpcFacade;
5 use bt_facade_helpers::RxAdapter;
6 use bt_facade_proto::common::Data;
7 use bt_facade_proto::empty::Empty;
8 use bt_facade_proto::hal_facade_grpc::{create_hci_hal_facade, HciHalFacade};
9 use bt_packets::hci::{AclPacket, CommandPacket, EventPacket, IsoPacket};
10 use gddi::{module, provides, Stoppable};
11 use grpcio::*;
12 
13 module! {
14     hal_facade_module,
15     providers {
16         HciHalFacadeService => provide_facade,
17     }
18 }
19 
20 #[provides]
provide_facade(control: ControlHal, acl: AclHal, iso: IsoHal) -> HciHalFacadeService21 async fn provide_facade(control: ControlHal, acl: AclHal, iso: IsoHal) -> HciHalFacadeService {
22     HciHalFacadeService {
23         evt_rx: RxAdapter::from_arc(control.rx.clone()),
24         acl_rx: RxAdapter::from_arc(acl.rx.clone()),
25         iso_rx: RxAdapter::from_arc(iso.rx.clone()),
26         control,
27         acl,
28         iso,
29     }
30 }
31 
32 /// HCI HAL facade service
33 #[derive(Clone, Stoppable)]
34 pub struct HciHalFacadeService {
35     evt_rx: RxAdapter<EventPacket>,
36     acl_rx: RxAdapter<AclPacket>,
37     iso_rx: RxAdapter<IsoPacket>,
38     control: ControlHal,
39     acl: AclHal,
40     iso: IsoHal,
41 }
42 
43 impl GrpcFacade for HciHalFacadeService {
into_grpc(self) -> grpcio::Service44     fn into_grpc(self) -> grpcio::Service {
45         create_hci_hal_facade(self)
46     }
47 }
48 
49 impl HciHalFacade for HciHalFacadeService {
send_command(&mut self, ctx: RpcContext<'_>, mut data: Data, sink: UnarySink<Empty>)50     fn send_command(&mut self, ctx: RpcContext<'_>, mut data: Data, sink: UnarySink<Empty>) {
51         let cmd_tx = self.control.tx.clone();
52         ctx.spawn(async move {
53             cmd_tx.send(CommandPacket::parse(&data.take_payload()).unwrap()).await.unwrap();
54             sink.success(Empty::default()).await.unwrap();
55         });
56     }
57 
send_acl(&mut self, ctx: RpcContext<'_>, mut data: Data, sink: UnarySink<Empty>)58     fn send_acl(&mut self, ctx: RpcContext<'_>, mut data: Data, sink: UnarySink<Empty>) {
59         let acl_tx = self.acl.tx.clone();
60         ctx.spawn(async move {
61             acl_tx.send(AclPacket::parse(&data.take_payload()).unwrap()).await.unwrap();
62             sink.success(Empty::default()).await.unwrap();
63         });
64     }
65 
send_sco(&mut self, _ctx: RpcContext<'_>, _sco: Data, _sink: UnarySink<Empty>)66     fn send_sco(&mut self, _ctx: RpcContext<'_>, _sco: Data, _sink: UnarySink<Empty>) {
67         unimplemented!()
68     }
69 
send_iso(&mut self, ctx: RpcContext<'_>, mut data: Data, sink: UnarySink<Empty>)70     fn send_iso(&mut self, ctx: RpcContext<'_>, mut data: Data, sink: UnarySink<Empty>) {
71         let iso_tx = self.iso.tx.clone();
72         ctx.spawn(async move {
73             iso_tx.send(IsoPacket::parse(&data.take_payload()).unwrap()).await.unwrap();
74             sink.success(Empty::default()).await.unwrap();
75         });
76     }
77 
stream_events(&mut self, ctx: RpcContext<'_>, _: Empty, sink: ServerStreamingSink<Data>)78     fn stream_events(&mut self, ctx: RpcContext<'_>, _: Empty, sink: ServerStreamingSink<Data>) {
79         self.evt_rx.stream_grpc(ctx, sink);
80     }
81 
stream_acl(&mut self, ctx: RpcContext<'_>, _: Empty, sink: ServerStreamingSink<Data>)82     fn stream_acl(&mut self, ctx: RpcContext<'_>, _: Empty, sink: ServerStreamingSink<Data>) {
83         self.acl_rx.stream_grpc(ctx, sink);
84     }
85 
stream_sco(&mut self, _ctx: RpcContext<'_>, _: Empty, _sink: ServerStreamingSink<Data>)86     fn stream_sco(&mut self, _ctx: RpcContext<'_>, _: Empty, _sink: ServerStreamingSink<Data>) {
87         unimplemented!()
88     }
89 
stream_iso(&mut self, ctx: RpcContext<'_>, _: Empty, sink: ServerStreamingSink<Data>)90     fn stream_iso(&mut self, ctx: RpcContext<'_>, _: Empty, sink: ServerStreamingSink<Data>) {
91         self.iso_rx.stream_grpc(ctx, sink);
92     }
93 }
94