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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc3/] [or1ksim/] [peripheral/] [channels/] [fd.c] - Blame information for rev 1751

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 1748 jeremybenn
   Copyright (C) 2008 Embecosm Limited
6 1070 rprescott
 
7 1748 jeremybenn
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8 1070 rprescott
 
9 1748 jeremybenn
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
10 1070 rprescott
 
11 1748 jeremybenn
   This program is free software; you can redistribute it and/or modify it
12
   under the terms of the GNU General Public License as published by the Free
13
   Software Foundation; either version 3 of the License, or (at your option)
14
   any later version.
15 1070 rprescott
 
16 1748 jeremybenn
   This program is distributed in the hope that it will be useful, but WITHOUT
17
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19
   more details.
20 1070 rprescott
 
21 1748 jeremybenn
   You should have received a copy of the GNU General Public License along
22
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23 1244 hpanther
 
24 1748 jeremybenn
/* This program is commented throughout in a fashion suitable for processing
25
   with Doxygen. */
26 1070 rprescott
 
27 1748 jeremybenn
 
28
/* Autoconf and/or portability configuration */
29
#include "config.h"
30
#include "port.h"
31
 
32
/* System includes */
33
#include <stdlib.h>
34 1751 jeremybenn
#include <sys/types.h>
35
#include <sys/time.h>
36 1748 jeremybenn
#include <unistd.h>
37
#include <errno.h>
38
#include <stdio.h>
39
 
40
/* Package includes */
41
#include "fd.h"
42 1070 rprescott
#include "channel.h"
43
#include "generic.h"
44
 
45 1748 jeremybenn
 
46
/* Forward declarations of static functions */
47
static void *fd_init (const char *args);
48
static int   fd_isok (void *data);
49
static char *fd_status (void *data);
50
 
51
/*! Global data structure representing the operations for communicating
52
  through a file descriptor channel */
53
struct channel_ops  fd_channel_ops = {
54
        .init   = fd_init,
55
        .open   = generic_open,
56
        .close  = generic_close,
57
        .read   = fd_read,
58
        .write  = fd_write,
59
        .free   = generic_free,
60
        .isok   = fd_isok,
61
        .status = fd_status,
62
};
63
 
64
 
65
static void *
66
fd_init (const char *args)
67 1070 rprescott
{
68 1748 jeremybenn
  struct fd_channel *retval;
69 1070 rprescott
 
70 1748 jeremybenn
  retval = (struct fd_channel *) calloc (1, sizeof (struct fd_channel));
71 1070 rprescott
 
72 1748 jeremybenn
  if (!retval)
73
    {
74
      return NULL;
75
    }
76 1070 rprescott
 
77
 
78 1748 jeremybenn
  retval->fdin = atoi (args);   /* so 0 if garbage */
79
  /* TODO: strtoul */
80 1070 rprescott
 
81 1748 jeremybenn
  args = strchr (args, ',');
82 1070 rprescott
 
83 1748 jeremybenn
  if (args)
84
    {
85
      retval->fdout = atoi (args + 1);
86
    }
87
  else
88
    {
89
      retval->fdout = retval->fdin;
90
    }
91 1070 rprescott
 
92 1748 jeremybenn
  return (void *) retval;
93 1070 rprescott
}
94
 
95 1748 jeremybenn
int
96
fd_read (void *data, char *buffer, int size)
97 1070 rprescott
{
98 1748 jeremybenn
  struct fd_channel *fds = (struct fd_channel *) data;
99
  struct timeval timeout = { 0, 0 };
100
  fd_set rfds;
101
  int retval;
102 1070 rprescott
 
103 1748 jeremybenn
  if (!fds)
104
    {
105
      errno = ENODEV;
106
      return -1;
107
    }
108 1070 rprescott
 
109 1748 jeremybenn
  FD_ZERO (&rfds);
110
  FD_SET (fds->fdin, &rfds);
111 1070 rprescott
 
112 1748 jeremybenn
  retval = select (fds->fdin + 1, &rfds, NULL, NULL, &timeout);
113 1070 rprescott
 
114 1748 jeremybenn
  if (retval <= 0)
115
    return retval;
116 1070 rprescott
 
117 1748 jeremybenn
  return read (fds->fdin, buffer, size);
118 1070 rprescott
}
119
 
120 1748 jeremybenn
int
121
fd_write (void *data, const char *buffer, int size)
122 1070 rprescott
{
123 1748 jeremybenn
  struct fd_channel *fds = (struct fd_channel *) data;
124
  if (fds)
125
    {
126
      return write (fds->fdout, buffer, size);
127
    }
128
  errno = ENODEV;
129
  return -1;
130 1070 rprescott
}
131
 
132 1748 jeremybenn
static int
133
fd_isok (void *data)
134 1070 rprescott
{
135 1748 jeremybenn
  struct fd_channel *fds = (struct fd_channel *) data;
136
  if (fds)
137
    {
138
      return fds->fdout != -1 && fds->fdin != -1;
139
    }
140
  return 0;
141 1070 rprescott
}
142
 
143 1748 jeremybenn
static int
144
fd_status_fd (int fd, char *str, int size)
145 1070 rprescott
{
146 1748 jeremybenn
  if (fd == -1)
147
    return snprintf (str, size, "closed");
148 1070 rprescott
 
149 1748 jeremybenn
  return snprintf (str, size, "opened(fd=%d)", fd);
150 1070 rprescott
}
151
 
152 1748 jeremybenn
static char *
153
fd_status (void *data)
154 1070 rprescott
{
155 1748 jeremybenn
  static char retval[256];
156
  int index = 0;
157 1070 rprescott
 
158 1748 jeremybenn
  struct fd_channel *fds = (struct fd_channel *) data;
159
  if (fds)
160
    {
161
      index += snprintf (retval + index, sizeof (retval) - index, "in ");
162
      index +=
163
        fd_status_fd (fds->fdin, retval + index, sizeof (retval) - index);
164 1070 rprescott
 
165 1748 jeremybenn
      index += snprintf (retval + index, sizeof (retval) - index, "out ");
166
      index +=
167
        fd_status_fd (fds->fdout, retval + index, sizeof (retval) - index);
168
    }
169
  else
170
    {
171
      snprintf (retval, sizeof (retval), "(null)");
172
    }
173
  return retval;
174 1070 rprescott
}
175
 

powered by: WebSVN 2.1.0

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