DemesneGH commented on code in PR #164:
URL: 
https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/164#discussion_r1923009085


##########
examples/udp_socket-rs/ta/src/main.rs:
##########
@@ -52,17 +53,64 @@ fn destroy() {
 fn invoke_command(cmd_id: u32, _params: &mut Parameters) -> Result<()> {
     trace_println!("[+] TA invoke command");
     match Command::from(cmd_id) {
-        Command::Start => {
-            udp_socket();
-            Ok(())
-        }
+        Command::Start => udp_socket(),
         _ => Err(Error::new(ErrorKind::BadParameters)),
     }
 }
 
-fn udp_socket() {
-    let mut stream = UdpSocket::connect("127.0.0.1", 34254).unwrap();
-    stream.write_all(b"[TA]: Hello, Teaclave!").unwrap();
+#[cfg(not(target_os = "optee"))]
+fn udp_socket() -> Result<()> {
+    use alloc::string::String;
+    use alloc::vec::Vec;
+    use optee_utee::net::Setup;
+
+    let setup = Setup::new_v4("127.0.0.1", 34254)?;
+    let mut stream = UdpSocket::open(setup).map_err(|err| {

Review Comment:
   We don't need to implement `std::io::{Read, Write}` like what we do in 
`std`. How about making the API compatible? like:
   ```
   // the no_std TcpStream:
   pub struct TcpStream {
      ...
   }
   impl TcpStream {
      pub fn connect(address: &str, port: u16) -> Result<Self, SocketError> 
{...}
      pub fn read(&mut self, buf: &mut [u8]) -> Result<Self, SocketError> {...}
      pub fn write(&mut self, buf: &[u8]) -> Result<Self, SocketError> {...}
      ...
   }
   ```
   
   In examples we don't need to modify the main code:
   ```
   // use different TcpStream
   #[cfg(not(target_os = "optee"))]
   use optee_utee::net::no_std::TcpStream;  // module name can be adjusted, 
just example here
   #[cfg(target_os = "optee")]
   use optee_utee::net::std::TcpStream;
   
   // expose the same API for examples
   let mut stream = TcpStream::connect(xxxx);
   stream.write(xxx);
   stream.read(xxx);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org
For additional commands, e-mail: dev-h...@teaclave.apache.org

Reply via email to