Skip to content

Commit c7654df

Browse files
committed
Stop accepting RPCs 100ms before device shuts down
Signed-off-by: Deomid "rojer" Ryabkov <rojer@rojer.me>
1 parent 17383b6 commit c7654df

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/mgos_rpc.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "mgos_config_util.h"
3333
#include "mgos_debug.h"
3434
#include "mgos_debug_hal.h"
35+
#include "mgos_event.h"
3536
#include "mgos_hal.h"
3637
#if defined(MGOS_HAVE_HTTP_SERVER) && MGOS_ENABLE_RPC_CHANNEL_HTTP
3738
#include "mgos_http_server.h"
@@ -50,7 +51,12 @@
5051
#include "mgos_pppos.h"
5152
#endif
5253

54+
#ifndef MGOS_RPC_REBOOT_LOCKOUT_MS
55+
#define MGOS_RPC_REBOOT_LOCKOUT_MS 100
56+
#endif
57+
5358
static struct mg_rpc *s_global_mg_rpc;
59+
static int64_t s_reboot_at_uptime_micros;
5460

5561
extern const char *mg_build_id;
5662
extern const char *mg_build_version;
@@ -485,6 +491,16 @@ static bool mgos_rpc_req_prehandler(struct mg_rpc_request_info *ri,
485491
const char *auth_file = NULL;
486492
struct mg_str acl_entry = MG_NULL_STR;
487493

494+
if (s_reboot_at_uptime_micros > 0) {
495+
int64_t time_left_ms =
496+
(s_reboot_at_uptime_micros - mgos_uptime_micros()) / 1000;
497+
if (time_left_ms < MGOS_RPC_REBOOT_LOCKOUT_MS) {
498+
mg_rpc_send_errorf(ri, 418, "shutting down in %d ms", (int) time_left_ms);
499+
ri = NULL;
500+
goto out;
501+
}
502+
}
503+
488504
enum mgos_rpc_authz_result authz_res = mgos_rpc_check_authz_internal(
489505
ri, mgos_sys_config_get_rpc_acl(), mgos_sys_config_get_rpc_acl_file(),
490506
&acl_entry);
@@ -583,6 +599,13 @@ static bool mgos_rpc_req_prehandler(struct mg_rpc_request_info *ri,
583599
return ret;
584600
}
585601

602+
static void mgos_rpc_reboot_after_ev_handler(int ev, void *evd, void *cb_arg) {
603+
const struct mgos_event_reboot_after_arg *arg = evd;
604+
s_reboot_at_uptime_micros = arg->reboot_at_uptime_micros;
605+
(void) ev;
606+
(void) cb_arg;
607+
}
608+
586609
static void observer_cb(struct mg_rpc *c, void *cb_arg, enum mg_rpc_event ev,
587610
void *ev_arg) {
588611
switch (ev) {
@@ -608,6 +631,8 @@ bool mgos_rpc_common_init(void) {
608631

609632
/* Add mgos-specific prehandler */
610633
mg_rpc_set_prehandler(c, mgos_rpc_req_prehandler, NULL);
634+
mgos_event_add_handler(MGOS_EVENT_REBOOT_AFTER,
635+
mgos_rpc_reboot_after_ev_handler, NULL);
611636

612637
#if defined(MGOS_HAVE_HTTP_SERVER) && MGOS_ENABLE_RPC_CHANNEL_HTTP
613638
if (mgos_sys_config_get_rpc_http_enable()) {

0 commit comments

Comments
 (0)