1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2021 Brian J. Tarricone <brian@tarricone.org>
//
// This file is part of ESPMonitor.
//
// ESPMonitor is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ESPMonitor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ESPMonitor.  If not, see <https://www.gnu.org/licenses/>.

use clap::{ArgEnum, Parser};
use std::{
    convert::TryFrom,
    ffi::OsString,
    io::{Error as IoError, ErrorKind},
};

#[derive(Debug, Clone, Copy, PartialEq, ArgEnum)]
pub enum Framework {
    Baremetal,
    EspIdf,
}

impl Framework {
    pub fn from_target<S: AsRef<str>>(target: S) -> Result<Self, IoError> {
        let target = target.as_ref();
        if target.ends_with("-espidf") {
            Ok(Framework::EspIdf)
        } else if target.ends_with("-none-elf") {
            Ok(Framework::Baremetal)
        } else {
            Err(IoError::new(
                ErrorKind::InvalidInput,
                format!("Can't figure out framework from target '{}'", target),
            ))
        }
    }
}

impl TryFrom<&str> for Framework {
    type Error = IoError;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "baremetal" => Ok(Framework::Baremetal),
            "esp-idf" | "espidf" => Ok(Framework::EspIdf),
            _ => Err(IoError::new(
                ErrorKind::InvalidInput,
                format!("'{}' is not a valid framework", value),
            )),
        }
    }
}

impl Default for Framework {
    fn default() -> Self {
        Framework::Baremetal
    }
}

#[derive(Debug, Clone, Copy, PartialEq, ArgEnum)]
pub enum Chip {
    ESP32,
    ESP32S2,
    ESP8266,
    ESP32C3,
}

impl Chip {
    pub fn from_target<S: AsRef<str>>(target: S) -> Result<Chip, IoError> {
        let target = target.as_ref();
        if target.contains("-esp32-") {
            Ok(Chip::ESP32)
        } else if target.contains("-esp32s2-") {
            Ok(Chip::ESP32S2)
        } else if target.contains("-esp8266-") {
            Ok(Chip::ESP8266)
        } else {
            Err(IoError::new(
                ErrorKind::InvalidInput,
                format!(
                    "Can't figure out chip from target '{}'; try specifying the --chip option",
                    target
                ),
            ))
        }
    }
}

impl Chip {
    pub fn target(&self, framework: Framework) -> String {
        let mut target = String::new();
        target.push_str(match self {
            Chip::ESP32C3 => "riscv32imc-",
            _ => "xtensa-",
        });
        target.push_str(match self {
            Chip::ESP32 => "esp32-",
            Chip::ESP32S2 => "esp32s2-",
            Chip::ESP8266 => "esp8266-",
            Chip::ESP32C3 => match framework {
                Framework::Baremetal => "unknown-",
                Framework::EspIdf => "esp-",
            },
        });
        target.push_str(match framework {
            Framework::Baremetal => "none-elf",
            Framework::EspIdf => "espidf",
        });
        target
    }
}

impl TryFrom<&str> for Chip {
    type Error = IoError;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "esp32" => Ok(Chip::ESP32),
            "esp32c3" => Ok(Chip::ESP32C3),
            "esp8266" => Ok(Chip::ESP8266),
            _ => Err(IoError::new(
                ErrorKind::InvalidInput,
                format!("'{}' is not a valid chip", value),
            )),
        }
    }
}

impl Default for Chip {
    fn default() -> Self {
        Chip::ESP32
    }
}

#[derive(Parser, Debug)]
#[clap(author, version, about)]
pub struct AppArgs {
    /// Reset the chip on start [default]
    #[clap(short, long)]
    pub reset: bool,

    /// Do not reset the chip on start
    #[clap(long, conflicts_with("reset"))]
    pub no_reset: bool,

    /// Baud rate of serial device
    #[clap(long, short, default_value = "115200", name = "BAUD")]
    pub speed: usize,

    /// Path to executable matching what is on the device
    #[clap(long, short, name = "BINARY")]
    pub bin: Option<OsString>,

    /// Path to the serial device
    #[clap(name = "SERIAL_DEVICE")]
    pub serial: String,
}