Add message IDs, transport interface, client package, extract protocol headers
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
51
include/device.h
Normal file
51
include/device.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#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<uint8_t> pack_envelope(uint32_t message_id, const std::vector<uint8_t> &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 <typename T>
|
||||
inline std::vector<uint8_t> 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<DecodedMessage> try_decode(const uint8_t *data, size_t len) {
|
||||
msgpackpp::parser p(data, static_cast<int>(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<int>(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 <size_t N>
|
||||
inline msgpackpp::result<DecodedMessage> try_decode(const static_vector<uint8_t, N> &buf) {
|
||||
return try_decode(buf.data(), buf.size());
|
||||
}
|
||||
@@ -718,6 +718,13 @@ inline result<parser> unpack(const parser &p, std::string_view &out) {
|
||||
return p.next();
|
||||
}
|
||||
|
||||
inline result<parser> 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<parser> unpack(const parser &p, std::vector<uint8_t> &out) {
|
||||
auto v = p.get_binary_view();
|
||||
if (!v) return std::unexpected(v.error());
|
||||
|
||||
34
include/protocol.h
Normal file
34
include/protocol.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
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<uint8_t> payload;
|
||||
auto as_tuple() const { return std::tie(message_id, checksum, payload); }
|
||||
auto as_tuple() { return std::tie(message_id, checksum, payload); }
|
||||
};
|
||||
90
lib/client/client.go
Normal file
90
lib/client/client.go
Normal file
@@ -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
|
||||
}
|
||||
28
lib/client/types.go
Normal file
28
lib/client/types.go
Normal file
@@ -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))
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
9
lib/transport/transport.go
Normal file
9
lib/transport/transport.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package transport
|
||||
|
||||
import "time"
|
||||
|
||||
type Transport interface {
|
||||
Send(data []byte) error
|
||||
Receive(timeout time.Duration) ([]byte, error)
|
||||
Close() error
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
68
picomap.cpp
68
picomap.cpp
@@ -1,40 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
#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<uint8_t> payload;
|
||||
auto as_tuple() const { return std::tie(checksum, payload); }
|
||||
auto as_tuple() { return std::tie(checksum, payload); }
|
||||
};
|
||||
|
||||
static std::vector<uint8_t> pack_envelope(const std::vector<uint8_t> &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<uint8_t> &data) {
|
||||
for (auto b : data) {
|
||||
@@ -43,31 +10,6 @@ static void send_bytes(const std::vector<uint8_t> &data) {
|
||||
stdio_flush();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
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<uint8_t, 256> &buf) {
|
||||
msgpackpp::parser p(buf.data(), static_cast<int>(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<int>(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<uint8_t>(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;
|
||||
|
||||
Reference in New Issue
Block a user