drtio_mgmt: reorganize drtio instructions

This commit is contained in:
occheung 2024-08-23 11:09:07 +08:00
parent 5a8db1dcf6
commit 006981306f
3 changed files with 97 additions and 114 deletions

View File

@ -297,11 +297,6 @@ pub enum Packet {
destination: u8, destination: u8,
clear: bool, clear: bool,
}, },
CoreMgmtGetLogReply {
last: bool,
length: u16,
data: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE],
},
CoreMgmtClearLogRequest { CoreMgmtClearLogRequest {
destination: u8, destination: u8,
}, },
@ -321,12 +316,6 @@ pub enum Packet {
CoreMgmtConfigReadContinue { CoreMgmtConfigReadContinue {
destination: u8, destination: u8,
}, },
CoreMgmtConfigReadReply {
succeeded: bool,
length: u16,
last: bool,
value: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE],
},
CoreMgmtConfigWriteRequest { CoreMgmtConfigWriteRequest {
destination: u8, destination: u8,
length: u16, length: u16,
@ -347,9 +336,18 @@ pub enum Packet {
CoreMgmtAllocatorDebugRequest { CoreMgmtAllocatorDebugRequest {
destination: u8, destination: u8,
}, },
CoreMgmtAck { CoreMgmtGetLogReply {
succeeded: bool, last: bool,
length: u16,
data: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE],
}, },
CoreMgmtConfigReadReply {
length: u16,
last: bool,
value: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE],
},
CoreMgmtAck,
CoreMgmtNack,
} }
impl Packet { impl Packet {
@ -630,29 +628,18 @@ impl Packet {
destination: reader.read_u8()?, destination: reader.read_u8()?,
clear: reader.read_bool()?, clear: reader.read_bool()?,
}, },
0xd1 => { 0xd1 => Packet::CoreMgmtClearLogRequest {
let last = reader.read_bool()?;
let length = reader.read_u16()?;
let mut data: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE] = [0; CORE_MGMT_PAYLOAD_MAX_SIZE];
reader.read_exact(&mut data[0..length as usize])?;
Packet::CoreMgmtGetLogReply {
last: last,
length: length,
data: data,
}
}
0xd2 => Packet::CoreMgmtClearLogRequest {
destination: reader.read_u8()?, destination: reader.read_u8()?,
}, },
0xd5 => Packet::CoreMgmtSetLogLevelRequest { 0xd2 => Packet::CoreMgmtSetLogLevelRequest {
destination: reader.read_u8()?, destination: reader.read_u8()?,
log_level: reader.read_u8()?, log_level: reader.read_u8()?,
}, },
0xd6 => Packet::CoreMgmtSetUartLogLevelRequest { 0xd3 => Packet::CoreMgmtSetUartLogLevelRequest {
destination: reader.read_u8()?, destination: reader.read_u8()?,
log_level: reader.read_u8()?, log_level: reader.read_u8()?,
}, },
0xd7 => { 0xd4 => {
let destination = reader.read_u8()?; let destination = reader.read_u8()?;
let length = reader.read_u16()?; let length = reader.read_u16()?;
let mut key: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE] = [0; CORE_MGMT_PAYLOAD_MAX_SIZE]; let mut key: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE] = [0; CORE_MGMT_PAYLOAD_MAX_SIZE];
@ -663,20 +650,10 @@ impl Packet {
key: key, key: key,
} }
} }
0xd8 => { 0xd5 => Packet::CoreMgmtConfigReadContinue {
let succeeded = reader.read_bool()?; destination: reader.read_u8()?,
let length = reader.read_u16()?; },
let last = reader.read_bool()?; 0xd6 => {
let mut value: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE] = [0; CORE_MGMT_PAYLOAD_MAX_SIZE];
reader.read_exact(&mut value[0..length as usize])?;
Packet::CoreMgmtConfigReadReply {
succeeded: succeeded,
length: length,
last: last,
value: value,
}
}
0xd9 => {
let destination = reader.read_u8()?; let destination = reader.read_u8()?;
let length = reader.read_u16()?; let length = reader.read_u16()?;
let last = reader.read_bool()?; let last = reader.read_bool()?;
@ -689,7 +666,7 @@ impl Packet {
data: data, data: data,
} }
} }
0xda => { 0xd7 => {
let destination = reader.read_u8()?; let destination = reader.read_u8()?;
let length = reader.read_u16()?; let length = reader.read_u16()?;
let mut key: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE] = [0; CORE_MGMT_PAYLOAD_MAX_SIZE]; let mut key: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE] = [0; CORE_MGMT_PAYLOAD_MAX_SIZE];
@ -700,21 +677,39 @@ impl Packet {
key: key, key: key,
} }
} }
0xdb => Packet::CoreMgmtConfigEraseRequest { 0xd8 => Packet::CoreMgmtConfigEraseRequest {
destination: reader.read_u8()?, destination: reader.read_u8()?,
}, },
0xdc => Packet::CoreMgmtRebootRequest { 0xd9 => Packet::CoreMgmtRebootRequest {
destination: reader.read_u8()?, destination: reader.read_u8()?,
}, },
0xdd => Packet::CoreMgmtAllocatorDebugRequest { 0xda => Packet::CoreMgmtAllocatorDebugRequest {
destination: reader.read_u8()?, destination: reader.read_u8()?,
}, },
0xde => Packet::CoreMgmtAck { 0xdb => {
succeeded: reader.read_bool()?, let last = reader.read_bool()?;
let length = reader.read_u16()?;
let mut data: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE] = [0; CORE_MGMT_PAYLOAD_MAX_SIZE];
reader.read_exact(&mut data[0..length as usize])?;
Packet::CoreMgmtGetLogReply {
last: last,
length: length,
data: data,
}
}, },
0xdf => Packet::CoreMgmtConfigReadContinue { 0xdc => {
destination: reader.read_u8()?, let length = reader.read_u16()?;
let last = reader.read_bool()?;
let mut value: [u8; CORE_MGMT_PAYLOAD_MAX_SIZE] = [0; CORE_MGMT_PAYLOAD_MAX_SIZE];
reader.read_exact(&mut value[0..length as usize])?;
Packet::CoreMgmtConfigReadReply {
length: length,
last: last,
value: value,
}
}, },
0xdd => Packet::CoreMgmtAck,
0xde => Packet::CoreMgmtNack,
ty => return Err(Error::UnknownPacket(ty)), ty => return Err(Error::UnknownPacket(ty)),
}) })
@ -1091,28 +1086,23 @@ impl Packet {
writer.write_u8(0xcc)?; writer.write_u8(0xcc)?;
writer.write_u8(destination)?; writer.write_u8(destination)?;
} }
Packet::CoreMgmtGetLogRequest { destination, clear } => { Packet::CoreMgmtGetLogRequest { destination, clear } => {
writer.write_u8(0xd0)?; writer.write_u8(0xd0)?;
writer.write_u8(destination)?; writer.write_u8(destination)?;
writer.write_bool(clear)?; writer.write_bool(clear)?;
} }
Packet::CoreMgmtGetLogReply { last, length, data } => {
writer.write_u8(0xd1)?;
writer.write_bool(last)?;
writer.write_u16(length)?;
writer.write_all(&data[0..length as usize])?;
}
Packet::CoreMgmtClearLogRequest { destination } => { Packet::CoreMgmtClearLogRequest { destination } => {
writer.write_u8(0xd2)?; writer.write_u8(0xd1)?;
writer.write_u8(destination)?; writer.write_u8(destination)?;
} }
Packet::CoreMgmtSetLogLevelRequest { destination, log_level } => { Packet::CoreMgmtSetLogLevelRequest { destination, log_level } => {
writer.write_u8(0xd5)?; writer.write_u8(0xd2)?;
writer.write_u8(destination)?; writer.write_u8(destination)?;
writer.write_u8(log_level)?; writer.write_u8(log_level)?;
} }
Packet::CoreMgmtSetUartLogLevelRequest { destination, log_level } => { Packet::CoreMgmtSetUartLogLevelRequest { destination, log_level } => {
writer.write_u8(0xd6)?; writer.write_u8(0xd3)?;
writer.write_u8(destination)?; writer.write_u8(destination)?;
writer.write_u8(log_level)?; writer.write_u8(log_level)?;
} }
@ -1121,22 +1111,14 @@ impl Packet {
length, length,
key, key,
} => { } => {
writer.write_u8(0xd7)?; writer.write_u8(0xd4)?;
writer.write_u8(destination)?; writer.write_u8(destination)?;
writer.write_u16(length)?; writer.write_u16(length)?;
writer.write_all(&key[0..length as usize])?; writer.write_all(&key[0..length as usize])?;
} }
Packet::CoreMgmtConfigReadReply { Packet::CoreMgmtConfigReadContinue { destination } => {
succeeded, writer.write_u8(0xd5)?;
length, writer.write_u8(destination)?;
last,
value,
} => {
writer.write_u8(0xd8)?;
writer.write_bool(succeeded)?;
writer.write_u16(length)?;
writer.write_bool(last)?;
writer.write_all(&value[0..length as usize])?;
} }
Packet::CoreMgmtConfigWriteRequest { Packet::CoreMgmtConfigWriteRequest {
destination, destination,
@ -1144,7 +1126,7 @@ impl Packet {
last, last,
data, data,
} => { } => {
writer.write_u8(0xd9)?; writer.write_u8(0xd6)?;
writer.write_u8(destination)?; writer.write_u8(destination)?;
writer.write_u16(length)?; writer.write_u16(length)?;
writer.write_bool(last)?; writer.write_bool(last)?;
@ -1155,31 +1137,41 @@ impl Packet {
length, length,
key, key,
} => { } => {
writer.write_u8(0xda)?; writer.write_u8(0xd7)?;
writer.write_u8(destination)?; writer.write_u8(destination)?;
writer.write_u16(length)?; writer.write_u16(length)?;
writer.write_all(&key[0..length as usize])?; writer.write_all(&key[0..length as usize])?;
} }
Packet::CoreMgmtConfigEraseRequest { destination } => { Packet::CoreMgmtConfigEraseRequest { destination } => {
writer.write_u8(0xdb)?; writer.write_u8(0xd8)?;
writer.write_u8(destination)?; writer.write_u8(destination)?;
} }
Packet::CoreMgmtRebootRequest { destination } => { Packet::CoreMgmtRebootRequest { destination } => {
writer.write_u8(0xdc)?; writer.write_u8(0xd9)?;
writer.write_u8(destination)?; writer.write_u8(destination)?;
} }
Packet::CoreMgmtAllocatorDebugRequest { destination } => { Packet::CoreMgmtAllocatorDebugRequest { destination } => {
writer.write_u8(0xdd)?; writer.write_u8(0xda)?;
writer.write_u8(destination)?; writer.write_u8(destination)?;
} }
Packet::CoreMgmtAck { succeeded } => { Packet::CoreMgmtGetLogReply { last, length, data } => {
writer.write_u8(0xde)?; writer.write_u8(0xdb)?;
writer.write_bool(succeeded)?; writer.write_bool(last)?;
writer.write_u16(length)?;
writer.write_all(&data[0..length as usize])?;
} }
Packet::CoreMgmtConfigReadContinue { destination } => { Packet::CoreMgmtConfigReadReply {
writer.write_u8(0xdf)?; length,
writer.write_u8(destination)?; last,
value,
} => {
writer.write_u8(0xdc)?;
writer.write_u16(length)?;
writer.write_bool(last)?;
writer.write_all(&value[0..length as usize])?;
} }
Packet::CoreMgmtAck => writer.write_u8(0xdd)?,
Packet::CoreMgmtNack => writer.write_u8(0xde)?,
} }
Ok(()) Ok(())
} }

