/* simple.c -- A TCP/IP server that interacts with clients and delivers them the accelerometer information from reading /dev/accel By Adam Rea, Travis Martin and Michael Goldschmidt */ #include #include #include #include #include #include #include #include #ifdef STRONGARM #include #endif #define BUF_SIZE 64 #define OUR_PORT 15000 #define PAUSE_IN_US 50000 #define DEVICE_NAME "/dev/accel" /* holds accel data */ struct accel_data { long unsigned x_dir; long unsigned y_dir; }; /* global to make it usable in signal handler */ int s, handle, cli_s, total; void signal_handler( int signo ) { printf( "\nShutdown..." ); close( s ); close( cli_s ); close( handle ); printf( "done\n" ); printf( "Wrote %d byte(s)\n", total ); exit( 0 ); } int main( int argc, char **argv ) { int on, rv, port = OUR_PORT, len; struct sockaddr_in sin, cli; const int backlog = 5; struct linger ling; char flag = 1; /* count of total bytes send */ total = 0; handle = open( DEVICE_NAME, O_RDONLY ); if ( -1 == handle ) { fprintf( stderr, "Cannot open device\n" ); return -1; } s = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP ); if ( -1 == s ) { fprintf( stderr, "socket() failed\n" ); return -1; } /* make fast re-use */ on = 1; rv = setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof( on ) ); if ( -1 == rv ) { fprintf( stderr, "setsockopt() failed\n" ); return -1; } /* turn off lingering */ memset( &ling, 0, sizeof( ling ) ); ling.l_onoff = 1; ling.l_linger = 30; rv = setsockopt( s, SOL_SOCKET, SO_LINGER, (const char*) &ling, sizeof( ling ) ); if ( -1 == rv ) { fprintf( stderr, "setsockopt() failed\n" ); return -1; } memset( &sin, 0, sizeof( struct sockaddr_in ) ); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl( INADDR_ANY ); sin.sin_port = htons( port ); rv = bind( s, (struct sockaddr *) &sin, sizeof( sin ) ); if ( -1 == rv ) { fprintf( stderr, "bind() failed\n" ); return -1; } /* set signal handler */ signal( SIGINT, signal_handler ); rv = listen( s, backlog ); if ( -1 == rv ) { fprintf( stderr, "listen() failed\n" ); return -1; } else { printf( "Listening on port %d\n", port ); } memset( &cli, 0, sizeof( cli ) ); len = sizeof( cli ); cli_s = accept( s, (struct sockaddr *) &cli, &len ); if ( -1 == cli_s ) { fprintf( stderr, "accept() failed\n" ); return -1; } else { printf( "Got connection...streaming position data\n" ); } close( s ); // our loop while ( 1 ) { char buf[BUF_SIZE]; struct accel_data data; long unsigned raw; int x, y; /* BUGBUG enable later */ rv = read( handle, buf, BUF_SIZE ); if ( -1 == rv ) { fprintf( stderr, "\nCannot read()\n" ); close( s ); close( cli_s ); return -1; } raw = atoi( buf ); x = raw / 65536; y = raw % 65536; // we want to normalize these values from between 0 - 1023 to -50 and +50 x -= 512; y -= 512; if (x > 0) x = x * x; else x = -x * x; x /= 5500; if (y > 0) y = y * y; else y = -y * y; y /= 5500; data.x_dir = x; data.y_dir = y; total += write( cli_s, &data, sizeof( data ) ); if ( flag ) { printf( "#" ); flag = 0; } else { printf( "\b" ); flag = 1; } fflush( stdout ); usleep( PAUSE_IN_US ); } /* NOT reached */ close( cli_s ); printf( "\nWrote %d byte(s)\n", total ); return 0; }