==> include/mesh/place_wire.h <==1/* @ajax/mesh-place-wire: the placement frames between machines (mesh-abi "placement", place.rs):
2 * F_PLACE: op, id, name, mode, aux, then bytes (a state, or a reason)
3 * F_FRAME: one frame's state (from the machine whose state it is; empty from a split follower) and
4 * the parts in `parts`, ascending
5 * F_INPUT: input bytes for a program's owner
6 * Each frame is its type byte, a fixed head, then a body whose length the head gives; a hostile length
7 * is refused before anything is read. */
8#ifndef MESH_PLACE_WIRE_H
9#define MESH_PLACE_WIRE_H
10
11#include "mesh/abi/base.h"
12#include "mesh/abi/placement.h"
13#include "mesh/place_mode.h"
14
15#include <stdbool.h>
16#include <stddef.h>
17#include <stdint.h>
18
19/* PL_REQ's aux for `split auto`: this bit and the member mask in the low bits. */
20#define MPW_AUX_AUTO (1u << 31)
21/* F_PLACE's fixed head, then len bytes. */
22#define MPW_PLACE_HEAD (4 + 4 + MESH_NAME_BYTES + 4 + 4 + MESH_PLACE_PARTS_MAX + 4 + 4)
23/* F_FRAME's fixed head, then state_len + data_len bytes. */
24#define MPW_FRAME_HEAD (MESH_NAME_BYTES + 5 * 4)
25/* F_INPUT's fixed head, then len bytes. */
26#define MPW_INPUT_HEAD (MESH_NAME_BYTES + 4)
27
28/* A name in its 16 wire bytes (NUL padded, at most NAME_MAX). */
29void mpw_name16(const char *name, uint8_t out[MESH_NAME_BYTES]);
30/* The name in wire bytes, checked ([a-z0-9-]{1,15}); false with *err. out holds NAME_BYTES + 1. */
31bool mpw_name_of(const uint8_t *b, size_t n, char *out, char **err);
32
33/* A growable byte buffer. */
34typedef struct {
35 uint8_t *b;
36 size_t n, cap;
37} mpw_bytes;
38
39void mpw_put(mpw_bytes *o, const void *p, size_t n);
40void mpw_put32(mpw_bytes *o, uint32_t v);
41void mpw_bytes_free(mpw_bytes *o);
42uint32_t mpw_u32_at(const uint8_t *b, size_t at);
43uint64_t mpw_u64_at(const uint8_t *b, size_t at);
44
45typedef struct {
46 uint32_t op, id;
47 char name[MESH_NAME_BYTES + 1];
48 mpm_mode mode;
49 uint32_t aux;
50 uint8_t *bytes;
51 size_t nbytes;
52} mpw_place;
53
54/* The frame, type byte first. */
55mpw_bytes mpw_place_encode(const mpw_place *m);
56/* The body's length beyond the head (checked against F_PLACE_MAX). */
57bool mpw_place_rest_len(const uint8_t *head, size_t *n, char **err);
58/* head: MPW_PLACE_HEAD bytes; rest: the body (copied). */
59bool mpw_place_decode(const uint8_t *head, const uint8_t *rest, size_t nrest, mpw_place *out, char **err);
60void mpw_place_free(mpw_place *m);
61
62typedef struct {
63 char name[MESH_NAME_BYTES + 1];
64 uint32_t frame, parts, sys;
65 uint8_t *state;
66 size_t nstate;
67 uint8_t *data;
68 size_t ndata;
69} mpw_frame;
70
71mpw_bytes mpw_frame_encode(const mpw_frame *m);
72bool mpw_frame_rest_len(const uint8_t *head, size_t *n, char **err);
73bool mpw_frame_decode(const uint8_t *head, const uint8_t *rest, size_t nrest, mpw_frame *out, char **err);
74mpw_frame mpw_frame_clone(const mpw_frame *m);
75void mpw_frame_free(mpw_frame *m);
76
77mpw_bytes mpw_input_encode(const char *name, const uint8_t *bytes, size_t n);
78bool mpw_input_len(const uint8_t *head, size_t *n, char **err);
79
80#endif