TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
messages.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// Builders for viewer protocol v1 messages (docs/13-viewer-protocol.md). Pure functions returning one JSON
4// object per message, ready for events::Sink::publish(). No Boost or networking: the router links only this
5// library (tm::viz) and publishes into whatever Sink it was given.
6//
7// Every message starts with its "type" key (insertion-ordered JSON), so a server can classify messages by
8// looking at the first bytes without parsing them.
9#include <cstdint>
10#include <span>
11#include <optional>
12#include <string>
13#include <string_view>
14#include <utility>
15#include <vector>
16
17#include "model/board.hpp"
18
19namespace tmk::server {
20
21using ObjectId = std::int64_t;
22
23// Ids used in the `board` snapshot: tracks and vias use their index in Board::tracks / Board::vias, pads their
24// index in Board::pads. Arc tracks are sent as chains of straight segments with negative ids
25// (-1, -2, …) so producers that number new tracks from Board::tracks.size() upwards never collide with them.
26
27// Full scene snapshot (`board`).
28// `freeze` false adds `"freeze": false` to the snapshot: the viewer does not hold the scene on a routing stall (route --no-freeze, issue #8).
29std::string board_snapshot_json(const model::Board& b, const std::string& name, bool freeze = true);
30
31std::string track_add(ObjectId id, const model::Track& t);
32std::string track_add(ObjectId id, model::Point a, model::Point b, Coord width, int layer, model::NetId net);
33std::string track_remove(ObjectId id);
34std::string via_add(ObjectId id, const model::Via& v);
35std::string via_remove(ObjectId id);
36std::string footprint_move(std::string_view ref, model::Point pos, double angle);
37
42std::string ratsnest(std::span<const RatsEdge> edges);
43
44// Transient: sampled points of an active search frontier on one copper layer.
45std::string frontier(std::int64_t conn, int layer, std::span<const model::Point> pts);
46
47struct PathPoint {
49 int layer = 0;
50};
51// Transient: a candidate path being evaluated.
52std::string path_try(std::int64_t conn, std::span<const PathPoint> pts);
53
54struct Failure {
55 std::int64_t conn = 0;
57 int rung = 0;
58 std::string cause;
60 std::vector<ObjectId> blockers;
61 geom::Box region; // empty box = none
62 // Why the attempt was rejected (doc 13, issue #8); all optional, and an old reader ignores them. Lengths in nm; unset = left out.
63 std::vector<model::NetId> blocker_nets; // nets of the connections the rejected route would have ripped
64 std::string reason; // boxed_in, no_legal_path, exact_check, rip_limit, ...
65 std::int64_t width_nm = 0, clearance_nm = 0, min_width_nm = 0; // the attempt's track width and clearance, and the narrowest legal width
66 std::int64_t gap_nm = -1, gap_fixed_nm = -1; // widest track that gets through (routed copper counted / only fixed copper)
67 int gap_layer = -1;
68 std::optional<model::Point> at; // where the way is narrowest (or the closest approach)
69 std::string verdict; // placement | rule | contested | unknown
70 std::string rule; // where the width and clearance came from
71 std::vector<PathPoint> rejected; // the path that was tried
72};
73std::string failure(const Failure& f);
74
75struct Stats {
76 std::string stage;
77 std::int64_t iteration = 0;
78 std::int64_t routed = 0, total = 0, unrouted = 0;
79 std::int64_t rips = 0, failures = 0;
80 double elapsed_s = 0;
81 std::vector<std::pair<std::string, double>> extra; // shown as extra counters in the HUD
82};
83std::string stats(const Stats& s);
84
85// state is "begin" or "end".
86std::string stage(std::string_view name, std::string_view state, std::string_view detail = {});
87// level: "debug" | "info" | "warn" | "error".
88std::string log(std::string_view level, std::string_view text);
89
90// Overlay grid: data is row-major w*h bytes (0–255), cell size in nm, origin (x0, y0); layer -1 = all layers.
91std::string heatmap(std::string_view name, model::Point origin, Coord cell, int w, int h, int layer, double max,
92 std::span<const std::uint8_t> data);
93
94// Returns the value of the top-level "type" key of a message, or an empty string. Fast path for messages built
95// here ("type" first); falls back to a full parse otherwise.
96std::string message_type(std::string_view json);
97
98} // namespace tmk::server
The board model (design doc 02 §3): every object TraceMaker reads from a .kicad_pcb,...
std::int32_t NetId
0 = no net
Definition board.hpp:21
std::int64_t ObjectId
Definition messages.hpp:21
std::string track_add(ObjectId id, const model::Track &t)
std::string message_type(std::string_view json)
Returns the value of the top-level "type" key of a message, or an empty string.
std::string track_remove(ObjectId id)
std::string path_try(std::int64_t conn, std::span< const PathPoint > pts)
Transient: a candidate path being evaluated.
std::string stats(const Stats &s)
std::string footprint_move(std::string_view ref, model::Point pos, double angle)
std::string via_remove(ObjectId id)
std::string via_add(ObjectId id, const model::Via &v)
std::string frontier(std::int64_t conn, int layer, std::span< const model::Point > pts)
Transient: sampled points of an active search frontier on one copper layer.
std::string stage(std::string_view name, std::string_view state, std::string_view detail={})
state is "begin" or "end".
std::string log(std::string_view level, std::string_view text)
level: "debug" | "info" | "warn" | "error".
std::string board_snapshot_json(const model::Board &b, const std::string &name, bool freeze=true)
Full scene snapshot (board).
std::string heatmap(std::string_view name, model::Point origin, Coord cell, int w, int h, int layer, double max, std::span< const std::uint8_t > data)
Overlay grid: data is row-major w*h bytes (0–255), cell size in nm, origin (x0, y0); layer -1 = all l...
std::string ratsnest(std::span< const RatsEdge > edges)
std::string failure(const Failure &f)
std::int64_t Coord
Engine coordinate: signed 64-bit nanometres.
Definition units.hpp:12
std::string cause
Definition messages.hpp:58
std::int64_t gap_fixed_nm
widest track that gets through (routed copper counted / only fixed copper)
Definition messages.hpp:66
std::int64_t gap_nm
Definition messages.hpp:66
std::int64_t width_nm
Definition messages.hpp:65
std::string rule
where the width and clearance came from
Definition messages.hpp:70
std::vector< ObjectId > blockers
Definition messages.hpp:60
std::string verdict
placement | rule | contested | unknown
Definition messages.hpp:69
geom::Box region
empty box = none
Definition messages.hpp:61
std::optional< model::Point > at
where the way is narrowest (or the closest approach)
Definition messages.hpp:68
std::int64_t clearance_nm
Definition messages.hpp:65
std::string reason
boxed_in, no_legal_path, exact_check, rip_limit, ...
Definition messages.hpp:64
std::vector< model::NetId > blocker_nets
Why the attempt was rejected (doc 13, issue #8); all optional, and an old reader ignores them....
Definition messages.hpp:63
std::int64_t conn
Definition messages.hpp:55
model::NetId net
Definition messages.hpp:56
std::int64_t min_width_nm
the attempt's track width and clearance, and the narrowest legal width
Definition messages.hpp:65
std::vector< PathPoint > rejected
the path that was tried
Definition messages.hpp:71
std::int64_t failures
Definition messages.hpp:79
std::int64_t rips
Definition messages.hpp:79
std::int64_t unrouted
Definition messages.hpp:78
std::string stage
Definition messages.hpp:76
std::vector< std::pair< std::string, double > > extra
shown as extra counters in the HUD
Definition messages.hpp:81
std::int64_t total
Definition messages.hpp:78
std::int64_t iteration
Definition messages.hpp:77
std::int64_t routed
Definition messages.hpp:78