|
| 1 | +/* |
| 2 | + * libtime.h |
| 3 | + * |
| 4 | + */ |
| 5 | + |
| 6 | +#ifndef INC_LIBTIME_H_ |
| 7 | +#define INC_LIBTIME_H_ |
| 8 | + |
| 9 | +#define CLKSYNC_SECONDS(x) x |
| 10 | +#define CLKSYNC_MINUTE(x) (x * 60) |
| 11 | + |
| 12 | +/** |
| 13 | + * Clock status |
| 14 | + */ |
| 15 | +enum clkstatus_e { |
| 16 | + CLK_STATUS_NOTSYNCED,/**< System clock not synced */ |
| 17 | + CLK_STATUS_SYNCED /**< System clock synced */ |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * Clock Source priority, for multiple clock sources, |
| 22 | + * highest priority source is selected for sync |
| 23 | + * |
| 24 | + * Following are the library provided clock sources and their |
| 25 | + * priorities: |
| 26 | + * # Network Provider Clock : Low Priority |
| 27 | + * # NTP : Medium Priority |
| 28 | + * # GPS : High Priority |
| 29 | + */ |
| 30 | +enum clkprio_e { |
| 31 | + CLK_SRC_PRIO_VLOW, /**< Priority very low */ |
| 32 | + CLK_SRC_PRIO_LOW, /**< Priority low */ |
| 33 | + CLK_SRC_PRIO_MED, /**< Priority medium */ |
| 34 | + CLK_SRC_PRIO_HIGH, /**< Priority high */ |
| 35 | + CLK_SRC_PRIO_VHIGH, /**< Priority Very High */ |
| 36 | +}; |
| 37 | + |
| 38 | +/** |
| 39 | + * Clock synchronization mode select |
| 40 | + */ |
| 41 | +enum clkmode_e { |
| 42 | + CLOCK_MODE_AUTO, /**< Auto Sync */ |
| 43 | + CLOCK_MODE_MANUAL,/**< Manual Sync */ |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * Clock Source Structure |
| 48 | + */ |
| 49 | +struct clocksrc_t { |
| 50 | + const char *name; /**< Clock Source name */ |
| 51 | + int prio; /**< Clock source priority @a clockprio_e */ |
| 52 | + int sync_duration; /**< Clock sync duration in seconds */ |
| 53 | + int (*sync_cb)(void); /**< Clock source sync callback function */ |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * Time structure for sync |
| 58 | + */ |
| 59 | +struct clocktm_t { |
| 60 | + int year; /**< Year from 0 */ |
| 61 | + int month; /**< Month 1 to 12 */ |
| 62 | + int day; /**< Day 1 to 31 */ |
| 63 | + int hour; /**< Hour 0 to 23 */ |
| 64 | + int minute; /**< Minute 0 to 59 */ |
| 65 | + int second; /**< Second 0 to 59 */ |
| 66 | + int reserved; /**< reserved must be 0 */ |
| 67 | +}; |
| 68 | + |
| 69 | +/* Clock source API */ |
| 70 | +/** |
| 71 | + * Register clock sync source, clock is considered invalid by default |
| 72 | + * |
| 73 | + * @param src [in] Clock source structure pointer @a clocksrc_t |
| 74 | + * @return 0 on success, negative value on error |
| 75 | + */ |
| 76 | +int clock_src_register(const struct clocksrc_t *src); |
| 77 | + |
| 78 | +/** |
| 79 | + * Set clock source as valid, after clock source is marked valid |
| 80 | + * system will call @a clocksrc_t::sync_cb after 30s. |
| 81 | + * @param name [in] Clock source name |
| 82 | + * @param status [in] 1 for valid, 0 for invalid |
| 83 | + * @return 0 on success, negative on error |
| 84 | + */ |
| 85 | +int clock_src_setvalid(const char *name, int status); |
| 86 | + |
| 87 | +/** |
| 88 | + * Get clock source valid status |
| 89 | + * @param name [in] Clock source name |
| 90 | + * @return returns 1 if valid, 0 if not valid |
| 91 | + */ |
| 92 | +int clock_src_getvalid(const char *name); |
| 93 | + |
| 94 | +/** |
| 95 | + * Get currently used clock source name for system |
| 96 | + * clock synchronization |
| 97 | + * @return returns clock source name |
| 98 | + */ |
| 99 | +const char *clock_getsourcename(void); |
| 100 | + |
| 101 | +/** |
| 102 | + * Get system clock sync status |
| 103 | + * @return returns @a clkstatus_e |
| 104 | + */ |
| 105 | +int clock_get_syncstatus(void); |
| 106 | + |
| 107 | +/** |
| 108 | + * Set ntp server address, default is "siwi.in" |
| 109 | + * @param server [in] NTP server domain or IP |
| 110 | + * @return return 0 on success, negative on error |
| 111 | + */ |
| 112 | +int clock_set_ntpserver(const char *server); |
| 113 | + |
| 114 | +/** |
| 115 | + * Get currently configured NTP server address |
| 116 | + * @return returns NTP server address |
| 117 | + */ |
| 118 | +const char *clock_get_ntpserver(void); |
| 119 | + |
| 120 | +/** |
| 121 | + * Set clock synchronization mode |
| 122 | + * @param mode [in] Clock mode @a clkmode_e |
| 123 | + * @return 0 on success, negative on failure |
| 124 | + */ |
| 125 | +int clock_set_mode(int mode); |
| 126 | + |
| 127 | +/** |
| 128 | + * Get current clock synchronization mode |
| 129 | + * @return returns @a clockmode_e |
| 130 | + */ |
| 131 | +int clock_get_mode(void); |
| 132 | + |
| 133 | +/** |
| 134 | + * Update system clock, this function can be called from |
| 135 | + * clock source sync callback to update system time. |
| 136 | + * @param timeval [in] Time structure with current time information |
| 137 | + * @return return 0 on success, negative on failure |
| 138 | + */ |
| 139 | +int clock_sync(struct clocktm_t *timeval); |
| 140 | + |
| 141 | +#endif /* INC_LIBTIME_H_ */ |
0 commit comments