32
32
#include "mgos_config_util.h"
33
33
#include "mgos_debug.h"
34
34
#include "mgos_debug_hal.h"
35
+ #include "mgos_event.h"
35
36
#include "mgos_hal.h"
36
37
#if defined(MGOS_HAVE_HTTP_SERVER ) && MGOS_ENABLE_RPC_CHANNEL_HTTP
37
38
#include "mgos_http_server.h"
50
51
#include "mgos_pppos.h"
51
52
#endif
52
53
54
+ #ifndef MGOS_RPC_REBOOT_LOCKOUT_MS
55
+ #define MGOS_RPC_REBOOT_LOCKOUT_MS 100
56
+ #endif
57
+
53
58
static struct mg_rpc * s_global_mg_rpc ;
59
+ static int64_t s_reboot_at_uptime_micros ;
54
60
55
61
extern const char * mg_build_id ;
56
62
extern const char * mg_build_version ;
@@ -485,6 +491,16 @@ static bool mgos_rpc_req_prehandler(struct mg_rpc_request_info *ri,
485
491
const char * auth_file = NULL ;
486
492
struct mg_str acl_entry = MG_NULL_STR ;
487
493
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
+
488
504
enum mgos_rpc_authz_result authz_res = mgos_rpc_check_authz_internal (
489
505
ri , mgos_sys_config_get_rpc_acl (), mgos_sys_config_get_rpc_acl_file (),
490
506
& acl_entry );
@@ -583,6 +599,13 @@ static bool mgos_rpc_req_prehandler(struct mg_rpc_request_info *ri,
583
599
return ret ;
584
600
}
585
601
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
+
586
609
static void observer_cb (struct mg_rpc * c , void * cb_arg , enum mg_rpc_event ev ,
587
610
void * ev_arg ) {
588
611
switch (ev ) {
@@ -608,6 +631,8 @@ bool mgos_rpc_common_init(void) {
608
631
609
632
/* Add mgos-specific prehandler */
610
633
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 );
611
636
612
637
#if defined(MGOS_HAVE_HTTP_SERVER ) && MGOS_ENABLE_RPC_CHANNEL_HTTP
613
638
if (mgos_sys_config_get_rpc_http_enable ()) {
0 commit comments