FastLED 3.10.6
Loading...
Searching...
No Matches
parser.cpp.hpp
Go to the documentation of this file.
2
3#include "fl/stl/bit_cast.h"
4#include "fl/stl/cstring.h"
5#include "fl/stl/json.h"
6#include "fl/stl/string.h"
7
8namespace fl {
9namespace fled {
10
11namespace {
12
13// Mirrors src/fl/video/pixel_stream.cpp.hpp constants. Kept local on
14// purpose; sharing the parser with fl::Video is a future refactor.
15constexpr fl::u8 kMagic[4] = {'F', 'L', 'E', 'D'};
16constexpr fl::u8 kVersionV1 = 1;
17constexpr fl::size kHeaderBytes = 12;
18constexpr fl::size kMaxJsonBytes = 1u * 1024u * 1024u;
19
20} // namespace
21
22bool parseHeaderAndEnvelope(const fl::u8* data, fl::size len,
23 ParsedHeader* outHeader,
24 fl::json* outEnvelope) FL_NO_EXCEPT {
25 if (!outHeader || !outEnvelope) {
26 return false;
27 }
28 if (!data) {
29 return false;
30 }
31 if (len < kHeaderBytes) {
32 return false;
33 }
34 if (data[0] != kMagic[0] || data[1] != kMagic[1] ||
35 data[2] != kMagic[2] || data[3] != kMagic[3]) {
36 return false;
37 }
38 const fl::u8 ver = data[4];
39 if (ver != kVersionV1) {
40 return false;
41 }
42 if (data[6] != 0 || data[7] != 0) {
43 return false;
44 }
45 const fl::u8 pixelFormat = data[5];
46 const fl::u32 jsonLen =
47 static_cast<fl::u32>(data[8]) |
48 (static_cast<fl::u32>(data[9]) << 8) |
49 (static_cast<fl::u32>(data[10]) << 16) |
50 (static_cast<fl::u32>(data[11]) << 24);
51 // Bound-check in u32 BEFORE narrowing to fl::size - on 16-bit targets
52 // (AVR) fl::size is 16 bits and naive narrowing would wraparound.
53 if (jsonLen > static_cast<fl::u32>(kMaxJsonBytes)) {
54 return false;
55 }
56 if (jsonLen > static_cast<fl::u32>(len - kHeaderBytes)) {
57 return false;
58 }
59 const fl::size jsonLenSz = static_cast<fl::size>(jsonLen);
60 // Build a string view over the JSON bytes (fl::string copies the bytes
61 // internally - required because json::parse takes const string&).
62 fl::string jsonText(fl::reinterpret_cast_<const char *>(data + kHeaderBytes),
63 jsonLenSz);
64 fl::json parsed = fl::json::parse(jsonText);
65 // parse() returns json(nullptr) on failure - that flags an envelope
66 // that is not a valid JSON document. Strict-reject per design.
67 if (!parsed.has_value()) {
68 return false;
69 }
70 outHeader->version = ver;
71 outHeader->pixelFormat = pixelFormat;
72 outHeader->payloadOffset = kHeaderBytes + jsonLenSz;
73 *outEnvelope = parsed;
74 return true;
75}
76
77bool sectionNameIsPayload(const char* name) FL_NO_EXCEPT {
78 if (!name) return false;
79 if (fl::strcmp(name, "frame_payload") == 0) return true;
80 if (fl::strcmp(name, "payload") == 0) return true;
81 return false;
82}
83
84} // namespace fled
85} // namespace fl
bool has_value() const FL_NO_EXCEPT
Definition json.h:819
static json parse(const fl::string &txt) FL_NO_EXCEPT
Definition json.h:842
FastLED's Elegant JSON Library: fl::json
bool parseHeaderAndEnvelope(const fl::u8 *data, fl::size len, ParsedHeader *outHeader, fl::json *outEnvelope) FL_NO_EXCEPT
bool sectionNameIsPayload(const char *name) FL_NO_EXCEPT
unsigned char u8
Definition stdint.h:131
To reinterpret_cast_(const From &from) FL_NO_EXCEPT
Definition bit_cast.h:88
int strcmp(const char *s1, const char *s2) FL_NO_EXCEPT
InputGamut g FL_NO_EXCEPT
Definition rgbw.h:121
Base definition for an LED controller.
Definition crgb.hpp:179