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 
24 #include <utils/time/time.h>
25 
26 #include <default-hook.h>
27 #include <oprs-rerror_f-pub.h>
28 
29 extern "C"
30 PBoolean
31 pred_time_lt(TermList terms)
32 {
33  Term *t1_sec, *t1_usec, *t2_sec, *t2_usec;
34  t1_sec = (Term *)get_list_pos(terms, 1);
35  t1_usec = (Term *)get_list_pos(terms, 2);
36  t2_sec = (Term *)get_list_pos(terms, 3);
37  t2_usec = (Term *)get_list_pos(terms, 4);
38 
39  long long int t1_sec_val, t1_usec_val, t2_sec_val, t2_usec_val;
40  if (t1_sec->type == LONG_LONG) {
41  t1_sec_val = t1_sec->u.llintval;
42  } else if (t1_sec->type == INTEGER) {
43  t1_sec_val = t1_sec->u.intval;
44  } else {
45  fprintf(stderr, "time-lt: t1_sec neither of type integer nor long long\n");
46  return false;
47  }
48  if (t1_usec->type == LONG_LONG) {
49  t1_usec_val = t1_usec->u.llintval;
50  } else if (t1_usec->type == INTEGER) {
51  t1_usec_val = t1_usec->u.intval;
52  } else {
53  fprintf(stderr, "time-lt: t1_usec neither of type integer nor long long\n");
54  return false;
55  }
56  if (t2_sec->type == LONG_LONG) {
57  t2_sec_val = t2_sec->u.llintval;
58  } else if (t2_sec->type == INTEGER) {
59  t2_sec_val = t2_sec->u.intval;
60  } else {
61  fprintf(stderr, "time-lt: t2_sec neither of type integer nor long long\n");
62  return false;
63  }
64  if (t2_usec->type == LONG_LONG) {
65  t2_usec_val = t2_usec->u.llintval;
66  } else if (t2_usec->type == INTEGER) {
67  t2_usec_val = t2_usec->u.intval;
68  } else {
69  fprintf(stderr, "time-lt: t2_usec neither of type integer nor long long\n");
70  return false;
71  }
72 
73  return ((t1_sec_val < t2_sec_val) || (t1_sec_val == t2_sec_val && t1_usec_val < t2_usec_val));
74 }
75 
76 
77 extern "C"
78 PBoolean
79 pred_time_eq(TermList terms)
80 {
81  Term *t1_sec, *t1_usec, *t2_sec, *t2_usec;
82  t1_sec = (Term *)get_list_pos(terms, 1);
83  t1_usec = (Term *)get_list_pos(terms, 2);
84  t2_sec = (Term *)get_list_pos(terms, 3);
85  t2_usec = (Term *)get_list_pos(terms, 4);
86 
87  if (t1_sec->type != LONG_LONG || t1_usec->type != LONG_LONG ||
88  t2_sec->type != LONG_LONG || t2_usec->type != LONG_LONG)
89  {
90  fprintf(stderr, "time-eq: time values not (all) of type integer (types %i %i %i %i)\n",
91  t1_sec->type, t1_usec->type, t2_sec->type, t2_usec->type);
92  return FALSE;
93  }
94 
95  //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,
96  // (t1_sec->u.intval == t2_sec->u.intval && t1_usec->u.intval != t2_usec->u.intval) ? "YES" : "NO");
97 
98  if (t1_sec->u.intval == t2_sec->u.intval && t1_usec->u.intval == t2_usec->u.intval)
99  {
100  return TRUE;
101  } else {
102  return FALSE;
103  }
104 }
105 
106 
107 extern "C"
108 PBoolean
109 pred_time_neq(TermList terms)
110 {
111  Term *t1_sec, *t1_usec, *t2_sec, *t2_usec;
112  t1_sec = (Term *)get_list_pos(terms, 1);
113  t1_usec = (Term *)get_list_pos(terms, 2);
114  t2_sec = (Term *)get_list_pos(terms, 3);
115  t2_usec = (Term *)get_list_pos(terms, 4);
116 
117  if (t1_sec->type != LONG_LONG || t1_usec->type != LONG_LONG ||
118  t2_sec->type != LONG_LONG || t2_usec->type != LONG_LONG)
119  {
120  fprintf(stderr, "time-neq: time values not (all) of type integer (types %i %i %i %i)\n",
121  t1_sec->type, t1_usec->type, t2_sec->type, t2_usec->type);
122  return FALSE;
123  }
124 
125  /*
126  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,
127  (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");
128  */
129 
130  if ((t1_sec->u.intval != t2_sec->u.intval) ||
131  (t1_sec->u.intval == t2_sec->u.intval && t1_usec->u.intval != t2_usec->u.intval))
132  {
133  return TRUE;
134  } else {
135  return FALSE;
136  }
137 }
138 
139 
140 extern "C"
141 PBoolean
142 pred_timeout(TermList terms)
143 {
144  Term *t1_sec, *t1_usec, *t2_sec, *t2_usec, *interval;
145  t1_sec = (Term *)get_list_pos(terms, 1);
146  t1_usec = (Term *)get_list_pos(terms, 2);
147  t2_sec = (Term *)get_list_pos(terms, 3);
148  t2_usec = (Term *)get_list_pos(terms, 4);
149  interval = (Term *)get_list_pos(terms, 5);
150 
151  if (t1_sec->type != LONG_LONG || t1_usec->type != LONG_LONG ||
152  t2_sec->type != LONG_LONG || t2_usec->type != LONG_LONG ||
153  (interval->type != LONG_LONG && interval->type != FLOAT && interval->type != INTEGER))
154  {
155  fprintf(stderr, "timeout: time values not (all) of type LONG_LONG (types %i %i %i %i)\n",
156  t1_sec->type, t1_usec->type, t2_sec->type, t2_usec->type);
157  return FALSE;
158  }
159 
160  double compare_val = 0;
161  if (interval->type == LONG_LONG) {
162  compare_val = interval->u.llintval;
163  } else if (interval->type == INTEGER) {
164  compare_val = interval->u.intval;
165  } else if (interval->type == FLOAT) {
166  compare_val = *interval->u.doubleptr;
167  }
168 
169  return (fawkes::time_diff_sec(t1_sec->u.llintval, t1_usec->u.llintval,
170  t2_sec->u.llintval, t2_usec->u.llintval) > compare_val);
171 }
172 
173 extern "C"
174 Term *
175 action_set_idle_looptime(TermList terms)
176 {
177  Term *t_sec, *t_usec;
178 
179  t_sec = (Term *)get_list_pos(terms, 1);
180  t_usec = (Term *)get_list_pos(terms, 2);
181 
182  if ((t_sec->type != INTEGER && t_sec->type != LONG_LONG) ||
183  (t_usec->type != INTEGER && t_usec->type != LONG_LONG))
184  {
185  fprintf(stderr, "time-set-looptime: time values not (all) of type "
186  "integer (types %i %i)\n", t_sec->type, 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 
207 /** Entry function for the OpenPRS module. */
208 extern "C"
209 void init()
210 {
211  printf("*** LOADING mod_time\n");
212  make_and_declare_eval_pred("time-lt", pred_time_lt, 4, TRUE);
213  make_and_declare_eval_pred("time-eq", pred_time_eq, 4, TRUE);
214  make_and_declare_eval_pred("time-neq", pred_time_neq, 4, TRUE);
215  make_and_declare_eval_pred("timeout", pred_timeout, 5, TRUE);
216  make_and_declare_action("time-set-idle-looptime", action_set_idle_looptime, 2);
217 }
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.
Definition: time.h:40