fix: protocol 2 core review findings

From the review of #99:
- A sync could stall when a client took a slot the moment another left:
  loop() used up the new client's sync on the old one. The slots move
  to ProtoSlots, a pure module with native tests, and loop() takes a
  slot's wants only for the client it read (takeWants).
- Deleting a group sends its shades, whose inGroup changed. Deleting a
  shade unlinks it from its groups and sends them, so a new shade given
  its id doesn't join them.
- next and sync with an id get an ok reply, and a message in pieces
  gets one bad request, not one per piece.
- Feeds check for loop() before they write the event buffer, and log
  an event too large, as events do.
- editChanges() (ModelEditor, native tests) says what clients get after
  an edit; the edit result carries the shade a group gained or lost.
  order() takes a sort_list_t.
- The frame writers move to ProtoFrame. Renames: pf to proto_field_t,
  the X-macro letters to TYPE/FIELD/ERR/FEED, proto_request_t to
  proto_message_t, REQ_* to WANTS_*, fmt to eventJson. The event
  bodies share sendItem(), the connection type name lives in
  ESPNetwork, and the loop() task check in SocketEmitter.
- e2e: the heap floor check skips when the low since boot is already
  under it; tests for replies to next and sync and a long message.
- SECURITY_PLAN.md splits step 1 into 1a-1d.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cjkas 2026-10-24 13:17:15 +02:00
parent d869bb53d5
commit dc2f1478b6
29 changed files with 1087 additions and 670 deletions

View file

@ -24,11 +24,13 @@ enum class model_edit_type_t : uint8_t {
room_order, shade_order, group_order
};
// What an edit did, sent to the clients as requestResult. id is the room,
// shade or group it added or changed, 0 when none. code is the validation
// error of a shade that wasn't saved, 0 otherwise.
// shade or group it added or changed, 0 when none. shadeId is the shade a
// group gained or lost. code is the validation error of a shade that wasn't
// saved, 0 otherwise.
struct edit_result_t {
bool ok = true;
uint8_t id = 0;
uint8_t shadeId = 0;
int8_t code = 0;
const char *desc = "";
static edit_result_t error(const char *desc, int8_t code = 0) {
@ -40,6 +42,20 @@ struct edit_result_t {
}
};
// The lists an edit can put in a new order.
enum class sort_list_t : uint8_t { none, rooms, shades, groups };
// What protocol 2 clients are sent once an edit has gone through: the whole
// items it changed (0 for none), a list's new order, the repeaters. Deletes
// send their removals as they happen, so they need nothing here.
struct edit_changes_t {
uint8_t shadeId = 0;
uint8_t groupId = 0;
uint8_t roomId = 0;
sort_list_t order = sort_list_t::none;
bool repeaters = false;
};
edit_changes_t editChanges(model_edit_type_t type, const edit_result_t &result);
// The lowest id from 1 to lastId that isn't in usedIds, or 0 when every one
// is taken.
uint8_t firstFreeId(const uint8_t *usedIds, size_t count, uint8_t lastId);