Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 1 | /* |
| 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 | |
| 33 | /* |
| 34 | see man clock_gettime |
| 35 | */ |
| 36 | static int pim_gettime(clockid_t clk_id, struct timeval *tv) |
| 37 | { |
| 38 | return quagga_gettime(clk_id, tv); |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | pim_time_monotonic_sec(): |
| 43 | number of seconds since some unspecified starting point |
| 44 | */ |
| 45 | int64_t pim_time_monotonic_sec() |
| 46 | { |
| 47 | struct timeval now_tv; |
| 48 | |
| 49 | if (pim_gettime(CLOCK_MONOTONIC, &now_tv)) { |
| 50 | zlog_err("%s: gettime(CLOCK_MONOTONIC) failure: errno=%d: %s", |
| 51 | __PRETTY_FUNCTION__, |
Everton Marques | e96f0af | 2009-08-11 15:48:02 -0300 | [diff] [blame^] | 52 | errno, safe_strerror(errno)); |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | return now_tv.tv_sec; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | pim_time_monotonic_dsec(): |
| 61 | number of deciseconds since some unspecified starting point |
| 62 | */ |
| 63 | int64_t pim_time_monotonic_dsec() |
| 64 | { |
| 65 | struct timeval now_tv; |
| 66 | int64_t now_dsec; |
| 67 | |
| 68 | if (pim_gettime(CLOCK_MONOTONIC, &now_tv)) { |
| 69 | zlog_err("%s: gettime(CLOCK_MONOTONIC) failure: errno=%d: %s", |
| 70 | __PRETTY_FUNCTION__, |
Everton Marques | e96f0af | 2009-08-11 15:48:02 -0300 | [diff] [blame^] | 71 | errno, safe_strerror(errno)); |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | now_dsec = ((int64_t) now_tv.tv_sec) * 10 + ((int64_t) now_tv.tv_usec) / 100000; |
| 76 | |
| 77 | return now_dsec; |
| 78 | } |
| 79 | |
| 80 | int pim_time_mmss(char *buf, int buf_size, long sec) |
| 81 | { |
| 82 | long mm; |
| 83 | int wr; |
| 84 | |
| 85 | zassert(buf_size >= 5); |
| 86 | |
| 87 | mm = sec / 60; |
| 88 | sec %= 60; |
| 89 | |
| 90 | wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec); |
| 91 | |
| 92 | return wr != 8; |
| 93 | } |
| 94 | |
| 95 | static int pim_time_hhmmss(char *buf, int buf_size, long sec) |
| 96 | { |
| 97 | long hh; |
| 98 | long mm; |
| 99 | int wr; |
| 100 | |
| 101 | zassert(buf_size >= 8); |
| 102 | |
| 103 | hh = sec / 3600; |
| 104 | sec %= 3600; |
| 105 | mm = sec / 60; |
| 106 | sec %= 60; |
| 107 | |
| 108 | wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec); |
| 109 | |
| 110 | return wr != 8; |
| 111 | } |
| 112 | |
| 113 | void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer) |
| 114 | { |
| 115 | if (t_timer) { |
| 116 | pim_time_mmss(buf, buf_size, |
| 117 | thread_timer_remain_second(t_timer)); |
| 118 | } |
| 119 | else { |
| 120 | snprintf(buf, buf_size, "--:--"); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer) |
| 125 | { |
| 126 | if (t_timer) { |
| 127 | pim_time_hhmmss(buf, buf_size, |
| 128 | thread_timer_remain_second(t_timer)); |
| 129 | } |
| 130 | else { |
| 131 | snprintf(buf, buf_size, "--:--:--"); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec) |
| 136 | { |
| 137 | zassert(buf_size >= 8); |
| 138 | |
| 139 | pim_time_hhmmss(buf, buf_size, uptime_sec); |
| 140 | } |
| 141 | |
| 142 | long pim_time_timer_remain_msec(struct thread *t_timer) |
| 143 | { |
| 144 | /* FIXME: Actually fetch msec resolution from thread */ |
| 145 | |
| 146 | /* no timer thread running means timer has expired: return 0 */ |
| 147 | |
| 148 | return t_timer ? |
| 149 | 1000 * thread_timer_remain_second(t_timer) : |
| 150 | 0; |
| 151 | } |