1 /***************************************************************************
4 * Wed Nov 18 10:46:06 2009
5 * Copyright 2009 Bartlomiej Swiercz
6 * <bartlomiej.swiercz@teleca.com>
7 ****************************************************************************/
13 #include "ts7200_client.h"
15 typedef enum { GREEN, RED } led_color_t;
19 printf( "TS-7200 initialized\n" );
22 void switchOnLed( led_color_t color )
27 printf( "Green led switched on\n" );
30 printf( "Red led switched on\n" );
33 printf( "Unknown led!\n" );
37 void switchOffLed( led_color_t color )
42 printf( "Green led switched off\n" );
45 printf( "Red led switched off\n" );
48 printf( "Unknown led!\n" );
52 int openNota( int *sock, nota_addr_t *addr )
55 *sock = Hsocket( Hgetinstance(), AF_NOTA, SOCK_STREAM, 0 );
62 err = Hbind( Hgetinstance(), *sock, (struct sockaddr*) addr, sizeof(*addr) );
65 Hclose( Hgetinstance(), *sock );
73 int waitForConnection( int sock )
76 err = Hlisten( Hgetinstance(), sock, 1 );
78 Hclose( Hgetinstance(), sock );
83 client = Haccept( Hgetinstance(), sock, NULL, NULL );
86 Hclose( Hgetinstance(), sock );
93 char processCmd( char cmd )
98 switchOffLed( GREEN );
111 void startServer( nota_addr_t *addr )
113 int sock, client, err;
116 if (openNota( &sock, addr ) != 0)
118 printf( "openNota error!\n" );
121 printf( "Waiting for connection ...\n" );
122 switchOnLed( GREEN );
124 client = waitForConnection( sock );
127 printf( "waitForConnection error!\n" );
131 printf( "Server started and connected\n" );
136 err = Hrecv( Hgetinstance(), client, &buf, 1, 0 );
141 printf( "Recived: %c\n", buf );
143 buf = processCmd( buf );
145 err = Hsend( Hgetinstance(), client, &buf, 1, 0 );
154 Hclose( Hgetinstance(), client );
155 Hclose( Hgetinstance(), sock );
159 int main( int argc, char *argv[] )
165 printf( "Usage: ./test_server sid\n" );
169 sid = atoi( argv[1] );
173 addr.port = DEFAULT_PORT;
176 switchOnLed( GREEN );
180 printf( "Test server is registered with sid: %d\n", sid );
184 switchOffLed( GREEN );
185 startServer( &addr );