ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
partition_monitor.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file partition_monitor.h
6 * @brief Flash partition-map monitor (PROTOCORE_ENABLE_PARTITION_MONITOR).
7 *
8 * Reports the device's flash partition table as JSON for diagnostics / OTA
9 * dashboards: each entry's label, a human "kind" (factory / ota / nvs / spiffs /
10 * littlefs / coredump / ...), the raw type/subtype, flash offset, size, and which
11 * app slot is currently running. The partition walk uses esp_partition /
12 * esp_ota_ops (ESP32-only); the kind classifier and the JSON serializer are pure
13 * and host-tested.
14 *
15 * @author Douglas Quigg (dstroy0)
16 * @date 2026
17 */
18
19#ifndef PROTOCORE_PARTITION_MONITOR_H
20#define PROTOCORE_PARTITION_MONITOR_H
21
22#include "protocore_config.h" // the entry point: protocore_types.h for the widths
23
24#if PROTOCORE_ENABLE_PARTITION_MONITOR
25
27
28// This module holds nothing between calls, so it carves no borrow and states none. An entry
29// takes one all the same, and never reads it, so every namespace in the tree is invoked the
30// same way.
31
32/** @brief One flash partition entry. */
33typedef struct
34{
35 char label[17]; ///< partition label (null-terminated).
36 uint8_t type; ///< esp_partition type (0 = app, 1 = data).
37 uint8_t subtype; ///< esp_partition subtype.
38 uint32_t address; ///< flash offset (bytes).
39 uint32_t size; ///< partition size (bytes).
40 proto_bool running; ///< true for the currently-running app partition.
41} protocore_partition_info;
42
43/** @brief What kind takes: type, subtype. */
44typedef struct
45{
46 uint8_t type;
47 uint8_t subtype;
48} PartitionMonitorKindArgs;
49
50/** @brief What json takes: parts, count, out, cap. */
51typedef struct
52{
53 const protocore_partition_info *parts;
54 uint8_t count;
55 char *out;
56 uint32_t cap;
57} PartitionMonitorJsonArgs;
58
59/** @brief What collect takes: out, max. */
60typedef struct
61{
62 protocore_partition_info *out;
63 uint8_t max;
64} PartitionMonitorCollectArgs;
65
66/** @brief What begin takes: path. */
67typedef struct
68{
69 const char *path;
70} PartitionMonitorBeginArgs;
71
72/**
73 * @brief Flash partition-map monitor (PROTOCORE_ENABLE_PARTITION_MONITOR). Reports the device's flash partition table
74 * ...
75 *
76 * A caller sets the members a call takes, invokes it through ::PartitionMonitor with the bytes it runs
77 * out of, and reads the outcome off the same handle.
78 *
79 * PartitionMonitor.kind_args.type = ...;
80 * PartitionMonitor.kind_args.subtype = ...;
81 * PartitionMonitor.kind(work);
82 * // PartitionMonitor.text is what the call reports
83 *
84 * @var PartitionMonitorNs::kind_args what kind takes: type, subtype
85 * @var PartitionMonitorNs::json_args what json takes: parts, count, out, cap
86 * @var PartitionMonitorNs::collect_args what collect takes: out, max
87 * @var PartitionMonitorNs::begin_args what begin takes: path
88 * @var PartitionMonitorNs::ok a call's true/false outcome
89 * @var PartitionMonitorNs::text the string a call reports
90 * @var PartitionMonitorNs::n characters written, or 0 if cap is too small
91 * @var PartitionMonitorNs::u8 number of partitions written (<= max)
92 * @var PartitionMonitorNs::kind human name for a partition type/subtype (e.g. "factory", "ota", ...
93 * @var PartitionMonitorNs::json serialize a partition array as JSON `{"partitions":[...]}` into out
94 * @var PartitionMonitorNs::collect walk the flash partition table into out (ESP32; 0 on host builds)
95 * @var PartitionMonitorNs::begin serve the partition map as JSON at path (GET). Default "/partitions"
96 *
97 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
98 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
99 * a caller drives every namespace the same way.
100 */
101typedef struct
102{
103 PartitionMonitorKindArgs kind_args;
104 PartitionMonitorJsonArgs json_args;
105 PartitionMonitorCollectArgs collect_args;
106 PartitionMonitorBeginArgs begin_args;
107 proto_bool ok;
108 const char *text;
109 int32_t n;
110 uint8_t u8;
111} PartitionMonitorVars;
112
113/** @brief The operands and the outcome. */
114extern PartitionMonitorVars PartitionMonitorV;
115
116/** @brief The entries. */
117typedef struct
118{
119 void (*const kind)(uint8_t *work);
120 void (*const json)(uint8_t *work);
121 void (*const collect)(uint8_t *work);
122 void (*const begin)(uint8_t *work);
123} PartitionMonitorNs;
124
125// What the table binds, defined once in the .c and taking one parameter each: everything
126// else an entry needs is an operand in PartitionMonitorV or a region of the borrow at a fixed offset.
127void protocore_partition_monitor_kind(uint8_t *work);
128void protocore_partition_monitor_json(uint8_t *work);
129void protocore_partition_monitor_collect(uint8_t *work);
130void protocore_partition_monitor_begin(uint8_t *work);
131
132// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
133// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
134// `PartitionMonitor.kind(work)` resolves to a named function and becomes a DIRECT call. An extern table
135// leaves the call indirect and the symbol live at every level, -O2 -flto included.
136static const PartitionMonitorNs PartitionMonitor __attribute__((unused)) = {
137 .kind = protocore_partition_monitor_kind,
138 .json = protocore_partition_monitor_json,
139 .collect = protocore_partition_monitor_collect,
140 .begin = protocore_partition_monitor_begin,
141};
142
144
145#endif // PROTOCORE_ENABLE_PARTITION_MONITOR
146
147#endif // PROTOCORE_PARTITION_MONITOR_H
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
Definition types.h:96
_Bool proto_bool
The truth value.
Definition types.h:64
#define PROTOCORE_END_DECLS
Definition types.h:97