nanoflash 0.1.1

Lay out the flash of ESP32 and Arduino boards and write firmware to them through their bootloaders, natively or in the browser over Web Serial.
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! The serial transport of the browser, over Web Serial (Chromium-based browsers).
//!
//! A [`WebSerialPort`] wraps a `SerialPort` the page obtained from `navigator.serial`; a
//! [`WebPortProvider`] finds a device that came back as a new port among the ports the page was
//! granted. Both stay `no_std`: time is measured by `setTimeout`, not by a clock.

mod bindings;

use alloc::string::{String, ToString};
use alloc::vec::Vec;

use js_sys::{Array, Object, Promise, Reflect, Uint8Array};
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;

use crate::transport::{
    DeviceFilter, Duration, PortProvider, SerialPort, Signals, TransportError, UsbIdentity,
};

/// The receive buffer of the browser, large enough for a whole flash block.
const BUFFER_SIZE: u32 = 65536;

/// How long to wait before opening again a port that is still open or closing.
const OPEN_RETRY_INTERVAL: Duration = Duration::from_millis(100);

/// How many times to open again a port that is still open or closing.
const OPEN_RETRIES: u32 = 10;

/// The read errors after which Web Serial gives a new `readable` stream: a break, a framing or
/// parity error, or an overflow of the receive buffer.
const RECOVERABLE_READ_ERRORS: [&str; 4] = [
    "BreakError",
    "FramingError",
    "ParityError",
    "BufferOverrunError",
];

/// How many recoverable errors one read gets over, each with a new reader.
const READ_RECOVERIES: u32 = 3;

/// How often the provider looks for a device that has not appeared yet.
const POLL_INTERVAL: Duration = Duration::from_millis(250);

/// How long a write may wait beyond the time its bytes take on the line, as the native
/// transport waits for a stalled write.
const WRITE_TIMEOUT: Duration = Duration::from_secs(1);

/// How long opening, closing, cancelling a read, setting the signals or listing the ports may
/// take.
const PORT_TIMEOUT: Duration = Duration::from_secs(3);

/// An object with the given properties.
fn object(properties: &[(&str, JsValue)]) -> Object {
    let object = Object::new();
    for (name, value) in properties {
        // Setting a property of a fresh plain object cannot fail.
        let _ = Reflect::set(&object, &JsValue::from_str(name), value);
    }
    object
}

/// A property of `value`, `undefined` when it has none or is not an object.
fn property(value: &JsValue, name: &str) -> JsValue {
    if !value.is_object() {
        return JsValue::UNDEFINED;
    }
    Reflect::get(value, &JsValue::from_str(name)).unwrap_or(JsValue::UNDEFINED)
}

/// The `name` of a JavaScript error, such as `NetworkError`.
fn error_name(error: &JsValue) -> Option<String> {
    property(error, "name").as_string()
}

/// What a JavaScript error says: its `message`, else its string form.
fn describe(error: &JsValue) -> String {
    if let Some(message) = property(error, "message").as_string() {
        return message;
    }
    if let Some(text) = error.as_string() {
        return text;
    }
    bindings::string(error).unwrap_or_else(|_| "an unknown error".to_string())
}

/// The transport error for a rejection of Web Serial: a lost device (`NetworkError`) is
/// `Disconnected`.
fn transport_error(error: &JsValue) -> TransportError {
    match error_name(error).as_deref() {
        Some("NetworkError") => TransportError::Disconnected,
        _ => TransportError::Io {
            message: describe(error),
        },
    }
}

/// Waits up to `limit` for `promise`: its value, `None` when the limit passed first, or what it
/// rejected with.
async fn bounded(promise: &Promise, limit: Duration) -> Result<Option<JsValue>, JsValue> {
    // The timer resolves to an object nothing else can give.
    let expired: JsValue = Object::new().into();
    let mut id = JsValue::UNDEFINED;
    let timer = Promise::new(&mut |resolve, _reject| {
        id = bindings::set_timeout_with(&resolve, milliseconds(limit), &expired);
    });
    let outcome = JsFuture::from(Promise::race(&Array::of2(promise, &timer))).await;
    if matches!(&outcome, Ok(value) if Object::is(value, &expired)) {
        return Ok(None);
    }
    bindings::clear_timeout(&id);
    outcome.map(Some)
}

