Skip to content
Open
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RISCV ?= $(HOME)/.platformio/tools/toolchain-riscv32-esp/bin/riscv32-esp-elf-
# will fail to link. Add a small runtime to sdk/ when an app first needs one.
APPFLAGS = -march=rv32im -mabi=ilp32 -Os -ffreestanding -nostdlib -mno-relax \
-fno-tree-loop-distribute-patterns -Wall -Isdk -T sdk/app.ld -Wl,--no-warn-rwx-segments
APPS = hello selftest oob bench drawtest lightsout dice sudoku captest nettest asynctest home minimal
APPS = hello selftest oob bench drawtest lightsout dice sudoku captest nettest asynctest home minimal qrtest
SD_APPS = hello lightsout dice sudoku home minimal

# Rust is part of `make test` and `make sd`: the Rust SDK, the hand-linked Rust
Expand Down Expand Up @@ -101,7 +101,7 @@ test: $(if $(WITH_RUST),rust-check) all $(if $(WITH_RUST),rust xpui)
printf 'the book bytes here' > $$nr/example.test/book.bin; \
DESSEN_NET_ROOT=$$nr DESSEN_FS_ROOT=$$fs build/dessen build/nettest.bin; rc=$$?; \
rm -rf $$nr $$fs; test $$rc -eq 0 && echo 'nettest: PASS'
build/test_freeink build/hello.bin build/oob.bin build/lightsout.bin build/dice.bin build/sudoku.bin build/drawtest.bin build/asynctest.bin build/home.bin
build/test_freeink build/hello.bin build/oob.bin build/lightsout.bin build/dice.bin build/sudoku.bin build/drawtest.bin build/asynctest.bin build/home.bin build/qrtest.bin
@echo PASS

# Fails early, with the fix, when `test`/`sd` would need Rust that is not here.
Expand Down
3 changes: 3 additions & 0 deletions abi/freeink.api
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@
101 open_home(u8 dest) -> i32 : return self.requestLaunchHome((int)dest);
103 wifi_connect() -> i32 : return self.beginWifi();
105 book_cover(buf path, u16 height, out pixels) -> i32 : result = svcBookCover(self, path, path_len, height, pixels, pixels_len);

# --- QR code bitmap generation ---
108 qr_bitmap(buf data, out pixels) -> i32 : result = svcQrBitmap(self, data, data_len, pixels, pixels_len);
19 changes: 19 additions & 0 deletions apps/qrtest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Exercise the QR capability boundary with a desktop host or a mock encoder.
#include "dessen.h"

int main(void) {
unsigned char data[4] = {'a', 0, 'b', 0};
unsigned char out[68] = {0};
if (dsn_qr_bitmap(data, 0, out, sizeof out) != DSN_ERR_INVALID) return 1;
if (dsn_qr_bitmap(data, 3, out, 3) != DSN_ERR_TOO_BIG) return 2;
if (dsn_qr_bitmap(data, 3, data, sizeof data) != DSN_ERR_INVALID) return 3;
if (dsn_qr_bitmap((const unsigned char*)0x7fffffffu, 3, out, sizeof out) != DSN_ERR_INVALID) return 4;
int n = dsn_qr_bitmap(data, 3, out, sizeof out);
if (n == DSN_ERR_UNAVAILABLE) return 0;
// Mock encoder: 21 by 21, 3 bytes per row, plus the four-byte header.
if (n != 67 || out[0] != 21 || out[1] != 0 || out[2] != 21 || out[3] != 0 ||
out[4] != 0x80 || out[67] != 0) return 5;
out[0] = 0x55;
if (dsn_qr_bitmap(data, 3, out, 66) != DSN_ERR_TOO_BIG || out[0] != 0x55) return 6;
return 0;
}
12 changes: 12 additions & 0 deletions docs/writing-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -804,3 +804,15 @@ shape, frontlight, a file read), you add **one line** to
regenerates both the app-side wrapper and the firmware-side dispatch. See
`AGENTS.md` for the recipe and the rules every new call must follow (clip, pin,
bounds-check — every app value is untrusted).

### QR bitmaps

`dsn_qr_bitmap(data, len, pixels, cap)` asks the host to encode a QR code.
The input is length-delimited bytes (1–2953 bytes; a host may support less).
Input and output buffers must not overlap. The result is the byte count,
or a negative `DSN_ERR_*` (including UNAVAILABLE on hosts without an encoder).
A successful result starts with little-endian u16 width and height, followed
by byte-padded rows of MSB-first module bits (1 = dark). The host chooses the
QR version and correction level. Add a four-module white quiet zone when
drawing, and use integer scaling. No framebuffer drawing or network access
is performed by the call. The host must leave the output unchanged on error.
11 changes: 11 additions & 0 deletions freeink/DessenFreeInk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,17 @@ int svcProgress(FreeInkHost& self, const uint8_t* path, uint32_t pathLen) {
return s->bookProgress(pbuf);
}

