MeterLogger
Functions | Variables
unix_time.c File Reference
#include <esp8266.h>
#include "debug.h"
#include "unix_time.h"
#include <sntp.h>
Include dependency graph for unix_time.c:

Go to the source code of this file.

Functions

ICACHE_FLASH_ATTR static void sntp_check_timer_func (void *arg)
 
ICACHE_FLASH_ATTR static void ntp_offline_second_counter_timer_func (void *arg)
 
ICACHE_FLASH_ATTR void init_unix_time (void)
 
ICACHE_FLASH_ATTR uint32_t get_unix_time (void)
 
ICACHE_FLASH_ATTR uint32_t get_uptime (void)
 

Variables

static os_timer_t sntp_check_timer
 
uint32_t init_time = 0
 
uint32_t current_unix_time
 
uint32_t ntp_offline_second_counter = 0
 
uint64_t boot_time
 
static os_timer_t ntp_offline_second_counter_timer
 

Function Documentation

◆ get_unix_time()

ICACHE_FLASH_ATTR uint32_t get_unix_time ( void  )

Definition at line 55 of file unix_time.c.

References current_unix_time, and sntp_get_current_timestamp().

Referenced by en61107_received_task(), en61107_request_send(), kmp_received_task(), kmp_request_send(), and minute_timer_func().

55  {
57 
58  return current_unix_time;
59 }
uint32 sntp_get_current_timestamp()
uint32_t current_unix_time
Definition: unix_time.c:9
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_uptime()

ICACHE_FLASH_ATTR uint32_t get_uptime ( void  )

Definition at line 61 of file unix_time.c.

References current_unix_time, init_time, ntp_offline_second_counter, and sntp_get_current_timestamp().

Referenced by add_watchdog(), reset_watchdog(), and watchdog_timer_func().

61  {
63  if (init_time == 0) {
65  }
66  else {
68  }
69 }
uint32 sntp_get_current_timestamp()
uint32_t init_time
Definition: unix_time.c:8
uint32_t ntp_offline_second_counter
Definition: unix_time.c:10
uint32_t current_unix_time
Definition: unix_time.c:9
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_unix_time()

ICACHE_FLASH_ATTR void init_unix_time ( void  )

Definition at line 37 of file unix_time.c.

References ntp_offline_second_counter, ntp_offline_second_counter_timer, ntp_offline_second_counter_timer_func(), NULL, os_timer_arm, os_timer_disarm, os_timer_func_t, os_timer_setfn, sntp_check_timer, sntp_check_timer_func(), sntp_init(), sntp_set_timezone(), and sntp_setservername().

37  {
38  // init sntp
39  sntp_setservername(0, "dk.pool.ntp.org"); // set server 0 by domain name
40  sntp_setservername(1, "us.pool.ntp.org"); // set server 1 by domain name
41  sntp_set_timezone(0); // UTC time
42  sntp_init();
43 
44  // start timer to make sure we go ntp reply
47  os_timer_arm(&sntp_check_timer, 2000, 0);
48 
52  os_timer_arm(&ntp_offline_second_counter_timer, 1000, 1); // every seconds
53 }
static os_timer_t ntp_offline_second_counter_timer
Definition: unix_time.c:14
void sntp_init(void)
#define os_timer_disarm
Definition: osapi.h:51
#define NULL
Definition: def.h:47
#define os_timer_func_t
Definition: os_type.h:35
ICACHE_FLASH_ATTR static void ntp_offline_second_counter_timer_func(void *arg)
Definition: unix_time.c:33
#define os_timer_setfn
Definition: osapi.h:52
ICACHE_FLASH_ATTR static void sntp_check_timer_func(void *arg)
Definition: unix_time.c:16
void sntp_setservername(u8_t idx, char *server)
static os_timer_t sntp_check_timer
Definition: unix_time.c:6
#define os_timer_arm(a, b, c)
Definition: osapi.h:50
uint32_t ntp_offline_second_counter
Definition: unix_time.c:10
bool sntp_set_timezone(sint8 timezone)
Here is the call graph for this function:

◆ ntp_offline_second_counter_timer_func()

ICACHE_FLASH_ATTR static void ntp_offline_second_counter_timer_func ( void *  arg)
static

Definition at line 33 of file unix_time.c.

References ntp_offline_second_counter.

Referenced by init_unix_time().

33  {
35 }
uint32_t ntp_offline_second_counter
Definition: unix_time.c:10
Here is the caller graph for this function:

◆ sntp_check_timer_func()

ICACHE_FLASH_ATTR static void sntp_check_timer_func ( void *  arg)
static

Definition at line 16 of file unix_time.c.

References current_unix_time, init_time, ntp_offline_second_counter_timer, os_timer_arm, os_timer_disarm, sntp_check_timer, and sntp_get_current_timestamp().

Referenced by init_unix_time().

16  {
18 
19  if (current_unix_time == 0) {
21  os_timer_arm(&sntp_check_timer, 2000, 0);
22  } else {
24  // save init time for use in get_uptime()
25  if (init_time == 0) { // only set init_time at boot
26  os_timer_disarm(&ntp_offline_second_counter_timer); // stop offline second counter
27 
29  }
30  }
31 }
static os_timer_t ntp_offline_second_counter_timer
Definition: unix_time.c:14
#define os_timer_disarm
Definition: osapi.h:51
uint32 sntp_get_current_timestamp()
uint32_t init_time
Definition: unix_time.c:8
static os_timer_t sntp_check_timer
Definition: unix_time.c:6
#define os_timer_arm(a, b, c)
Definition: osapi.h:50
uint32_t current_unix_time
Definition: unix_time.c:9
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ boot_time

uint64_t boot_time

Definition at line 12 of file unix_time.c.

◆ current_unix_time

uint32_t current_unix_time

◆ init_time

uint32_t init_time = 0

Definition at line 8 of file unix_time.c.

Referenced by get_uptime(), and sntp_check_timer_func().

◆ ntp_offline_second_counter

uint32_t ntp_offline_second_counter = 0

Definition at line 10 of file unix_time.c.

Referenced by get_uptime(), init_unix_time(), and ntp_offline_second_counter_timer_func().

◆ ntp_offline_second_counter_timer

os_timer_t ntp_offline_second_counter_timer
static

Definition at line 14 of file unix_time.c.

Referenced by init_unix_time(), and sntp_check_timer_func().

◆ sntp_check_timer

os_timer_t sntp_check_timer
static

Definition at line 6 of file unix_time.c.

Referenced by init_unix_time(), and sntp_check_timer_func().