blob: a4b1cc466c6439a20073859d0017edf9d91459ba [file] [log] [blame]
Everton Marques871dbcf2009-08-11 15:43:05 -03001/*
2 PIM for Quagga
3 Copyright (C) 2008 Everton da Silva Marques
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 MA 02110-1301 USA
19
20 $QuaggaId: $Format:%an, %ai, %h$ $
21*/
22
23#include <string.h>
24#include <sys/time.h>
25#include <time.h>
26
27#include <zebra.h>
28#include "log.h"
29#include "thread.h"
30
31#include "pim_time.h"
32
Everton Marquesf9e05e52010-03-11 11:17:33 -030033static int pim_gettime(enum quagga_clkid clkid, struct timeval *tv)
Everton Marques871dbcf2009-08-11 15:43:05 -030034{
Everton Marquesf9e05e52010-03-11 11:17:33 -030035 int result;
36
37 result = quagga_gettime(clkid, tv);
38 if (result) {
39 zlog_err("%s: quagga_gettime(clkid=%d) failure: errno=%d: %s",
40 __PRETTY_FUNCTION__, clkid,
41 errno, safe_strerror(errno));
42 }
43
44 return result;
45}
46
47static int gettime_monotonic(struct timeval *tv)
48{
49 int result;
50
51 result = pim_gettime(QUAGGA_CLK_MONOTONIC, tv);
52 if (result) {
53 zlog_err("%s: pim_gettime(QUAGGA_CLK_MONOTONIC=%d) failure: errno=%d: %s",
54 __PRETTY_FUNCTION__, CLOCK_MONOTONIC,
55 errno, safe_strerror(errno));
56 }
57
58 return result;
Everton Marques871dbcf2009-08-11 15:43:05 -030059}
60
61/*
62 pim_time_monotonic_sec():
63 number of seconds since some unspecified starting point
64*/
65int64_t pim_time_monotonic_sec()
66{
67 struct timeval now_tv;
68
Everton Marquesf9e05e52010-03-11 11:17:33 -030069 if (gettime_monotonic(&now_tv)) {
70 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
Everton Marques871dbcf2009-08-11 15:43:05 -030071 __PRETTY_FUNCTION__,
Everton Marquese96f0af2009-08-11 15:48:02 -030072 errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -030073 return -1;
74 }
75
76 return now_tv.tv_sec;
77}
78
79/*
80 pim_time_monotonic_dsec():
81 number of deciseconds since some unspecified starting point
82*/
83int64_t pim_time_monotonic_dsec()
84{
85 struct timeval now_tv;
86 int64_t now_dsec;
87
Everton Marquesf9e05e52010-03-11 11:17:33 -030088 if (gettime_monotonic(&now_tv)) {
89 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
Everton Marques871dbcf2009-08-11 15:43:05 -030090 __PRETTY_FUNCTION__,
Everton Marquese96f0af2009-08-11 15:48:02 -030091 errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -030092 return -1;
93 }
94
95 now_dsec = ((int64_t) now_tv.tv_sec) * 10 + ((int64_t) now_tv.tv_usec) / 100000;
96
97 return now_dsec;
98}
99
100int pim_time_mmss(char *buf, int buf_size, long sec)
101{
102 long mm;
103 int wr;
104
105 zassert(buf_size >= 5);
106
107 mm = sec / 60;
108 sec %= 60;
109
110 wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec);
111
112 return wr != 8;
113}
114
115static int pim_time_hhmmss(char *buf, int buf_size, long sec)
116{
117 long hh;
118 long mm;
119 int wr;
120
121 zassert(buf_size >= 8);
122
123 hh = sec / 3600;
124 sec %= 3600;
125 mm = sec / 60;
126 sec %= 60;
127
128 wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec);
129
130 return wr != 8;
131}
132
133void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer)
134{
135 if (t_timer) {
136 pim_time_mmss(buf, buf_size,
137 thread_timer_remain_second(t_timer));
138 }
139 else {
140 snprintf(buf, buf_size, "--:--");
141 }
142}
143
144void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer)
145{
146 if (t_timer) {
147 pim_time_hhmmss(buf, buf_size,
148 thread_timer_remain_second(t_timer));
149 }
150 else {
151 snprintf(buf, buf_size, "--:--:--");
152 }
153}
154
155void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec)
156{
157 zassert(buf_size >= 8);
158
159 pim_time_hhmmss(buf, buf_size, uptime_sec);
160}
161
162long pim_time_timer_remain_msec(struct thread *t_timer)
163{
164 /* FIXME: Actually fetch msec resolution from thread */
165
166 /* no timer thread running means timer has expired: return 0 */
167
168 return t_timer ?
169 1000 * thread_timer_remain_second(t_timer) :
170 0;
171}