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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#![deny(clippy::all)]
#![warn(clippy::pedantic)]

use casper_utils::csl;
use kstring::KString;
use std::{
    self,
    collections::HashMap,
    fmt::Write,
    net::UdpSocket,
    time::Duration,
};
use tftp_client::{
    download,
    upload,
};
use thiserror::Error;
use tracing::debug;

pub const FLASH_SECTOR_SIZE: u32 = 0x10000;
pub const DEFAULT_TIMEOUT: Duration = Duration::from_millis(500);
pub const MAX_TIMEOUT: Duration = Duration::from_secs(5);

#[derive(Debug, Error)]
pub enum Error {
    #[error(transparent)]
    Tftp(#[from] tftp_client::Error),
    #[error("Some part of the received payload was incomplete")]
    Incomplete,
    #[error("While trying to parse a string from a response, we received invalid UTF8")]
    Utf8(#[from] std::str::Utf8Error),
    #[error("No metadata returned when we requested metadata")]
    MissingMetadata,
    #[error(transparent)]
    Csl(#[from] csl::Error),
}

// The FPGA handles errors poorly, so when we try to move to quick (esp with sequential commands),
// we want to retry. We'll create wrappers around the tftp functions to retry on procotol errors,
// but bail on all others
fn retrying_download(
    filename: &str,
    socket: &UdpSocket,
    timeout: Duration,
    max_timeout: Duration,
    retries: usize,
) -> Result<Vec<u8>, Error> {
    let mut local_retries = 0;
    let mut this_timeout = timeout;
    loop {
        if local_retries == retries {
            return Err(Error::Tftp(tftp_client::Error::Timeout));
        }
        let res = download(filename, socket, timeout, max_timeout, retries);
        match res {
            Ok(v) => return Ok(v),
            Err(tftp_client::Error::Protocol { code, msg }) => {
                debug!("Protocol error: {:?} {msg}", code);
                std::thread::sleep(this_timeout);
                local_retries += 1;
                this_timeout += this_timeout / 2;
                if this_timeout > MAX_TIMEOUT {
                    this_timeout = MAX_TIMEOUT;
                }
                continue;
            }
            Err(e) => {
                return Err(Error::Tftp(e));
            }
        }
    }
}

fn retrying_upload(
    filename: &str,
    data: &[u8],
    socket: &UdpSocket,
    timeout: Duration,
    max_timeout: Duration,
    retries: usize,
) -> Result<(), Error> {
    let mut local_retries = 0;
    let mut this_timeout = timeout;
    loop {
        if local_retries == retries {
            return Err(Error::Tftp(tftp_client::Error::Timeout));
        }
        let res = upload(filename, data, socket, timeout, max_timeout, retries);
        match res {
            Ok(()) => return Ok(()),
            Err(tftp_client::Error::Protocol { code, msg }) => {
                debug!("Protocol error: {:?} {msg}", code);
                local_retries += 1;
                std::thread::sleep(this_timeout);
                local_retries += 1;
                this_timeout += this_timeout / 2;
                if this_timeout > MAX_TIMEOUT {
                    this_timeout = MAX_TIMEOUT;
                }
                continue;
            }
            Err(e) => {
                return Err(Error::Tftp(e));
            }
        }
    }
}

/// Gets the temperature of the remote device in Celsius
/// # Errors
/// Returns an error on TFTP errors
pub fn temp(socket: &UdpSocket, retries: usize) -> Result<f32, Error> {
    let bytes = retrying_download("/temp", socket, DEFAULT_TIMEOUT, MAX_TIMEOUT, retries)?;
    let four_bytes = bytes.get(..4).ok_or(Error::Incomplete)?;
    Ok(f32::from_be_bytes(
        four_bytes.try_into().map_err(|_| Error::Incomplete)?,
    ))
}

/// Gets the list of top level commands (as a string)
/// # Errors
/// Returns an error on TFTP errors
pub fn help(socket: &UdpSocket, retries: usize) -> Result<String, Error> {
    let bytes = retrying_download("/help", socket, DEFAULT_TIMEOUT, MAX_TIMEOUT, retries)?;
    Ok(std::str::from_utf8(&bytes)?.to_string())
}

/// Gets the list of all devices supported by the currently running gateware
/// Returns a hash map from device name to (addr,length)
/// # Errors
/// Returns an error on TFTP errors
pub fn listdev(socket: &UdpSocket, retries: usize) -> Result<HashMap<String, (u32, u32)>, Error> {
    // Grab CSL bytes
    let bytes = retrying_download("/listdev", socket, DEFAULT_TIMEOUT, MAX_TIMEOUT, retries)?;
    // Unpack CSL
    let csl = csl::from_bytes(&bytes)?;
    // Translate into our device map
    csl.into_iter()
        .map(|(k, v)| {
            // Value should be exactly 8 bytes
            // First 4 is offset, second is length
            let addr = u32::from_be_bytes(v[..4].try_into().map_err(|_| Error::Incomplete)?);
            let length = u32::from_be_bytes(v[4..].try_into().map_err(|_| Error::Incomplete)?);
            Ok((k, (addr, length)))
        })
        .collect()
}

/// Read memory associated with the gateware device `device`
/// We can read `offset` words (4 bytes) into a given device in multiples on `n` words
/// The special case of `n` = 0 will read all the bytes at that location
/// # Errors
/// Returns an error on TFTP errors
pub fn read_device(
    device: &str,
    offset: usize,
    n: usize,
    socket: &UdpSocket,
    retries: usize,
) -> Result<Vec<u8>, Error> {
    // To start the request, we need to form the filename string, defined by the TAPCP
    // spec as - `/dev/DEV_NAME[.WORD_OFFSET[.NWORDS]]` with WORD_OFFSET and NWORDs in hexadecimal
    let filename = format!("/dev/{device}.{offset:x}.{n:x}");
    let bytes = retrying_download(&filename, socket, DEFAULT_TIMEOUT, MAX_TIMEOUT, retries)?;
    if n != 0 && bytes.len() != n * 4 {
        Err(Error::Incomplete)
    } else {
        Ok(bytes)
    }
}

/// Write bytes to the device named `device`
/// # Errors
/// Returns an error on TFTP errors
pub fn write_device(
    device: &str,
    offset: usize,
    data: &[u8],
    socket: &UdpSocket,
    retries: usize,
) -> Result<(), Error> {
    // To start the request, we need to form the filename string, defined by the TAPCP
    // spec as - `/dev/DEV_NAME[.WORD_OFFSET]` with WORD_OFFSET and NWORDs in hexadecimal
    let filename = format!("/dev/{device}.{offset:x}");
    // Then do it
    retrying_upload(
        &filename,
        data,
        socket,
        DEFAULT_TIMEOUT,
        MAX_TIMEOUT,
        retries,
    )
}

/// Read memory from the onboard flash
/// `offset` and `n` are in increments of 4 byte words, just like `read_device`
/// # Errors
/// Returns an error on TFTP errors
pub fn read_flash(
    offset: usize,
    n: usize,
    socket: &UdpSocket,
    retries: usize,
) -> Result<Vec<u8>, Error> {
    // spec as - `/flash.WORD_OFFSET[.NWORDS]` with WORD_OFFSET and NWORDs in hexadecimal
    let filename = format!("/flash.{offset:x}.{n:x}");
    let bytes = retrying_download(&filename, socket, DEFAULT_TIMEOUT, MAX_TIMEOUT, retries)?;
    Ok(bytes)
}

/// Writes data to the onboard flash
/// `offset` are in increments of 4 byte words, just like `read_device`
/// # Errors
/// Returns an error on TFTP errors
pub fn write_flash(
    offset: usize,
    data: &[u8],
    socket: &UdpSocket,
    retries: usize,
) -> Result<(), Error> {
    let filename = format!("/flash.{offset:x}");
    retrying_upload(
        &filename,
        data,
        socket,
        DEFAULT_TIMEOUT,
        MAX_TIMEOUT,
        retries,
    )
}

/// Reboot the FPGA from the bitstream program at the 32-bit address `addr`.
/// No validation is performed to ensure a program actually exists there
/// # Errors
/// Returns an error on TFTP errors
pub fn progdev(addr: u32, socket: &UdpSocket) -> Result<(), Error> {
    match upload(
        "/progdev",
        &addr.to_be_bytes(),
        socket,
        DEFAULT_TIMEOUT,
        MAX_TIMEOUT,
        0,
    ) {
        Ok(()) | Err(_) => (),
    }
    // Then wait as the FPGA takes a while to reboot
    std::thread::sleep(Duration::from_secs(10));
    Ok(())
}

/// Retrieves the most recent metadata (stored at the 32-bit `user_flash_loc` address)
/// # Errors
/// Returns an error on TFTP errors or if the metadata couldn't be found
pub fn get_metadata(
    socket: &UdpSocket,
    user_flash_loc: u32,
    retries: usize,
) -> Result<HashMap<KString, String>, Error> {
    let mut dict_str = String::new();
    let mut chunks = 0;
    let chunk_size = 1024 / 4;
    loop {
        if chunks > 128 {
            return Err(Error::MissingMetadata);
        }
        let raw = read_flash(
            (user_flash_loc / 4 + chunks * chunk_size) as usize,
            chunk_size as usize,
            socket,
            retries,
        )?;
        dict_str.push_str(std::str::from_utf8(&raw)?);
        match dict_str.find("?end") {
            Some(idx) => {
                dict_str = dict_str.split_at(idx).0.to_string();
                break;
            }
            None => chunks += 1,
        }
    }
    Ok(dict_str
        .split('?')
        .filter_map(|kv| kv.split_once('\t'))
        .map(|(k, v)| (k.to_string().into(), v.to_string()))
        .collect())
}

/// Program arbitrary metadata (stored at the 32-bit `user_flash_loc` address)
/// # Errors
/// Returns an error on TFTP errors or if the metadata couldn't be found
#[allow(clippy::implicit_hasher)]
pub fn set_metadata(
    data: &HashMap<KString, String>,
    socket: &UdpSocket,
    user_flash_loc: u32,
    retries: usize,
) -> Result<(), Error> {
    // Dict is written as ?<key>\t<value> pairs followed by ?end
    // It must be padded with zeros to be a multiple of 1024
    let mut dict_str = data.iter().fold(String::new(), |mut output, (k, v)| {
        let _ = write!(output, "?{k}\t{v}");
        output
    });
    dict_str.push_str("?end");
    let mut bytes = dict_str.as_bytes().to_vec();
    // Padding
    if bytes.len() % 1024 != 0 {
        bytes.append(&mut vec![b'0'; 1024 - bytes.len() % 1024]);
    }
    // Write
    write_flash((user_flash_loc / 4) as usize, &bytes, socket, retries)
}