pub(crate) fn maybe_write_packet(&mut self) {

        match self.state {
            State::WaitingToSend(response) => {

                // zeros leftover bytes
                let mut packet = [0u8; PACKET_SIZE];
                packet[..4].copy_from_slice(&response.channel.to_be_bytes());
                // packet[4] = response.command.into() | 0x80u8;
                packet[4] = response.command.into_u8() | 0x80;
                packet[5..7].copy_from_slice(&response.length.to_be_bytes());

                let fits_in_one_packet = 7 + response.length as usize <= PACKET_SIZE;
                if fits_in_one_packet {
                    packet[7..][..response.length as usize]
                        .copy_from_slice( &self.buffer[..response.length as usize]);
                    self.state = State::Idle;
                } else {
                    packet[7..].copy_from_slice(&self.buffer[..PACKET_SIZE - 7]);
                }

                // try actually sending
                // info_now!("attempting to write init packet {:?}, {:?}",
                //           &packet[..32], &packet[32..]);
                let result = self.write_endpoint.write(&packet);

                match result {
                    Err(UsbError::WouldBlock) => {
                        // fine, can't write try later
                        // this shouldn't happen probably
                        info!("hid usb WouldBlock");
                    },
                    Err(_) => {
                        // info_now!("weird USB errrorrr");
                        panic!("unexpected error writing packet!");
                    },
                    Ok(PACKET_SIZE) => {
                        // goodie, this worked
                        if fits_in_one_packet {
                            self.state = State::Idle;
                            // info_now!("StartSent {} bytes, idle again", response.length);
                            // info_now!("IDLE again");
                        } else {
                            self.state = State::Sending((response, MessageState::default()));
                            // info_now!(
                            //     "StartSent {} of {} bytes, waiting to send again",
                            //     PACKET_SIZE - 7, response.length);
                            // info_now!("State: {:?}", &self.state);
                        }
                    },
                    Ok(_) => {
                        // info_now!("short write");
                        panic!("unexpected size writing packet!");
                    },
                };
            },

            State::Sending((response, mut message_state)) => {
                // info_now!("in StillSending");
                let mut packet = [0u8; PACKET_SIZE];
                packet[..4].copy_from_slice(&response.channel.to_be_bytes());
                packet[4] = message_state.next_sequence;

                let sent = message_state.transmitted;
                let remaining = response.length as usize - sent;
                let last_packet = 5 + remaining <= PACKET_SIZE;
                if last_packet {
                    packet[5..][..remaining].copy_from_slice(
                        &self.buffer[message_state.transmitted..][..remaining]);
                } else {
                    packet[5..].copy_from_slice(
                        &self.buffer[message_state.transmitted..][..PACKET_SIZE - 5]);
                }

                // try actually sending
                // info_now!("attempting to write cont packet {:?}, {:?}",
                //           &packet[..32], &packet[32..]);
                let result = self.write_endpoint.write(&packet);

                match result {
                    Err(UsbError::WouldBlock) => {
                        // fine, can't write try later
                        // this shouldn't happen probably
                        // info_now!("can't send seq {}, write endpoint busy",
                        //           message_state.next_sequence);
                    },
                    Err(_) => {
                        // info_now!("weird USB error");
                        panic!("unexpected error writing packet!");
                    },
                    Ok(PACKET_SIZE) => {
                        // goodie, this worked
                        if last_packet {
                            self.state = State::Idle;
                            // info_now!("in IDLE state after {:?}", &message_state);
                        } else {
                            message_state.absorb_packet();
                            // DANGER! destructuring in the match arm copies out
                            // message state, so need to update state
                            // info_now!("sent one more, now {:?}", &message_state);
                            self.state = State::Sending((response, message_state));
                        }
                    },
                    Ok(_) => {
                        debug!("short write");
                        panic!("unexpected size writing packet!");
                    },
                };
            },

            // nothing to send
            _ => {
            },
        }
    }
