1
0
Fork 0

Send report to connected socket

- Remove active report in flash settings
This commit is contained in:
linuswck 2024-03-19 17:38:29 +08:00
parent 99cf17f7e4
commit 1867935047
3 changed files with 34 additions and 15 deletions

View File

@ -31,7 +31,6 @@ static mut ETH_DATA_BUFFER: [u8; 1024] = [0; 1024];
#[derive(Deserialize, Serialize, Clone, Copy, Debug)]
pub struct DeviceSettings{
report_readings: bool,
ip_settings: IpSettings,
}
@ -59,10 +58,11 @@ fn main() -> ! {
let (mut wd, mut flash_store, mut laser, mut thermostat,) = bootup(core_perif, perif);
let mut device_settings = DeviceSettings {
report_readings: false,
ip_settings: IpSettings::default()
};
let mut active_report: [bool; net::net::NUM_OF_SOCKETS] = [false; net::net::NUM_OF_SOCKETS];
let mut state = State::default();
loop {
@ -146,17 +146,20 @@ fn main() -> ! {
thermostat.power_down();
}
net::net::for_each(|mut socket| {
if net::net::eth_is_socket_active(socket) {
if device_settings.report_readings {
net::net::for_each(|mut socket, id| {
if net::net::eth_is_socket_active(socket) && net::net::eth_is_socket_connected(socket) {
if active_report[id] {
unsafe {
net::cmd_handler::send_status_report(&mut ETH_DATA_BUFFER, &mut laser, &mut thermostat, &mut socket);
}
}
}
else {
active_report[id] = false;
}
});
}
net::net::for_each(|mut socket| {
net::net::for_each(|mut socket, id| {
if net::net::eth_is_socket_active(socket) {
cortex_m::interrupt::free(|cs|
{
@ -174,7 +177,7 @@ fn main() -> ! {
info!("Ts: {:?}", sys_timer::now());
debug!("Number of bytes recv: {:?}", bytes);
// State Transition
net::cmd_handler::execute_cmd(&mut ETH_DATA_BUFFER, bytes, &mut socket, &mut laser, &mut thermostat, &mut state, &mut device_settings);
net::cmd_handler::execute_cmd(&mut ETH_DATA_BUFFER, bytes, &mut socket, &mut laser, &mut thermostat, &mut state, &mut device_settings, &mut active_report[id]);
}
}
}
@ -226,7 +229,7 @@ fn main() -> ! {
wd.feed();
laser.power_down();
thermostat.power_down();
net::net::for_each(|mut socket| {
net::net::for_each(|mut socket, _| {
if net::net::eth_is_socket_active(socket) {
unsafe {
net::cmd_handler::send_response(&mut ETH_DATA_BUFFER, net::cmd_handler::ResponseEnum::HardReset, None, &mut socket);
@ -239,10 +242,10 @@ fn main() -> ! {
laser.power_down();
thermostat.power_down();
let mut any_socket_alive = false;
net::net::for_each(|socket| {
net::net::for_each(|socket, _| {
if net::net::eth_is_socket_active(socket) {
net::net::eth_close_socket(socket);
any_socket_alive = true;
any_socket_alive = true;
}
});

View File

@ -221,7 +221,7 @@ pub struct TecSetICmd {
/// Make sure kirdy's firmware is flashed with release builds.
/// The received message must contain only one json cmd. TCP client should set TCP_NODELAY or equivalent flag in its TCP Socket
/// Settings to avoid unwanted buffering on TX Data and minimize TX latency.
pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHandle, laser: &mut LdDrive, thermostat: &mut Thermostat, state: &mut State, device_settings: &mut DeviceSettings){
pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHandle, laser: &mut LdDrive, thermostat: &mut Thermostat, state: &mut State, device_settings: &mut DeviceSettings, active_report: &mut bool){
let mut cmd = TecSetICmd {
json: TecSetICmdJson::default()
};
@ -266,7 +266,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan
match cmd.json.data_bool{
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
device_settings.report_readings = val;
*active_report = val;
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_BOOL), socket);

View File

@ -61,7 +61,7 @@ pub struct EthernetMgmtPins {
}
pub type EthInterface = Interface;
const NUM_OF_SOCKETS : usize = 4;
pub const NUM_OF_SOCKETS : usize = 4;
const TCP_BUFFER_SIZE: usize = 2048;
static mut RX_RING: Option<[RxRingEntry; 8]> = None;
static mut TX_RING: Option<[TxRingEntry; 2]> = None;
@ -225,6 +225,11 @@ impl ServerHandle {
self.iface.poll(now_fn(), &mut &mut self.dma, &mut self.socket_set);
}
pub fn is_socket_connected(&mut self, socket_handles: SocketHandle)->bool {
let socket = self.socket_set.get_mut::<Socket>(socket_handles);
socket.state() == State::Established
}
pub fn poll_socket_status(&mut self, socket_handles: SocketHandle)-> bool {
let socket = self.socket_set.get_mut::<Socket>(socket_handles);
if !socket.is_listening() && !socket.is_open() || socket.state() == State::CloseWait {
@ -385,6 +390,17 @@ pub fn eth_recv(buffer: &mut [u8], socket_handles: SocketHandle)-> usize{
}
}
pub fn eth_is_socket_connected(socket_handles: SocketHandle) -> bool {
unsafe {
if let Some(ref mut server_handle ) = SERVER_HANDLE {
server_handle.is_socket_connected(socket_handles)
}
else {
panic!("eth_is_socket_connected is called before init");
}
}
}
pub fn eth_is_socket_active(socket_handles: SocketHandle) -> bool {
unsafe {
if let Some(ref mut server_handle ) = SERVER_HANDLE {
@ -407,11 +423,11 @@ pub fn eth_close_socket(socket_handles: SocketHandle) {
}
}
pub fn for_each<F: FnMut(SocketHandle)>(mut callback: F) {
pub fn for_each<F: FnMut(SocketHandle, usize)>(mut callback: F) {
unsafe {
if let Some(ref mut server_handle ) = SERVER_HANDLE {
for i in 0..NUM_OF_SOCKETS {
callback(server_handle.socket_handles[i]);
callback(server_handle.socket_handles[i], i);
}
}
else {