Skip to content

Commit aae3d67

Browse files
committed
[opentitanproxy] move the proxy server out from opentitanlib
Signed-off-by: Gary Guo <[email protected]>
1 parent 2f40add commit aae3d67

File tree

18 files changed

+118
-49
lines changed

18 files changed

+118
-49
lines changed

sw/host/opentitanlib/BUILD

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,6 @@ rust_library(
122122
"src/ownership/mod.rs",
123123
"src/ownership/owner.rs",
124124
"src/ownership/rescue.rs",
125-
"src/proxy/handler.rs",
126-
"src/proxy/mod.rs",
127-
"src/proxy/nonblocking_uart.rs",
128-
"src/proxy/protocol.rs",
129-
"src/proxy/socket_server.rs",
130125
"src/rescue/mod.rs",
131126
"src/rescue/serial.rs",
132127
"src/rescue/xmodem.rs",

sw/host/opentitanlib/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pub mod image;
1717
pub mod io;
1818
pub mod otp;
1919
pub mod ownership;
20-
pub mod proxy;
2120
pub mod rescue;
2221
pub mod spiflash;
2322
pub mod test_utils;

sw/host/opentitansession/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ rust_binary(
1111
srcs = ["src/main.rs"],
1212
deps = [
1313
"//sw/host/opentitanlib",
14+
"//sw/host/ot_proxy/server",
1415
"@crate_index//:anyhow",
1516
"@crate_index//:clap",
1617
"@crate_index//:directories",

sw/host/opentitansession/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::str::FromStr;
1919
use std::time::Duration;
2020

2121
use opentitanlib::backend;
22-
use opentitanlib::proxy::SessionHandler;
22+
use ot_proxy::SessionHandler;
2323

2424
#[derive(Debug, Parser)]
2525
#[command(

sw/host/ot_proxy/proto/BUILD

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright lowRISC contributors (OpenTitan project).
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
load("@rules_rust//rust:defs.bzl", "rust_doc", "rust_library", "rust_test")
6+
7+
package(default_visibility = ["//visibility:public"])
8+
9+
rust_library(
10+
name = "proto",
11+
srcs = [
12+
"src/lib.rs",
13+
],
14+
crate_name = "ot_proxy_proto",
15+
deps = [
16+
"//sw/host/opentitanlib:opentitanlib_base",
17+
"@crate_index//:serde",
18+
],
19+
)
20+
21+
rust_doc(
22+
name = "proto_doc",
23+
crate = ":proto",
24+
)
25+
26+
rust_test(
27+
name = "proto_test",
28+
crate = ":proto",
29+
)

sw/host/opentitanlib/src/proxy/protocol.rs renamed to sw/host/ot_proxy/proto/src/lib.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
// SPDX-License-Identifier: Apache-2.0
44

5-
use serde::{Deserialize, Serialize};
65
use std::collections::HashMap;
76

8-
use crate::bootstrap::BootstrapOptions;
9-
use crate::io::emu::{EmuState, EmuValue};
10-
use crate::io::gpio::{
7+
use serde::{Deserialize, Serialize};
8+
9+
use opentitanlib::bootstrap::BootstrapOptions;
10+
use opentitanlib::io::emu::{EmuState, EmuValue};
11+
use opentitanlib::io::gpio::{
1112
ClockNature, MonitoringReadResponse, MonitoringStartResponse, PinMode, PullMode,
1213
};
13-
use crate::io::i2c::DeviceStatus;
14-
use crate::io::spi::{MaxSizes, TransferMode};
15-
use crate::io::uart::{FlowControl, Parity};
16-
use crate::transport::Capabilities;
17-
use crate::util::serializable_error::SerializedError;
18-
use crate::util::voltage::Voltage;
14+
use opentitanlib::io::i2c::DeviceStatus;
15+
use opentitanlib::io::spi::{MaxSizes, TransferMode};
16+
use opentitanlib::io::uart::{FlowControl, Parity};
17+
use opentitanlib::transport::Capabilities;
18+
use opentitanlib::util::serializable_error::SerializedError;
19+
use opentitanlib::util::voltage::Voltage;
1920

2021
#[derive(Serialize, Deserialize)]
2122
pub enum Message {

sw/host/ot_proxy/server/BUILD

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright lowRISC contributors (OpenTitan project).
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
load("@rules_rust//rust:defs.bzl", "rust_doc", "rust_library", "rust_test")
6+
7+
package(default_visibility = ["//visibility:public"])
8+
9+
rust_library(
10+
name = "server",
11+
srcs = [
12+
"src/handler.rs",
13+
"src/lib.rs",
14+
"src/nonblocking_uart.rs",
15+
"src/socket_server.rs",
16+
],
17+
crate_name = "ot_proxy",
18+
deps = [
19+
"//sw/host/opentitanlib:opentitanlib_base",
20+
"//sw/host/ot_proxy/proto",
21+
"@crate_index//:anyhow",
22+
"@crate_index//:clap",
23+
"@crate_index//:log",
24+
"@crate_index//:serde",
25+
"@crate_index//:serde_json",
26+
"@crate_index//:thiserror",
27+
"@crate_index//:tokio",
28+
"@crate_index//:typetag",
29+
],
30+
)
31+
32+
rust_doc(
33+
name = "server_doc",
34+
crate = ":server",
35+
)
36+
37+
rust_test(
38+
name = "server_test",
39+
crate = ":server",
40+
)

sw/host/opentitanlib/src/proxy/handler.rs renamed to sw/host/ot_proxy/server/src/handler.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ use std::rc::Rc;
1010
use std::sync::{Arc, Mutex};
1111
use std::time::Duration;
1212

13-
use super::CommandHandler;
14-
use super::protocol::{
13+
use opentitanlib::app::TransportWrapper;
14+
use opentitanlib::bootstrap::Bootstrap;
15+
use opentitanlib::io::gpio::{
16+
BitbangEntry, DacBangEntry, GpioBitbangOperation, GpioDacBangOperation, GpioPin,
17+
};
18+
use opentitanlib::io::{i2c, spi};
19+
use opentitanlib::transport::TransportError;
20+
use opentitanlib::util::serializable_error::SerializedError;
21+
use ot_proxy_proto::{
1522
BitbangEntryRequest, BitbangEntryResponse, DacBangEntryRequest, EmuRequest, EmuResponse,
1623
GpioBitRequest, GpioBitResponse, GpioDacRequest, GpioDacResponse, GpioMonRequest,
1724
GpioMonResponse, GpioRequest, GpioResponse, I2cRequest, I2cResponse, I2cTransferRequest,
1825
I2cTransferResponse, Message, ProxyRequest, ProxyResponse, Request, Response, SpiRequest,
1926
SpiResponse, SpiTransferRequest, SpiTransferResponse, UartRequest, UartResponse,
2027
};
21-
use crate::app::TransportWrapper;
22-
use crate::bootstrap::Bootstrap;
23-
use crate::io::gpio::{
24-
BitbangEntry, DacBangEntry, GpioBitbangOperation, GpioDacBangOperation, GpioPin,
25-
};
26-
use crate::io::{i2c, spi};
27-
use crate::proxy::Connection;
28-
use crate::proxy::nonblocking_uart::NonblockingUartRegistry;
29-
use crate::transport::TransportError;
30-
use crate::util::serializable_error::SerializedError;
28+
29+
use super::nonblocking_uart::NonblockingUartRegistry;
30+
use super::{CommandHandler, Connection};
3131

3232
/// Implementation of the handling of each protocol request, by means of an underlying
3333
/// `Transport` implementation.

sw/host/opentitanlib/src/proxy/mod.rs renamed to sw/host/ot_proxy/server/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
// SPDX-License-Identifier: Apache-2.0
44

5-
use anyhow::{Result, bail};
6-
use handler::TransportCommandHandler;
7-
use protocol::Message;
8-
use socket_server::{Connection, JsonSocketServer};
95
use std::net::SocketAddr;
106
use std::sync::{Arc, Mutex};
7+
8+
use anyhow::{Result, bail};
119
use tokio::net::TcpListener;
1210

13-
use crate::app::TransportWrapper;
11+
use opentitanlib::app::TransportWrapper;
12+
use ot_proxy_proto::Message;
13+
14+
use handler::TransportCommandHandler;
15+
use socket_server::{Connection, JsonSocketServer};
1416

1517
mod handler;
1618
mod nonblocking_uart;
17-
pub mod protocol;
1819
mod socket_server;
1920

2021
/// Interface for handlers of protocol messages, responding to each message with a single
@@ -37,7 +38,7 @@ impl SessionHandler {
3738
// Find a suitable port to bind to.
3839
let socket = loop {
3940
let addr = SocketAddr::from(([0u8; 4], port));
40-
match crate::util::runtime::block_on(async { TcpListener::bind(addr).await }) {
41+
match opentitanlib::util::runtime::block_on(async { TcpListener::bind(addr).await }) {
4142
Ok(socket) => break socket,
4243
Err(e) if port >= limit => bail!(e),
4344
Err(_) => port += 1,
@@ -56,7 +57,7 @@ impl SessionHandler {
5657
}
5758

5859
pub fn run_loop(&mut self) -> Result<()> {
59-
crate::util::runtime::block_on(crate::util::runtime::with_graceful_shutdown(
60+
opentitanlib::util::runtime::block_on(opentitanlib::util::runtime::with_graceful_shutdown(
6061
self.socket_server.run_loop(),
6162
))
6263
}

sw/host/opentitanlib/src/proxy/nonblocking_uart.rs renamed to sw/host/ot_proxy/server/src/nonblocking_uart.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
// SPDX-License-Identifier: Apache-2.0
44

5-
use anyhow::Result;
65
use std::collections::HashMap;
76
use std::collections::hash_map::Entry::{Occupied, Vacant};
87
use std::hash::{Hash, Hasher};
98
use std::ptr::hash;
109
use std::rc::Rc;
1110
use std::sync::{Arc, Mutex, Weak};
1211

12+
use anyhow::Result;
13+
14+
use opentitanlib::io::uart::Uart;
15+
1316
use super::socket_server::Connection;
14-
use crate::io::uart::Uart;
1517

1618
pub struct NonblockingUartRegistry {
1719
// Set of Uart objects in "nonblocking read" mode. Key is the address of the Uart object.
@@ -113,9 +115,9 @@ impl NonblockingUart {
113115
for conn in connections.into_iter() {
114116
conn.lock()
115117
.unwrap()
116-
.transmit_outgoing_msg(super::protocol::Message::Async {
118+
.transmit_outgoing_msg(ot_proxy_proto::Message::Async {
117119
channel,
118-
msg: super::protocol::AsyncMessage::UartData {
120+
msg: ot_proxy_proto::AsyncMessage::UartData {
119121
data: buf[0..len].to_vec(),
120122
},
121123
})?

0 commit comments

Comments
 (0)