Skip to content

Commit

Permalink
core: Process the KNOT_MSG_UNREG_REQ message
Browse files Browse the repository at this point in the history
Send a KNOT_MSG_UNREG_RSP response and factory reset the device when it
receives a KNOT_MSG_UNREG_REQ message.

Signed-off-by: Allysson Lukas <[email protected]>
  • Loading branch information
alla3 authored and vitbaq committed Nov 8, 2019
1 parent 86608ab commit 9f58c4a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
8 changes: 8 additions & 0 deletions core/src/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ size_t msg_create_data(knot_msg *msg, u8_t id,

return (sizeof(msg->hdr) + msg->hdr.payload_len);
}

size_t msg_create_unreg(knot_msg *msg)
{
msg->hdr.type = KNOT_MSG_UNREG_RSP;
msg->hdr.payload_len = 0;

return sizeof(msg->hdr);
}
1 change: 1 addition & 0 deletions core/src/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ size_t msg_create_schema(knot_msg *msg, u8_t id,
size_t msg_create_data(knot_msg *msg, u8_t id,
const knot_value_type *value, uint8_t value_len,
bool resp);
size_t msg_create_unreg(knot_msg *msg);
2 changes: 1 addition & 1 deletion core/src/proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static void proto_thread(void)
peripheral_flag_status();

/* Handle reset flag */
reset = peripheral_get_reset();
reset = peripheral_get_reset() || sm_get_reset();
if (reset) {
/* TODO: Unregister before reseting */
LOG_INF("Reseting system...");
Expand Down
8 changes: 8 additions & 0 deletions core/src/sm.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static bool to_xpr; /* Timeout expired */
static char uuid[KNOT_PROTOCOL_UUID_LEN + 1]; /* Device uuid */
static char token[KNOT_PROTOCOL_TOKEN_LEN + 1]; /* Device token */
static u64_t device_id; /* Device id */
static bool rst_flag; /* Reset flag */

enum sm_state {
STATE_REG, /* Registers new device */
Expand Down Expand Up @@ -335,6 +336,8 @@ static size_t process_cmd(const u8_t *ipdu, size_t ilen,
switch (imsg->hdr.type) {
case KNOT_MSG_UNREG_REQ:
/* Clear NVM */
rst_flag = true;
len = msg_create_unreg(omsg);
break;
case KNOT_MSG_POLL_DATA_REQ:
id = imsg->data.sensor_id;
Expand Down Expand Up @@ -613,3 +616,8 @@ int sm_run(const u8_t *ipdu, size_t ilen, u8_t *opdu, size_t olen)

return len;
}

bool sm_get_reset(void)
{
return rst_flag;
}
2 changes: 2 additions & 0 deletions core/src/sm.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ int sm_start(void);
void sm_stop(void);

int sm_run(const u8_t *ipdu, size_t ilen, u8_t *opdu, size_t olen);

bool sm_get_reset(void);

0 comments on commit 9f58c4a

Please sign in to comment.