Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: reduce the number of top-level modules, combine common functionality #245

Merged
merged 2 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion espflash/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use bytemuck::{Pod, Zeroable};
use serialport::UsbPortInfo;
use slip_codec::SlipDecoder;

use self::encoder::SlipEncoder;
use crate::{
command::{Command, CommandType},
encoder::SlipEncoder,
error::{ConnectionError, Error, ResultExt, RomError, RomErrorKind},
interface::Interface,
};
Expand Down Expand Up @@ -305,3 +305,54 @@ pub fn reset_after_flash(serial: &mut Interface, pid: u16) -> Result<(), serialp

Ok(())
}

mod encoder {
use std::io::Write;

const END: u8 = 0xC0;
const ESC: u8 = 0xDB;
const ESC_END: u8 = 0xDC;
const ESC_ESC: u8 = 0xDD;

pub struct SlipEncoder<'a, W: Write> {
writer: &'a mut W,
len: usize,
}

impl<'a, W: Write> SlipEncoder<'a, W> {
/// Creates a new encoder context
pub fn new(writer: &'a mut W) -> std::io::Result<Self> {
let len = writer.write(&[END])?;
Ok(Self { writer, len })
}

pub fn finish(mut self) -> std::io::Result<usize> {
self.len += self.writer.write(&[END])?;
Ok(self.len)
}
}

impl<'a, W: Write> Write for SlipEncoder<'a, W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
for value in buf.iter() {
match *value {
END => {
self.len += self.writer.write(&[ESC, ESC_END])?;
}
ESC => {
self.len += self.writer.write(&[ESC, ESC_ESC])?;
}
_ => {
self.len += self.writer.write(&[*value])?;
}
}
}

Ok(buf.len())
}

fn flush(&mut self) -> std::io::Result<()> {
self.writer.flush()
}
}
}
48 changes: 0 additions & 48 deletions espflash/src/encoder.rs

This file was deleted.

4 changes: 3 additions & 1 deletion espflash/src/flasher.rs → espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use log::debug;
use serialport::UsbPortInfo;
use strum_macros::{Display, EnumVariantNames};

use self::stubs::FlashStub;
use crate::{
chip::Chip,
command::{Command, CommandType},
Expand All @@ -14,10 +15,11 @@ use crate::{
error::{ConnectionError, FlashDetectError, ResultExt},
image_format::ImageFormatId,
interface::Interface,
stubs::FlashStub,
Error,
};

mod stubs;

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(3);

pub(crate) const FLASH_SECTOR_SIZE: usize = 0x1000;
Expand Down
12 changes: 6 additions & 6 deletions espflash/src/stubs.rs → espflash/src/flasher/stubs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ pub struct FlashStub {

// Include stub objects in binary

const STUB_32: &str = include_str!("../resources/stubs/stub_flasher_32.json");
const STUB_32C2: &str = include_str!("../resources/stubs/stub_flasher_32c2.json");
const STUB_32C3: &str = include_str!("../resources/stubs/stub_flasher_32c3.json");
const STUB_32S2: &str = include_str!("../resources/stubs/stub_flasher_32s2.json");
const STUB_32S3: &str = include_str!("../resources/stubs/stub_flasher_32s3.json");
const STUB_8266: &str = include_str!("../resources/stubs/stub_flasher_8266.json");
const STUB_32: &str = include_str!("../../resources/stubs/stub_flasher_32.json");
const STUB_32C2: &str = include_str!("../../resources/stubs/stub_flasher_32c2.json");
const STUB_32C3: &str = include_str!("../../resources/stubs/stub_flasher_32c3.json");
const STUB_32S2: &str = include_str!("../../resources/stubs/stub_flasher_32s2.json");
const STUB_32S3: &str = include_str!("../../resources/stubs/stub_flasher_32s3.json");
const STUB_8266: &str = include_str!("../../resources/stubs/stub_flasher_8266.json");

impl FlashStub {
/// Fetch flash stub for the provided chip
Expand Down
2 changes: 0 additions & 2 deletions espflash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ pub mod cli;
pub mod command;
pub mod connection;
pub mod elf;
pub mod encoder;
pub mod error;
pub mod flash_target;
pub mod flasher;
pub mod image_format;
pub mod interface;
pub mod stubs;

pub mod logging {
use env_logger::Env;
Expand Down