// called from poll, and when a packet has been sent
called from poll, and when a packet has been sent
called from poll and when a packet has been sent
pub(crate) fn maybe_write_packet(&mut self) {
        match self.state {
            State::WaitingToSend(response) => {
                // zeros leftover bytes
                let mut packet = [0u8; PACKET_SIZE];
                packet[..4].copy_from_slice(&response.channel.to_be_bytes());
                // packet[4] = response.command.into() | 0x80u8;
                packet[4] = response.command.into_u8() | 0x80;
                packet[5..7].copy_from_slice(&response.length.to_be_bytes());
                let fits_in_one_packet = 7 + response.length as usize <= PACKET_SIZE;
                if fits_in_one_packet {
                    packet[7..][..response.length as usize]
                        .copy_from_slice( &self.buffer[..response.length as usize]);
                    self.state = State::Idle;
                } else {
                    packet[7..].copy_from_slice(&self.buffer[..PACKET_SIZE - 7]);
                }
                // try actually sending
                // info_now!("attempting to write init packet {:?}, {:?}",
                //           &packet[..32], &packet[32..]);
                let result = self.write_endpoint.write(&packet);
                match result {
                    Err(UsbError::WouldBlock) => {
                        // fine, can't write try later
                        // this shouldn't happen probably
                        info!("hid usb WouldBlock");
                    },
                    Err(_) => {
                        // info_now!("weird USB errrorrr");
                        panic!("unexpected error writing packet!");
                    },
                    Ok(PACKET_SIZE) => {
                        // goodie, this worked
                        if fits_in_one_packet {
                            self.state = State::Idle;
                            // info_now!("StartSent {} bytes, idle again", response.length);
                            // info_now!("IDLE again");
                        } else {
                            self.state = State::Sending((response, MessageState::default()));
                            // info_now!(
                            //     "StartSent {} of {} bytes, waiting to send again",
                            //     PACKET_SIZE - 7, response.length);
                            // info_now!("State: {:?}", &self.state);
                        }
                    },
                    Ok(_) => {
                        // info_now!("short write");
                        panic!("unexpected size writing packet!");
                    },
                };
            },
            State::Sending((response, mut message_state)) => {
                // info_now!("in StillSending");
                let mut packet = [0u8; PACKET_SIZE];
                packet[..4].copy_from_slice(&response.channel.to_be_bytes());
                packet[4] = message_state.next_sequence;
                let sent = message_state.transmitted;
                let remaining = response.length as usize - sent;
                let last_packet = 5 + remaining <= PACKET_SIZE;
                if last_packet {
                    packet[5..][..remaining].copy_from_slice(
                        &self.buffer[message_state.transmitted..][..remaining]);
                } else {
                    packet[5..].copy_from_slice(
                        &self.buffer[message_state.transmitted..][..PACKET_SIZE - 5]);
                }
                // try actually sending
                // info_now!("attempting to write cont packet {:?}, {:?}",
                //           &packet[..32], &packet[32..]);
                let result = self.write_endpoint.write(&packet);
                match result {
                    Err(UsbError::WouldBlock) => {
                        // fine, can't write try later
                        // this shouldn't happen probably
                        // info_now!("can't send seq {}, write endpoint busy",
                        //           message_state.next_sequence);
                    },
                    Err(_) => {
                        // info_now!("weird USB error");
                        panic!("unexpected error writing packet!");
                    },
                    Ok(PACKET_SIZE) => {
                        // goodie, this worked
                        if last_packet {
                            self.state = State::Idle;
                            // info_now!("in IDLE state after {:?}", &message_state);
                        } else {
                            message_state.absorb_packet();
                            // DANGER! destructuring in the match arm copies out
                            // message state, so need to update state
                            // info_now!("sent one more, now {:?}", &message_state);
                            self.state = State::Sending((response, message_state));
                        }
                    },
                    Ok(_) => {
                        debug!("short write");
                        panic!("unexpected size writing packet!");
                    },
                };
            },
            // nothing to send
            _ => {
            },
        }
    }
