/* Copyright (C) 2006 Cole Krumbholz */ /* */ /* This is free software; you can redistribute it and/or modify it */ /* under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* */ /* This program is distributed in the hope that it will be useful, but */ /* WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ /* General Public License for more details. */ #include "tabletracker.h" #include #include #include #include #include #include #include #include #include #define TT_NUM_VALS 6 #define TT_VAL_LEN 10 #define TT_DATA_LEN (TT_NUM_VALS*TT_VAL_LEN) static int tt_sock; int TT_open(unsigned short port) /* opens UDP socket for receiving table tracks */ { struct sockaddr_in my_addr; if ((tt_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { return TT_ERR_SOCK; } memset(&my_addr, 0, sizeof(my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = htons(INADDR_ANY); my_addr.sin_port = htons(port); if (bind(tt_sock, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) { return TT_ERR_BIND; } fcntl(tt_sock, F_SETFL, O_NONBLOCK); return TT_OK; } int TT_poll(unsigned int timeout) /* checks to see if messages are waiting */ { struct pollfd pollevt; int pollret; pollevt.fd = tt_sock; pollevt.events = POLLIN; pollevt.revents = 0; pollret = poll(&pollevt, 1, timeout); if (pollret == 1) { if (pollevt.revents & (POLLERR | POLLHUP | POLLNVAL)) { return 0; } else return 1; } else return 0; } int TT_get(int *id, double *xpos, double *ypos, double *angle, int *type) /* gets the next message */ { int recvsize, i, retval; char tt_data[TT_DATA_LEN], parse [TT_NUM_VALS*(TT_VAL_LEN+1)]; retval = TT_ERR_MESG; memset((void *) tt_data, 0, TT_DATA_LEN); memset((void *) parse, 0, TT_NUM_VALS*(TT_VAL_LEN+1)); recvsize = recvfrom(tt_sock, (void *) tt_data, TT_DATA_LEN, 0, NULL, 0); if (recvsize == TT_DATA_LEN) { for (i=0; i