Merge "Replace Arc<Vec<u8>> with bytes::Bytes in between transport and echip layer" into main
diff --git a/rust/daemon/src/echip/ble_beacon.rs b/rust/daemon/src/echip/ble_beacon.rs
index 7350188..3530ef4 100644
--- a/rust/daemon/src/echip/ble_beacon.rs
+++ b/rust/daemon/src/echip/ble_beacon.rs
@@ -16,6 +16,7 @@
use crate::devices::chip::{ChipIdentifier, FacadeIdentifier};
use crate::echip::{EmulatedChip, SharedEmulatedChip};
+use bytes::Bytes;
use log::{error, info};
use netsim_proto::model::Chip as ProtoChip;
use netsim_proto::model::ChipCreate as ChipCreateProto;
@@ -39,7 +40,7 @@
}
impl EmulatedChip for BleBeacon {
- fn handle_request(&self, packet: &[u8]) {
+ fn handle_request(&self, packet: Bytes) {
#[cfg(not(test))]
ffi_bluetooth::handle_bt_request(self.facade_id, packet[0], &packet[1..].to_vec());
#[cfg(test)]
diff --git a/rust/daemon/src/echip/bluetooth.rs b/rust/daemon/src/echip/bluetooth.rs
index 5335821..71d4611 100644
--- a/rust/daemon/src/echip/bluetooth.rs
+++ b/rust/daemon/src/echip/bluetooth.rs
@@ -16,6 +16,7 @@
use crate::echip::{EmulatedChip, SharedEmulatedChip};
use crate::ffi::ffi_bluetooth;
+use bytes::Bytes;
use cxx::{let_cxx_string, CxxString, CxxVector};
use lazy_static::lazy_static;
use log::{error, info};
@@ -79,7 +80,7 @@
}
impl EmulatedChip for Bluetooth {
- fn handle_request(&self, packet: &[u8]) {
+ fn handle_request(&self, packet: Bytes) {
// Lock to protect device_to_transport_ table in C++
let _guard = ECHIP_BT_MUTEX.lock().expect("Failed to acquire lock on ECHIP_BT_MUTEX");
ffi_bluetooth::handle_bt_request(self.rootcanal_id, packet[0], &packet[1..].to_vec())
diff --git a/rust/daemon/src/echip/emulated_chip.rs b/rust/daemon/src/echip/emulated_chip.rs
index 6b9d224..e8355a8 100644
--- a/rust/daemon/src/echip/emulated_chip.rs
+++ b/rust/daemon/src/echip/emulated_chip.rs
@@ -17,6 +17,7 @@
sync::{Arc, Mutex},
};
+use bytes::Bytes;
use lazy_static::lazy_static;
use netsim_proto::model::Chip as ProtoChip;
@@ -68,7 +69,7 @@
/// * Wi-Fi - packet is Radiotap format
/// * UWB - packet is UCI format
/// * NFC - packet is NCI format
- fn handle_request(&self, packet: &[u8]);
+ fn handle_request(&self, packet: Bytes);
/// Reset the internal state of the emulated chip for the virtual device.
/// The transmitted and received packet count will be set to 0 and the chip
diff --git a/rust/daemon/src/echip/mocked.rs b/rust/daemon/src/echip/mocked.rs
index 895a009..425bb15 100644
--- a/rust/daemon/src/echip/mocked.rs
+++ b/rust/daemon/src/echip/mocked.rs
@@ -15,6 +15,7 @@
use crate::devices::chip::ChipIdentifier;
use crate::echip::{EmulatedChip, SharedEmulatedChip};
+use bytes::Bytes;
use netsim_proto::common::ChipKind as ProtoChipKind;
use netsim_proto::model::Chip as ProtoChip;
use netsim_proto::stats::{netsim_radio_stats, NetsimRadioStats as ProtoRadioStats};
@@ -33,7 +34,7 @@
}
impl EmulatedChip for Mock {
- fn handle_request(&self, _packet: &[u8]) {}
+ fn handle_request(&self, _packet: Bytes) {}
fn reset(&self) {}
diff --git a/rust/daemon/src/echip/packet.rs b/rust/daemon/src/echip/packet.rs
index 41fffae..8459a9c 100644
--- a/rust/daemon/src/echip/packet.rs
+++ b/rust/daemon/src/echip/packet.rs
@@ -39,14 +39,14 @@
// When a connection arrives, the transport registers a responder
// implementing Response trait for the packet stream.
pub trait Response {
- fn response(&mut self, packet: Vec<u8>, packet_type: u8);
+ fn response(&mut self, packet: Bytes, packet_type: u8);
}
// When a responder is registered a responder thread is created to
// decouple the chip controller from the network. The thread reads
// ResponsePacket from a queue and sends to responder.
struct ResponsePacket {
- packet: Vec<u8>,
+ packet: Bytes,
packet_type: u8,
}
@@ -103,12 +103,12 @@
// 1. Per EChip Struct should contain private field of channel & facade_id
// 2. Lookup from ECHIPS with given chip_id
// 3. Call echips.handle_response
- let packet_vec = packet.as_slice().to_vec();
- captures_handler::handle_packet_response(chip_id, &packet_vec, packet_type.into());
+ let packet_bytes = Bytes::from(packet.as_slice().to_vec());
+ captures_handler::handle_packet_response(chip_id, &packet_bytes, packet_type.into());
let mut binding = SENDERS.lock();
if let Some(responder) = binding.get(&chip_id) {
- if responder.send(ResponsePacket { packet: packet_vec, packet_type }).is_err() {
+ if responder.send(ResponsePacket { packet: packet_bytes, packet_type }).is_err() {
warn!("handle_response: send failed for chip_id: {chip_id}");
binding.remove(&chip_id);
}
@@ -125,7 +125,7 @@
let mut binding = SENDERS.lock();
if let Some(responder) = binding.get(&chip_id) {
- if responder.send(ResponsePacket { packet: packet.to_vec(), packet_type }).is_err() {
+ if responder.send(ResponsePacket { packet, packet_type }).is_err() {
warn!("handle_response_rust: send failed for chip_id: {chip_id}");
binding.remove(&chip_id);
}
@@ -141,7 +141,10 @@
let mut senders = SENDERS.lock();
if let Some(responder) = senders.get(&client_id) {
- if responder.send(ResponsePacket { packet: packet.to_owned(), packet_type }).is_err() {
+ if responder
+ .send(ResponsePacket { packet: Bytes::from(packet.to_vec()), packet_type })
+ .is_err()
+ {
warn!("send failed for client: {client_id}");
senders.remove(&client_id); // Remove from the map using the value itself
}
@@ -151,27 +154,28 @@
}
/// Handle requests from transports.
-pub fn handle_request(chip_id: ChipIdentifier, packet: &mut Vec<u8>, packet_type: u8) {
+pub fn handle_request(chip_id: ChipIdentifier, packet: &Bytes, packet_type: u8) {
captures_handler::handle_packet_request(chip_id, packet, packet_type.into());
+ let mut packet_vec = packet.to_vec();
// Prepend packet_type to packet if specified
if PacketType::HCI_PACKET_UNSPECIFIED.value()
!= <u8 as std::convert::Into<i32>>::into(packet_type)
{
- packet.insert(0, packet_type);
+ packet_vec.insert(0, packet_type);
}
// Perform handle_request
match get(chip_id) {
- Some(emulated_chip) => emulated_chip.handle_request(packet),
+ Some(emulated_chip) => emulated_chip.handle_request(Bytes::from(packet_vec)),
None => warn!("SharedEmulatedChip doesn't exist for {chip_id}"),
};
}
/// Handle requests from transports in C++.
pub fn handle_request_cxx(chip_id: u32, packet: &cxx::CxxVector<u8>, packet_type: u8) {
- let mut packet_vec = packet.as_slice().to_vec();
- handle_request(chip_id, &mut packet_vec, packet_type);
+ let packet_bytes = Bytes::from(packet.as_slice().to_vec());
+ handle_request(chip_id, &packet_bytes, packet_type);
}
#[cfg(test)]
@@ -180,7 +184,7 @@
struct TestTransport {}
impl Response for TestTransport {
- fn response(&mut self, _packet: Vec<u8>, _packet_type: u8) {}
+ fn response(&mut self, _packet: Bytes, _packet_type: u8) {}
}
#[test]
diff --git a/rust/daemon/src/echip/uwb.rs b/rust/daemon/src/echip/uwb.rs
index 9a5cdb9..f7ce279 100644
--- a/rust/daemon/src/echip/uwb.rs
+++ b/rust/daemon/src/echip/uwb.rs
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+use bytes::Bytes;
use futures::{channel::mpsc::UnboundedSender, sink::SinkExt};
use lazy_static::lazy_static;
use pica::{Handle, Pica};
@@ -56,7 +57,7 @@
}
impl EmulatedChip for Uwb {
- fn handle_request(&self, packet: &[u8]) {
+ fn handle_request(&self, packet: Bytes) {
// TODO(b/330788870): Increment tx_count
self.uci_stream_writer
.unbounded_send(packet.into())
diff --git a/rust/daemon/src/echip/wifi.rs b/rust/daemon/src/echip/wifi.rs
index 6441436..7448c75 100644
--- a/rust/daemon/src/echip/wifi.rs
+++ b/rust/daemon/src/echip/wifi.rs
@@ -93,9 +93,8 @@
}
impl EmulatedChip for Wifi {
- fn handle_request(&self, packet: &[u8]) {
- let bytes = Bytes::copy_from_slice(packet);
- WIFI_MANAGER.request_sender.send((self.chip_id, bytes)).unwrap();
+ fn handle_request(&self, packet: Bytes) {
+ WIFI_MANAGER.request_sender.send((self.chip_id, packet)).unwrap();
}
fn reset(&self) {
diff --git a/rust/daemon/src/transport/fd.rs b/rust/daemon/src/transport/fd.rs
index f8e2de1..4cab764 100644
--- a/rust/daemon/src/transport/fd.rs
+++ b/rust/daemon/src/transport/fd.rs
@@ -23,6 +23,7 @@
use crate::echip;
use crate::echip::packet::{register_transport, unregister_transport, Response};
use crate::ffi::ffi_transport;
+use bytes::Bytes;
use lazy_static::lazy_static;
use log::{error, info, warn};
use netsim_proto::common::ChipKind;
@@ -84,7 +85,7 @@
}
impl Response for FdTransport {
- fn response(&mut self, packet: Vec<u8>, packet_type: u8) {
+ fn response(&mut self, packet: Bytes, packet_type: u8) {
let mut buffer = Vec::<u8>::new();
if packet_type != (PacketType::HCI_PACKET_UNSPECIFIED.value() as u8) {
buffer.push(packet_type);
@@ -122,13 +123,13 @@
error!("End reader connection with fd={}. Failed to reading uci control packet: {:?}", fd_rx, e);
break;
}
- Ok(uci::Packet { mut payload }) => {
- echip::handle_request(chip_id, &mut payload, 0);
+ Ok(uci::Packet { payload }) => {
+ echip::handle_request(chip_id, &payload, 0);
}
},
ChipKindEnum::BLUETOOTH => match h4::read_h4_packet(&mut rx) {
- Ok(h4::Packet { h4_type, mut payload }) => {
- echip::handle_request(chip_id, &mut payload, h4_type);
+ Ok(h4::Packet { h4_type, payload }) => {
+ echip::handle_request(chip_id, &payload, h4_type);
}
Err(PacketError::IoError(e))
if e.kind() == ErrorKind::UnexpectedEof =>
@@ -297,7 +298,7 @@
}
Ok(uci::Packet { payload }) => {
let mut request = PacketRequest::new();
- request.set_packet(payload);
+ request.set_packet(payload.to_vec());
let proto_bytes = request.write_to_bytes().unwrap();
ffi_transport::write_packet_request(stream_id, &proto_bytes);
}
@@ -307,7 +308,7 @@
let mut request = PacketRequest::new();
let hci_packet = HCIPacket {
packet_type: EnumOrUnknown::from_i32(h4_type as i32),
- packet: payload,
+ packet: payload.to_vec(),
..Default::default()
};
request.set_hci_packet(hci_packet);
diff --git a/rust/daemon/src/transport/grpc.rs b/rust/daemon/src/transport/grpc.rs
index 0ada43e..b736bf0 100644
--- a/rust/daemon/src/transport/grpc.rs
+++ b/rust/daemon/src/transport/grpc.rs
@@ -14,6 +14,7 @@
use crate::echip::packet::{register_transport, unregister_transport, Response};
use crate::ffi::ffi_transport::handle_grpc_response;
+use bytes::Bytes;
/// Grpc transport.
///
@@ -25,8 +26,8 @@
}
impl Response for GrpcTransport {
- fn response(&mut self, packet: Vec<u8>, packet_type: u8) {
- handle_grpc_response(self.chip_id, &packet, packet_type)
+ fn response(&mut self, packet: Bytes, packet_type: u8) {
+ handle_grpc_response(self.chip_id, &packet.to_vec(), packet_type)
}
}
diff --git a/rust/daemon/src/transport/h4.rs b/rust/daemon/src/transport/h4.rs
index 6cad9ce..e1b5ef4 100644
--- a/rust/daemon/src/transport/h4.rs
+++ b/rust/daemon/src/transport/h4.rs
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+use bytes::Bytes;
use log::{error, warn};
use std::collections::VecDeque;
use std::io::{Error, Read};
@@ -19,7 +20,7 @@
#[derive(Debug)]
pub struct Packet {
pub h4_type: u8,
- pub payload: Vec<u8>,
+ pub payload: Bytes,
}
#[derive(Debug)]
@@ -99,7 +100,7 @@
);
return Err(PacketError::IoError(e));
}
- Ok(Packet { h4_type, payload: packet })
+ Ok(Packet { h4_type, payload: Bytes::from(packet) })
}
/// Skip all received bytes until the HCI Reset command is received.
@@ -131,7 +132,10 @@
buffer.push_back(byte[0]);
if buffer == reset_pattern {
warn!("Received HCI Reset command, exiting recovery state");
- return Ok(Packet { h4_type: RESET_COMMAND[0], payload: RESET_COMMAND[1..].to_vec() });
+ return Ok(Packet {
+ h4_type: RESET_COMMAND[0],
+ payload: Bytes::from(RESET_COMMAND[1..].to_vec()),
+ });
}
}
}
diff --git a/rust/daemon/src/transport/socket.rs b/rust/daemon/src/transport/socket.rs
index 8e30a75..bc95d64 100644
--- a/rust/daemon/src/transport/socket.rs
+++ b/rust/daemon/src/transport/socket.rs
@@ -18,6 +18,7 @@
use crate::echip;
use crate::echip::packet::{register_transport, unregister_transport, Response};
use crate::transport::h4;
+use bytes::Bytes;
use log::{error, info, warn};
use netsim_proto::common::ChipKind;
use std::io::{ErrorKind, Write};
@@ -39,7 +40,7 @@
}
impl Response for SocketTransport {
- fn response(&mut self, packet: Vec<u8>, packet_type: u8) {
+ fn response(&mut self, packet: Bytes, packet_type: u8) {
let mut buffer = Vec::new();
buffer.push(packet_type);
buffer.extend(packet);
@@ -130,8 +131,8 @@
loop {
if let ChipKind::BLUETOOTH = kind {
match h4::read_h4_packet(&mut tcp_rx) {
- Ok(mut packet) => {
- echip::handle_request(chip_id, &mut packet.payload, packet.h4_type);
+ Ok(packet) => {
+ echip::handle_request(chip_id, &packet.payload, packet.h4_type);
}
Err(PacketError::IoError(e)) if e.kind() == ErrorKind::UnexpectedEof => {
info!("End socket reader connection with {}.", &tcp_rx.peer_addr().unwrap());
diff --git a/rust/daemon/src/transport/uci.rs b/rust/daemon/src/transport/uci.rs
index 80e847a..6220531 100644
--- a/rust/daemon/src/transport/uci.rs
+++ b/rust/daemon/src/transport/uci.rs
@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+use bytes::Bytes;
+
use std::io::{Error, Read};
/// This module implements control packet parsing for UWB.
@@ -26,7 +28,7 @@
#[derive(Debug)]
pub struct Packet {
- pub payload: Vec<u8>,
+ pub payload: Bytes,
}
#[derive(Debug)]
@@ -42,5 +44,5 @@
let length = buffer[UCI_PAYLOAD_LENGTH_FIELD] as usize + UCI_HEADER_SIZE;
buffer.resize(length, 0);
reader.read_exact(&mut buffer[UCI_HEADER_SIZE..]).map_err(PacketError::IoError)?;
- Ok(Packet { payload: buffer })
+ Ok(Packet { payload: Bytes::from(buffer) })
}
diff --git a/rust/daemon/src/transport/websocket.rs b/rust/daemon/src/transport/websocket.rs
index 7d7dc21..c5059e3 100644
--- a/rust/daemon/src/transport/websocket.rs
+++ b/rust/daemon/src/transport/websocket.rs
@@ -15,6 +15,7 @@
use std::sync::{Arc, Mutex};
use std::{collections::HashMap, io::Cursor, net::TcpStream};
+use bytes::Bytes;
use http::Request;
use log::{error, info, warn};
use netsim_proto::common::ChipKind;
@@ -71,7 +72,7 @@
}
impl Response for WebSocketTransport {
- fn response(&mut self, packet: Vec<u8>, packet_type: u8) {
+ fn response(&mut self, packet: Bytes, packet_type: u8) {
let mut buffer = Vec::new();
buffer.push(packet_type);
buffer.extend(packet);
@@ -148,8 +149,8 @@
if packet_msg.is_binary() {
let mut cursor = Cursor::new(packet_msg.into_data());
match h4::read_h4_packet(&mut cursor) {
- Ok(mut packet) => {
- echip::handle_request(result.chip_id, &mut packet.payload, packet.h4_type);
+ Ok(packet) => {
+ echip::handle_request(result.chip_id, &packet.payload, packet.h4_type);
}
Err(error) => {
error!(