/// Waits up to `limit` for the promise a call of Web Serial returned; what it threw or rejected
/// with is a transport error, and so is the limit passing first, naming `what` was done.
async fn wait(
    promise: Result<Promise, JsValue>,
    limit: Duration,
    what: &str,
) -> Result<JsValue, TransportError> {
    let promise = promise.map_err(|error| transport_error(&error))?;
    match bounded(&promise, limit).await {
        Ok(Some(value)) => Ok(value),
        Ok(None) => Err(TransportError::Io {
            message: alloc::format!("{what} did not finish within {} ms", limit.as_millis()),
        }),
        Err(error) => Err(transport_error(&error)),
    }
}

/// Lets a promise settle unobserved, without an unhandled rejection.
fn ignore(promise: Result<Promise, JsValue>) {
    if let Ok(promise) = promise {
        // The handlers the future attaches outlive it, and swallow the outcome.
        drop(JsFuture::from(promise));
    }
}

/// The milliseconds of `duration`, at most `i32::MAX` (24 days) as `setTimeout` takes.
fn milliseconds(duration: Duration) -> i32 {
    i32::try_from(duration.as_millis()).unwrap_or(i32::MAX)
}

/// Waits for `duration`.
async fn sleep(duration: Duration) {
    let promise = Promise::new(&mut |resolve, _reject| {
        bindings::set_timeout(&resolve, milliseconds(duration));
    });
    // The promise only resolves.
    let _ = JsFuture::from(promise).await;
}

/// How long writing `length` bytes at `baud_rate` may take: twice their time on the line (ten
/// bits a byte), and [`WRITE_TIMEOUT`].
fn write_limit(length: usize, baud_rate: u32) -> Duration {
    let bits = u64::try_from(length).unwrap_or(u64::MAX).saturating_mul(20);
    let line = bits.saturating_mul(1000) / u64::from(baud_rate.max(1));
    WRITE_TIMEOUT.saturating_add(Duration::from_millis(line))
}

/// Closes `port`, a `SerialPort` of Web Serial whose streams are unlocked, waiting at most a few
/// seconds.
pub async fn close_port(port: &JsValue) -> Result<(), TransportError> {
    let port: &bindings::SerialPort = port.unchecked_ref();
    wait(port.close(), PORT_TIMEOUT, "closing the port")
        .await
        .map(drop)
}

/// A USB identifier from `getInfo()`: a whole number of 16 bits.
fn identifier(value: &JsValue) -> Option<u16> {
    let number = value.as_f64()?;
    if !(0.0..=f64::from(u16::MAX)).contains(&number) {
        return None;
    }
    // In range, so the conversion only drops a fraction, which makes the identifier invalid.
    let identifier = number as u16;
    (f64::from(identifier) == number).then_some(identifier)
}

/// The USB identity `getInfo()` gives, when it has both identifiers. Web Serial does not give
/// the serial number.
fn identity_of(port: &bindings::SerialPort) -> Option<UsbIdentity> {
    let information = port.get_info().ok()?;
    Some(UsbIdentity {
        vendor: identifier(&property(&information, "usbVendorId"))?,
        product: identifier(&property(&information, "usbProductId"))?,
        serial_number: None,
    })
}

