Skip to content

Commit

Permalink
setup: Reset after receiving GATT command to do so
Browse files Browse the repository at this point in the history
Automatically reset device if receiving a GATT command to do so.

Send boolean signal to notify main loop if a reset command was received.

Signed-off-by: Joao Cordeiro <jvcc@cesar.org.br>
  • Loading branch information
jcorde committed Aug 9, 2019
1 parent 36e0f9d commit 0452823
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
3 changes: 3 additions & 0 deletions setup/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ CONFIG_SYSTEM_CLOCK_DISABLE=y

# Peripherals usage
CONFIG_GPIO=y

# Reboot
CONFIG_REBOOT=y
4 changes: 2 additions & 2 deletions setup/src/bt_srv.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static void ot_updated(void)
}
}

int bt_srv_init(void)
int bt_srv_init(bool *reset_signal)
{
int err;

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion setup/src/bt_srv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
22 changes: 20 additions & 2 deletions setup/src/gatt_ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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;
}

Expand All @@ -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;

Expand All @@ -68,5 +83,8 @@ int gatt_ctrl_init(void)
return err;
}

/* Set signals to be used */
out_reset_signal = reset_signal;

return 0;
}
2 changes: 1 addition & 1 deletion setup/src/gatt_ctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
* SPDX-License-Identifier: Apache-2.0
*/

int gatt_ctrl_init(void);
int gatt_ctrl_init(bool *reset_signal);
13 changes: 11 additions & 2 deletions setup/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <logging/log.h>
#include <settings/settings.h>
#include <settings/settings_ot.h>
#include <misc/reboot.h>

#include "bt_srv.h"
#include "peripheral.h"
Expand All @@ -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");

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

0 comments on commit 0452823

Please sign in to comment.