summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan McGee <dpmcgee@gmail.com>2011-11-16 01:36:31 -0600
committerDan McGee <dpmcgee@gmail.com>2011-11-16 01:36:31 -0600
commita83a7d1021071db180f93ca24292cd48fffb1d3d (patch)
tree1894834c8065517d060a12311f6aa66e367cef40
parent5b4ec0c382bfd394e9f35c94f424b43ba5d814f6 (diff)
downloadonkyocontrol-a83a7d1021071db180f93ca24292cd48fffb1d3d.tar.gz
onkyocontrol-a83a7d1021071db180f93ca24292cd48fffb1d3d.zip
Consolidate timeval helper functions into util.c
And add a clear macro that saves a bit of verbosity. Signed-off-by: Dan McGee <dpmcgee@gmail.com>
-rw-r--r--command.c6
-rw-r--r--onkyo.c55
-rw-r--r--onkyo.h6
-rw-r--r--receiver.c6
-rw-r--r--util.c28
5 files changed, 51 insertions, 50 deletions
diff --git a/command.c b/command.c
index e8d0265..9149147 100644
--- a/command.c
+++ b/command.c
@@ -488,11 +488,9 @@ static int handle_fakesleep(struct receiver *rcvr,
} else if(strcmp(arg, "off") == 0) {
/* clear out any future receiver set sleep time */
if(zone == '2') {
- rcvr->zone2_sleep.tv_sec = 0;
- rcvr->zone2_sleep.tv_usec = 0;
+ timeval_clear(rcvr->zone2_sleep);
} else if(zone == '3') {
- rcvr->zone3_sleep.tv_sec = 0;
- rcvr->zone3_sleep.tv_usec = 0;
+ timeval_clear(rcvr->zone3_sleep);
}
} else {
/* otherwise we probably have a number */
diff --git a/onkyo.c b/onkyo.c
index 95206fb..561136b 100644
--- a/onkyo.c
+++ b/onkyo.c
@@ -580,19 +580,6 @@ static int open_socket_listener(const char *path)
return(listen_and_add(fd));
}
-static void diff_timeval(struct timeval * restrict a,
- struct timeval * restrict b, struct timeval * restrict result)
-{
- /* Calculate time difference as `a - b`.
- * Make sure we end up with an in-range usecs value. */
- result->tv_sec = a->tv_sec - b->tv_sec;
- result->tv_usec = a->tv_usec - b->tv_usec;
- if(result->tv_usec < 0) {
- result->tv_usec += 1000000;
- result->tv_sec -= 1;
- }
-}
-
/**
* Determine if we can send a command to the receiver by ensuring it has been
* a certain time since the previous sent command. If we can send a command,
@@ -609,7 +596,7 @@ static int can_send_command(struct timeval * restrict last,
{
/* ensure it has been long enough since the last sent command */
struct timeval diff, wait;
- diff_timeval(now, last, &diff);
+ timeval_diff(now, last, &diff);
wait.tv_usec = 1000 * COMMAND_WAIT;
wait.tv_sec = wait.tv_usec / 1000000;
@@ -622,7 +609,7 @@ static int can_send_command(struct timeval * restrict last,
}
/* it hasn't been long enough, set the timeout as necessary */
- diff_timeval(&wait, &diff, timeoutval);
+ timeval_diff(&wait, &diff, timeoutval);
return(0);
}
@@ -737,19 +724,6 @@ int write_to_connections(struct receiver *rcvr, const char *msg)
return 0;
}
-static struct timeval min_timeval(struct timeval *tv1, struct timeval *tv2)
-{
- if(tv1->tv_sec == 0 && tv1->tv_usec == 0) {
- return(*tv2);
- } if(tv1->tv_sec < tv2->tv_sec) {
- return(*tv1);
- } else if(tv1->tv_sec > tv2->tv_sec) {
- return(*tv2);
- }
- /* getting here means seconds are equal */
- return(tv1->tv_usec < tv2->tv_usec ? *tv1 : *tv2);
-}
-
static const struct option opts[] = {
{"bind", optional_argument, 0, 'b'},
{"daemon", no_argument, 0, 'd'},
@@ -938,23 +912,21 @@ int main(int argc, char *argv[])
/* do we need to queue a power off command for sleep? */
if(r->zone2_sleep.tv_sec) {
- diff_timeval(&r->zone2_sleep, &now, &diff);
+ timeval_diff(&r->zone2_sleep, &now, &diff);
if(diff.tv_sec >= 0 && diff.tv_usec > 0) {
- timeoutval = min_timeval(&timeoutval, &diff);
+ timeoutval = timeval_min(&timeoutval, &diff);
} else {
process_command(r, "zone2power off");
- r->zone2_sleep.tv_sec = 0;
- r->zone2_sleep.tv_usec = 0;
+ timeval_clear(r->zone2_sleep);
}
}
if(r->zone3_sleep.tv_sec) {
- diff_timeval(&r->zone3_sleep, &now, &diff);
+ timeval_diff(&r->zone3_sleep, &now, &diff);
if(diff.tv_sec >= 0 && diff.tv_usec > 0) {
- timeoutval = min_timeval(&timeoutval, &diff);
+ timeoutval = timeval_min(&timeoutval, &diff);
} else {
process_command(r, "zone3power off");
- r->zone3_sleep.tv_sec = 0;
- r->zone3_sleep.tv_usec = 0;
+ timeval_clear(r->zone3_sleep);
}
}
/* if we still have sleep timers, we'll wake up at 60-second
@@ -966,14 +938,13 @@ int main(int argc, char *argv[])
r->next_sleep_update = now;
r->next_sleep_update.tv_sec += 60;
}
- diff_timeval(&r->next_sleep_update, &now, &diff);
+ timeval_diff(&r->next_sleep_update, &now, &diff);
if(diff.tv_sec >= 0 && diff.tv_usec > 0) {
- timeoutval = min_timeval(&timeoutval, &diff);
+ timeoutval = timeval_min(&timeoutval, &diff);
}
} else {
/* clear any sleep update if we have no timers running */
- r->next_sleep_update.tv_sec = 0;
- r->next_sleep_update.tv_usec = 0;
+ timeval_clear(r->next_sleep_update);
}
/* check for write possibility if we have commands in queue */
@@ -983,7 +954,7 @@ int main(int argc, char *argv[])
} else {
/* We want the smallest timeout, so replace the
* existing if new is smaller. */
- timeoutval = min_timeval(&timeoutval, &diff);
+ timeoutval = timeval_min(&timeoutval, &diff);
}
}
@@ -1045,7 +1016,7 @@ int main(int argc, char *argv[])
if(r->next_sleep_update.tv_sec) {
struct timeval diff;
gettimeofday(&now, NULL);
- diff_timeval(&now, &r->next_sleep_update, &diff);
+ timeval_diff(&now, &r->next_sleep_update, &diff);
if(diff.tv_sec >= 0 && diff.tv_usec > 0) {
if(r->zone2_sleep.tv_sec > 0)
write_fakesleep_status(r, now, '2');
diff --git a/onkyo.h b/onkyo.h
index bf3c2be..66eb1d3 100644
--- a/onkyo.h
+++ b/onkyo.h
@@ -95,6 +95,12 @@ ssize_t xread(int fd, void *buf, size_t len);
ssize_t xwrite(int fd, const void *buf, size_t len);
unsigned long hash_sdbm(const char *str);
+void timeval_diff(struct timeval * restrict a,
+ struct timeval * restrict b, struct timeval * restrict result);
+struct timeval timeval_min(struct timeval *restrict a,
+ struct timeval * restrict b);
+#define timeval_clear(tv) do { (tv).tv_sec = 0; (tv).tv_usec = 0; } while(0)
+
#endif /* ONKYO_H */
/* vim: set ts=4 sw=4 noet: */
diff --git a/receiver.c b/receiver.c
index dbbc563..83d3ed0 100644
--- a/receiver.c
+++ b/receiver.c
@@ -521,14 +521,12 @@ void update_power_status(struct receiver *rcvr, const char *msg)
rcvr->power |= MAIN_POWER;
} else if(strcmp(msg, "OK:zone2power:off\n") == 0) {
rcvr->power &= ~ZONE2_POWER;
- rcvr->zone2_sleep.tv_sec = 0;
- rcvr->zone2_sleep.tv_usec = 0;
+ timeval_clear(rcvr->zone2_sleep);
} else if(strcmp(msg, "OK:zone2power:on\n") == 0) {
rcvr->power |= ZONE2_POWER;
} else if(strcmp(msg, "OK:zone3power:off\n") == 0) {
rcvr->power &= ~ZONE3_POWER;
- rcvr->zone3_sleep.tv_sec = 0;
- rcvr->zone3_sleep.tv_usec = 0;
+ timeval_clear(rcvr->zone3_sleep);
} else if(strcmp(msg, "OK:zone3power:on\n") == 0) {
rcvr->power |= ZONE3_POWER;
}
diff --git a/util.c b/util.c
index ddf0856..b842d31 100644
--- a/util.c
+++ b/util.c
@@ -19,6 +19,7 @@
#include <stdlib.h> /* malloc */
#include <sys/stat.h> /* open */
+#include <sys/time.h> /* struct timeval */
#include <fcntl.h> /* open */
#include <unistd.h> /* close, read, write */
#include <errno.h> /* for errno refs */
@@ -88,4 +89,31 @@ unsigned long hash_sdbm(const char *str)
return(hash);
}
+void timeval_diff(struct timeval * restrict a,
+ struct timeval * restrict b, struct timeval * restrict result)
+{
+ /* Calculate time difference as `a - b`.
+ * Make sure we end up with an in-range usecs value. */
+ result->tv_sec = a->tv_sec - b->tv_sec;
+ result->tv_usec = a->tv_usec - b->tv_usec;
+ if(result->tv_usec < 0) {
+ result->tv_usec += 1000000;
+ result->tv_sec -= 1;
+ }
+}
+
+struct timeval timeval_min(struct timeval *restrict a,
+ struct timeval * restrict b)
+{
+ if(a->tv_sec == 0 && a->tv_usec == 0) {
+ return(*b);
+ } if(a->tv_sec < b->tv_sec) {
+ return(*a);
+ } else if(a->tv_sec > b->tv_sec) {
+ return(*b);
+ }
+ /* getting here means seconds are equal */
+ return(a->tv_usec < b->tv_usec ? *a : *b);
+}
+
/* vim: set ts=4 sw=4 noet: */