Skip to content

Commit 96a7545

Browse files
Johan Hedbergcarlescufi
authored andcommitted
samples: reel_board/mesh_badge: Add protection for fast key presses
It was possible to freeze the application by pressing the user button too frequently. This was likely due to the stack not keeping up with the send requests and running out of buffers. Add rate-limiting to the app, so that at most one message per 500ms can be sent. Also add a user message if the user presses the button too many times in rapid succession, and inform the other badges of this misbehaving user. Signed-off-by: Johan Hedberg <[email protected]>
1 parent 45a4fbf commit 96a7545

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

samples/boards/reel_board/mesh_badge/src/mesh.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
#define MOD_LF 0x0000
2121
#define OP_HELLO 0xbb
2222
#define OP_HEARTBEAT 0xbc
23+
#define OP_BADUSER 0xbd
2324
#define OP_VND_HELLO BT_MESH_MODEL_OP_3(OP_HELLO, BT_COMP_ID_LF)
2425
#define OP_VND_HEARTBEAT BT_MESH_MODEL_OP_3(OP_HEARTBEAT, BT_COMP_ID_LF)
26+
#define OP_VND_BADUSER BT_MESH_MODEL_OP_3(OP_BADUSER, BT_COMP_ID_LF)
2527

2628
#define DEFAULT_TTL 31
2729
#define GROUP_ADDR 0xc123
@@ -56,6 +58,7 @@ struct sensor_hdr_b {
5658
} __packed;
5759

5860
static struct k_work hello_work;
61+
static struct k_work baduser_work;
5962
static struct k_work mesh_start_work;
6063

6164
/* Definitions of models user data (Start) */
@@ -334,6 +337,30 @@ static void vnd_hello(struct bt_mesh_model *model,
334337
board_blink_leds();
335338
}
336339

340+
static void vnd_baduser(struct bt_mesh_model *model,
341+
struct bt_mesh_msg_ctx *ctx,
342+
struct net_buf_simple *buf)
343+
{
344+
char str[32];
345+
size_t len;
346+
347+
printk("\"Bad user\" message from 0x%04x\n", ctx->addr);
348+
349+
if (ctx->addr == bt_mesh_model_elem(model)->addr) {
350+
printk("Ignoring message from self\n");
351+
return;
352+
}
353+
354+
len = MIN(buf->len, HELLO_MAX);
355+
memcpy(str, buf->data, len);
356+
str[len] = '\0';
357+
358+
strcat(str, " is misbehaving!");
359+
board_show_text(str, false, K_SECONDS(3));
360+
361+
board_blink_leds();
362+
}
363+
337364
static void vnd_heartbeat(struct bt_mesh_model *model,
338365
struct bt_mesh_msg_ctx *ctx,
339366
struct net_buf_simple *buf)
@@ -357,6 +384,7 @@ static void vnd_heartbeat(struct bt_mesh_model *model,
357384
static const struct bt_mesh_model_op vnd_ops[] = {
358385
{ OP_VND_HELLO, 1, vnd_hello },
359386
{ OP_VND_HEARTBEAT, 1, vnd_heartbeat },
387+
{ OP_VND_BADUSER, 1, vnd_baduser },
360388
BT_MESH_MODEL_OP_END,
361389
};
362390

@@ -432,6 +460,33 @@ void mesh_send_hello(void)
432460
k_work_submit(&hello_work);
433461
}
434462

463+
static void send_baduser(struct k_work *work)
464+
{
465+
NET_BUF_SIMPLE_DEFINE(msg, 3 + HELLO_MAX + 4);
466+
struct bt_mesh_msg_ctx ctx = {
467+
.net_idx = NET_IDX,
468+
.app_idx = APP_IDX,
469+
.addr = GROUP_ADDR,
470+
.send_ttl = DEFAULT_TTL,
471+
};
472+
const char *name = bt_get_name();
473+
474+
bt_mesh_model_msg_init(&msg, OP_VND_BADUSER);
475+
net_buf_simple_add_mem(&msg, name,
476+
MIN(HELLO_MAX, first_name_len(name)));
477+
478+
if (bt_mesh_model_send(&vnd_models[0], &ctx, &msg, NULL, NULL) == 0) {
479+
board_show_text("Bad user!", false, K_SECONDS(2));
480+
} else {
481+
board_show_text("Sending Failed!", false, K_SECONDS(2));
482+
}
483+
}
484+
485+
void mesh_send_baduser(void)
486+
{
487+
k_work_submit(&baduser_work);
488+
}
489+
435490
static int provision_and_configure(void)
436491
{
437492
static const u8_t net_key[16] = {
@@ -545,6 +600,7 @@ int mesh_init(void)
545600
};
546601

547602
k_work_init(&hello_work, send_hello);
603+
k_work_init(&baduser_work, send_baduser);
548604
k_work_init(&mesh_start_work, start_mesh);
549605

550606
return bt_mesh_init(&prov, &comp);

samples/boards/reel_board/mesh_badge/src/mesh.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct led_onoff_state {
3434
};
3535

3636
void mesh_send_hello(void);
37+
void mesh_send_baduser(void);
3738

3839
u16_t mesh_get_addr(void);
3940
bool mesh_is_initialized(void);

samples/boards/reel_board/mesh_badge/src/reel_board.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,27 @@ static void button_interrupt(struct device *dev, struct gpio_callback *cb,
480480
return;
481481
case SCREEN_MAIN:
482482
if (pins & BIT(SW0_GPIO_PIN)) {
483-
mesh_send_hello();
483+
u32_t uptime = k_uptime_get_32();
484+
static u32_t bad_count, press_ts;
485+
486+
if (uptime - press_ts < 500) {
487+
bad_count++;
488+
} else {
489+
bad_count = 0;
490+
}
491+
492+
press_ts = uptime;
493+
494+
if (bad_count) {
495+
if (bad_count > 5) {
496+
mesh_send_baduser();
497+
bad_count = 0;
498+
} else {
499+
printk("Ignoring press\n");
500+
}
501+
} else {
502+
mesh_send_hello();
503+
}
484504
}
485505
return;
486506
default:

0 commit comments

Comments
 (0)