whe.el

emacs support for the thrustmaster t-150 steering wheel
Log | Files | Refs | README

wheeldaemon.c (1558B)


      1 #include <fcntl.h>
      2 #include <stdio.h>
      3 #include <sys/socket.h>
      4 #include <netinet/in.h>
      5 #include <string.h>
      6 #include <sys/types.h>
      7 #include <arpa/inet.h>
      8 #include <fcntl.h>
      9 #include <unistd.h>
     10 #include <stdio.h>
     11 
     12 #include "wheelmesg.h"
     13 
     14 #define PORT 8070
     15 #define PATH "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-6"
     16 #define BUFSIZE 512
     17 
     18 int main() {
     19 	int s = socket(AF_INET, SOCK_DGRAM, 0);
     20 	struct sockaddr_in addr;
     21 	memset((char *)&addr, 0, sizeof(addr));	
     22 	addr.sin_family = AF_INET; // Specify address family.
     23 	addr.sin_addr.s_addr = htonl(INADDR_ANY); // INADDR_ANY just 0.0.0.0, machine IP address
     24 	addr.sin_port = htons(PORT); // Specify port.
     25 	bind(s, (struct sockaddr *)&addr, sizeof(addr));
     26 	struct sockaddr_in remaddr;
     27 	socklen_t addrlen = sizeof(remaddr);
     28 	int recvlen;
     29 	unsigned char buf[BUFSIZE];
     30 
     31 	while (1 == 1) {
     32 		recvlen = recvfrom(s, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
     33 		struct wheel_cmd cmd = *(struct wheel_cmd*)buf;
     34 		int fd;
     35 		char val[5] = {0};
     36 		switch (cmd.type) {
     37 		case AUTOCENTER:		   
     38 			fd = open(PATH "/enable_autocenter", O_WRONLY);
     39 			write(fd, cmd.value.autocenter ? "y" : "n", 1);
     40 			break;
     41 		case AUTOCENTER_FORCE:
     42 			fd = open(PATH "/autocenter", O_WRONLY);
     43 			sprintf(val, "%d", cmd.value.autocenter_force);
     44 			write(fd, val, 3);
     45 			break;
     46 		case GAIN:
     47 			fd = open(PATH "/gain", O_WRONLY);
     48 			sprintf(val, "%d", cmd.value.gain);
     49 			write(fd, val, 3);
     50 			break;
     51 		case RANGE:
     52 			fd = open(PATH "/range", O_WRONLY);
     53 			sprintf(val, "%d", cmd.value.range);
     54 			write(fd, val, 4);
     55 			break;
     56 		}
     57 	}
     58 	
     59 }