View File

@ -200,7 +200,7 @@ mod remote_coremgmt {
.await?; .await?;
match reply { match reply {
Packet::CoreMgmtAck { succeeded: true } => { Packet::CoreMgmtAck => {
write_i8(stream, Reply::Success as i8).await?; write_i8(stream, Reply::Success as i8).await?;
Ok(()) Ok(())
} }
@ -277,7 +277,7 @@ mod remote_coremgmt {
.await?; .await?;
match reply { match reply {
Packet::CoreMgmtAck { succeeded: true } => { Packet::CoreMgmtAck => {
write_i8(stream, Reply::Success as i8).await?; write_i8(stream, Reply::Success as i8).await?;
Ok(()) Ok(())
} }
@ -310,7 +310,7 @@ mod remote_coremgmt {
.await?; .await?;
match reply { match reply {
Packet::CoreMgmtAck { succeeded: true } => { Packet::CoreMgmtAck => {
write_i8(stream, Reply::Success as i8).await?; write_i8(stream, Reply::Success as i8).await?;
Ok(()) Ok(())
} }
@ -352,7 +352,6 @@ mod remote_coremgmt {
loop { loop {
match reply { match reply {
Packet::CoreMgmtConfigReadReply { Packet::CoreMgmtConfigReadReply {
succeeded: true,
length, length,
last, last,
value, value,
@ -413,7 +412,7 @@ mod remote_coremgmt {
data: *slice, data: *slice,
}, },
|reply| match reply { |reply| match reply {
Packet::CoreMgmtAck { succeeded: true } => Ok(()), Packet::CoreMgmtAck => Ok(()),
_ => { _ => {
error!("received unknown packet"); error!("received unknown packet");
Err(drtio::Error::UnexpectedReply.into()) Err(drtio::Error::UnexpectedReply.into())
@ -461,7 +460,7 @@ mod remote_coremgmt {
.await?; .await?;
match reply { match reply {
Packet::CoreMgmtAck { succeeded: true } => { Packet::CoreMgmtAck => {
write_i8(stream, Reply::Success as i8).await?; write_i8(stream, Reply::Success as i8).await?;
Ok(()) Ok(())
} }
@ -492,7 +491,7 @@ mod remote_coremgmt {
.await?; .await?;
match reply { match reply {
Packet::CoreMgmtAck { succeeded: true } => { Packet::CoreMgmtAck => {
write_i8(stream, Reply::Success as i8).await?; write_i8(stream, Reply::Success as i8).await?;
Ok(()) Ok(())
} }
@ -524,7 +523,7 @@ mod remote_coremgmt {
.await?; .await?;
match reply { match reply {
Packet::CoreMgmtAck { succeeded: true } => { Packet::CoreMgmtAck => {
write_i8(stream, Reply::RebootImminent as i8).await?; write_i8(stream, Reply::RebootImminent as i8).await?;
Ok(()) Ok(())
} }
@ -556,7 +555,7 @@ mod remote_coremgmt {
.await?; .await?;
match reply { match reply {
Packet::CoreMgmtAck { succeeded: true } => { Packet::CoreMgmtAck => {
write_i8(stream, Reply::Success as i8).await?; write_i8(stream, Reply::Success as i8).await?;
Ok(()) Ok(())
} }

View File

@ -1058,7 +1058,7 @@ fn process_aux_packet(
timer timer
); );
mgmt::clear_log(); mgmt::clear_log();
drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck { succeeded: true }) drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck)
} }
drtioaux::Packet::CoreMgmtSetLogLevelRequest { drtioaux::Packet::CoreMgmtSetLogLevelRequest {
destination: _destination, destination: _destination,
@ -1078,9 +1078,9 @@ fn process_aux_packet(
if let Ok(level_filter) = mgmt::byte_to_level_filter(log_level) { if let Ok(level_filter) = mgmt::byte_to_level_filter(log_level) {
info!("Changing log level to {}", log_level); info!("Changing log level to {}", log_level);
log::set_max_level(level_filter); log::set_max_level(level_filter);
drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck { succeeded: true }) drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck)
} else { } else {
drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck { succeeded: false }) drtioaux::send(0, &drtioaux::Packet::CoreMgmtNack)
} }
} }
drtioaux::Packet::CoreMgmtSetUartLogLevelRequest { drtioaux::Packet::CoreMgmtSetUartLogLevelRequest {
@ -1106,9 +1106,9 @@ fn process_aux_packet(
.unwrap() .unwrap()
.set_uart_log_level(level_filter); .set_uart_log_level(level_filter);
} }
drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck { succeeded: true }) drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck)
} else { } else {
drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck { succeeded: false }) drtioaux::send(0, &drtioaux::Packet::CoreMgmtNack)
} }
} }
drtioaux::Packet::CoreMgmtConfigReadRequest { drtioaux::Packet::CoreMgmtConfigReadRequest {
@ -1134,12 +1134,7 @@ fn process_aux_packet(
error!("invalid key"); error!("invalid key");
drtioaux::send( drtioaux::send(
0, 0,
&drtioaux::Packet::CoreMgmtConfigReadReply { &drtioaux::Packet::CoreMgmtNack,
succeeded: false,
length: 0,
last: true,
value: value_slice,
},
) )
} else { } else {
let key = core::str::from_utf8(key_slice).unwrap(); let key = core::str::from_utf8(key_slice).unwrap();
@ -1148,7 +1143,6 @@ fn process_aux_packet(
drtioaux::send( drtioaux::send(
0, 0,
&drtioaux::Packet::CoreMgmtConfigReadReply { &drtioaux::Packet::CoreMgmtConfigReadReply {
succeeded: true,
length: meta.len as u16, length: meta.len as u16,
last: meta.status.is_last(), last: meta.status.is_last(),
value: value_slice, value: value_slice,
@ -1157,12 +1151,7 @@ fn process_aux_packet(
} else { } else {
drtioaux::send( drtioaux::send(
0, 0,
&drtioaux::Packet::CoreMgmtConfigReadReply { &drtioaux::Packet::CoreMgmtNack,
succeeded: false,
length: 0,
last: true,
value: value_slice,
},
) )
} }
} }
@ -1186,7 +1175,6 @@ fn process_aux_packet(
drtioaux::send( drtioaux::send(
0, 0,
&drtioaux::Packet::CoreMgmtConfigReadReply { &drtioaux::Packet::CoreMgmtConfigReadReply {
succeeded: true,
length: meta.len as u16, length: meta.len as u16,
last: meta.status.is_last(), last: meta.status.is_last(),
value: value_slice, value: value_slice,
@ -1218,7 +1206,11 @@ fn process_aux_packet(
core_manager.clear_data(); core_manager.clear_data();
} }
drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck { succeeded: succeeded }) if succeeded {
drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck)
} else {
drtioaux::send(0, &drtioaux::Packet::CoreMgmtNack)
}
} }
drtioaux::Packet::CoreMgmtConfigRemoveRequest { drtioaux::Packet::CoreMgmtConfigRemoveRequest {
destination: _destination, destination: _destination,
@ -1239,11 +1231,11 @@ fn process_aux_packet(
let key_slice = &key[..length as usize]; let key_slice = &key[..length as usize];
if !key_slice.is_ascii() { if !key_slice.is_ascii() {
error!("invalid key"); error!("invalid key");
drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck { succeeded: false }) drtioaux::send(0, &drtioaux::Packet::CoreMgmtNack)
} else { } else {
let key = core::str::from_utf8(key_slice).unwrap(); let key = core::str::from_utf8(key_slice).unwrap();
let succeeded = core_manager.remove_config(key).is_ok(); let succeeded = core_manager.remove_config(key).is_ok();
drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck { succeeded }) drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck)
} }
} }
drtioaux::Packet::CoreMgmtConfigEraseRequest { drtioaux::Packet::CoreMgmtConfigEraseRequest {
@ -1278,7 +1270,7 @@ fn process_aux_packet(
timer timer
); );
drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck { succeeded: true })?; drtioaux::send(0, &drtioaux::Packet::CoreMgmtAck)?;
info!("reboot imminent"); info!("reboot imminent");
slcr::reboot(); slcr::reboot();
Ok(()) Ok(())