From 9f58c4a05e65050077037ca61f4c13b93ce3c38f Mon Sep 17 00:00:00 2001 From: Allysson Lukas Date: Tue, 5 Nov 2019 19:30:42 -0300 Subject: [PATCH] core: Process the KNOT_MSG_UNREG_REQ message 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 --- core/src/msg.c | 8 ++++++++ core/src/msg.h | 1 + core/src/proto.c | 2 +- core/src/sm.c | 8 ++++++++ core/src/sm.h | 2 ++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/core/src/msg.c b/core/src/msg.c index 7426426..80b0c31 100644 --- a/core/src/msg.c +++ b/core/src/msg.c @@ -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); +} diff --git a/core/src/msg.h b/core/src/msg.h index 50c9dcd..debc36d 100644 --- a/core/src/msg.h +++ b/core/src/msg.h @@ -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); diff --git a/core/src/proto.c b/core/src/proto.c index aeb5501..e9ceecf 100644 --- a/core/src/proto.c +++ b/core/src/proto.c @@ -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..."); diff --git a/core/src/sm.c b/core/src/sm.c index 77b4a52..9a1992d 100644 --- a/core/src/sm.c +++ b/core/src/sm.c @@ -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 */ @@ -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; @@ -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; +} diff --git a/core/src/sm.h b/core/src/sm.h index de9c10f..2f7d0bc 100644 --- a/core/src/sm.h +++ b/core/src/sm.h @@ -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);