blob: 0771960872576842934f2fd9723118674f7dcae4 [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 Marques0ef36d82010-03-11 14:47:42 -030033static int pim_gettime(int clk_id, struct timeval *tv)
Everton Marques871dbcf2009-08-11 15:43:05 -030034{
Everton Marques0ef36d82010-03-11 14:47:42 -030035 struct timespec ts;
Everton Marquesf9e05e52010-03-11 11:17:33 -030036 int result;
37
Everton Marques0ef36d82010-03-11 14:47:42 -030038#ifdef PIM_USE_QUAGGA_GETTIME
39 result = quagga_gettime(clk_id, tv);
Everton Marquesf9e05e52010-03-11 11:17:33 -030040 if (result) {
Everton Marques0ef36d82010-03-11 14:47:42 -030041 zlog_err("%s: quagga_gettime(clk_id=%d) failure: errno=%d: %s",
42 __PRETTY_FUNCTION__, clk_id,
Everton Marquesf9e05e52010-03-11 11:17:33 -030043 errno, safe_strerror(errno));
44 }
Everton Marques0ef36d82010-03-11 14:47:42 -030045#else
46 result = clock_gettime(clk_id, &ts);
47 if (result) {
48 zlog_err("%s: clock_gettime(clk_id=%d) failure: errno=%d: %s",
49 __PRETTY_FUNCTION__, clk_id,
50 errno, safe_strerror(errno));
51 return result;
52 }
53 if (tv) {
54 tv->tv_sec = ts.tv_sec;
55 tv->tv_usec = 1000 * ts.tv_nsec;
56 }
57#endif
Everton Marquesf9e05e52010-03-11 11:17:33 -030058
59 return result;
60}
61
62static int gettime_monotonic(struct timeval *tv)
63{
64 int result;
65
Everton Marques0ef36d82010-03-11 14:47:42 -030066#ifdef PIM_GETTIME_USE_GETTIMEOFDAY
67 result = gettimeofday(tv, 0);
68 if (result) {
69 zlog_err("%s: gettimeofday() failure: errno=%d: %s",
70 __PRETTY_FUNCTION__,
71 errno, safe_strerror(errno));
72 }
73#elif defined(PIM_USE_QUAGGA_GETTIME)
Everton Marquesf9e05e52010-03-11 11:17:33 -030074 result = pim_gettime(QUAGGA_CLK_MONOTONIC, tv);
75 if (result) {
76 zlog_err("%s: pim_gettime(QUAGGA_CLK_MONOTONIC=%d) failure: errno=%d: %s",
Everton Marques0ef36d82010-03-11 14:47:42 -030077 __PRETTY_FUNCTION__, QUAGGA_CLK_MONOTONIC,
78 errno, safe_strerror(errno));
79 }
80#else
81 result = pim_gettime(CLOCK_MONOTONIC, tv);
82 if (result) {
83 zlog_err("%s: pim_gettime(CLOCK_MONOTONIC=%d) failure: errno=%d: %s",
Everton Marquesf9e05e52010-03-11 11:17:33 -030084 __PRETTY_FUNCTION__, CLOCK_MONOTONIC,
85 errno, safe_strerror(errno));
86 }
Everton Marques0ef36d82010-03-11 14:47:42 -030087#endif
Everton Marquesf9e05e52010-03-11 11:17:33 -030088
89 return result;
Everton Marques871dbcf2009-08-11 15:43:05 -030090}
91
92/*
93 pim_time_monotonic_sec():
94 number of seconds since some unspecified starting point
95*/
96int64_t pim_time_monotonic_sec()
97{
98 struct timeval now_tv;
99
Everton Marquesf9e05e52010-03-11 11:17:33 -0300100 if (gettime_monotonic(&now_tv)) {
101 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
Everton Marques871dbcf2009-08-11 15:43:05 -0300102 __PRETTY_FUNCTION__,
Everton Marquese96f0af2009-08-11 15:48:02 -0300103 errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300104 return -1;
105 }
106
107 return now_tv.tv_sec;
108}
109
110/*
111 pim_time_monotonic_dsec():
112 number of deciseconds since some unspecified starting point
113*/
114int64_t pim_time_monotonic_dsec()
115{
116 struct timeval now_tv;
117 int64_t now_dsec;
118
Everton Marquesf9e05e52010-03-11 11:17:33 -0300119 if (gettime_monotonic(&now_tv)) {
120 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
Everton Marques871dbcf2009-08-11 15:43:05 -0300121 __PRETTY_FUNCTION__,
Everton Marquese96f0af2009-08-11 15:48:02 -0300122 errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300123 return -1;
124 }
125
126 now_dsec = ((int64_t) now_tv.tv_sec) * 10 + ((int64_t) now_tv.tv_usec) / 100000;
127
128 return now_dsec;
129}
130
131int pim_time_mmss(char *buf, int buf_size, long sec)
132{
133 long mm;
134 int wr;
135
136 zassert(buf_size >= 5);
137
138 mm = sec / 60;
139 sec %= 60;
140
141 wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec);
142
143 return wr != 8;
144}
145
146static int pim_time_hhmmss(char *buf, int buf_size, long sec)
147{
148 long hh;
149 long mm;
150 int wr;
151
152 zassert(buf_size >= 8);
153
154 hh = sec / 3600;
155 sec %= 3600;
156 mm = sec / 60;
157 sec %= 60;
158
159 wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec);
160
161 return wr != 8;
162}
163
164void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer)
165{
166 if (t_timer) {
167 pim_time_mmss(buf, buf_size,
168 thread_timer_remain_second(t_timer));
169 }
170 else {
171 snprintf(buf, buf_size, "--:--");
172 }
173}
174
175void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer)
176{
177 if (t_timer) {
178 pim_time_hhmmss(buf, buf_size,
179 thread_timer_remain_second(t_timer));
180 }
181 else {
182 snprintf(buf, buf_size, "--:--:--");
183 }
184}
185
186void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec)
187{
188 zassert(buf_size >= 8);
189
190 pim_time_hhmmss(buf, buf_size, uptime_sec);
191}
192
193long pim_time_timer_remain_msec(struct thread *t_timer)
194{
195 /* FIXME: Actually fetch msec resolution from thread */
196
197 /* no timer thread running means timer has expired: return 0 */
198
199 return t_timer ?
200 1000 * thread_timer_remain_second(t_timer) :
201 0;
202}