xref: /aosp_15_r20/tools/netsim/rust/daemon/src/transport/grpc.rs (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use crate::wireless::packet::Response;
16 use bytes::Bytes;
17 use futures_executor::block_on;
18 use futures_util::SinkExt as _;
19 use grpcio::WriteFlags;
20 use netsim_proto::hci_packet::{hcipacket::PacketType, HCIPacket};
21 use netsim_proto::packet_streamer::PacketResponse;
22 use protobuf::Enum;
23 use protobuf::EnumOrUnknown;
24 
25 /// Grpc transport.
26 pub struct RustGrpcTransport {
27     pub sink: grpcio::DuplexSink<PacketResponse>,
28 }
29 
30 impl Response for RustGrpcTransport {
response(&mut self, packet: Bytes, packet_type: u8)31     fn response(&mut self, packet: Bytes, packet_type: u8) {
32         let mut response = PacketResponse::new();
33         if packet_type != (PacketType::HCI_PACKET_UNSPECIFIED.value() as u8) {
34             let hci_packet = HCIPacket {
35                 packet_type: EnumOrUnknown::from_i32(packet_type as i32),
36                 packet: packet.to_vec(),
37                 ..Default::default()
38             };
39             response.set_hci_packet(hci_packet);
40         } else {
41             response.set_packet(packet.to_vec());
42         }
43         block_on(async {
44             let _ = self.sink.send((response, WriteFlags::default())).await;
45         });
46     }
47 }
48