1 /***************************************************************************
4 * Wed Nov 18 10:46:06 2009
5 * Copyright 2009 Bartlomiej Swiercz
6 * <bartlomiej.swiercz@teleca.com>
7 ****************************************************************************/
13 #include <sys/types.h>
17 #include "ts7200_client.h"
19 typedef enum { GREEN, RED } led_color_t;
20 volatile unsigned int *PEDR, *PEDDR;
26 int fd_mem = open( "/dev/mem", O_RDWR );
27 start = mmap( 0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd_mem, 0x80840000 );
28 PEDR = (unsigned int *) (start + 0x20); //port e data
29 PEDDR = (unsigned int *) (start + 0x24); //port e direction
30 *PEDDR = 0xff; //all output (just 2 bits)
32 printf( "TS-7200 initialized\n" );
41 void switchOnLed( led_color_t color )
47 printf( "Green led switched on\n" );
51 printf( "Red led switched on\n" );
54 printf( "Unknown led!\n" );
58 void switchOffLed( led_color_t color )
64 printf( "Green led switched off\n" );
68 printf( "Red led switched off\n" );
71 printf( "Unknown led!\n" );
75 int openNota( int *sock, nota_addr_t *addr )
78 *sock = Hsocket( Hgetinstance(), AF_NOTA, SOCK_STREAM, 0 );
85 err = Hbind( Hgetinstance(), *sock, (struct sockaddr*) addr, sizeof(*addr) );
88 Hclose( Hgetinstance(), *sock );
96 int waitForConnection( int sock )
99 err = Hlisten( Hgetinstance(), sock, 1 );
101 Hclose( Hgetinstance(), sock );
106 client = Haccept( Hgetinstance(), sock, NULL, NULL );
109 Hclose( Hgetinstance(), sock );
116 char processCmd( char cmd )
121 switchOffLed( GREEN );
134 void startServer( nota_addr_t *addr )
136 int sock, client, err;
139 if (openNota( &sock, addr ) != 0)
141 printf( "openNota error!\n" );
144 printf( "Waiting for connection ...\n" );
145 switchOnLed( GREEN );
147 client = waitForConnection( sock );
150 printf( "waitForConnection error!\n" );
154 printf( "Server started and connected\n" );
159 err = Hrecv( Hgetinstance(), client, &buf, 1, 0 );
164 printf( "Recived: %c\n", buf );
166 buf = processCmd( buf );
168 err = Hsend( Hgetinstance(), client, &buf, 1, 0 );
177 Hclose( Hgetinstance(), client );
178 Hclose( Hgetinstance(), sock );
182 int main( int argc, char *argv[] )
188 printf( "Usage: ./test_server sid\n" );
192 sid = atoi( argv[1] );
196 addr.port = DEFAULT_PORT;
199 switchOnLed( GREEN );
203 printf( "Test server is registered with sid: %d\n", sid );
207 switchOffLed( GREEN );
208 startServer( &addr );