pub ( crate ) fn maybe_write_packet ( & mut self ) { match self . state { State :: WaitingToSend ( response ) => { // zeros leftover bytes let mut packet = [ 0u8 ; PACKET_SIZE ] ; packet [ .. 4 ] . copy_from_slice ( & response . channel . to_be_bytes ( ) ) ; // packet[4] = response.command.into() | 0x80u8; packet [ 4 ] = response . command . into_u8 ( ) | 0x80 ; packet [ 5 .. 7 ] . copy_from_slice ( & response . length . to_be_bytes ( ) ) ; let fits_in_one_packet = 7 + response . length as usize <= PACKET_SIZE ; if fits_in_one_packet { packet [ 7 .. ] [ .. response . length as usize ] . copy_from_slice ( & self . buffer [ .. response . length as usize ] ) ; self . state = State :: Idle ; } else { packet [ 7 .. ] . copy_from_slice ( & self . buffer [ .. PACKET_SIZE - 7 ] ) ; } // try actually sending // info_now!("attempting to write init packet {:?}, {:?}", //           &packet[..32], &packet[32..]); let result = self . write_endpoint . write ( & packet ) ; match result { Err ( UsbError :: WouldBlock ) => { // fine, can't write try later // this shouldn't happen probably info ! ( " " ) ; } , Err ( _ ) => { // info_now!("weird USB errrorrr"); panic ! ( " " ) ; } , Ok ( PACKET_SIZE ) => { // goodie, this worked if fits_in_one_packet { self . state = State :: Idle ; // info_now!("StartSent {} bytes, idle again", response.length); // info_now!("IDLE again"); } else { self . state = State :: Sending ( ( response , MessageState :: default ( ) ) ) ; // info_now!( //     "StartSent {} of {} bytes, waiting to send again", //     PACKET_SIZE - 7, response.length); // info_now!("State: {:?}", &self.state); } } , Ok ( _ ) => { // info_now!("short write"); panic ! ( " " ) ; } , } ; } , State :: Sending ( ( response , mut message_state ) ) => { // info_now!("in StillSending"); let mut packet = [ 0u8 ; PACKET_SIZE ] ; packet [ .. 4 ] . copy_from_slice ( & response . channel . to_be_bytes ( ) ) ; packet [ 4 ] = message_state . next_sequence ; let sent = message_state . transmitted ; let remaining = response . length as usize - sent ; let last_packet = 5 + remaining <= PACKET_SIZE ; if last_packet { packet [ 5 .. ] [ .. remaining ] . copy_from_slice ( & self . buffer [ message_state . transmitted .. ] [ .. remaining ] ) ; } else { packet [ 5 .. ] . copy_from_slice ( & self . buffer [ message_state . transmitted .. ] [ .. PACKET_SIZE - 5 ] ) ; } // try actually sending // info_now!("attempting to write cont packet {:?}, {:?}", //           &packet[..32], &packet[32..]); let result = self . write_endpoint . write ( & packet ) ; match result { Err ( UsbError :: WouldBlock ) => { // fine, can't write try later // this shouldn't happen probably // info_now!("can't send seq {}, write endpoint busy", //           message_state.next_sequence); } , Err ( _ ) => { // info_now!("weird USB error"); panic ! ( " " ) ; } , Ok ( PACKET_SIZE ) => { // goodie, this worked if last_packet { self . state = State :: Idle ; // info_now!("in IDLE state after {:?}", &message_state); } else { message_state . absorb_packet ( ) ; // DANGER! destructuring in the match arm copies out // message state, so need to update state // info_now!("sent one more, now {:?}", &message_state); self . state = State :: Sending ( ( response , message_state ) ) ; } } , Ok ( _ ) => { debug ! ( " " ) ; panic ! ( " " ) ; } , } ; } , // nothing to send _ => { } , } }
called from poll, and when a packet has been sent
called from poll and when a packet has been sent
&mut self
&mut self