Skip to content

Commit

Permalink
Let client set the name of the box.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Nov 20, 2024
1 parent 6acf562 commit ff9cc48
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions firmware/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct _Controller {
StateFunc state;
Sequencer *sequencer;
DataLink *datalink;
char box_name[24];
};


Expand Down Expand Up @@ -95,7 +96,7 @@ static void attributeChanged(Controller *me, AttributeId ai, ElementEncoding enc
aa->opcode = OC_REPORT_DATA;
aa->reserved = 0;
aa->attribute_id = ai;
// TODO Add subscription Id?
// TODO Add subscription Id if applicable?
nbtw += Matter_encode(packet + nbtw, enc, data, data_size);
DataLink_sendDatagram(me->datalink, packet, nbtw);
}
Expand Down Expand Up @@ -131,7 +132,7 @@ static void handleReadRequest(Controller *me, AttributeAction const *aa)
Sequencer_notifyPlayState(me->sequencer);
break;
case AI_BOX_NAME:
// TODO Implement.
attributeChanged(me, aa->attribute_id, EE_UTF8_1LEN, (uint8_t const *)me->box_name, strlen(me->box_name));
break;
default:
BSP_logf("%s: unknown attribute id=%hu\n", __func__, aa->attribute_id);
Expand All @@ -149,6 +150,16 @@ static EventType eventTypeForCommand(uint8_t const *cs, uint16_t len)
}


static void updateBoxName(Controller *me, uint8_t const *name, uint16_t len)
{
size_t nb = (len < sizeof me->box_name ? len : sizeof me->box_name - 1);
memcpy(me->box_name, name, nb);
me->box_name[nb] = '\0';
// TODO Store the name in nonvolatile memory rather than in RAM.
BSP_logf("Box name set to '%s'\n", me->box_name);
}


static void handleWriteRequest(Controller *me, AttributeAction const *aa)
{
switch (aa->attribute_id)
Expand All @@ -169,7 +180,9 @@ static void handleWriteRequest(Controller *me, AttributeAction const *aa)
}
break;
case AI_BOX_NAME:
// TODO Implement.
if (aa->data[0] == EE_UTF8_1LEN) {
updateBoxName(me, aa->data + 2, aa->data[1]);
}
break;
default:
BSP_logf("%s: unknown attribute id=%hu\n", __func__, aa->attribute_id);
Expand Down Expand Up @@ -288,6 +301,7 @@ Controller *Controller_new()

void Controller_init(Controller *me, Sequencer *sequencer, DataLink *datalink)
{
strncpy(me->box_name, "My first Neostim box", sizeof me->box_name - 1);
me->sequencer = sequencer;
me->datalink = datalink;
}
Expand Down

0 comments on commit ff9cc48

Please sign in to comment.