1 /**
2 * @file nullf.c
3 * @brief Implements a clock servo that always set the frequency offset to zero.
4 * @note Copyright (C) 2015 Richard Cochran <richardcochran@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 #include <stdlib.h>
21 #include <math.h>
23 #include "nullf.h"
24 #include "print.h"
25 #include "servo_private.h"
27 struct nullf_servo {
28 struct servo servo;
29 };
31 static void nullf_destroy(struct servo *servo)
32 {
33 struct nullf_servo *s = container_of(servo, struct nullf_servo, servo);
34 free(s);
35 }
37 static double nullf_sample(struct servo *servo, int64_t offset,
38 uint64_t local_ts, double weight,
39 enum servo_state *state)
40 {
41 if (!offset) {
42 *state = SERVO_LOCKED;
43 return 0.0;
44 }
46 if ((servo->first_update && servo->first_step_threshold &&
47 servo->first_step_threshold < fabs(offset)) ||
48 (servo->step_threshold && servo->step_threshold < fabs(offset))) {
49 *state = SERVO_JUMP;
50 } else {
51 *state = SERVO_UNLOCKED;
52 }
54 return 0.0;
55 }
57 static void nullf_sync_interval(struct servo *servo, double interval)
58 {
59 }
61 static void nullf_reset(struct servo *servo)
62 {
63 }
65 struct servo *nullf_servo_create(void)
66 {
67 struct nullf_servo *s;
69 s = calloc(1, sizeof(*s));
70 if (!s)
71 return NULL;
73 s->servo.destroy = nullf_destroy;
74 s->servo.sample = nullf_sample;
75 s->servo.sync_interval = nullf_sync_interval;
76 s->servo.reset = nullf_reset;
78 return &s->servo;
79 }