phyphox BLE
Plot your Arduino's data in the phyphox app and control the board from the phone
Loading...
Searching...
No Matches
Serializer.h
Go to the documentation of this file.
1// The streaming XML serializer.
2//
3// The document is a sequence of PARTS, each generated deterministically from the store:
4// 0 prologue: <phyphox>, title, category, description, data-containers, the
5// bluetooth input (data channels, phone sensors), the bluetooth output (one
6// <input> per input channel / sensor characteristic), the analysis block with
7// the change detector of every input channel
8// 1 … V one part per view (its opening tag, its elements, its closing tag)
9// V+1 epilogue: the error view if any, the export block, </phyphox>
10//
11// build() runs every part once through a CountingSink, recording each part's length; the
12// cumulative offsets form the PART TABLE. After that, any byte range of the document is served
13// by regenerating only the one or two parts it touches through a WindowSink. There is one full
14// pass per addExperiment(), never one per packet.
15//
16// Determinism is the invariant everything rests on: the same store must yield identical bytes
17// every time. The host tests assert it (part-table offsets ≡ a full serialization; every
18// window ≡ the slice of the full document).
19#ifndef PHYPHOX_BLE_CORE_SERIALIZER_H
20#define PHYPHOX_BLE_CORE_SERIALIZER_H
21
22#include "ExperimentStore.h"
23#include "Sink.h"
24
25namespace phyphox {
26
28public:
31 void build(const ExperimentStore& store, const char* deviceName, uint16_t mtu);
32
33 uint32_t size() const { return totalSize_; }
34 uint32_t crc() const { return crc_; }
35 uint16_t partCount() const { return partCount_; }
36
39 uint32_t read(uint32_t offset, uint8_t* out, uint32_t n) const;
41 void writeAll(Sink& sink) const;
42
44 void emitPart(uint16_t part, Sink& sink) const;
45
46private:
47 const ExperimentStore* store_ = nullptr;
48 const char* deviceName_ = nullptr;
49 uint16_t mtu_ = PHYPHOX_BLE_DEFAULT_MTU;
50 uint32_t totalSize_ = 0;
51 uint32_t crc_ = 0;
52 uint16_t partCount_ = 0;
55 uint16_t* partOffset_ = nullptr;
56
57 void emitPrologue(Sink&) const;
58 void emitView(uint8_t view, Sink&) const;
59 void emitElement(const ElementData&, Sink&) const;
60 void emitEpilogue(Sink&) const;
61};
62
66void writeEscaped(Sink&, const char* text);
67void writeFloat(Sink&, float v);
68void writeInt(Sink&, int32_t v);
70void writeInputCharUuid(Sink&, uint8_t channel);
71void writeSensorCharUuid(Sink&, uint8_t sensor);
72
73} // namespace phyphox
74
75#endif
#define PHYPHOX_BLE_DEFAULT_MTU
Default MTU payload (ATT_MTU 23 − 3). setMTU() raises it where the transport can negotiate.
Definition Config.h:52
Definition ExperimentStore.h:31
Definition Serializer.h:27
uint16_t partCount() const
Definition Serializer.h:35
void emitPart(uint16_t part, Sink &sink) const
Serialize one part into a sink (public for the tests).
void build(const ExperimentStore &store, const char *deviceName, uint16_t mtu)
Bind to a store and build the part table.
void writeAll(Sink &sink) const
Stream the entire document (printXML, host tests).
uint32_t size() const
bytes of the whole document
Definition Serializer.h:33
uint32_t read(uint32_t offset, uint8_t *out, uint32_t n) const
Copy document bytes [offset, offset + n) into out; returns bytes produced (fewer at the end).
uint32_t crc() const
CRC-32 of the whole document.
Definition Serializer.h:34
Receives the bytes of a serialization.
Definition Sink.h:14
Definition ChannelStore.h:11
void writeEscaped(Sink &, const char *text)
Text helpers shared by the emitters (public so the host tests can pin them): XML-escape text into the...
void writeInt(Sink &, int32_t v)
void writeFloat(Sink &, float v)
void writeSensorCharUuid(Sink &, uint8_t sensor)
void writeInputCharUuid(Sink &, uint8_t channel)
The UUID of the k-th input-channel / s-th sensor characteristic (docs/protocol.md).
One view element. label and xmlAttribute are common to all; the union holds the rest.
Definition ElementData.h:146