diff --git a/setup/prj.conf b/setup/prj.conf index 87ba114..a59f1be 100644 --- a/setup/prj.conf +++ b/setup/prj.conf @@ -58,3 +58,6 @@ CONFIG_SYSTEM_CLOCK_DISABLE=y # Peripherals usage CONFIG_GPIO=y + +# Reboot +CONFIG_REBOOT=y diff --git a/setup/src/bt_srv.c b/setup/src/bt_srv.c index 2972fca..d6ce279 100644 --- a/setup/src/bt_srv.c +++ b/setup/src/bt_srv.c @@ -139,7 +139,7 @@ static void ot_updated(void) } } -int bt_srv_init(void) +int bt_srv_init(bool *reset_signal) { int err; @@ -153,7 +153,7 @@ int bt_srv_init(void) } /* Control device GATT service */ - err = gatt_ctrl_init(); + err = gatt_ctrl_init(reset_signal); if (err) { LOG_ERR("Control device GATT service"\ " init failed (err %d)", err); diff --git a/setup/src/bt_srv.h b/setup/src/bt_srv.h index 72c95e5..4a2587d 100644 --- a/setup/src/bt_srv.h +++ b/setup/src/bt_srv.h @@ -15,6 +15,6 @@ * limitations under the License. */ -int bt_srv_init(void); +int bt_srv_init(bool *reset_signal); void bt_srv_toggle_advertising(void); diff --git a/setup/src/gatt_ctrl.c b/setup/src/gatt_ctrl.c index cefee37..546d5d7 100644 --- a/setup/src/gatt_ctrl.c +++ b/setup/src/gatt_ctrl.c @@ -16,7 +16,14 @@ LOG_MODULE_DECLARE(knot_setup, LOG_LEVEL_DBG); -static u8_t cmd; // Received command +/* Available commands */ +enum { + CTRL_CMD_UNSET = 0x00, + CTRL_CMD_RESET = 0x01, +}; + +static u8_t cmd = CTRL_CMD_UNSET; // Received command +static bool *out_reset_signal; /* Custom Service Variables */ static struct bt_uuid_128 service_uuid = BT_UUID_INIT_128( @@ -26,6 +33,12 @@ static const struct bt_uuid_128 cmd_uuid = BT_UUID_INIT_128( 0x71, 0xba, 0x2b, 0xfb, 0x05, 0x74, 0x4a, 0x64, 0x9a, 0x93, 0xc8, 0x7c, 0xf3, 0x8f, 0x41, 0x81); +static void process_cmd() +{ + if (cmd == CTRL_CMD_RESET) + *out_reset_signal = true; +} + /* Write characteristic callback function */ static ssize_t write_cmd(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) @@ -41,6 +54,8 @@ static ssize_t write_cmd(struct bt_conn *conn, const struct bt_gatt_attr *attr, memcpy(value + offset, buf, len); + process_cmd(); + return len; } @@ -57,7 +72,7 @@ static struct bt_gatt_attr config_gatt_attrs[] = { static struct bt_gatt_service config_svc = BT_GATT_SERVICE(config_gatt_attrs); -int gatt_ctrl_init(void) +int gatt_ctrl_init(bool *reset_signal) { int err; @@ -68,5 +83,8 @@ int gatt_ctrl_init(void) return err; } + /* Set signals to be used */ + out_reset_signal = reset_signal; + return 0; } diff --git a/setup/src/gatt_ctrl.h b/setup/src/gatt_ctrl.h index 5446cff..18a6e59 100644 --- a/setup/src/gatt_ctrl.h +++ b/setup/src/gatt_ctrl.h @@ -6,4 +6,4 @@ * SPDX-License-Identifier: Apache-2.0 */ -int gatt_ctrl_init(void); +int gatt_ctrl_init(bool *reset_signal); diff --git a/setup/src/main.c b/setup/src/main.c index 8b54f44..0ca3af3 100644 --- a/setup/src/main.c +++ b/setup/src/main.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "bt_srv.h" #include "peripheral.h" @@ -34,6 +35,7 @@ void main(void) int btn; bool ipv6_set; bool ot_set; + bool reset_signal = false; // Avoid early reseting LOG_DBG("Initializing storage services"); @@ -106,8 +108,8 @@ void main(void) /* Setup Application */ LOG_DBG("Initializing Setup App"); - /* Bluetooth services */ - err = bt_srv_init(); + /* Init bluetooth services and give access to signal flags */ + err = bt_srv_init(&reset_signal); if (err) { LOG_ERR("Failed to initialize bluetooth"); return; @@ -116,5 +118,12 @@ void main(void) while (1) { bt_srv_toggle_advertising(); peripheral_toggle_led(); + + /* Reset system if signal received */ + if (reset_signal == false) + continue; + + LOG_INF("Reseting system..."); + sys_reboot(SYS_REBOOT_WARM); } }