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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [peripheral/] [channels/] [fd.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1070 rprescott
/* fd.c -- Definition of functions and structures for
2
   peripheral to communicate with host through file descriptors
3
 
4
   Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
5
 
6
This file is part of OpenRISC 1000 Architectural Simulator.
7
 
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
 
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
 
22 1244 hpanther
#if HAVE_CONFIG_H
23
#include <config.h>
24
#endif
25
 
26 1070 rprescott
#include <sys/time.h>   /* struct timeval */
27
#include <sys/types.h>  /* fd_set */
28
#include <stdio.h>      /* perror */
29
#include <stdlib.h>     /* atoi */
30
#include <unistd.h>     /* read, write, select */
31 1244 hpanther
#if HAVE_MALLOC_H
32 1070 rprescott
#include <malloc.h>     /* calloc, free */
33 1244 hpanther
#endif
34 1070 rprescott
#include <string.h>     /* strchr */
35
#include <errno.h>      /* errno */
36
 
37
#include "channel.h"
38
#include "generic.h"
39
#include "fd.h"
40
 
41
static void * fd_init(const char * args)
42
{
43
        struct fd_channel * retval;
44
 
45
        retval = (struct fd_channel*)calloc(1, sizeof(struct fd_channel));
46
 
47
        if(!retval)
48
        {
49
                return NULL;
50
        }
51
 
52
 
53
        retval->fdin  = atoi(args);     /* so 0 if garbage */
54
                                        /* TODO: strtoul */
55
 
56
        args = strchr(args, ',');
57
 
58
        if(args)
59
        {
60
                retval->fdout  = atoi(args+1);
61
        }
62
        else
63
        {
64
                retval->fdout  = retval->fdin;
65
        }
66
 
67
        return (void*)retval;
68
}
69
 
70
int fd_read(void * data, char * buffer, int size)
71
{
72
        struct fd_channel * fds = (struct fd_channel *)data;
73
        struct timeval timeout = { 0, 0 };
74
        fd_set rfds;
75
        int retval;
76
 
77
        if(!fds)
78
        {
79
                errno = ENODEV;
80
                return -1;
81
        }
82
 
83
        FD_ZERO(&rfds);
84
        FD_SET(fds->fdin, &rfds);
85
 
86
        retval = select(fds->fdin+1, &rfds, NULL, NULL, &timeout);
87
 
88
        if(retval <= 0)
89
                return retval;
90
 
91
        return read(fds->fdin, buffer, size);
92
}
93
 
94
int fd_write(void * data, const char * buffer, int size)
95
{
96
        struct fd_channel * fds = (struct fd_channel *)data;
97
        if(fds)
98
        {
99
                return write(fds->fdout, buffer, size);
100
        }
101
        errno = ENODEV;
102
        return -1;
103
}
104
 
105
static int fd_isok(void * data)
106
{
107
        struct fd_channel * fds = (struct fd_channel *)data;
108
        if(fds)
109
        {
110
                return fds->fdout != -1 && fds->fdin != -1;
111
        }
112
        return 0;
113
}
114
 
115
static int fd_status_fd(int fd, char * str, int size)
116
{
117
        if(fd == -1)
118
                return snprintf(str, size, "closed");
119
 
120
        return snprintf(str, size, "opened(fd=%d)", fd);
121
}
122
 
123
char * fd_status(void * data)
124
{
125
        static char retval[256];
126
        int index = 0;
127
 
128
        struct fd_channel * fds = (struct fd_channel *)data;
129
        if(fds)
130
        {
131
                index += snprintf(retval + index, sizeof(retval) - index, "in ");
132
                index += fd_status_fd(fds->fdin, retval + index, sizeof(retval) - index);
133
 
134
                index += snprintf(retval + index, sizeof(retval) - index, "out ");
135
                index += fd_status_fd(fds->fdout, retval + index, sizeof(retval) - index);
136
        }
137
        else
138
        {
139
                snprintf(retval, sizeof(retval), "(null)");
140
        }
141
        return retval;
142
}
143
 
144
struct channel_ops fd_channel_ops =
145
{
146
        init:   fd_init,
147
        open:   generic_open,
148
        close:  generic_close,
149
        read:   fd_read,
150
        write:  fd_write,
151
        free:   generic_free,
152
        isok:   fd_isok,
153
        status: fd_status,
154
};
155
 
156
/*
157
 * Local variables:
158
 * c-file-style: "linux"
159
 * End:
160
 */

powered by: WebSVN 2.1.0

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