[opentitanlib] Adds session proxy communication protocol

Introduces a set of serializable struct/enum as implementation of
communication protocol used between 'Proxy Transport Instance' and
opentitansession process.

This protocol is part of issues: lowRISC#10889, lowRISC#10217

Signed-off-by: Michał Mazurek <maz@semihalf.com>
Signed-off-by: Jes B. Klinke <jbk@chromium.org>
diff --git a/sw/host/opentitanlib/BUILD b/sw/host/opentitanlib/BUILD
index fe8df10..20586eb 100644
--- a/sw/host/opentitanlib/BUILD
+++ b/sw/host/opentitanlib/BUILD
@@ -43,6 +43,8 @@
         "src/spiflash/flash.rs",
         "src/spiflash/mod.rs",
         "src/spiflash/sfdp.rs",
+        "src/proxy/mod.rs",
+        "src/proxy/protocol.rs",
         "src/transport/common/mod.rs",
         "src/transport/common/uart.rs",
         "src/transport/cw310/gpio.rs",
diff --git a/sw/host/opentitanlib/src/io/spi.rs b/sw/host/opentitanlib/src/io/spi.rs
index ac80f77..1570751 100644
--- a/sw/host/opentitanlib/src/io/spi.rs
+++ b/sw/host/opentitanlib/src/io/spi.rs
@@ -65,7 +65,7 @@
 /// Represents the SPI transfer mode.
 /// See https://en.wikipedia.org/wiki/Serial_Peripheral_Interface#Clock_polarity_and_phase
 /// for details about SPI transfer modes.
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
 pub enum TransferMode {
     /// `Mode0` is CPOL=0, CPHA=0.
     Mode0,
@@ -90,13 +90,13 @@
     }
 }
 
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
 pub enum ClockPhase {
     SampleLeading,
     SampleTrailing,
 }
 
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
 pub enum ClockPolarity {
     IdleLow,
     IdleHigh,
diff --git a/sw/host/opentitanlib/src/lib.rs b/sw/host/opentitanlib/src/lib.rs
index be5c096..522f984 100644
--- a/sw/host/opentitanlib/src/lib.rs
+++ b/sw/host/opentitanlib/src/lib.rs
@@ -6,9 +6,10 @@
 pub mod backend;
 pub mod bootstrap;
 pub mod crypto;
+pub mod image;
 pub mod io;
 pub mod otp;
-pub mod image;
+pub mod proxy;
 pub mod spiflash;
 pub mod transport;
 pub mod util;
diff --git a/sw/host/opentitanlib/src/proxy/mod.rs b/sw/host/opentitanlib/src/proxy/mod.rs
new file mode 100644
index 0000000..605811a
--- /dev/null
+++ b/sw/host/opentitanlib/src/proxy/mod.rs
@@ -0,0 +1,5 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+pub mod protocol;
diff --git a/sw/host/opentitanlib/src/proxy/protocol.rs b/sw/host/opentitanlib/src/proxy/protocol.rs
new file mode 100644
index 0000000..bae6ec0
--- /dev/null
+++ b/sw/host/opentitanlib/src/proxy/protocol.rs
@@ -0,0 +1,162 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+use serde::{Deserialize, Serialize};
+
+use crate::io::gpio::{PinMode, PullMode};
+use crate::io::spi::TransferMode;
+use crate::transport::TransportError;
+use crate::util::voltage::Voltage;
+
+#[derive(Serialize, Deserialize)]
+pub enum Message {
+    Req(Request),
+    Res(Result<Response, TransportError>),
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum Request {
+    Gpio { id: String, command: GpioRequest },
+    Uart { id: String, command: UartRequest },
+    Spi { id: String, command: SpiRequest },
+    I2c { id: String, command: I2cRequest },
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum Response {
+    Gpio(GpioResponse),
+    Uart(UartResponse),
+    Spi(SpiResponse),
+    I2c(I2cResponse),
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum GpioRequest {
+    Write { logic: bool },
+    Read,
+    SetMode { mode: PinMode },
+    SetPullMode { pull: PullMode },
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum GpioResponse {
+    Write,
+    Read { value: bool },
+    SetMode,
+    SetPullMode,
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum UartRequest {
+    GetBaudrate,
+    SetBaudrate {
+        rate: u32,
+    },
+    Read {
+        timeout_millis: Option<u32>,
+        len: u32,
+    },
+    Write {
+        data: Vec<u8>,
+    },
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum UartResponse {
+    GetBaudrate { rate: u32 },
+    SetBaudrate,
+    Read { data: Vec<u8> },
+    Write,
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum SpiTransferRequest {
+    Read { len: u32 },
+    Write { data: Vec<u8> },
+    Both { data: Vec<u8> },
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum SpiTransferResponse {
+    Read { data: Vec<u8> },
+    Write,
+    Both { data: Vec<u8> },
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum SpiRequest {
+    GetTransferMode,
+    SetTransferMode {
+        mode: TransferMode,
+    },
+    GetBitsPerWord,
+    SetBitsPerWord {
+        bits_per_word: u32,
+    },
+    GetMaxSpeed,
+    SetMaxSpeed {
+        value: u32,
+    },
+    GetMaxTransferCount,
+    GetMaxChunkSize,
+    SetVoltage {
+        voltage: Voltage,
+    },
+    RunTransaction {
+        transaction: Vec<SpiTransferRequest>,
+    },
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum SpiResponse {
+    GetTransferMode {
+        mode: TransferMode,
+    },
+    SetTransferMode,
+    GetBitsPerWord {
+        bits_per_word: u32,
+    },
+    SetBitsPerWord,
+    GetMaxSpeed {
+        speed: u32,
+    },
+    SetMaxSpeed,
+    GetMaxTransferCount {
+        number: usize,
+    },
+    GetMaxChunkSize {
+        size: usize,
+    },
+    SetVoltage,
+    RunTransaction {
+        transaction: Vec<SpiTransferResponse>,
+    },
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum I2cTransferRequest {
+    Read { len: u32 },
+    Write { data: Vec<u8> },
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum I2cTransferResponse {
+    Read { data: Vec<u8> },
+    Write,
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum I2cRequest {
+    RunTransaction {
+        address: u8,
+        transaction: Vec<I2cTransferRequest>,
+    },
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum I2cResponse {
+    RunTransaction {
+        transaction: Vec<I2cTransferResponse>,
+    },
+}
diff --git a/sw/host/opentitanlib/src/util/voltage.rs b/sw/host/opentitanlib/src/util/voltage.rs
index 0a14d9b..17cd608 100644
--- a/sw/host/opentitanlib/src/util/voltage.rs
+++ b/sw/host/opentitanlib/src/util/voltage.rs
@@ -2,10 +2,11 @@
 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
 // SPDX-License-Identifier: Apache-2.0
 
+use serde::{Deserialize, Serialize};
 use std::num::ParseFloatError;
 use std::str::FromStr;
 
-#[derive(Debug, Default, Clone, Copy)]
+#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
 pub struct Voltage(pub f64);
 
 impl FromStr for Voltage {