/// Opens `port` at `baud_rate`, again while it is still open or closing (`InvalidStateError`).
async fn open_port(port: &bindings::SerialPort, baud_rate: u32) -> Result<(), TransportError> {
    let options = object(&[
        ("baudRate", JsValue::from(baud_rate)),
        ("bufferSize", JsValue::from(BUFFER_SIZE)),
    ]);
    let mut retries = 0;
    loop {
        let outcome = match port.open(&options) {
            Ok(promise) => bounded(&promise, PORT_TIMEOUT).await,
            Err(error) => Err(error),
        };
        match outcome {
            Ok(Some(_)) => return Ok(()),
            Ok(None) => {
                return Err(TransportError::Io {
                    message: alloc::format!(
                        "opening the port did not finish within {} ms",
                        PORT_TIMEOUT.as_millis()
                    ),
                });
            }
            Err(error)
                if retries < OPEN_RETRIES
                    && error_name(&error).as_deref() == Some("InvalidStateError") =>
            {
                retries += 1;
                sleep(OPEN_RETRY_INTERVAL).await;
            }
            Err(error) => return Err(transport_error(&error)),
        }
    }
}

/// Locks the streams of an open port.
fn lock_streams(
    port: &bindings::SerialPort,
) -> Result<(bindings::Reader, bindings::Writer), TransportError> {
    let closed = || TransportError::Io {
        message: "the port has no stream: it is not open".to_string(),
    };
    let readable = port.readable();
    let writable = port.writable();
    if !readable.is_object() || !writable.is_object() {
        return Err(closed());
    }
    let readable: bindings::ReadableStream = readable.unchecked_into();
    let writable: bindings::WritableStream = writable.unchecked_into();
    let reader = readable
        .get_reader()
        .map_err(|error| transport_error(&error))?;
    match writable.get_writer() {
        Ok(writer) => Ok((reader, writer)),
        Err(error) => {
            let _ = reader.release_lock();
            Err(transport_error(&error))
        }
    }
}

/// A Web Serial port, open, with its streams locked.
///
/// A chunk larger than the buffer of a read is kept for the next reads, and a read that times
/// out keeps its pending `reader.read()` for the next one: no byte is lost or reordered.
pub struct WebSerialPort {
    port: bindings::SerialPort,
    reader: Option<bindings::Reader>,
    writer: Option<bindings::Writer>,
    /// A `reader.read()` that lost the race against a timeout.
    pending: Option<Promise>,
    /// The bytes of the last chunk that did not fit in the buffer of the read.
    leftover: Option<Uint8Array>,
    identity: Option<UsbIdentity>,
    /// The baud rate the port is open at, which bounds how long a write may take.
    baud_rate: u32,
}

impl WebSerialPort {
    /// Opens `port`, a `SerialPort` of Web Serial, at `baud_rate`, and locks its streams.
    pub async fn open(port: JsValue, baud_rate: u32) -> Result<WebSerialPort, TransportError> {
        let port: bindings::SerialPort = port.unchecked_into();
        open_port(&port, baud_rate).await?;
        let (reader, writer) = match lock_streams(&port) {
            Ok(streams) => streams,
            Err(error) => {
                ignore(port.close());
                return Err(error);
            }
        };
        let identity = identity_of(&port);
        Ok(WebSerialPort {
            port,
            reader: Some(reader),
            writer: Some(writer),
            pending: None,
            leftover: None,
            identity,
            baud_rate,
        })
    }

    /// Releases the streams and closes the port.
    pub async fn close(mut self) -> Result<(), TransportError> {
        self.release().await;
        close_port(&self.port).await
    }

    /// The `SerialPort` of Web Serial.
    pub fn handle(&self) -> JsValue {
        JsValue::clone(&self.port)
    }

    /// Cancels the reader and waits for the cancellation, forgets what was read, and unlocks
    /// both streams.
    async fn release(&mut self) {
        self.pending = None;
        self.leftover = None;
        if let Some(reader) = self.reader.take() {
            // A failed cancellation leaves the stream locked: closing reports it.
            let _ = wait(reader.cancel(), PORT_TIMEOUT, "cancelling the read").await;
            let _ = reader.release_lock();
        }
        if let Some(writer) = self.writer.take() {
            let _ = writer.release_lock();
        }
    }

