1 | //time.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2012 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | #ifdef ROAR_HAVE_H_SYS_TIME |
---|
39 | #include <sys/time.h> |
---|
40 | #endif |
---|
41 | #ifdef ROAR_HAVE_H_TIME |
---|
42 | #include <time.h> |
---|
43 | #endif |
---|
44 | |
---|
45 | int roar_time_to_msg(struct roar_message * mes, struct roar_time * time) { |
---|
46 | uint64_t * data; |
---|
47 | |
---|
48 | if ( mes == NULL || time == NULL ) { |
---|
49 | roar_err_set(ROAR_ERROR_FAULT); |
---|
50 | return -1; |
---|
51 | } |
---|
52 | |
---|
53 | memset(mes, 0, sizeof(struct roar_message)); |
---|
54 | |
---|
55 | data = (uint64_t*)mes->data; |
---|
56 | |
---|
57 | data[0] = ROAR_HOST2NET64(0); |
---|
58 | data[1] = ROAR_HOST2NET64(time->t_sec); |
---|
59 | data[2] = ROAR_HOST2NET64(time->t_ssec); |
---|
60 | data[3] = ROAR_HOST2NET64(time->c_freq); |
---|
61 | data[4] = ROAR_HOST2NET64(time->c_drift); |
---|
62 | |
---|
63 | mes->datalen = 5*8; |
---|
64 | |
---|
65 | if ( time->c_drift == 0 ) { |
---|
66 | mes->datalen -= 8; |
---|
67 | if ( time->c_freq == 0 ) { |
---|
68 | mes->datalen -= 8; |
---|
69 | if ( time->t_ssec == 0 ) { |
---|
70 | mes->datalen -= 8; |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | return 0; |
---|
76 | } |
---|
77 | |
---|
78 | int roar_time_from_msg(struct roar_time * time, struct roar_message * mes) { |
---|
79 | uint64_t * data; |
---|
80 | |
---|
81 | if ( mes == NULL || time == NULL ) { |
---|
82 | roar_err_set(ROAR_ERROR_FAULT); |
---|
83 | return -1; |
---|
84 | } |
---|
85 | |
---|
86 | data = (uint64_t*)mes->data; |
---|
87 | |
---|
88 | if ( mes->datalen < 8 ) { |
---|
89 | roar_err_set(ROAR_ERROR_BADMSG); |
---|
90 | return -1; |
---|
91 | } |
---|
92 | |
---|
93 | if ( mes->datalen % 8 ) { |
---|
94 | roar_err_set(ROAR_ERROR_BADMSG); |
---|
95 | return -1; |
---|
96 | } |
---|
97 | |
---|
98 | if ( ROAR_NET2HOST64(data[0]) != 0 ) { |
---|
99 | roar_err_set(ROAR_ERROR_NSVERSION); |
---|
100 | return -1; |
---|
101 | } |
---|
102 | |
---|
103 | memset(time, 0, sizeof(struct roar_time)); |
---|
104 | |
---|
105 | if ( mes->datalen >= 16 ) { |
---|
106 | time->t_sec = ROAR_NET2HOST64(data[1]); |
---|
107 | if ( mes->datalen >= 24 ) { |
---|
108 | time->t_ssec = ROAR_NET2HOST64(data[2]); |
---|
109 | if ( mes->datalen >= 32 ) { |
---|
110 | time->c_freq = ROAR_NET2HOST64(data[3]); |
---|
111 | if ( mes->datalen >= 40 ) { |
---|
112 | time->c_drift = ROAR_NET2HOST64(data[4]); |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | return 0; |
---|
119 | } |
---|
120 | |
---|
121 | int roar_get_time (struct roar_connection * con, struct roar_time * time) { |
---|
122 | struct roar_message mes; |
---|
123 | |
---|
124 | if ( con == NULL || time == NULL ) { |
---|
125 | roar_err_set(ROAR_ERROR_FAULT); |
---|
126 | return -1; |
---|
127 | } |
---|
128 | |
---|
129 | memset(&mes, 0, sizeof(mes)); |
---|
130 | |
---|
131 | mes.cmd = ROAR_CMD_GETTIMEOFDAY; |
---|
132 | mes.stream = -1; |
---|
133 | mes.datalen = 8; /* 64 bit */ |
---|
134 | // mes.data is already cleared. |
---|
135 | |
---|
136 | if ( roar_req(con, &mes, NULL) == -1 ) |
---|
137 | return -1; |
---|
138 | |
---|
139 | if ( mes.cmd != ROAR_CMD_OK ) { |
---|
140 | return -1; |
---|
141 | } |
---|
142 | |
---|
143 | if ( roar_time_from_msg(time, &mes) == -1 ) |
---|
144 | return -1; |
---|
145 | |
---|
146 | return 0; |
---|
147 | } |
---|
148 | |
---|
149 | int roar_clock_gettime(struct roar_time * rt, int clock) { |
---|
150 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
151 | struct timeval tv; |
---|
152 | #elif defined(ROAR_HAVE_TIME) |
---|
153 | time_t now; |
---|
154 | #endif |
---|
155 | |
---|
156 | ROAR_DBG("roar_clock_gettime(rt=%p, clock=%i) = ?", rt, clock); |
---|
157 | |
---|
158 | if ( rt == NULL ) { |
---|
159 | roar_err_set(ROAR_ERROR_FAULT); |
---|
160 | return -1; |
---|
161 | } |
---|
162 | |
---|
163 | memset(rt, 0, sizeof(struct roar_time)); |
---|
164 | |
---|
165 | if ( clock == ROAR_CLOCK_DEFAULT ) |
---|
166 | clock = ROAR_CLOCK_REALTIME; |
---|
167 | |
---|
168 | switch (clock) { |
---|
169 | case ROAR_CLOCK_REALTIME: |
---|
170 | ROAR_DBG("roar_clock_gettime(rt=%p, clock=(%i)ROAR_CLOCK_REALTIME) = ?", rt, clock); |
---|
171 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
172 | if ( gettimeofday(&tv, NULL) == -1 ) { |
---|
173 | roar_err_from_errno(); |
---|
174 | ROAR_DBG("roar_clock_gettime(rt=%p, clock=(%i)ROAR_CLOCK_REALTIME) = -1 //error=%i", rt, clock, roar_error); |
---|
175 | return -1; |
---|
176 | } |
---|
177 | rt->t_sec = tv.tv_sec; |
---|
178 | rt->t_ssec = (uint64_t)tv.tv_usec * (uint64_t)18446744073709ULL; |
---|
179 | rt->c_freq = 1000000LLU*1000000000LLU; |
---|
180 | #elif defined(ROAR_HAVE_TIME) |
---|
181 | now = time(NULL); |
---|
182 | rt->t_sec = now; |
---|
183 | rt->c_freq = 1000000000LLU; |
---|
184 | #else |
---|
185 | ROAR_DBG("roar_clock_gettime(rt=%p, clock=(%i)ROAR_CLOCK_REALTIME) = -1 // error=NOTSUP", rt, clock); |
---|
186 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
187 | return -1; |
---|
188 | #endif |
---|
189 | ROAR_DBG("roar_clock_gettime(rt=%p, clock=(%i)ROAR_CLOCK_REALTIME) = 0", rt, clock); |
---|
190 | return 0; |
---|
191 | break; |
---|
192 | } |
---|
193 | |
---|
194 | ROAR_DBG("roar_clock_gettime(rt=%p, clock=%i) = -1 // error=NOTSUP", rt, clock); |
---|
195 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
196 | return -1; |
---|
197 | } |
---|
198 | |
---|
199 | //ll |
---|