ProtoParse.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef PROTO_PARSE_H | ||
| 2 | #define PROTO_PARSE_H | ||
| 3 | #include <stddef.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <ArduinoJson.h> | ||
| 6 | #include "ProtoSchema.h" | ||
| 7 | |||
| 8 | // Reading the frames a socket protocol 2 client sends (docs/PROTOCOL.md). | ||
| 9 | // Kept free of the web server so it can be unit tested on the PC | ||
| 10 | // (pio test -e native). | ||
| 11 | |||
| 12 | // The largest frame a client may send; a longer one is refused unread. | ||
| 13 | #define PROTO_MAX_CLIENT_FRAME 4096 | ||
| 14 | |||
| 15 | struct proto_message_t { | ||
| 16 | uint8_t type = 0; | ||
| 17 | uint32_t id = 0; // 0 when the frame had none | ||
| 18 | }; | ||
| 19 | // Reads one frame into doc and out. none when it is a message a client may | ||
| 20 | // send; otherwise the error to reply with. out.id is set whenever the frame | ||
| 21 | // had one, so the reply can name it. The payload is doc["d"]. | ||
| 22 | proto_error_t parseClientFrame(const char *text, size_t len, JsonDocument &doc, proto_message_t &out); | ||
| 23 | // An op (16-127) as opposed to a control message. | ||
| 24 | 3 | inline bool protoIsOp(uint8_t type) { return type >= 16 && type <= 127; } | |
| 25 | |||
| 26 | // A field of a payload, or its default when it was left out. | ||
| 27 | int32_t protoInt(JsonObjectConst d, proto_field_t field); | ||
| 28 | bool protoBool(JsonObjectConst d, proto_field_t field); | ||
| 29 | // nullptr when it was left out, as a string's default is "". | ||
| 30 | const char *protoString(JsonObjectConst d, proto_field_t field); | ||
| 31 | bool protoHas(JsonObjectConst d, proto_field_t field); | ||
| 32 | |||
| 33 | // A value sent for field f, put into out as the HTTP route reads it: booleans | ||
| 34 | // as true and false. False when it isn't of the field's kind. | ||
| 35 | bool protoRouteValue(const proto_field_info_t &f, JsonVariantConst v, JsonVariant out); | ||
| 36 | // A kind as a bad request names it: "whole number", "string" ... | ||
| 37 | const char *protoKindName(char kind); | ||
| 38 | #endif | ||
| 39 |