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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [peripheral/] [channels/] [fd.c] - Blame information for rev 1070

Go to most recent revision | 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
#include <sys/time.h>   /* struct timeval */
23
#include <sys/types.h>  /* fd_set */
24
#include <stdio.h>      /* perror */
25
#include <stdlib.h>     /* atoi */
26
#include <unistd.h>     /* read, write, select */
27
#include <malloc.h>     /* calloc, free */
28
#include <string.h>     /* strchr */
29
#include <errno.h>      /* errno */
30
 
31
#include "channel.h"
32
#include "generic.h"
33
#include "fd.h"
34
 
35
static void * fd_init(const char * args)
36
{
37
        struct fd_channel * retval;
38
 
39
        retval = (struct fd_channel*)calloc(1, sizeof(struct fd_channel));
40
 
41
        if(!retval)
42
        {
43
                return NULL;
44
        }
45
 
46
 
47
        retval->fdin  = atoi(args);     /* so 0 if garbage */
48
                                        /* TODO: strtoul */
49
 
50
        args = strchr(args, ',');
51
 
52
        if(args)
53
        {
54
                retval->fdout  = atoi(args+1);
55
        }
56
        else
57
        {
58
                retval->fdout  = retval->fdin;
59
        }
60
 
61
        return (void*)retval;
62
}
63
 
64
int fd_read(void * data, char * buffer, int size)
65
{
66
        struct fd_channel * fds = (struct fd_channel *)data;
67
        struct timeval timeout = { 0, 0 };
68
        fd_set rfds;
69
        int retval;
70
 
71
        if(!fds)
72
        {
73
                errno = ENODEV;
74
                return -1;
75
        }
76
 
77
        FD_ZERO(&rfds);
78
        FD_SET(fds->fdin, &rfds);
79
 
80
        retval = select(fds->fdin+1, &rfds, NULL, NULL, &timeout);
81
 
82
        if(retval <= 0)
83
                return retval;
84
 
85
        return read(fds->fdin, buffer, size);
86
}
87
 
88
int fd_write(void * data, const char * buffer, int size)
89
{
90
        struct fd_channel * fds = (struct fd_channel *)data;
91
        if(fds)
92
        {
93
                return write(fds->fdout, buffer, size);
94
        }
95
        errno = ENODEV;
96
        return -1;
97
}
98
 
99
static int fd_isok(void * data)
100
{
101
        struct fd_channel * fds = (struct fd_channel *)data;
102
        if(fds)
103
        {
104
                return fds->fdout != -1 && fds->fdin != -1;
105
        }
106
        return 0;
107
}
108
 
109
static int fd_status_fd(int fd, char * str, int size)
110
{
111
        if(fd == -1)
112
                return snprintf(str, size, "closed");
113
 
114
        return snprintf(str, size, "opened(fd=%d)", fd);
115
}
116
 
117
char * fd_status(void * data)
118
{
119
        static char retval[256];
120
        int index = 0;
121
 
122
        struct fd_channel * fds = (struct fd_channel *)data;
123
        if(fds)
124
        {
125
                index += snprintf(retval + index, sizeof(retval) - index, "in ");
126
                index += fd_status_fd(fds->fdin, retval + index, sizeof(retval) - index);
127
 
128
                index += snprintf(retval + index, sizeof(retval) - index, "out ");
129
                index += fd_status_fd(fds->fdout, retval + index, sizeof(retval) - index);
130
        }
131
        else
132
        {
133
                snprintf(retval, sizeof(retval), "(null)");
134
        }
135
        return retval;
136
}
137
 
138
struct channel_ops fd_channel_ops =
139
{
140
        init:   fd_init,
141
        open:   generic_open,
142
        close:  generic_close,
143
        read:   fd_read,
144
        write:  fd_write,
145
        free:   generic_free,
146
        isok:   fd_isok,
147
        status: fd_status,
148
};
149
 
150
/*
151
 * Local variables:
152
 * c-file-style: "linux"
153
 * End:
154
 */

powered by: WebSVN 2.1.0

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