forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsproc.c
239 lines (198 loc) · 4.95 KB
/
tsproc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* @file tsproc.c
* @note Copyright (C) 2015 Miroslav Lichvar <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdlib.h>
#include <inttypes.h>
#include "tsproc.h"
#include "filter.h"
#include "print.h"
struct tsproc {
/* Processing options */
enum tsproc_mode mode;
/* Current ratio between remote and local clock frequency */
double clock_rate_ratio;
/* Latest down measurement */
tmv_t t1;
tmv_t t2;
/* Latest up measurement */
tmv_t t3;
tmv_t t4;
/* Current filtered delay */
tmv_t filtered_delay;
int filtered_delay_valid;
/* Delay filter */
struct filter *delay_filter;
};
static int weighting(struct tsproc *tsp)
{
switch (tsp->mode) {
case TSPROC_FILTER:
case TSPROC_RAW:
return 0;
case TSPROC_FILTER_WEIGHT:
case TSPROC_RAW_WEIGHT:
return 1;
}
return 0;
}
struct tsproc *tsproc_create(enum tsproc_mode mode,
enum filter_type delay_filter, int filter_length)
{
struct tsproc *tsp;
tsp = calloc(1, sizeof(*tsp));
if (!tsp)
return NULL;
switch (mode) {
case TSPROC_FILTER:
case TSPROC_RAW:
case TSPROC_FILTER_WEIGHT:
case TSPROC_RAW_WEIGHT:
tsp->mode = mode;
break;
default:
free(tsp);
return NULL;
}
tsp->delay_filter = filter_create(delay_filter, filter_length);
if (!tsp->delay_filter) {
free(tsp);
return NULL;
}
tsp->clock_rate_ratio = 1.0;
return tsp;
}
void tsproc_destroy(struct tsproc *tsp)
{
filter_destroy(tsp->delay_filter);
free(tsp);
}
void tsproc_down_ts(struct tsproc *tsp, tmv_t remote_ts, tmv_t local_ts)
{
tsp->t1 = remote_ts;
tsp->t2 = local_ts;
}
void tsproc_up_ts(struct tsproc *tsp, tmv_t local_ts, tmv_t remote_ts)
{
tsp->t3 = local_ts;
tsp->t4 = remote_ts;
}
void tsproc_set_clock_rate_ratio(struct tsproc *tsp, double clock_rate_ratio)
{
tsp->clock_rate_ratio = clock_rate_ratio;
}
void tsproc_set_delay(struct tsproc *tsp, tmv_t delay)
{
tsp->filtered_delay = delay;
tsp->filtered_delay_valid = 1;
}
tmv_t get_raw_delay(struct tsproc *tsp)
{
tmv_t t23, t41, delay;
/* delay = ((t2 - t3) * rr + (t4 - t1)) / 2 */
t23 = tmv_sub(tsp->t2, tsp->t3);
if (tsp->clock_rate_ratio != 1.0)
t23 = dbl_tmv(tmv_dbl(t23) * tsp->clock_rate_ratio);
t41 = tmv_sub(tsp->t4, tsp->t1);
delay = tmv_div(tmv_add(t23, t41), 2);
if (delay < 0) {
pr_debug("negative delay %10" PRId64, delay);
pr_debug("delay = (t2 - t3) * rr + (t4 - t1)");
pr_debug("t2 - t3 = %+10" PRId64, t23);
pr_debug("t4 - t1 = %+10" PRId64, t41);
pr_debug("rr = %.9f", tsp->clock_rate_ratio);
}
return delay;
}
int tsproc_update_delay(struct tsproc *tsp, tmv_t *delay)
{
tmv_t raw_delay;
if (tmv_is_zero(tsp->t2) || tmv_is_zero(tsp->t3))
return -1;
raw_delay = get_raw_delay(tsp);
tsp->filtered_delay = filter_sample(tsp->delay_filter, raw_delay);
tsp->filtered_delay_valid = 1;
pr_debug("delay filtered %10" PRId64 " raw %10" PRId64,
tsp->filtered_delay, raw_delay);
if (!delay) {
return 0;
}
switch (tsp->mode) {
case TSPROC_FILTER:
case TSPROC_FILTER_WEIGHT:
*delay = tsp->filtered_delay;
break;
case TSPROC_RAW:
case TSPROC_RAW_WEIGHT:
*delay = raw_delay;
break;
}
return 0;
}
int tsproc_update_offset(struct tsproc *tsp, tmv_t *offset, double *weight)
{
tmv_t delay = 0, raw_delay = 0;
if (tmv_is_zero(tsp->t1) || tmv_is_zero(tsp->t2))
return -1;
switch (tsp->mode) {
case TSPROC_FILTER:
if (!tsp->filtered_delay_valid) {
return -1;
}
delay = tsp->filtered_delay;
break;
case TSPROC_RAW:
case TSPROC_RAW_WEIGHT:
if (tmv_is_zero(tsp->t3)) {
return -1;
}
raw_delay = get_raw_delay(tsp);
delay = raw_delay;
break;
case TSPROC_FILTER_WEIGHT:
if (tmv_is_zero(tsp->t3) || !tsp->filtered_delay_valid) {
return -1;
}
raw_delay = get_raw_delay(tsp);
delay = tsp->filtered_delay;
break;
}
/* offset = t2 - t1 - delay */
*offset = tmv_sub(tmv_sub(tsp->t2, tsp->t1), delay);
if (!weight)
return 0;
if (weighting(tsp) && tsp->filtered_delay > 0 && raw_delay > 0) {
*weight = (double)tsp->filtered_delay / raw_delay;
if (*weight > 1.0)
*weight = 1.0;
} else {
*weight = 1.0;
}
return 0;
}
void tsproc_reset(struct tsproc *tsp, int full)
{
tsp->t1 = tmv_zero();
tsp->t2 = tmv_zero();
tsp->t3 = tmv_zero();
tsp->t4 = tmv_zero();
if (full) {
tsp->clock_rate_ratio = 1.0;
filter_reset(tsp->delay_filter);
tsp->filtered_delay_valid = 0;
}
}