    /// Unlocks the reader, which a recoverable error left errored, and locks the new stream Web
    /// Serial gives in `port.readable`.
    fn renew_reader(&mut self) -> Result<(), TransportError> {
        if let Some(reader) = self.reader.take() {
            let _ = reader.release_lock();
        }
        let readable = self.port.readable();
        if !readable.is_object() {
            return Err(TransportError::Io {
                message: "the port has no stream: it is not open".to_string(),
            });
        }
        let readable: bindings::ReadableStream = readable.unchecked_into();
        let reader = readable
            .get_reader()
            .map_err(|error| transport_error(&error))?;
        self.reader = Some(reader);
        Ok(())
    }

    /// Copies the start of `chunk` into `buffer` and keeps the rest.
    fn take_from(&mut self, chunk: Uint8Array, buffer: &mut [u8]) -> usize {
        let length = chunk.length();
        let count = u32::try_from(buffer.len()).unwrap_or(u32::MAX).min(length);
        // `count` is at most `buffer.len()`.
        let Some(destination) = buffer.get_mut(..count as usize) else {
            return 0;
        };
        chunk.subarray(0, count).copy_to(destination);
        if count < length {
            self.leftover = Some(chunk.subarray(count, length));
        }
        destination.len()
    }

    fn not_open() -> TransportError {
        TransportError::Io {
            message: "the port is not open".to_string(),
        }
    }
}

impl Drop for WebSerialPort {
    /// Cancels a pending read and unlocks both streams, so that the port can be closed.
    fn drop(&mut self) {
        if let Some(reader) = self.reader.take() {
            if self.pending.take().is_some() {
                ignore(reader.cancel());
            }
            let _ = reader.release_lock();
        }
        if let Some(writer) = self.writer.take() {
            let _ = writer.release_lock();
        }
    }
}

impl SerialPort for WebSerialPort {
    async fn read(
        &mut self,
        buffer: &mut [u8],
        timeout: Duration,
    ) -> Result<usize, TransportError> {
        if let Some(leftover) = self.leftover.take() {
            return Ok(self.take_from(leftover, buffer));
        }
        let mut recoveries = 0;
        let result = loop {
            let pending = match self.pending.take() {
                Some(pending) => pending,
                None => {
                    let reader = self.reader.as_ref().ok_or_else(WebSerialPort::not_open)?;
                    reader.read().map_err(|error| transport_error(&error))?
                }
            };
            // Kept until it settles, so that a read dropped while waiting loses nothing.
            self.pending = Some(pending.clone());
            match bounded(&pending, timeout).await {
                Ok(None) => return Ok(0),
                Ok(Some(result)) => break result,
                Err(error) => {
                    self.pending = None;
                    let recoverable = error_name(&error)
                        .is_some_and(|name| RECOVERABLE_READ_ERRORS.contains(&name.as_str()));
                    // The bytes of the error are lost; the protocol retries what it misses.
                    if !recoverable || recoveries >= READ_RECOVERIES || self.renew_reader().is_err()
                    {
                        return Err(transport_error(&error));
                    }
                    recoveries += 1;
                }
            }
        };
        self.pending = None;
        if property(&result, "done").as_bool() == Some(true) {
            return Err(TransportError::Disconnected);
        }
        let chunk = property(&result, "value")
            .dyn_into::<Uint8Array>()
            .map_err(|_| TransportError::Io {
                message: "the port gave a chunk that is not a Uint8Array".to_string(),
            })?;
        Ok(self.take_from(chunk, buffer))
    }

    async fn write(&mut self, data: &[u8]) -> Result<(), TransportError> {
        let writer = self.writer.as_ref().ok_or_else(WebSerialPort::not_open)?;
        // A copy: the memory of the module may move while the write is pending.
        let chunk = Uint8Array::from(data);
        let limit = write_limit(data.len(), self.baud_rate);
        wait(writer.write(&chunk), limit, "writing to the port")
            .await
            .map(drop)
    }

    async fn set_baud_rate(&mut self, baud_rate: u32) -> Result<(), TransportError> {
        // Web Serial has no baud rate change: close the port and open it again.
        self.release().await;
        close_port(&self.port).await?;
        open_port(&self.port, baud_rate).await?;
        self.baud_rate = baud_rate;
        let (reader, writer) = lock_streams(&self.port)?;
        self.reader = Some(reader);
        self.writer = Some(writer);
        Ok(())
    }

