summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaromil <jaromil@dyne.org>2011-04-19 11:31:41 (GMT)
committer Jaromil <jaromil@dyne.org>2011-04-19 11:31:41 (GMT)
commit11de3001a20ccac18ba1ec8fbc9b1f89244ea2db (patch)
tree61bb189bb8b38cec17cac36c7cd288dc0d59b672
parentd250120c0f925008f057109b8b8c79627791d8db (diff)
correct parsing of GetTransportInfo response
now we also have some better code setup for parsing more responses
-rw-r--r--src/avremote.c37
-rw-r--r--src/avremote.h2
-rw-r--r--src/cmdline.c58
3 files changed, 90 insertions, 7 deletions
diff --git a/src/avremote.c b/src/avremote.c
index 8977b80..808df8d 100644
--- a/src/avremote.c
+++ b/src/avremote.c
@@ -25,16 +25,24 @@
*/
+#define _GNU_SOURCE 1
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <sys/time.h>
#include <sys/socket.h>
+#include <sys/select.h>
+#include <sys/param.h>
+#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
+#include <poll.h>
+
#include <libgen.h>
#include <errno.h>
@@ -182,10 +190,33 @@ int send_upnp(upnp_t *upnp) {
return(1);
}
-int recv_upnp(upnp_t *upnp) {
+int recv_upnp(upnp_t *upnp, int timeout) {
int res;
- res = read(upnp->sockfd, upnp->res,MAX_RES_SIZE-1);
- return(1);
+ struct pollfd fds[1]; /* for the poll */
+ fd_set socketSet;
+ struct timeval timeval;
+
+ fds[0].fd = upnp->sockfd;
+ fds[0].events = POLLRDHUP; // needs _GNU_SOURCE
+ res = poll(fds, 1, timeout);
+ if (res < 0) {
+ fprintf(stderr,"error polling reply: %s\n",strerror(errno));
+ return(0);
+ } else if (!res) return(0);
+
+ FD_ZERO(&socketSet);
+ FD_SET(upnp->sockfd, &socketSet);
+ timeval.tv_sec = timeout / 1000;
+ timeval.tv_usec = (timeout % 1000) * 1000;
+ res = select(FD_SETSIZE, &socketSet, NULL, NULL, &timeval);
+ if (res < 0) {
+ fprintf(stderr,"error on socket select: %s\n",strerror(errno));
+ return(0);
+ } else if (!res) return(0);
+
+
+ res = recv(upnp->sockfd, upnp->res, MAX_RES_SIZE-1, 0);
+ return(res);
}
int print_upnp(upnp_t *upnp) {
diff --git a/src/avremote.h b/src/avremote.h
index 66e5347..e1e80e6 100644
--- a/src/avremote.h
+++ b/src/avremote.h
@@ -105,7 +105,7 @@ void render_upnp(upnp_t *upnp, char *action, char *arg);
void render_uri_meta(upnp_t *upnp, char *path);
int send_upnp(upnp_t *upnp);
-int recv_upnp(upnp_t *upnp);
+int recv_upnp(upnp_t *upnp, int timeout);
int print_upnp(upnp_t *upnp);
#endif
diff --git a/src/cmdline.c b/src/cmdline.c
index 7516786..fea75d2 100644
--- a/src/cmdline.c
+++ b/src/cmdline.c
@@ -27,7 +27,6 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
-#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
@@ -155,6 +154,12 @@ void cmdline(int argc, char **argv) {
}
}
+// function pointers to various parsers
+typedef void (parser_f)(char *res);
+static parser_f *parser = NULL;
+// parsers implemented
+void gettransportinfo(char *res);
+
int main(int argc, char **argv) {
upnp_t *upnp;
@@ -222,6 +227,7 @@ int main(int argc, char **argv) {
case 'g':
render_upnp(upnp,"GetTransportInfo","");
+ parser = gettransportinfo;
break;
default:
@@ -235,11 +241,57 @@ int main(int argc, char **argv) {
else
{
send_upnp(upnp);
- recv_upnp(upnp);
- fprintf(stderr,"%s\n",upnp->res);
+ recv_upnp(upnp, 1000);
+ if (parser)
+ (*parser)(upnp->res);
+ else
+ fprintf(stderr,"%s\n",upnp->res);
}
free_upnp(upnp);
exit(0);
}
+
+void gettransportinfo(char *res) {
+ char *p, *pp;
+ char state[64];
+ char status[64];
+ char speed[32];
+
+ // as you can see, i had a lot of fun with pointers when i was a kid
+
+ p = strstr(res, "CurrentTransportState");
+ if (!p) {
+ fprintf(stderr, "error parsing response:\n%s\n",res);
+ return;
+ }
+ pp = p; do pp+=1; while (*pp != '>'); pp++;
+ p = pp; do p+=1; while (*p != '<'); *p = 0;
+ snprintf(state,63,"%s",pp);
+
+ p++;
+
+ p = strstr(p, "CurrentTransportStatus");
+ if (!p) {
+ fprintf(stderr, "error parsing response:\n%s\n",res);
+ return;
+ }
+ pp = p; do pp+=2; while (*pp != '>'); pp++;
+ p = pp; do p+=1; while (*p != '<'); *p = 0;
+ snprintf(status,63,"%s",pp);
+
+ p++;
+
+ p = strstr(p, "CurrentSpeed");
+ if (!p) {
+ fprintf(stderr, "error parsing response:\n%s\n",res);
+ return;
+ }
+ pp = p; do pp+=2; while (*pp != '>'); pp++;
+ p = pp; do p+=1; while (*p != '<'); *p = 0;
+ snprintf(speed,31,"%s",pp);
+
+ fprintf(stderr,"%s\t%s\t%s\n", state, status, speed);
+}
+