Fix unregister handling
This fix avoid reset thing immediately after sending unregister response
message. The thing now waits a few seconds to clear, disconnect and
reset itself.
diff --git a/src/knot_thing_protocol.c b/src/knot_thing_protocol.c
index 301a6e4..4d584fb 100644
--- a/src/knot_thing_protocol.c
+++ b/src/knot_thing_protocol.c
@@ -73,6 +73,7 @@
static struct nrf24_config config = { .mac = 0, .channel = 76 , .name = NULL};
static unsigned long clear_time;
static uint32_t last_timeout;
+static uint32_t unreg_timeout;
static int sock = -1;
static int cli_sock = -1;
static bool schema_flag = false;
@@ -92,6 +93,19 @@
sizeof(struct nrf24_mac));
}
+static void thing_disconnect_exit(void)
+{
+ /* reset EEPROM (UUID/Token) and generate new MAC addr */
+ hal_storage_reset_end();
+ set_nrf24MAC();
+
+ /* close connection */
+ knot_thing_protocol_exit();
+
+ /* reset thing */
+ reset_function();
+}
+
static void verify_clear_data(void)
{
if (!hal_gpio_digital_read(CLEAR_EEPROM_PIN)) {
@@ -100,16 +114,7 @@
clear_time = hal_time_ms();
if (hal_timeout(hal_time_ms(), clear_time, BUTTON_PRESSED_TIME)) {
- /* close connection */
- knot_thing_protocol_exit();
-
- /* generate new MAC addr */
- hal_storage_reset_end();
- set_nrf24MAC();
-
- /* reset thing */
- reset_function();
-
+ thing_disconnect_exit();
}
} else
clear_time = 0;
@@ -223,23 +228,21 @@
}
}
-static void handle_unregister(void) {
+
+
+static int send_unregister(void)
+{
/* send KNOT_MSG_UNREGISTER_RESP message */
msg.hdr.type = KNOT_MSG_UNREGISTER_RESP;
- msg.action.result = KNOT_SUCCESS;
- msg.hdr.payload_len = sizeof(msg.action.result);
- hal_comm_write(cli_sock, &(msg.buffer),
- sizeof(msg.hdr) + msg.hdr.payload_len);
+ msg.hdr.payload_len = 0;
- /* reset EEPROM (UUID/Token) and generate new MAC addr */
- hal_storage_reset_end();
- set_nrf24MAC();
+ if (hal_comm_write(cli_sock, &(msg.buffer),
+ sizeof(msg.hdr) + msg.hdr.payload_len) < 0)
+ return -1;
- /* close connection */
- knot_thing_protocol_exit();
+ unreg_timeout = hal_time_ms();
- /* reset thing */
- reset_function();
+ return 0;
}
static int send_register(void)
@@ -274,8 +277,7 @@
return nbytes;
if (msg.hdr.type == KNOT_MSG_UNREGISTER_REQ) {
- handle_unregister();
- return -1;
+ return send_unregister();
}
if (msg.hdr.type != KNOT_MSG_REGISTER_RESP)
@@ -300,15 +302,15 @@
return nbytes;
if (msg.hdr.type == KNOT_MSG_UNREGISTER_REQ) {
- handle_unregister();
- return -1;
+ return send_unregister();
}
if (msg.hdr.type != KNOT_MSG_AUTH_RESP)
return -1;
if (msg.action.result != KNOT_SUCCESS)
- return -1;
+ thing_disconnect_exit();
+
return 0;
}
@@ -450,11 +452,9 @@
msg_get_data(msg.item.sensor_id);
}
break;
-
case KNOT_MSG_UNREGISTER_REQ:
- handle_unregister();
+ send_unregister();
break;
-
default:
/* Invalid command, ignore */
break;
@@ -482,6 +482,9 @@
run_state = STATE_DISCONNECTED;
}
+ if (unreg_timeout && hal_timeout(hal_time_ms(), unreg_timeout, 10000) > 0)
+ thing_disconnect_exit();
+
/* Network message handling state machine */
switch (run_state) {
case STATE_DISCONNECTED:
@@ -620,7 +623,7 @@
hal_log_str("SCH_R");
if (hal_comm_read(cli_sock, &(msg.buffer), KNOT_MSG_SIZE) > 0) {
if (msg.hdr.type == KNOT_MSG_UNREGISTER_REQ) {
- handle_unregister();
+ send_unregister();
break;
}
if (msg.hdr.type != KNOT_MSG_SCHEMA_RESP &&
@@ -659,7 +662,6 @@
hal_log_str("RUN");
}
break;
-
case STATE_RUNNING:
led_status(BLINK_ONLINE);
read_online_messages();
@@ -675,7 +677,6 @@
}
}
break;
-
case STATE_ERROR:
hal_gpio_digital_write(PIN_LED_STATUS, 1);
hal_log_str("ERR");
@@ -683,7 +684,6 @@
run_state = STATE_DISCONNECTED;
hal_delay_ms(1000);
break;
-
default:
hal_log_str("INV");
run_state = STATE_DISCONNECTED;