    async fn set_signals(&mut self, signals: Signals) -> Result<(), TransportError> {
        let signals = object(&[
            ("dataTerminalReady", JsValue::from_bool(signals.dtr)),
            ("requestToSend", JsValue::from_bool(signals.rts)),
        ]);
        wait(
            self.port.set_signals(&signals),
            PORT_TIMEOUT,
            "setting the signals",
        )
        .await
        .map(drop)
    }

    async fn sleep(&mut self, duration: Duration) {
        sleep(duration).await;
    }

    fn identity(&self) -> Option<UsbIdentity> {
        self.identity.clone()
    }
}

/// Opens a device again among the ports the page was granted (`navigator.serial.getPorts()`),
/// and remembers the ports it opened, for the caller to close them.
///
/// A device that comes back after a reset is a new port object: ports granted when the provider
/// was created are tried last, and ports still open are skipped.
pub struct WebPortProvider {
    /// The ports granted when the provider was created.
    known: Vec<JsValue>,
    opened: Vec<JsValue>,
}

impl WebPortProvider {
    /// A provider that remembers the ports granted now, before the device resets.
    pub async fn new() -> WebPortProvider {
        let known = match wait(bindings::get_ports(), PORT_TIMEOUT, "listing the ports").await {
            Ok(ports) => Array::from(&ports).iter().collect(),
            Err(_) => Vec::new(),
        };
        WebPortProvider {
            known,
            opened: Vec::new(),
        }
    }

    /// The `SerialPort` of every port this provider opened.
    pub fn opened(&self) -> Vec<JsValue> {
        self.opened.clone()
    }

    /// Opens the first granted port that `filter` selects, if any.
    async fn open_granted(
        &mut self,
        filter: &DeviceFilter,
        baud_rate: u32,
    ) -> Option<WebSerialPort> {
        // Listing and opening errors are retried: the device may still be appearing.
        let ports = wait(bindings::get_ports(), PORT_TIMEOUT, "listing the ports")
            .await
            .ok()?;
        let (known, fresh): (Vec<JsValue>, Vec<JsValue>) = Array::from(&ports)
            .iter()
            .filter(|port| {
                let handle: &bindings::SerialPort = port.unchecked_ref();
                // An open port, such as the one the device left, would only refuse to open.
                !handle.readable().is_object()
                    && identity_of(handle).is_some_and(|identity| filter.matches(&identity))
            })
            .partition(|port| self.known.iter().any(|known| Object::is(known, port)));
        // New port objects first, in the order of the list.
        for port in fresh.into_iter().chain(known) {
            if let Ok(opened) = WebSerialPort::open(port, baud_rate).await {
                self.opened.push(opened.handle());
                return Some(opened);
            }
        }
        None
    }
}

impl PortProvider for WebPortProvider {
    type Port = WebSerialPort;

    /// A first grant fails after this wait (`not-granted`) while Caterina still runs, and a press
    /// of the reset button restarts it for the user to choose its port.
    fn caterina_reopen_timeout(&self) -> Duration {
        Duration::from_secs(5)
    }

    async fn open(
        &mut self,
        filter: &DeviceFilter,
        baud_rate: u32,
        timeout: Duration,
    ) -> Result<WebSerialPort, TransportError> {
        // Counted in polls: the library has no clock.
        let polls = timeout.as_millis() / POLL_INTERVAL.as_millis();
        let mut poll = 0;
        loop {
            if let Some(port) = self.open_granted(filter, baud_rate).await {
                return Ok(port);
            }
            if poll >= polls {
                // Web Serial only lists the ports the user granted: a device that came back
                // under another identity needs to be selected once.
                return Err(TransportError::NotGranted {
                    filter: filter.to_string(),
                });
            }
            poll += 1;
            sleep(POLL_INTERVAL).await;
        }
    }
}