00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <sys/types.h>
00030 #include <unistd.h>
00031 #include <errno.h>
00032 #include <string>
00033 #include <stdio.h>
00034 #include <sys/socket.h>
00035 #include <netinet/in.h>
00036 #include <netdb.h>
00037 #include <errno.h>
00038 #include <iostream>
00039
00040 #include "trackerd_position.hh"
00041
00042 using namespace std;
00043
00044 TrackerdPosition::TrackerdPosition(const char* host, unsigned int port, unsigned int tid) throw (DeviceError)
00045 {
00046 id = tid;
00047 struct sockaddr_in address;
00048 if( (socket_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
00049 throw DeviceError(string("Error creating socket: ") + strerror(errno));
00050 }
00051 struct hostent* he;
00052 if(!(he = gethostbyname(host))) {
00053 throw DeviceError(string("Invalid address: ") + host);
00054 }
00055 memset(&address, 0, sizeof(address));
00056 address.sin_port=htons(port);
00057 address.sin_family=AF_INET;
00058 address.sin_addr=*((struct in_addr *)he->h_addr);
00059 if( connect(socket_fd, (struct sockaddr *)&address, sizeof(address)) == -1) {
00060 char port_s[16];
00061 snprintf(port_s, sizeof(port_s), "%d", port);
00062 throw DeviceError(string("Error connecting to server ") + host + ":" + port_s + ". " + strerror(errno));
00063 }
00064 }
00065
00066 TrackerdPosition::~TrackerdPosition()
00067 {
00068 shutdown(socket_fd, 2);
00069 }
00070
00071 void TrackerdPosition::getPosition(double* x, double* y, double* z) throw(DeviceError)
00072 {
00073 char msg[256];
00074 unsigned int id = 0;
00075 if( write(socket_fd, "p:0", 4) != 4) {
00076 throw DeviceError( string("Error sending command to trackerd: ") + strerror(errno));
00077 }
00078 int nrecvd = recv(socket_fd, msg, 256, 0);
00079
00080 if(sscanf(msg, "p:%u=%lf,%lf,%lf", &id, x, y, z) < 4) {
00081 throw DeviceError("Misparse of data from trackerd");
00082 }
00083 *x *= xScale;
00084 *y *= yScale;
00085 *z *= zScale;
00086 *x += xOffset;
00087 *y += yOffset;
00088 *z += zOffset;
00089
00090 }
00091