OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [fileio/] [v2_0/] [tests/] [select.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      select.c
4
//
5
//      Test select implementation
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//==========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):           nickg
44
// Contributors:        nickg
45
// Date:                2000-05-25
46
// Purpose:             Test select implementation
47
// Description:         
48
//                      
49
//                      
50
//                      
51
//
52
//####DESCRIPTIONEND####
53
//
54
//==========================================================================
55
 
56
#include <pkgconf/system.h>
57
#include <pkgconf/isoinfra.h>
58
 
59
#ifndef CYGINT_ISO_PTHREAD_IMPL
60
# define NA_MSG "POSIX threads needed to run test"
61
#endif
62
 
63
#include <cyg/infra/testcase.h>
64
 
65
#ifndef NA_MSG
66
 
67
#include <pkgconf/hal.h>
68
#include <pkgconf/kernel.h>
69
#include <pkgconf/io_fileio.h>
70
 
71
#ifdef CYGPKG_IO_SERIAL
72
#include <pkgconf/io_serial.h>
73
#endif
74
 
75
#define __ECOS 1                        // dont like this at all
76
 
77
#include <cyg/kernel/ktypes.h>         // base kernel types
78
#include <cyg/infra/cyg_trac.h>        // tracing macros
79
#include <cyg/infra/cyg_ass.h>         // assertion macros
80
 
81
#include <unistd.h>
82
#include <fcntl.h>
83
#include <sys/stat.h>
84
#include <errno.h>
85
#include <string.h>
86
 
87
#ifdef CYGPKG_NET
88
#include <network.h>
89
#include <arpa/inet.h>
90
#define TEST_NET
91
#endif
92
 
93
#ifdef CYGPKG_IO_SERIAL_LOOP
94
#define TEST_DEV
95
#endif
96
 
97
#include <pthread.h>
98
#include <signal.h>
99
 
100
 
101
#include <cyg/infra/diag.h>            // HAL polled output
102
 
103
//--------------------------------------------------------------------------
104
 
105
#define SHOW_RESULT( _fn, _res ) \
106
diag_printf("INFO: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
107
 
108
//--------------------------------------------------------------------------
109
// Thread stack.
110
 
111
char thread1_stack[PTHREAD_STACK_MIN*2];
112
char thread2_stack[PTHREAD_STACK_MIN*2];
113
 
114
//--------------------------------------------------------------------------
115
// Local variables
116
 
117
// Thread IDs
118
pthread_t thread1;
119
pthread_t thread2;
120
 
121
#ifdef TEST_NET        
122
struct sockaddr_in sa;
123
#endif
124
 
125
//--------------------------------------------------------------------------
126
// Test buffers
127
// The buffer size here must be less that the size of the serial device
128
// buffers since the serial devices do not currently implement flow
129
// control.
130
 
131
#define TEST_BUFSIZE 100
132
 
133
#ifdef TEST_NET        
134
static char buf1[TEST_BUFSIZE];
135
static char buf2[TEST_BUFSIZE];
136
static char buf3[TEST_BUFSIZE];
137
#endif
138
#ifdef TEST_DEV
139
static char sbuf1[TEST_BUFSIZE];
140
static char sbuf2[TEST_BUFSIZE];
141
static char sbuf3[TEST_BUFSIZE];
142
#endif
143
 
144
 
145
//--------------------------------------------------------------------------
146
 
147
void show_fdsets( char *s, int nfd, fd_set *rd, fd_set *wr, fd_set *ex )
148
{
149
    int i;
150
    diag_printf("INFO:<%s nfd %d ",s,nfd);
151
 
152
    if( rd )
153
    {
154
        diag_printf("rd: [");
155
        for( i = 0; i < nfd ; i++ )
156
            if( FD_ISSET( i, rd ) ) diag_printf("%d ",i);
157
        diag_printf("] ");
158
    }
159
    if( wr )
160
    {
161
        diag_printf("wr: [");
162
        for( i = 0; i < nfd ; i++ )
163
            if( FD_ISSET( i, wr ) ) diag_printf("%d ",i);
164
        diag_printf("] ");
165
    }
166
    if( ex )
167
    {
168
        diag_printf("ex: [");
169
        for( i = 0; i < nfd ; i++ )
170
            if( FD_ISSET( i, ex ) ) diag_printf("%d ",i);
171
        diag_printf("] ");
172
    }
173
 
174
    diag_printf(">\n");
175
}
176
 
177
 
178
//--------------------------------------------------------------------------
179
 
180
void *pthread_entry1( void *arg)
181
{
182
#ifdef TEST_NET        
183
    int fd = 0, fd2 = -1;
184
    struct sockaddr_in accsa;
185
    socklen_t accsa_len = sizeof(accsa);
186
    int netstate = 0;
187
#endif    
188
#ifdef TEST_DEV    
189
    int ser0;
190
    int serstate = 0;
191
#endif
192
#if defined(TEST_DEV) || defined(TEST_NET)
193
    int i;
194
    ssize_t done;
195
#endif    
196
    int netdone = 0;
197
    int serdone = 0;
198
    int err;
199
    fd_set rd, wr;
200
 
201
    CYG_TEST_INFO( "Thread 1 running" );
202
 
203
    FD_ZERO( &rd );
204
    FD_ZERO( &wr );
205
 
206
#ifdef TEST_DEV
207
 
208
    CYG_TEST_INFO( "Thread1: calling open()");
209
    ser0 = open("/dev/ser0", O_RDWR );
210
    if( ser0 < 0 ) SHOW_RESULT( open, ser0 );
211
    CYG_TEST_CHECK( ser0 >= 0, "open(/dev/ser0) returned error");
212
 
213
    FD_SET( ser0, &rd );
214
#else
215
    serdone = 1;
216
#endif
217
 
218
#ifdef TEST_NET
219
 
220
    for( i = 0; i < TEST_BUFSIZE; i++ ) buf1[i] = i;
221
 
222
    CYG_TEST_INFO( "Thread1: calling socket()");
223
    fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
224
    if( fd < 0 ) SHOW_RESULT( socket, fd );
225
    CYG_TEST_CHECK( fd >= 0, "socket() returned error");
226
 
227
    CYG_TEST_INFO( "Thread1: calling bind()");
228
    err = bind( fd, (struct sockaddr *)&sa, sizeof(sa));
229
    if( err < 0 ) SHOW_RESULT( bind, err );
230
    CYG_TEST_CHECK( err == 0, "bind() returned error");
231
 
232
    CYG_TEST_INFO( "Thread1: calling listen()");
233
    err = listen( fd, 3);
234
    if( err < 0 ) SHOW_RESULT( listen, err );
235
    CYG_TEST_CHECK( err == 0, "listen() returned error");
236
 
237
    FD_SET( fd, &rd );
238
 
239
#else
240
    netdone = 1;
241
#endif
242
 
243
    while(!(netdone && serdone))
244
    {
245
        fd_set rd_res = rd;
246
        fd_set wr_res = wr;
247
 
248
        CYG_TEST_INFO( "Thread1: calling select()");
249
        show_fdsets( "Thread1 request: ", 8, &rd_res, &wr_res, NULL );
250
        err = select( 8, &rd_res, &wr_res, NULL, NULL );
251
        if( err < 0 ) SHOW_RESULT( select, err );
252
        CYG_TEST_CHECK( err >= 0, "select() returned error");
253
        show_fdsets( "Thread1 result: ", 8, &rd_res, &wr_res, NULL );
254
 
255
#ifdef TEST_NET
256
        switch( netstate )
257
        {
258
        case 0:
259
            CYG_TEST_INFO( "Thread1: netstate 0");
260
            if( FD_ISSET( fd, &rd_res ) )
261
            {
262
                CYG_TEST_INFO( "Thread1: calling accept(fd)");
263
                fd2 = accept( fd, (struct sockaddr *)&accsa, &accsa_len );
264
                if( fd2 < 0 ) SHOW_RESULT( accept, fd2 );
265
                CYG_TEST_CHECK( fd2 >= 0, "accept() returned error");
266
 
267
                FD_CLR( fd, &rd );
268
                FD_SET( fd2, &wr );
269
 
270
                netstate++;
271
            }
272
            break;
273
 
274
 
275
        case 1:
276
            CYG_TEST_INFO( "Thread1: netstate 1");
277
            if( FD_ISSET( fd2, &wr_res ) )
278
            {
279
 
280
                CYG_TEST_INFO( "Thread1: calling write(fd2)");
281
                done = write( fd2, buf1, TEST_BUFSIZE);
282
                if( done != TEST_BUFSIZE ) SHOW_RESULT( write, done );
283
                CYG_TEST_CHECK( done == TEST_BUFSIZE, "write() returned bad size");
284
 
285
                FD_CLR( fd2, &wr );
286
                FD_SET( fd2, &rd );
287
 
288
                netstate++;
289
            }
290
            break;
291
 
292
        case 2:
293
            CYG_TEST_INFO( "Thread1: netstate 2");
294
            if( FD_ISSET( fd2, &rd_res ) )
295
            {
296
                CYG_TEST_INFO( "Thread1: calling read(fd2)");
297
                done = read( fd2, buf3, TEST_BUFSIZE);
298
                if( done != TEST_BUFSIZE ) SHOW_RESULT( read, done );
299
                CYG_TEST_CHECK( done == TEST_BUFSIZE, "read() returned bad size");
300
 
301
                for( i = 0; i < TEST_BUFSIZE; i++ )
302
                    if( buf1[i] != buf3[i] )
303
                        diag_printf("buf1[%d](%02x) != buf3[%d](%02x)\n",i,buf1[i],i,buf3[i]);
304
 
305
                FD_CLR( fd2, &rd );
306
 
307
                netstate++;
308
                netdone = 1;
309
                CYG_TEST_INFO( "Thread1: netdone");
310
            }
311
            break;
312
 
313
        }
314
#endif        
315
 
316
#ifdef TEST_DEV
317
        switch( serstate )
318
        {
319
        case 0:
320
            CYG_TEST_INFO( "Thread1: serstate 0");
321
            if( FD_ISSET( ser0, &rd_res ) )
322
            {
323
                CYG_TEST_INFO( "Thread1: calling read(ser0)");
324
                done = read( ser0, sbuf2, TEST_BUFSIZE);
325
                if( done != TEST_BUFSIZE ) SHOW_RESULT( read, done );
326
                CYG_TEST_CHECK( done == TEST_BUFSIZE, "read() returned bad size");
327
 
328
                for( i = 0; i < TEST_BUFSIZE; i++ )
329
                    if( sbuf1[i] != sbuf2[i] )
330
                        diag_printf("buf1[%d](%02x) != buf2[%d](%02x)\n",i,sbuf1[i],i,sbuf2[i]);
331
 
332
                FD_CLR( ser0, &rd );
333
                FD_SET( ser0, &wr );
334
                serstate++;
335
 
336
            }
337
            break;
338
 
339
        case 1:
340
            CYG_TEST_INFO( "Thread1: serstate 1");
341
            if( FD_ISSET( ser0, &wr_res ) )
342
            {
343
                CYG_TEST_INFO( "Thread1: calling write(ser0)");
344
                done = write( ser0, sbuf2, TEST_BUFSIZE);
345
                if( done != TEST_BUFSIZE ) SHOW_RESULT( write, done );
346
                CYG_TEST_CHECK( done == TEST_BUFSIZE, "write() returned bad size");
347
 
348
                FD_CLR( ser0, &wr );
349
 
350
                serstate++;
351
                serdone = 1;
352
                CYG_TEST_INFO( "Thread1: serdone");
353
            }
354
            else FD_SET( ser0, &wr );
355
            break;
356
        }
357
#endif        
358
    }
359
 
360
#ifdef TEST_NET    
361
    CYG_TEST_INFO( "Thread1: calling close(fd)");
362
    err = close(fd);
363
    if( err < 0 ) SHOW_RESULT( close, err );
364
    CYG_TEST_CHECK( err == 0, "close() returned error");
365
 
366
    if( fd2 >= 0 )
367
    {
368
        CYG_TEST_INFO( "Thread1: calling close(fd2)");
369
        err = close(fd2);
370
        if( err < 0 ) SHOW_RESULT( close, err );
371
        CYG_TEST_CHECK( err == 0, "close() returned error");
372
    }
373
#endif
374
#ifdef TEST_DEV
375
    CYG_TEST_INFO( "Thread1: calling close(ser0)");
376
    err = close(ser0);
377
    if( err < 0 ) SHOW_RESULT( close, err );
378
    CYG_TEST_CHECK( err == 0, "close() returned error");
379
#endif
380
 
381
    CYG_TEST_INFO( "Thread1: calling pthread_exit()");
382
    pthread_exit( arg );
383
}
384
 
385
//--------------------------------------------------------------------------
386
 
387
void *pthread_entry2( void *arg)
388
{
389
#ifdef TEST_NET        
390
    int fd;
391
    int netstate = 0;
392
#endif    
393
#ifdef TEST_DEV    
394
    int ser1;
395
    int serstate = 0;
396
#endif    
397
#if defined(TEST_DEV) || defined(TEST_NET)
398
    int i;
399
    ssize_t done;
400
#endif    
401
    int netdone = 0;
402
    int serdone = 0;
403
    int err;
404
    fd_set rd, wr;
405
 
406
    CYG_TEST_INFO( "Thread 2 running" );
407
 
408
    FD_ZERO( &rd );
409
    FD_ZERO( &wr );
410
 
411
#ifdef TEST_NET            
412
    CYG_TEST_INFO( "Thread2: calling socket()");
413
    fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
414
    if( fd < 0 ) SHOW_RESULT( socket, fd );
415
    CYG_TEST_CHECK( fd >= 0, "socket() returned error");
416
 
417
    CYG_TEST_INFO( "Thread2: calling connect()");
418
    err = connect( fd, (struct sockaddr *)&sa, sizeof(sa));
419
    if( err < 0 ) SHOW_RESULT( connect, err );
420
    CYG_TEST_CHECK( err == 0, "connect() returned error");
421
 
422
    FD_SET( fd, &rd );
423
#else
424
    netdone = 1;
425
#endif
426
 
427
#ifdef TEST_DEV
428
    for( i = 0; i < TEST_BUFSIZE; i++ ) sbuf1[i] = i;
429
 
430
    CYG_TEST_INFO( "Thread2: calling open(/dev/ser1)");
431
    ser1 = open("/dev/ser1", O_RDWR );
432
    if( ser1 < 0 ) SHOW_RESULT( open, ser1 );
433
    CYG_TEST_CHECK( ser1 >= 0, "open(/dev/ser1) returned error");
434
 
435
    CYG_TEST_INFO( "Thread2: calling write(ser1)");
436
    done = write( ser1, sbuf1, TEST_BUFSIZE);
437
    if( done != TEST_BUFSIZE ) SHOW_RESULT( write, done );
438
    CYG_TEST_CHECK( done == TEST_BUFSIZE, "write() returned bad size");
439
 
440
    FD_SET( ser1, &wr );
441
 
442
#else
443
    serdone = 1;
444
#endif    
445
 
446
    while(!(netdone && serdone))
447
    {
448
        fd_set rd_res = rd;
449
        fd_set wr_res = wr;
450
 
451
        CYG_TEST_INFO( "Thread2: calling select()");
452
        show_fdsets( "Thread2 request: ", 8, &rd_res, &wr_res, NULL );
453
        err = select( 8, &rd_res, &wr_res, NULL, NULL );
454
        if( err < 0 ) SHOW_RESULT( select, err );
455
        CYG_TEST_CHECK( err >= 0, "select() returned error");
456
        show_fdsets( "Thread2 result: ", 8, &rd_res, &wr_res, NULL );
457
 
458
#ifdef TEST_NET
459
        switch( netstate )
460
        {
461
        case 0:
462
            CYG_TEST_INFO( "Thread2: netstate 0");
463
            if( FD_ISSET( fd, &rd_res ) )
464
            {
465
                CYG_TEST_INFO( "Thread2: calling read()");
466
                done = read( fd, buf2, TEST_BUFSIZE);
467
                if( done != TEST_BUFSIZE ) SHOW_RESULT( read, done );
468
                CYG_TEST_CHECK( done == TEST_BUFSIZE, "read() returned bad size");
469
 
470
                for( i = 0; i < TEST_BUFSIZE; i++ )
471
                    if( buf1[i] != buf2[i] )
472
                        diag_printf("buf1[%d](%02x) != buf2[%d](%02x)\n",i,buf1[i],i,buf2[i]);
473
 
474
                netstate++;
475
 
476
                FD_CLR( fd, &rd );
477
                FD_SET( fd, &wr );
478
            }
479
            break;
480
 
481
        case 1:
482
            CYG_TEST_INFO( "Thread2: netstate 1");
483
            if( FD_ISSET( fd, &wr_res ) )
484
            {
485
 
486
                CYG_TEST_INFO( "Thread2: calling write()");
487
                done = write( fd, buf2, TEST_BUFSIZE);
488
                if( done != TEST_BUFSIZE ) SHOW_RESULT( write, done );
489
                CYG_TEST_CHECK( done == TEST_BUFSIZE, "write() returned bad size");
490
 
491
                FD_CLR( fd, &wr );
492
 
493
                netstate++;
494
                netdone = 1;
495
                CYG_TEST_INFO( "Thread2: netdone");
496
            }
497
            break;
498
 
499
        }
500
#endif
501
 
502
#ifdef TEST_DEV
503
        switch( serstate )
504
        {
505
        case 0:
506
            CYG_TEST_INFO( "Thread2: serstate 0");
507
            if( FD_ISSET( ser1, &wr_res ) )
508
            {
509
                FD_CLR( ser1, &wr );
510
                FD_SET( ser1, &rd );
511
                serstate++;
512
            }
513
            break;
514
 
515
        case 1:
516
            CYG_TEST_INFO( "Thread2: serstate 1");
517
            if( FD_ISSET( ser1, &rd_res ) )
518
            {
519
                CYG_TEST_INFO( "Thread2: calling read(ser1)");
520
                done = read( ser1, sbuf3, TEST_BUFSIZE);
521
                if( done != TEST_BUFSIZE ) SHOW_RESULT( read, done );
522
                CYG_TEST_CHECK( done == TEST_BUFSIZE, "read() returned bad size");
523
 
524
                for( i = 0; i < TEST_BUFSIZE; i++ )
525
                    if( sbuf1[i] != sbuf3[i] )
526
                        diag_printf("sbuf1[%d](%02x) != sbuf3[%d](%02x)\n",i,sbuf1[i],i,sbuf3[i]);
527
 
528
                FD_CLR( ser1, &rd );
529
 
530
                serstate++;
531
                serdone = 1;
532
                CYG_TEST_INFO( "Thread2: serdone");
533
            }
534
            break;
535
        }
536
#endif        
537
    }
538
 
539
#ifdef TEST_NET    
540
    CYG_TEST_INFO( "Thread2: calling close(fd)");
541
    err = close(fd);
542
    if( err < 0 ) SHOW_RESULT( close, err );
543
    CYG_TEST_CHECK( err == 0, "close() returned error");
544
#endif
545
#ifdef TEST_DEV
546
    CYG_TEST_INFO( "Thread2: calling close(ser1)");
547
    err = close(ser1);
548
    if( err < 0 ) SHOW_RESULT( close, err );
549
    CYG_TEST_CHECK( err == 0, "close(ser1) returned error");
550
#endif
551
 
552
 
553
    CYG_TEST_INFO( "Thread2: calling pthread_exit()");
554
    pthread_exit( arg );
555
}
556
 
557
//==========================================================================
558
// main
559
 
560
int main( int argc, char **argv )
561
{
562
    void *retval;
563
    pthread_attr_t attr;
564
    struct sched_param schedparam;
565
 
566
    CYG_TEST_INIT();
567
 
568
#ifdef TEST_NET
569
    sa.sin_family = AF_INET;
570
    sa.sin_len = sizeof(sa);
571
    inet_aton("127.0.0.1", &sa.sin_addr);
572
    sa.sin_port = htons(1234);
573
    init_all_network_interfaces();
574
#endif
575
 
576
    // Create test threads
577
 
578
    {
579
        pthread_attr_init( &attr );
580
 
581
        schedparam.sched_priority = 10;
582
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
583
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
584
        pthread_attr_setschedparam( &attr, &schedparam );
585
        pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
586
        pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );
587
 
588
        pthread_create( &thread1,
589
                        &attr,
590
                        pthread_entry1,
591
                        (void *)0x12345671);
592
    }
593
 
594
    {
595
        pthread_attr_init( &attr );
596
 
597
        schedparam.sched_priority = 5;
598
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
599
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
600
        pthread_attr_setschedparam( &attr, &schedparam );
601
        pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
602
        pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );
603
 
604
        pthread_create( &thread2,
605
                        &attr,
606
                        pthread_entry2,
607
                        (void *)0x12345672);
608
    }
609
 
610
    // Now join with thread1
611
    CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
612
    pthread_join( thread1, &retval );
613
 
614
    // And thread 2
615
    CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
616
    pthread_join( thread2, &retval );
617
 
618
    CYG_TEST_PASS_FINISH("select");
619
}
620
 
621
#else
622
 
623
//==========================================================================
624
// main
625
 
626
void cyg_start(void)
627
{
628
    CYG_TEST_INIT();
629
 
630
    CYG_TEST_NA(NA_MSG);
631
}
632
 
633
#endif
634
 
635
 
636
// -------------------------------------------------------------------------
637
// EOF socket.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.