MeterLogger
watchdog.c
Go to the documentation of this file.
1 #include <esp8266.h>
2 #include "driver/gpio16.h"
3 #include "watchdog.h"
4 #include "unix_time.h"
5 #include "wifi.h"
6 #include "config.h"
7 #include "led.h"
8 
9 #include "debug.h"
10 
14 
15 typedef struct{
16  volatile uint32_t id;
18  volatile uint32_t timeout;
19  volatile uint32_t last_reset;
20 } watchdog_t;
21 
23 volatile uint8_t watchdog_list_len;
24 
26  if (gpio16_input_get()) {
28  }
29  else {
31  }
32 }
33 
35  if (wifi_scan_is_running()) {
36  // reschedule if wifi scan is running
40 #ifdef DEBUG
41  os_printf("scanner still running - rescheduling reccont\n");
42 #endif
43  }
44  else {
46 #ifdef AP
47  if (sys_cfg.ap_enabled) {
50  }
51  }
52  else {
53  if (wifi_get_opmode() != STATION_MODE) {
55  }
56  }
57 #else
58  if (wifi_get_opmode() != STATION_MODE) {
60  }
61 #endif // AP
62  led_stop_pattern(); // DEBUG
63  set_my_auto_connect(true);
66 #ifdef DEBUG
67  os_printf("watchdog restarted wifi and started wifi scanner\n");
68 #endif
69  }
70 }
71 
72 ICACHE_FLASH_ATTR void static watchdog_timer_func(void *arg) {
73  uint32_t i;
74  uint32_t uptime;
75 
76  uptime = get_uptime();
77 // if (uptime) { // only run watchdog if we have unix time
78  for (i = 0; i < WATCHDOG_MAX; i++) {
79  if ((watchdog_list[i].type != NOT_ENABLED) &&
80  (watchdog_list[i].last_reset) &&
81  ((int32_t)watchdog_list[i].last_reset < (int32_t)(uptime - watchdog_list[i].timeout))) {
82 #ifdef DEBUG
83  os_printf("watchdog timeout, id: %d\n", watchdog_list[i].id);
84 #endif
85  switch (watchdog_list[i].type) {
86  case REBOOT:
88 #ifdef DEBUG
89  os_printf("reboot\n");
90 #endif
91  break;
92 
93  case NETWORK_RESTART:
94  // DEBUG: hack to get it to reconnect on weak wifi
95  // force reconnect to wireless
96  led_pattern_b(); // DEBUG to se if we ever try to restart network
98  set_my_auto_connect(false);
101 #ifdef DEBUG
102  os_printf("stopped wifi and wifi scanner\n");
103 #endif
104  // and (re)-connect when last wifi scan is done - wait 6 second before starting
108 #ifdef DEBUG
109  os_printf("scheduled wifi for restart...\n");
110 #endif
111  break;
112 
113  case REBOOT_VIA_EXT_WD:
115 #ifdef DEBUG
116  os_printf("reboot via ext watchdog\n");
117 #endif
118  break;
119  }
120  watchdog_list[i].last_reset = get_uptime();
121  }
122  }
123 }
124 
126  memset(watchdog_list, 0x00, sizeof(watchdog_list));
127  watchdog_list_len = 0;
128 }
129 
131 #ifdef DEBUG
132  os_printf("watchdog started\n");
133 #endif
134  // start configurable software watchdog system
137  os_timer_arm(&watchdog_timer, 1000, 1);
138 
139  // start extern watchdog timer (MCP1316)
142  os_timer_arm(&ext_watchdog_timer, 1000, 1);
143  //Set GPIO16 to output mode
145  gpio16_output_set(1);
146 }
147 
149 #ifdef DEBUG
150  os_printf("watchdog stopped\n");
151 #endif
153 }
154 
155 ICACHE_FLASH_ATTR bool add_watchdog(uint32_t id, watchdog_type_t type, uint32_t timeout) {
157 #ifdef DEBUG
158  os_printf("add watchdog, id: %d\n", id);
159 #endif
160  watchdog_list[watchdog_list_len].id = id;
161  watchdog_list[watchdog_list_len].type = type;
162  watchdog_list[watchdog_list_len].timeout = timeout;
163  watchdog_list[watchdog_list_len].last_reset = get_uptime();
165  return true;
166  }
167  else {
168 #ifdef DEBUG
169  os_printf("watchdog error, cant add more\n");
170 #endif
171  return false;
172  }
173 }
174 
176  if (watchdog_list_len > 0) {
177 #ifdef DEBUG
178  os_printf("remove watchdog, id: %d\n", id);
179 #endif
180  watchdog_list[watchdog_list_len].id = 0;
181  watchdog_list[watchdog_list_len].type = NOT_ENABLED;
182  watchdog_list[watchdog_list_len].timeout = 0;
184  return true;
185  }
186  else {
187 #ifdef DEBUG
188  os_printf("watchdog error, cant remove %d\n", id);
189 #endif
190  return false;
191  }
192 }
193 
195  uint32_t i;
196 
197 #ifdef DEBUG
198  os_printf("reset watchdog, id: %d\n", id);
199 #endif
200  for (i = 0; i < WATCHDOG_MAX; i++) {
201  if (watchdog_list[i].id == id) {
202  watchdog_list[i].last_reset = get_uptime();
203  }
204  }
205 }
void ICACHE_FLASH_ATTR wifi_start_scan()
Definition: wifi.c:570
ICACHE_FLASH_ATTR void led_pattern_b(void)
Definition: led.c:113
void ICACHE_FLASH_ATTR wifi_stop_scan()
Definition: wifi.c:577
ICACHE_FLASH_ATTR static void wifi_reconnect_timer_func(void *arg)
Definition: watchdog.c:34
#define os_timer_t
Definition: os_type.h:34
bool wifi_set_opmode_current(uint8 opmode)
volatile uint32_t timeout
Definition: watchdog.c:18
#define os_timer_disarm
Definition: osapi.h:51
ICACHE_FLASH_ATTR static void watchdog_timer_func(void *arg)
Definition: watchdog.c:72
#define memset(x, a, b)
Definition: platform.h:21
ICACHE_FLASH_ATTR static void ext_watchdog_timer_func(void *arg)
Definition: watchdog.c:25
volatile watchdog_type_t type
Definition: watchdog.c:17
watchdog_type_t
Definition: watchdog.h:8
ICACHE_FLASH_ATTR void stop_watchdog()
Definition: watchdog.c:148
#define NULL_MODE
#define STATIONAP_MODE
#define NULL
Definition: def.h:47
volatile uint32_t id
Definition: watchdog.c:16
volatile uint32_t last_reset
Definition: watchdog.c:19
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
void ICACHE_FLASH_ATTR gpio16_output_conf(void)
Definition: gpio16.c:6
#define os_timer_func_t
Definition: os_type.h:35
void ICACHE_FLASH_ATTR set_my_auto_connect(bool enabled)
Definition: wifi.c:591
ICACHE_FLASH_ATTR uint32_t get_uptime(void)
Definition: unix_time.c:61
#define os_printf
Definition: osapi.h:62
uint8 wifi_get_opmode(void)
#define os_timer_setfn
Definition: osapi.h:52
volatile uint8_t watchdog_list_len
Definition: watchdog.c:23
ICACHE_FLASH_ATTR void reset_watchdog(uint32_t id)
Definition: watchdog.c:194
bool ap_enabled
Definition: config.h:51
#define NETWORK_RESTART_DELAY
Definition: watchdog.h:6
ICACHE_FLASH_ATTR bool remove_watchdog(uint32_t id)
Definition: watchdog.c:175
void ICACHE_FLASH_ATTR gpio16_output_set(uint8 value)
Definition: gpio16.c:19
void system_restart(void)
bool ICACHE_FLASH_ATTR wifi_scan_is_running()
Definition: wifi.c:583
static os_timer_t watchdog_timer
Definition: watchdog.c:11
static os_timer_t ext_watchdog_timer
Definition: watchdog.c:12
uint8 ICACHE_FLASH_ATTR gpio16_input_get(void)
Definition: gpio16.c:39
ICACHE_FLASH_ATTR void start_watchdog()
Definition: watchdog.c:130
watchdog_t watchdog_list[WATCHDOG_MAX]
Definition: watchdog.c:22
bool wifi_station_disconnect(void)
#define STATION_MODE
#define os_timer_arm(a, b, c)
Definition: osapi.h:50
#define WATCHDOG_MAX
Definition: watchdog.h:1
ICACHE_FLASH_ATTR void led_stop_pattern(void)
Definition: led.c:128
ICACHE_FLASH_ATTR bool add_watchdog(uint32_t id, watchdog_type_t type, uint32_t timeout)
Definition: watchdog.c:155
ICACHE_FLASH_ATTR void init_watchdog()
Definition: watchdog.c:125
syscfg_t sys_cfg
Definition: config.c:12
bool wifi_station_connect(void)
static os_timer_t wifi_reconnect_timer
Definition: watchdog.c:13