Fawkes API Fawkes Development Version
mod_time.cpp
1
2/***************************************************************************
3 * mod_skiller.cpp - OpenPRS skiller module
4 *
5 * Created: Fri Aug 22 14:32:01 2014
6 * Copyright 2014 Tim Niemueller [www.niemueller.de]
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program 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 Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#include <plugins/openprs/mod_utils.h>
23#include <utils/time/time.h>
24
25#include <default-hook.h>
26#include <oprs-rerror_f-pub.h>
27
28extern "C" PBoolean
29pred_time_lt(TermList terms)
30{
31 Term *t1_sec, *t1_usec, *t2_sec, *t2_usec;
32 t1_sec = (Term *)get_list_pos(terms, 1);
33 t1_usec = (Term *)get_list_pos(terms, 2);
34 t2_sec = (Term *)get_list_pos(terms, 3);
35 t2_usec = (Term *)get_list_pos(terms, 4);
36
37 long long int t1_sec_val, t1_usec_val, t2_sec_val, t2_usec_val;
38 if (t1_sec->type == LONG_LONG) {
39 t1_sec_val = t1_sec->u.llintval;
40 } else if (t1_sec->type == INTEGER) {
41 t1_sec_val = t1_sec->u.intval;
42 } else {
43 fprintf(stderr, "time-lt: t1_sec neither of type integer nor long long\n");
44 return false;
45 }
46 if (t1_usec->type == LONG_LONG) {
47 t1_usec_val = t1_usec->u.llintval;
48 } else if (t1_usec->type == INTEGER) {
49 t1_usec_val = t1_usec->u.intval;
50 } else {
51 fprintf(stderr, "time-lt: t1_usec neither of type integer nor long long\n");
52 return false;
53 }
54 if (t2_sec->type == LONG_LONG) {
55 t2_sec_val = t2_sec->u.llintval;
56 } else if (t2_sec->type == INTEGER) {
57 t2_sec_val = t2_sec->u.intval;
58 } else {
59 fprintf(stderr, "time-lt: t2_sec neither of type integer nor long long\n");
60 return false;
61 }
62 if (t2_usec->type == LONG_LONG) {
63 t2_usec_val = t2_usec->u.llintval;
64 } else if (t2_usec->type == INTEGER) {
65 t2_usec_val = t2_usec->u.intval;
66 } else {
67 fprintf(stderr, "time-lt: t2_usec neither of type integer nor long long\n");
68 return false;
69 }
70
71 return ((t1_sec_val < t2_sec_val) || (t1_sec_val == t2_sec_val && t1_usec_val < t2_usec_val));
72}
73
74extern "C" PBoolean
75pred_time_eq(TermList terms)
76{
77 Term *t1_sec, *t1_usec, *t2_sec, *t2_usec;
78 t1_sec = (Term *)get_list_pos(terms, 1);
79 t1_usec = (Term *)get_list_pos(terms, 2);
80 t2_sec = (Term *)get_list_pos(terms, 3);
81 t2_usec = (Term *)get_list_pos(terms, 4);
82
83 if (t1_sec->type != LONG_LONG || t1_usec->type != LONG_LONG || t2_sec->type != LONG_LONG
84 || t2_usec->type != LONG_LONG) {
85 fprintf(stderr,
86 "time-eq: time values not (all) of type integer (types %i %i %i %i)\n",
87 t1_sec->type,
88 t1_usec->type,
89 t2_sec->type,
90 t2_usec->type);
91 return FALSE;
92 }
93
94 //printf("time-eq: %i %i == %i %i? %s\n", t1_sec->u.intval, t1_usec->u.intval, t2_sec->u.intval, t2_usec->u.intval,
95 // (t1_sec->u.intval == t2_sec->u.intval && t1_usec->u.intval != t2_usec->u.intval) ? "YES" : "NO");
96
97 if (t1_sec->u.intval == t2_sec->u.intval && t1_usec->u.intval == t2_usec->u.intval) {
98 return TRUE;
99 } else {
100 return FALSE;
101 }
102}
103
104extern "C" PBoolean
105pred_time_neq(TermList terms)
106{
107 Term *t1_sec, *t1_usec, *t2_sec, *t2_usec;
108 t1_sec = (Term *)get_list_pos(terms, 1);
109 t1_usec = (Term *)get_list_pos(terms, 2);
110 t2_sec = (Term *)get_list_pos(terms, 3);
111 t2_usec = (Term *)get_list_pos(terms, 4);
112
113 if (t1_sec->type != LONG_LONG || t1_usec->type != LONG_LONG || t2_sec->type != LONG_LONG
114 || t2_usec->type != LONG_LONG) {
115 fprintf(stderr,
116 "time-neq: time values not (all) of type integer (types %i %i %i %i)\n",
117 t1_sec->type,
118 t1_usec->type,
119 t2_sec->type,
120 t2_usec->type);
121 return FALSE;
122 }
123
124 /*
125 printf("time-neq: %i %i < %i %i? %s\n", t1_sec->u.intval, t1_usec->u.intval, t2_sec->u.intval, t2_usec->u.intval,
126 (t1_sec->u.intval != t2_sec->u.intval) || (t1_sec->u.intval == t2_sec->u.intval && t1_usec->u.intval != t2_usec->u.intval) ? "YES" : "NO");
127 */
128
129 if (t1_sec->u.intval != t2_sec->u.intval || t1_usec->u.intval != t2_usec->u.intval) {
130 return TRUE;
131 } else {
132 return FALSE;
133 }
134}
135
136extern "C" PBoolean
137pred_timeout(TermList terms)
138{
139 Term *t1_sec, *t1_usec, *t2_sec, *t2_usec, *interval;
140 t1_sec = (Term *)get_list_pos(terms, 1);
141 t1_usec = (Term *)get_list_pos(terms, 2);
142 t2_sec = (Term *)get_list_pos(terms, 3);
143 t2_usec = (Term *)get_list_pos(terms, 4);
144 interval = (Term *)get_list_pos(terms, 5);
145
146 if (t1_sec->type != LONG_LONG || t1_usec->type != LONG_LONG || t2_sec->type != LONG_LONG
147 || t2_usec->type != LONG_LONG
148 || (interval->type != LONG_LONG && interval->type != FLOAT && interval->type != INTEGER)) {
149 fprintf(stderr,
150 "timeout: time values not (all) of type LONG_LONG (types %i %i %i %i)\n",
151 t1_sec->type,
152 t1_usec->type,
153 t2_sec->type,
154 t2_usec->type);
155 return FALSE;
156 }
157
158 double compare_val = 0;
159 if (interval->type == LONG_LONG) {
160 compare_val = interval->u.llintval;
161 } else if (interval->type == INTEGER) {
162 compare_val = interval->u.intval;
163 } else if (interval->type == FLOAT) {
164 compare_val = *interval->u.doubleptr;
165 }
166
167 return (fawkes::time_diff_sec(
168 t1_sec->u.llintval, t1_usec->u.llintval, t2_sec->u.llintval, t2_usec->u.llintval)
169 > compare_val);
170}
171
172extern "C" Term *
173action_set_idle_looptime(TermList terms)
174{
175 Term *t_sec, *t_usec;
176
177 t_sec = (Term *)get_list_pos(terms, 1);
178 t_usec = (Term *)get_list_pos(terms, 2);
179
180 if ((t_sec->type != INTEGER && t_sec->type != LONG_LONG)
181 || (t_usec->type != INTEGER && t_usec->type != LONG_LONG)) {
182 fprintf(stderr,
183 "time-set-looptime: time values not (all) of type "
184 "integer (types %i %i)\n",
185 t_sec->type,
186 t_usec->type);
187 ACTION_FAIL();
188 }
189
190 if (t_sec->type == INTEGER) {
191 main_loop_pool_sec = t_sec->u.intval;
192 } else if (t_sec->type == LONG_LONG) {
193 main_loop_pool_sec = t_sec->u.llintval;
194 }
195
196 if (t_usec->type == INTEGER) {
197 main_loop_pool_usec = t_usec->u.intval;
198 } else if (t_usec->type == LONG_LONG) {
199 main_loop_pool_usec = t_usec->u.llintval;
200 }
201
202 printf("Setting idle loop time: %li sec %li usec\n", main_loop_pool_sec, main_loop_pool_usec);
203 ACTION_FINAL();
204}
205
206/** Entry function for the OpenPRS module. */
207extern "C" void
208init()
209{
210 printf("*** LOADING mod_time\n");
211 make_and_declare_eval_pred("time-lt", pred_time_lt, 4, TRUE);
212 make_and_declare_eval_pred("time-eq", pred_time_eq, 4, TRUE);
213 make_and_declare_eval_pred("time-neq", pred_time_neq, 4, TRUE);
214 make_and_declare_eval_pred("timeout", pred_timeout, 5, TRUE);
215 make_and_declare_action("time-set-idle-looptime", action_set_idle_looptime, 2);
216}
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.
Definition: time.h:41