diff --git a/cmd/load/main.go b/cmd/load/main.go index 17a589d..78269d1 100644 --- a/cmd/load/main.go +++ b/cmd/load/main.go @@ -7,9 +7,9 @@ import ( "path/filepath" "time" + "github.com/theater/picomap/lib/client" "github.com/theater/picomap/lib/picoserial" "github.com/theater/picomap/lib/picotool" - "github.com/theater/picomap/lib/wire" ) func main() { @@ -40,27 +40,18 @@ func run(buildDir string) error { return err } if dev != "" { - fmt.Printf("Sending bootsel request to %s...\n", dev) - req, err := wire.EncodeMessage(&wire.RequestBOOTSEL{}) - if err != nil { - return fmt.Errorf("encoding request: %w", err) - } - resp, err := picoserial.SendAndReceive(dev, req, 2*time.Second) + fmt.Printf("Sending BOOTSEL request to %s...\n", dev) + t, err := picoserial.Open(dev) if err != nil { return err } - if len(resp) > 0 { - msg, err := wire.DecodeMessage(resp) - if err != nil { - fmt.Fprintf(os.Stderr, "warning: failed to decode response: %v\n", err) - } else { - switch msg.(type) { - case *wire.ResponseBOOTSEL: - fmt.Println("Device confirmed reboot into BOOTSEL mode.") - default: - fmt.Printf("Unexpected response type: %T\n", msg) - } - } + c := client.New(t, 2*time.Second) + err = c.BOOTSEL() + c.Close() + if err != nil { + fmt.Fprintf(os.Stderr, "warning: BOOTSEL request failed: %v\n", err) + } else { + fmt.Println("Device confirmed reboot into BOOTSEL mode.") } time.Sleep(2 * time.Second) } diff --git a/include/device.h b/include/device.h new file mode 100644 index 0000000..5c31059 --- /dev/null +++ b/include/device.h @@ -0,0 +1,51 @@ +#pragma once +#include +#include +#include "msgpackpp.h" +#include "halfsiphash.h" +#include "static_vector.h" +#include "protocol.h" + +static constexpr uint8_t hash_key[8] = {}; + +struct DecodedMessage { + uint32_t message_id; + int8_t type_id; +}; + +inline std::vector pack_envelope(uint32_t message_id, const std::vector &payload) { + uint32_t checksum = halfsiphash::hash32(payload.data(), payload.size(), hash_key); + msgpackpp::packer p; + p.pack(Envelope{message_id, checksum, payload}); + return p.get_payload(); +} + +template +inline std::vector encode_response(uint32_t message_id, const T &msg) { + msgpackpp::packer inner; + inner.pack(msg); + return pack_envelope(message_id, inner.get_payload()); +} + +inline msgpackpp::result try_decode(const uint8_t *data, size_t len) { + msgpackpp::parser p(data, static_cast(len)); + + Envelope env; + auto r = msgpackpp::unpack(p, env); + if (!r) return std::unexpected(r.error()); + + uint32_t expected = halfsiphash::hash32(env.payload.data(), env.payload.size(), hash_key); + if (env.checksum != expected) return std::unexpected(msgpackpp::error_code::invalid); + + msgpackpp::parser inner(env.payload.data(), static_cast(env.payload.size())); + if (!inner.is_ext()) return std::unexpected(msgpackpp::error_code::type_error); + auto ext = inner.get_ext(); + if (!ext) return std::unexpected(ext.error()); + + return DecodedMessage{env.message_id, std::get<0>(*ext)}; +} + +template +inline msgpackpp::result try_decode(const static_vector &buf) { + return try_decode(buf.data(), buf.size()); +} diff --git a/include/msgpackpp.h b/include/msgpackpp.h index 6c960f0..78fa97f 100644 --- a/include/msgpackpp.h +++ b/include/msgpackpp.h @@ -718,6 +718,13 @@ inline result unpack(const parser &p, std::string_view &out) { return p.next(); } +inline result unpack(const parser &p, std::string &out) { + auto v = p.get_string(); + if (!v) return std::unexpected(v.error()); + out = std::string(v->data(), v->size()); + return p.next(); +} + inline result unpack(const parser &p, std::vector &out) { auto v = p.get_binary_view(); if (!v) return std::unexpected(v.error()); diff --git a/include/protocol.h b/include/protocol.h new file mode 100644 index 0000000..5cf4d19 --- /dev/null +++ b/include/protocol.h @@ -0,0 +1,34 @@ +#pragma once +#include +#include +#include +#include + +struct ResponseBOOTSEL { + static constexpr int8_t ext_id = 1; + auto as_tuple() const { return std::tie(); } + auto as_tuple() { return std::tie(); } +}; + +struct RequestBOOTSEL { + static constexpr int8_t ext_id = 2; + auto as_tuple() const { return std::tie(); } + auto as_tuple() { return std::tie(); } +}; + +struct DeviceError { + static constexpr int8_t ext_id = 3; + uint32_t code; + std::string message; + auto as_tuple() const { return std::tie(code, message); } + auto as_tuple() { return std::tie(code, message); } +}; + +struct Envelope { + static constexpr int8_t ext_id = 0; + uint32_t message_id; + uint32_t checksum; + std::vector payload; + auto as_tuple() const { return std::tie(message_id, checksum, payload); } + auto as_tuple() { return std::tie(message_id, checksum, payload); } +}; diff --git a/lib/client/client.go b/lib/client/client.go new file mode 100644 index 0000000..9b7cb6b --- /dev/null +++ b/lib/client/client.go @@ -0,0 +1,90 @@ +package client + +import ( + "fmt" + "sync/atomic" + "time" + + "github.com/theater/picomap/lib/halfsiphash" + "github.com/theater/picomap/lib/msgpack" + "github.com/theater/picomap/lib/transport" +) + +var HashKey = [8]byte{} + +type Client struct { + transport transport.Transport + timeout time.Duration + nextID atomic.Uint32 +} + +func New(t transport.Transport, timeout time.Duration) *Client { + return &Client{transport: t, timeout: timeout} +} + +func (c *Client) Close() error { + return c.transport.Close() +} + +func (c *Client) send(msg any) (uint32, error) { + id := c.nextID.Add(1) + payload, err := msgpack.Marshal(msg) + if err != nil { + return 0, fmt.Errorf("encode inner: %w", err) + } + checksum := halfsiphash.Sum32(payload, HashKey) + env := Envelope{MessageID: id, Checksum: checksum, Payload: payload} + data, err := msgpack.Marshal(&env) + if err != nil { + return 0, fmt.Errorf("encode envelope: %w", err) + } + return id, c.transport.Send(data) +} + +func (c *Client) receive(expectedID uint32) (any, error) { + data, err := c.transport.Receive(c.timeout) + if err != nil { + return nil, fmt.Errorf("receive: %w", err) + } + if len(data) == 0 { + return nil, fmt.Errorf("no response") + } + var env Envelope + if err := msgpack.Unmarshal(data, &env); err != nil { + return nil, fmt.Errorf("decode envelope: %w", err) + } + if env.MessageID != expectedID { + return nil, fmt.Errorf("message id mismatch: got %d, want %d", env.MessageID, expectedID) + } + expected := halfsiphash.Sum32(env.Payload, HashKey) + if env.Checksum != expected { + return nil, fmt.Errorf("checksum mismatch: got %08x, want %08x", env.Checksum, expected) + } + var inner any + if err := msgpack.Unmarshal(env.Payload, &inner); err != nil { + return nil, fmt.Errorf("decode inner: %w", err) + } + if devErr, ok := inner.(*DeviceError); ok { + return nil, devErr + } + return inner, nil +} + +func (c *Client) roundTrip(req any) (any, error) { + id, err := c.send(req) + if err != nil { + return nil, err + } + return c.receive(id) +} + +func (c *Client) BOOTSEL() error { + resp, err := c.roundTrip(&RequestBOOTSEL{}) + if err != nil { + return err + } + if _, ok := resp.(*ResponseBOOTSEL); !ok { + return fmt.Errorf("unexpected response: %T", resp) + } + return nil +} diff --git a/lib/client/types.go b/lib/client/types.go new file mode 100644 index 0000000..5a7b347 --- /dev/null +++ b/lib/client/types.go @@ -0,0 +1,28 @@ +package client + +import "github.com/theater/picomap/lib/msgpack" + +type ResponseBOOTSEL struct{} +type RequestBOOTSEL struct{} + +type DeviceError struct { + Code uint32 + Message string +} + +func (e *DeviceError) Error() string { + return e.Message +} + +type Envelope struct { + MessageID uint32 + Checksum uint32 + Payload []byte +} + +func init() { + msgpack.RegisterExt(0, (*Envelope)(nil)) + msgpack.RegisterExt(1, (*ResponseBOOTSEL)(nil)) + msgpack.RegisterExt(2, (*RequestBOOTSEL)(nil)) + msgpack.RegisterExt(3, (*DeviceError)(nil)) +} diff --git a/lib/picoserial/picoserial.go b/lib/picoserial/picoserial.go index de8ec27..e1b401e 100644 --- a/lib/picoserial/picoserial.go +++ b/lib/picoserial/picoserial.go @@ -21,29 +21,29 @@ func FindDevice() (string, error) { return "", nil } -func Open(portName string) (serial.Port, error) { - return serial.Open(portName, &serial.Mode{BaudRate: 115200}) +type SerialTransport struct { + port serial.Port } -// SendAndReceive sends data and reads the response with a timeout. -func SendAndReceive(portName string, data []byte, timeout time.Duration) ([]byte, error) { - port, err := Open(portName) +func Open(portName string) (*SerialTransport, error) { + port, err := serial.Open(portName, &serial.Mode{BaudRate: 115200}) if err != nil { return nil, fmt.Errorf("opening %s: %w", portName, err) } - defer port.Close() + return &SerialTransport{port: port}, nil +} - port.SetReadTimeout(timeout) - - _, err = port.Write(data) - if err != nil { - return nil, fmt.Errorf("writing to %s: %w", portName, err) - } +func (t *SerialTransport) Send(data []byte) error { + _, err := t.port.Write(data) + return err +} +func (t *SerialTransport) Receive(timeout time.Duration) ([]byte, error) { + t.port.SetReadTimeout(timeout) var resp []byte buf := make([]byte, 256) for { - n, err := port.Read(buf) + n, err := t.port.Read(buf) if n > 0 { resp = append(resp, buf[:n]...) } @@ -53,3 +53,7 @@ func SendAndReceive(portName string, data []byte, timeout time.Duration) ([]byte } return resp, nil } + +func (t *SerialTransport) Close() error { + return t.port.Close() +} diff --git a/lib/transport/transport.go b/lib/transport/transport.go new file mode 100644 index 0000000..664fc23 --- /dev/null +++ b/lib/transport/transport.go @@ -0,0 +1,9 @@ +package transport + +import "time" + +type Transport interface { + Send(data []byte) error + Receive(timeout time.Duration) ([]byte, error) + Close() error +} diff --git a/lib/wire/wire.go b/lib/wire/wire.go deleted file mode 100644 index 6c36f0f..0000000 --- a/lib/wire/wire.go +++ /dev/null @@ -1,57 +0,0 @@ -package wire - -import ( - "fmt" - - "github.com/theater/picomap/lib/halfsiphash" - "github.com/theater/picomap/lib/msgpack" -) - -var HashKey = [8]byte{} - -type ResponseBOOTSEL struct{} -type RequestBOOTSEL struct{} - -type Envelope struct { - Checksum uint32 - Payload []byte -} - -func init() { - msgpack.RegisterExt(0, (*Envelope)(nil)) - msgpack.RegisterExt(1, (*ResponseBOOTSEL)(nil)) - msgpack.RegisterExt(2, (*RequestBOOTSEL)(nil)) -} - -func EncodeMessage(msg any) ([]byte, error) { - payload, err := msgpack.Marshal(msg) - if err != nil { - return nil, fmt.Errorf("encode inner: %w", err) - } - - checksum := halfsiphash.Sum32(payload, HashKey) - env := Envelope{Checksum: checksum, Payload: payload} - data, err := msgpack.Marshal(&env) - if err != nil { - return nil, fmt.Errorf("encode envelope: %w", err) - } - return data, nil -} - -func DecodeMessage(data []byte) (any, error) { - var env Envelope - if err := msgpack.Unmarshal(data, &env); err != nil { - return nil, fmt.Errorf("decode envelope: %w", err) - } - - expected := halfsiphash.Sum32(env.Payload, HashKey) - if env.Checksum != expected { - return nil, fmt.Errorf("checksum mismatch: got %08x, want %08x", env.Checksum, expected) - } - - var inner any - if err := msgpack.Unmarshal(env.Payload, &inner); err != nil { - return nil, fmt.Errorf("decode inner: %w", err) - } - return inner, nil -} diff --git a/picomap.cpp b/picomap.cpp index fe99ee8..8204897 100644 --- a/picomap.cpp +++ b/picomap.cpp @@ -1,40 +1,7 @@ #include -#include -#include #include "pico/stdlib.h" #include "pico/bootrom.h" -#include "msgpackpp.h" -#include "halfsiphash.h" -#include "static_vector.h" - -static constexpr uint8_t hash_key[8] = {}; - -struct ResponseBOOTSEL { - static constexpr int8_t ext_id = 1; - auto as_tuple() const { return std::tie(); } - auto as_tuple() { return std::tie(); } -}; - -struct RequestBOOTSEL { - static constexpr int8_t ext_id = 2; - auto as_tuple() const { return std::tie(); } - auto as_tuple() { return std::tie(); } -}; - -struct Envelope { - static constexpr int8_t ext_id = 0; - uint32_t checksum; - std::vector payload; - auto as_tuple() const { return std::tie(checksum, payload); } - auto as_tuple() { return std::tie(checksum, payload); } -}; - -static std::vector pack_envelope(const std::vector &payload) { - uint32_t checksum = halfsiphash::hash32(payload.data(), payload.size(), hash_key); - msgpackpp::packer p; - p.pack(Envelope{checksum, payload}); - return p.get_payload(); -} +#include "device.h" static void send_bytes(const std::vector &data) { for (auto b : data) { @@ -43,31 +10,6 @@ static void send_bytes(const std::vector &data) { stdio_flush(); } -template -static void send_message(const T &msg) { - msgpackpp::packer inner; - inner.pack(msg); - auto envelope = pack_envelope(inner.get_payload()); - send_bytes(envelope); -} - -static int8_t try_decode(const static_vector &buf) { - msgpackpp::parser p(buf.data(), static_cast(buf.size())); - - Envelope env; - if (!msgpackpp::unpack(p, env)) return -1; - - uint32_t expected = halfsiphash::hash32(env.payload.data(), env.payload.size(), hash_key); - if (env.checksum != expected) return -1; - - msgpackpp::parser inner(env.payload.data(), static_cast(env.payload.size())); - if (!inner.is_ext()) return -1; - auto ext = inner.get_ext(); - if (!ext) return -1; - - return std::get<0>(*ext); -} - int main() { stdio_init_all(); @@ -79,17 +21,17 @@ int main() { rx_buf.push_back(static_cast(c)); - int8_t msg_type = try_decode(rx_buf); - if (msg_type < 0) { + auto msg = try_decode(rx_buf); + if (!msg) { if (rx_buf.full()) rx_buf.clear(); continue; } rx_buf.clear(); - switch (msg_type) { + switch (msg->type_id) { case RequestBOOTSEL::ext_id: - send_message(ResponseBOOTSEL{}); + send_bytes(encode_response(msg->message_id, ResponseBOOTSEL{})); sleep_ms(100); reset_usb_boot(0, 0); break;