summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan McGee <dpmcgee@gmail.com>2011-11-16 19:16:46 -0600
committerDan McGee <dpmcgee@gmail.com>2011-11-16 19:16:46 -0600
commitb272c34cf22917c744a652fa543fdce3bf1f2436 (patch)
tree95d10f15fd4bd7060218bac7a6db0762d0acaf61
parent9d4b18088f81a88a18e106c7e6dc67d267c2b7e0 (diff)
downloadonkyocontrol-b272c34cf22917c744a652fa543fdce3bf1f2436.tar.gz
onkyocontrol-b272c34cf22917c744a652fa543fdce3bf1f2436.zip
Revamp the way power status is kept
It was really silly to have to reparse the message we just generated. Instead, special case the six possible power messages and update the status directly when we find of them. Signed-off-by: Dan McGee <dpmcgee@gmail.com>
-rw-r--r--command.c2
-rw-r--r--onkyo.c4
-rw-r--r--onkyo.h3
-rw-r--r--receiver.c34
4 files changed, 22 insertions, 21 deletions
diff --git a/command.c b/command.c
index 2948132..25fee22 100644
--- a/command.c
+++ b/command.c
@@ -462,7 +462,7 @@ int write_fakesleep_status(struct receiver *rcvr,
mins = when > now.tv_sec ? (when - now.tv_sec + 59) / 60 : 0;
snprintf(msg, sizeof(msg), "OK:zone%csleep:%ld\n", zone, mins);
- write_to_connections(rcvr, msg);
+ write_to_connections(msg);
return 0;
}
diff --git a/onkyo.c b/onkyo.c
index 1ab2700..90cc983 100644
--- a/onkyo.c
+++ b/onkyo.c
@@ -703,7 +703,7 @@ static int process_input(struct conn *c)
* @param msg the message to write, including trailing newline
* @return 0 on success, -1 on failure
*/
-int write_to_connections(struct receiver *rcvr, const char *msg)
+int write_to_connections(const char *msg)
{
struct conn *c;
size_t len = strlen(msg);
@@ -718,8 +718,6 @@ int write_to_connections(struct receiver *rcvr, const char *msg)
}
c = c->next;
}
- /* check for power messages- update our power state variable */
- update_power_status(rcvr, msg);
return 0;
}
diff --git a/onkyo.h b/onkyo.h
index 42b7b9b..a74dba4 100644
--- a/onkyo.h
+++ b/onkyo.h
@@ -70,14 +70,13 @@ struct receiver {
/* onkyo.c - general functions */
-int write_to_connections(struct receiver *rcvr, const char *msg);
+int write_to_connections(const char *msg);
/* receiver.c - receiver interaction functions, status processing */
void init_statuses(void);
int rcvr_send_command(struct receiver *rcvr);
int process_incoming_message(struct receiver *rcvr, int logfd);
enum power initial_power_status(void);
-void update_power_status(struct receiver *rcvr, const char *msg);
/* command.c - user command processing */
void init_commands(void);
diff --git a/receiver.c b/receiver.c
index 38da6e2..d568a4b 100644
--- a/receiver.c
+++ b/receiver.c
@@ -351,6 +351,8 @@ void init_statuses(void)
printf("%u status messages prehashed in status list.\n", status_count);
}
+static void update_power_status(struct receiver *rcvr, int zone, int value);
+
/**
* Form the human readable status message from the receiver return value.
* Note that the status parameter is freely modified as necessary and
@@ -380,7 +382,7 @@ static int parse_status(struct receiver *rcvr, int size, char *status)
*eptr = '\0';
} else {
/* Hmm, we couldn't find the start chars. WTF? */
- write_to_connections(rcvr, rcvr_err);
+ write_to_connections(rcvr_err);
return -1;
}
@@ -389,7 +391,7 @@ static int parse_status(struct receiver *rcvr, int size, char *status)
st = statuses;
while(st->hash != 0) {
if(st->hash == hashval) {
- write_to_connections(rcvr, st->value);
+ write_to_connections(st->value);
return 0;
}
st++;
@@ -398,7 +400,8 @@ static int parse_status(struct receiver *rcvr, int size, char *status)
pwr_st = power_statuses;
while(pwr_st->hash != 0) {
if(pwr_st->hash == hashval) {
- write_to_connections(rcvr, pwr_st->value);
+ update_power_status(rcvr, pwr_st->zone, pwr_st->power);
+ write_to_connections(pwr_st->value);
return 0;
}
pwr_st++;
@@ -429,7 +432,7 @@ static int parse_status(struct receiver *rcvr, int size, char *status)
}
/* this block is special compared to the rest; we write out buf2 here
* but let the normal write at the end handle buf as usual */
- write_to_connections(rcvr, buf2);
+ write_to_connections(buf2);
}
/* TUN, TUZ, TU3 */
@@ -502,7 +505,7 @@ static int parse_status(struct receiver *rcvr, int size, char *status)
snprintf(buf, BUF_SIZE, "OK:todo:%s\n", sptr);
}
- write_to_connections(rcvr, buf);
+ write_to_connections(buf);
return 0;
}
@@ -516,29 +519,30 @@ enum power initial_power_status(void)
}
/**
- * Update the power status if necessary by looking at the given message. This
+ * Update the power status for the given zone to the given value. This
* message may or may not be related to power; if it is not then the status
* will not be updated. If it is, perform some bitmask-foo to update the power
* status depending on what zone was turned on or off.
* @param rcvr the receiver the message was received from
- * @param msg the message to process
+ * @param zone the value 1, 2, or 3 corresponding to the zone
+ * @param msg 1 for on, 0 for off
*/
-void update_power_status(struct receiver *rcvr, const char *msg)
+static void update_power_status(struct receiver *rcvr, int zone, int value)
{
/* var is a bitmask, manage power/zone2power separately */
- if(strcmp(msg, "OK:power:off\n") == 0) {
+ if(zone == 1 && value == 0) {
rcvr->power &= ~MAIN_POWER;
- } else if(strcmp(msg, "OK:power:on\n") == 0) {
+ } else if(zone == 1 && value == 1) {
rcvr->power |= MAIN_POWER;
- } else if(strcmp(msg, "OK:zone2power:off\n") == 0) {
+ } else if(zone == 2 && value == 0) {
rcvr->power &= ~ZONE2_POWER;
timeval_clear(rcvr->zone2_sleep);
- } else if(strcmp(msg, "OK:zone2power:on\n") == 0) {
+ } else if(zone == 2 && value == 1) {
rcvr->power |= ZONE2_POWER;
- } else if(strcmp(msg, "OK:zone3power:off\n") == 0) {
+ } else if(zone == 3 && value == 0) {
rcvr->power &= ~ZONE3_POWER;
timeval_clear(rcvr->zone3_sleep);
- } else if(strcmp(msg, "OK:zone3power:on\n") == 0) {
+ } else if(zone == 3 && value == 1) {
rcvr->power |= ZONE3_POWER;
}
}
@@ -566,7 +570,7 @@ int process_incoming_message(struct receiver *rcvr, int logfd)
if(!ret)
rcvr->msgs_received++;
} else {
- write_to_connections(rcvr, rcvr_err);
+ write_to_connections(rcvr_err);
ret = -1;
}