int svcQrBitmap(FreeInkHost& self, const uint8_t* data, uint32_t len, uint8_t* pixels, uint32_t cap) {
if (!len || len > 2953) return DSN_ERR_INVALID; // QR version 40-L byte capacity
if (cap < 4) return DSN_ERR_TOO_BIG;
// Input must remain intact throughout encoding, even with a malformed guest.
const auto src = reinterpret_cast<uintptr_t>(data);
const auto dst = reinterpret_cast<uintptr_t>(pixels);
if ((src <= dst && dst - src < len) || (dst < src && src - dst < cap))
return DSN_ERR_INVALID;
return self.services() ? self.services()->qrBitmap(data, len, pixels, cap) : DSN_ERR_UNAVAILABLE;
}

int svcBookCover(FreeInkHost& self, const uint8_t* path, uint32_t pathLen, uint32_t height,
uint8_t* pixels, uint32_t cap) {
Services* s = svc(self);
Expand Down
6 changes: 6 additions & 0 deletions freeink/DessenServices.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class Services {
// the first time (the cover is decoded and cached), so request one per redraw.
virtual int bookCover(const char*, uint32_t, uint8_t*, uint32_t) { return -4; }

// Encode bytes as a QR code, choosing a supported version and error correction
// level. Output: LE u16 width, u16 height, then MSB-first rows (byte padded,
// 1 = dark), no quiet zone. Return bytes written or DSN_ERR_*; leave output
// unchanged on failure. Host may reject payloads beyond its encoder limits.
virtual int qrBitmap(const uint8_t*, uint32_t, uint8_t*, uint32_t) { return -4; }

// ---- crypto (synchronous) ---- return bytes written, or a DSN_ERR_*
virtual int hash(int, const uint8_t*, uint32_t, uint8_t*, uint32_t) { return -4; }
virtual int aesCbc(int, const uint8_t*, const uint8_t*, uint32_t, uint8_t*, uint32_t) { return -4; } // keyiv = 32 bytes
Expand Down
15 changes: 15 additions & 0 deletions freeink/dessen_dispatch.inc
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,18 @@ case 105: { // book_cover
a[0] = result;
break;
}
case 108: { // qr_bitmap
const uint32_t data_len = a[1];
const uint8_t* data = static_cast<const uint8_t*>(dvm_ptr(vm, a[0], data_len));
(void)data_len;
if (!data) { a[0] = static_cast<uint32_t>(-1); break; }
const uint32_t pixels_len = a[3];
uint8_t* pixels = static_cast<uint8_t*>(dvm_ptr(vm, a[2], pixels_len));
(void)pixels_len;
const uint32_t pixels_addr = a[2]; (void)pixels_addr;
if (!pixels) { a[0] = static_cast<uint32_t>(-1); break; }
uint32_t result = 0;
result = svcQrBitmap(self, data, data_len, pixels, pixels_len);
a[0] = result;
break;
}
33 changes: 32 additions & 1 deletion freeink/test_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,38 @@ static void testManifest() {
assert(parseRequires(R"({"requires":["stylus"]})", needs) && !meets(needs, true, true, true));
}

struct MockQr : dessen::Services {
int calls = 0;
int qrBitmap(const uint8_t* data, uint32_t len, uint8_t* out, uint32_t cap) override {
++calls;
assert(len == 3 && data[0] == 'a' && data[1] == 0 && data[2] == 'b');
if (cap < 67) return DSN_ERR_TOO_BIG;
memset(out, 0, 67);
out[0] = out[2] = 21;
out[4] = 0x80;
return 67;
}
};

static void testQr(const char* path) {
for (bool supported : {false, true}) {
dessen::FreeInkHost host;
MockQr qr;
assert(host.load(mem, readApp(path), sizeof mem));
if (supported) host.setServices(&qr);
TraceTarget target;
DeviceContext dev;
dev.width = 400;
dev.height = 300;
InputSnapshot none;
while (host.step(target, dev, none) == dessen::State::Running) {}
assert(host.state() == dessen::State::Exited && host.exitCode() == 0);
assert(qr.calls == (supported ? 2 : 0));
}
}

int main(int argc, char** argv) {
assert(argc == 9);
assert(argc == 10);
DeviceContext screen;
screen.width = 480, screen.height = 800;
TraceTarget target;
Expand Down Expand Up @@ -504,6 +534,7 @@ int main(int argc, char** argv) {
testDesktopStorage();
testHome(argv[8]);
testManifest();
testQr(argv[9]);

puts("freeink binding: PASS");
return 0;
Expand Down
3 changes: 3 additions & 0 deletions sdk/dessen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub const DSN_SYS_BOOK_PROGRESS: usize = 100;
pub const DSN_SYS_OPEN_HOME: usize = 101;
pub const DSN_SYS_WIFI_CONNECT: usize = 103;
pub const DSN_SYS_BOOK_COVER: usize = 105;
pub const DSN_SYS_QR_BITMAP: usize = 108;

#[inline]
unsafe fn dsn_call0(n: usize) -> i32 {
Expand Down Expand Up @@ -293,3 +294,5 @@ pub fn dsn_open_home(dest: i32) -> i32 { unsafe { dsn_call1(DSN_SYS_OPEN_HOME, (
pub fn dsn_wifi_connect() -> i32 { unsafe { dsn_call0(DSN_SYS_WIFI_CONNECT) } }
#[inline]
pub fn dsn_book_cover(path: &[u8], height: i32, pixels: &mut [u8]) -> i32 { unsafe { dsn_call5(DSN_SYS_BOOK_COVER, path.as_ptr() as usize, path.len(), ((height as u32 & 0xffff)) as usize, pixels.as_mut_ptr() as usize, pixels.len()) } }
#[inline]
pub fn dsn_qr_bitmap(data: &[u8], pixels: &mut [u8]) -> i32 { unsafe { dsn_call4(DSN_SYS_QR_BITMAP, data.as_ptr() as usize, data.len(), pixels.as_mut_ptr() as usize, pixels.len()) } }
2 changes: 2 additions & 0 deletions sdk/dessen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub const DSN_SYS_BOOK_PROGRESS: usize = 100;
pub const DSN_SYS_OPEN_HOME: usize = 101;
pub const DSN_SYS_WIFI_CONNECT: usize = 103;
pub const DSN_SYS_BOOK_COVER: usize = 105;
pub const DSN_SYS_QR_BITMAP: usize = 108;

fn call0(n: usize) i32 {
return asm volatile ("ecall"
Expand Down Expand Up @@ -189,3 +190,4 @@ pub fn book_progress(path: []const u8) i32 { return call2(DSN_SYS_BOOK_PROGRESS,
pub fn open_home(dest: i32) i32 { return call1(DSN_SYS_OPEN_HOME, @as(usize, (@as(u32, @bitCast(dest)) & 0xff))); }
pub fn wifi_connect() i32 { return call0(DSN_SYS_WIFI_CONNECT); }
pub fn book_cover(path: []const u8, height: i32, pixels: []u8) i32 { return call5(DSN_SYS_BOOK_COVER, @intFromPtr(path.ptr), path.len, @as(usize, (@as(u32, @bitCast(height)) & 0xffff)), @intFromPtr(pixels.ptr), pixels.len); }
pub fn qr_bitmap(data: []const u8, pixels: []u8) i32 { return call4(DSN_SYS_QR_BITMAP, @intFromPtr(data.ptr), data.len, @intFromPtr(pixels.ptr), pixels.len); }
1 change: 1 addition & 0 deletions sdk/dessen_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ static inline int dsn_book_progress(const void *path, int path_len) { return (in
static inline int dsn_open_home(int dest) { return (int)dsn_call1(DSN_SYS_OPEN_HOME, (long)(((unsigned)dest & 0xff))); }
static inline int dsn_wifi_connect(void) { return (int)dsn_call0(DSN_SYS_WIFI_CONNECT); }
static inline int dsn_book_cover(const void *path, int path_len, int height, void *pixels, int pixels_len) { return (int)dsn_call5(DSN_SYS_BOOK_COVER, (long)path, path_len, (long)(((unsigned)height & 0xffff)), (long)pixels, pixels_len); }
static inline int dsn_qr_bitmap(const void *data, int data_len, void *pixels, int pixels_len) { return (int)dsn_call4(DSN_SYS_QR_BITMAP, (long)data, data_len, (long)pixels, pixels_len); }
1 change: 1 addition & 0 deletions sdk/dessen_sysnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@
#define DSN_SYS_OPEN_HOME 101
#define DSN_SYS_WIFI_CONNECT 103
#define DSN_SYS_BOOK_COVER 105
#define DSN_SYS_QR_BITMAP 108