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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc1/] [or1ksim/] [peripheral/] [channels/] [channel.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1549 nogj
/* channel.c -- Definition of types and structures for
2
   peripherals to communicate with host.  Adapted from UML.
3 1070 rprescott
 
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 1244 hpanther
 
28 1748 jeremybenn
/* Autoconf and/or portability configuration */
29
#include "config.h"
30
#include "port.h"
31 1244 hpanther
 
32 1748 jeremybenn
/* System includes */
33
#include <stdlib.h>
34
#include <stdio.h>
35
#include <errno.h>
36 1070 rprescott
 
37 1748 jeremybenn
/* Package includes */
38 1070 rprescott
#include "channel.h"
39 1748 jeremybenn
#include "fd.h"
40
#include "file.h"
41
#include "tcp.h"
42
#include "tty.h"
43
#include "xterm.h"
44 1070 rprescott
 
45
struct channel_factory
46
{
47 1748 jeremybenn
  const char *name;
48
  const struct channel_ops *ops;
49
  struct channel_factory *next;
50 1070 rprescott
};
51
 
52 1748 jeremybenn
static struct channel_factory preloaded[] = {
53
  {"fd",    &fd_channel_ops,    &preloaded[1]},
54
  {"file",  &file_channel_ops,  &preloaded[2]},
55
  {"xterm", &xterm_channel_ops, &preloaded[3]},
56
  {"tcp",   &tcp_channel_ops,   &preloaded[4]},
57
  {"tty",   &tty_channel_ops,   NULL}
58 1070 rprescott
};
59
 
60 1748 jeremybenn
static struct channel_factory *head = &preloaded[0];
61 1070 rprescott
 
62 1748 jeremybenn
/* Forward declaration of static functions */
63
static struct channel_factory *find_channel_factory (const char *name);
64 1070 rprescott
 
65 1748 jeremybenn
struct channel *
66
channel_init (const char *descriptor)
67 1070 rprescott
{
68 1748 jeremybenn
  struct channel *retval;
69
  struct channel_factory *current;
70
  char *args, *name;
71
  int count;
72 1070 rprescott
 
73 1748 jeremybenn
  if (!descriptor)
74
    {
75
      return NULL;
76
    }
77 1070 rprescott
 
78 1748 jeremybenn
  retval = (struct channel *) calloc (1, sizeof (struct channel));
79 1070 rprescott
 
80 1748 jeremybenn
  if (!retval)
81
    {
82
      perror (descriptor);
83
      exit (1);
84
    }
85 1070 rprescott
 
86 1748 jeremybenn
  args = strchr (descriptor, ':');
87 1070 rprescott
 
88 1748 jeremybenn
  if (args)
89
    {
90
      count = args - descriptor;
91
      args++;
92
    }
93
  else
94
    {
95
      count = strlen (descriptor);
96
    }
97 1070 rprescott
 
98 1748 jeremybenn
  name = (char *) strndup (descriptor, count);
99 1070 rprescott
 
100 1748 jeremybenn
  if (!name)
101
    {
102
      perror (name);
103
      exit (1);
104
    }
105 1070 rprescott
 
106 1748 jeremybenn
  current = find_channel_factory (name);
107 1070 rprescott
 
108 1748 jeremybenn
  if (!current)
109
    {
110
      errno = ENODEV;
111
      perror (descriptor);
112
      exit (1);
113
    }
114 1070 rprescott
 
115 1748 jeremybenn
  retval->ops = current->ops;
116 1070 rprescott
 
117 1748 jeremybenn
  free (name);
118 1070 rprescott
 
119 1748 jeremybenn
  if (!retval->ops)
120
    {
121
      errno = ENODEV;
122
      perror (descriptor);
123
      exit (1);
124
    }
125 1070 rprescott
 
126 1748 jeremybenn
  if (retval->ops->init)
127
    {
128
      retval->data = (retval->ops->init) (args);
129 1070 rprescott
 
130 1748 jeremybenn
      if (!retval->data)
131 1070 rprescott
        {
132 1748 jeremybenn
          perror (descriptor);
133
          exit (1);
134 1070 rprescott
        }
135 1748 jeremybenn
    }
136 1070 rprescott
 
137 1748 jeremybenn
  return retval;
138 1070 rprescott
}
139
 
140 1748 jeremybenn
int
141
channel_open (struct channel *channel)
142 1070 rprescott
{
143 1748 jeremybenn
  if (channel && channel->ops && channel->ops->open)
144
    {
145
      return (channel->ops->open) (channel->data);
146
    }
147
  errno = ENOSYS;
148
  return -1;
149 1070 rprescott
}
150
 
151 1748 jeremybenn
int
152
channel_read (struct channel *channel, char *buffer, int size)
153 1070 rprescott
{
154 1748 jeremybenn
  if (channel && channel->ops && channel->ops->read)
155
    {
156
      return (channel->ops->read) (channel->data, buffer, size);
157
    }
158
  errno = ENOSYS;
159
  return -1;
160 1070 rprescott
}
161
 
162 1748 jeremybenn
int
163
channel_write (struct channel *channel, const char *buffer, int size)
164 1070 rprescott
{
165 1748 jeremybenn
  if (channel && channel->ops && channel->ops->write)
166
    {
167
      return (channel->ops->write) (channel->data, buffer, size);
168
    }
169
  errno = ENOSYS;
170
  return -1;
171 1070 rprescott
}
172
 
173 1748 jeremybenn
void
174
channel_close (struct channel *channel)
175 1070 rprescott
{
176 1748 jeremybenn
  if (channel && channel->ops && channel->ops->close)
177
    {
178
      (channel->ops->close) (channel->data);
179
    }
180 1070 rprescott
}
181
 
182 1748 jeremybenn
static struct channel_factory *
183
find_channel_factory (const char *name)
184 1070 rprescott
{
185 1748 jeremybenn
  struct channel_factory *current = head;
186 1070 rprescott
 
187 1748 jeremybenn
  current = head;
188
  while (current && strcmp (current->name, name))
189
    {
190
      current = current->next;
191
    }
192 1070 rprescott
 
193 1748 jeremybenn
  return current;
194 1070 rprescott
}

powered by: WebSVN 2.1.0

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