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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [peripheral/] [channels/] [channel.c] - Blame information for rev 189

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jeremybenn
/* channel.c -- Definition of types and structures for
2
   peripherals to communicate with host.  Adapted from UML.
3
 
4
   Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
5
   Copyright (C) 2008 Embecosm Limited
6
 
7
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8
 
9
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
10
 
11
   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
 
16
   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
 
21
   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
 
24
/* This program is commented throughout in a fashion suitable for processing
25
   with Doxygen. */
26
 
27
 
28
/* Autoconf and/or portability configuration */
29
#include "config.h"
30
#include "port.h"
31
 
32
/* System includes */
33
#include <stdlib.h>
34
#include <stdio.h>
35
#include <errno.h>
36
 
37
/* Package includes */
38
#include "channel.h"
39
#include "fd.h"
40
#include "file.h"
41
#include "tcp.h"
42
#include "tty.h"
43
#include "xterm.h"
44
 
45
struct channel_factory
46
{
47
  const char *name;
48
  const struct channel_ops *ops;
49
  struct channel_factory *next;
50
};
51
 
52
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
};
59
 
60
static struct channel_factory *head = &preloaded[0];
61
 
62
/* Forward declaration of static functions */
63
static struct channel_factory *find_channel_factory (const char *name);
64
 
65
struct channel *
66
channel_init (const char *descriptor)
67
{
68
  struct channel *retval;
69
  struct channel_factory *current;
70
  char *args, *name;
71
  int count;
72
 
73
  if (!descriptor)
74
    {
75
      return NULL;
76
    }
77
 
78
  retval = (struct channel *) calloc (1, sizeof (struct channel));
79
 
80
  if (!retval)
81
    {
82
      perror (descriptor);
83
      exit (1);
84
    }
85
 
86
  args = strchr (descriptor, ':');
87
 
88
  if (args)
89
    {
90
      count = args - descriptor;
91
      args++;
92
    }
93
  else
94
    {
95
      count = strlen (descriptor);
96
    }
97
 
98
  name = (char *) strndup (descriptor, count);
99
 
100
  if (!name)
101
    {
102
      perror (name);
103
      exit (1);
104
    }
105
 
106
  current = find_channel_factory (name);
107
 
108
  if (!current)
109
    {
110
      errno = ENODEV;
111
      perror (descriptor);
112
      exit (1);
113
    }
114
 
115
  retval->ops = current->ops;
116
 
117
  free (name);
118
 
119
  if (!retval->ops)
120
    {
121
      errno = ENODEV;
122
      perror (descriptor);
123
      exit (1);
124
    }
125
 
126
  if (retval->ops->init)
127
    {
128
      retval->data = (retval->ops->init) (args);
129
 
130
      if (!retval->data)
131
        {
132
          perror (descriptor);
133
          exit (1);
134
        }
135
    }
136
 
137
  return retval;
138
}
139
 
140
int
141
channel_open (struct channel *channel)
142
{
143
  if (channel && channel->ops && channel->ops->open)
144
    {
145
      return (channel->ops->open) (channel->data);
146
    }
147
  errno = ENOSYS;
148
  return -1;
149
}
150
 
151
int
152
channel_read (struct channel *channel, char *buffer, int size)
153
{
154
  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
}
161
 
162
int
163
channel_write (struct channel *channel, const char *buffer, int size)
164
{
165
  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
}
172
 
173
void
174
channel_close (struct channel *channel)
175
{
176
  if (channel && channel->ops && channel->ops->close)
177
    {
178
      (channel->ops->close) (channel->data);
179
    }
180
}
181
 
182
static struct channel_factory *
183
find_channel_factory (const char *name)
184
{
185
  struct channel_factory *current = head;
186
 
187
  current = head;
188
  while (current && strcmp (current->name, name))
189
    {
190
      current = current->next;
191
    }
192
 
193
  return current;
194
}

powered by: WebSVN 2.1.0

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