vrmodel

diff src/inpclient.cc @ 4:a32b151fb3c6

moving along slowly
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 11 Sep 2014 00:08:23 +0300
parents a1784a4290c2
children 9e260c091f75
line diff
     1.1 --- a/src/inpclient.cc	Mon Sep 01 05:59:31 2014 +0300
     1.2 +++ b/src/inpclient.cc	Thu Sep 11 00:08:23 2014 +0300
     1.3 @@ -15,7 +15,7 @@
     1.4  static int send_request(int s, struct message *msg, long timeout);
     1.5  
     1.6  static int opt_dport = DEF_PORT;	/* discover broadcast port */
     1.7 -static long opt_timeout = 5000;	/* 10 sec discovery timeout */
     1.8 +static long opt_timeout = 5000;		/* 10 sec discovery timeout */
     1.9  
    1.10  static int sock = -1;
    1.11  
    1.12 @@ -40,7 +40,10 @@
    1.13  	memset(&msg, 0, sizeof msg);
    1.14  	msg.magic = MAGIC;
    1.15  	msg.type = REQ_START;
    1.16 -	return send_request(sock, &msg, opt_timeout);
    1.17 +	if(send_request(sock, &msg, opt_timeout) == -1) {
    1.18 +		return -1;
    1.19 +	}
    1.20 +	return msg.magic == MAGIC && msg.type == RSP_OK ? 0 : -1;
    1.21  }
    1.22  
    1.23  int netinp_stop()
    1.24 @@ -54,10 +57,14 @@
    1.25  	memset(&msg, 0, sizeof msg);
    1.26  	msg.magic = MAGIC;
    1.27  	msg.type = REQ_STOP;
    1.28 -	return send_request(sock, &msg, opt_timeout);
    1.29 +
    1.30 +	if(send_request(sock, &msg, opt_timeout) == -1) {
    1.31 +		return -1;
    1.32 +	}
    1.33 +	return msg.magic == MAGIC && msg.type == RSP_OK ? 0 : -1;
    1.34  }
    1.35  
    1.36 -int netinp_read(float *pos)
    1.37 +int netinp_read(float *pos, unsigned int *bnmask)
    1.38  {
    1.39  	struct message msg;
    1.40  
    1.41 @@ -70,15 +77,34 @@
    1.42  		return -1;
    1.43  	}
    1.44  
    1.45 -	pos[0] = ((float*)msg.data)[0];
    1.46 -	pos[1] = ((float*)msg.data)[1];
    1.47 -	pos[2] = ((float*)msg.data)[3];
    1.48 +	if(msg.magic != MAGIC || msg.type != MAGIC) {
    1.49 +		fprintf(stderr, "not enough magic in the message\n");
    1.50 +		return -1;
    1.51 +	}
    1.52 +
    1.53 +	pos[0] = msg.data.event.x;
    1.54 +	pos[1] = msg.data.event.y;
    1.55 +	pos[2] = msg.data.event.z;
    1.56 +	*bnmask = msg.data.event.bnmask;
    1.57  	return 0;
    1.58  }
    1.59  
    1.60  int netinp_reset()
    1.61  {
    1.62 -	return -1;	// TODO
    1.63 +	struct message msg;
    1.64 +
    1.65 +	if(sock == -1) {
    1.66 +		return -1;
    1.67 +	}
    1.68 +
    1.69 +	memset(&msg, 0, sizeof msg);
    1.70 +	msg.magic = MAGIC;
    1.71 +	msg.type = REQ_STOP;
    1.72 +
    1.73 +	if(send_request(sock, &msg, opt_timeout) == -1) {
    1.74 +		return -1;
    1.75 +	}
    1.76 +	return msg.magic == MAGIC && msg.type == RSP_OK ? 0 : -1;
    1.77  }
    1.78  
    1.79  static int discover(struct sockaddr_in *srv_sa)
    1.80 @@ -142,7 +168,7 @@
    1.81  				perror("failed to receive datagram\n");
    1.82  			}
    1.83  			if(msg.magic == MAGIC && msg.type == RSP_OK) {
    1.84 -				printf("found input server \"%s\" at: %s:%d\n", msg.data,
    1.85 +				printf("found input server \"%s\" at: %s:%d\n", msg.data.raw,
    1.86  						inet_ntoa(srv_sa->sin_addr), ntohs(srv_sa->sin_port));
    1.87  				return s;
    1.88  			}