Line 1... |
Line 1... |
/* fd.c -- Definition of functions and structures for
|
/* fd.c -- Definition of functions and structures for
|
peripheral to communicate with host through file descriptors
|
peripheral to communicate with host through file descriptors
|
|
|
Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
|
Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
|
|
Copyright (C) 2008 Embecosm Limited
|
|
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
|
|
|
This program is free software; you can redistribute it and/or modify
|
This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
|
|
|
#if HAVE_CONFIG_H
|
|
#include <config.h>
|
|
#endif
|
|
|
|
#include <sys/time.h> /* struct timeval */
|
|
#include <sys/types.h> /* fd_set */
|
|
#include <stdio.h> /* perror */
|
|
#include <stdlib.h> /* atoi */
|
|
#include <unistd.h> /* read, write, select */
|
|
#if HAVE_MALLOC_H
|
|
#include <malloc.h> /* calloc, free */
|
|
#endif
|
|
#include <string.h> /* strchr */
|
|
#include <errno.h> /* errno */
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it
|
|
under the terms of the GNU General Public License as published by the Free
|
|
Software Foundation; either version 3 of the License, or (at your option)
|
|
any later version.
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
more details.
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
/* This program is commented throughout in a fashion suitable for processing
|
|
with Doxygen. */
|
|
|
|
|
|
/* Autoconf and/or portability configuration */
|
|
#include "config.h"
|
|
#include "port.h"
|
|
|
|
/* System includes */
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
|
|
/* Package includes */
|
|
#include "fd.h"
|
#include "channel.h"
|
#include "channel.h"
|
#include "generic.h"
|
#include "generic.h"
|
#include "fd.h"
|
|
|
|
static void * fd_init(const char * args)
|
|
|
/* Forward declarations of static functions */
|
|
static void *fd_init (const char *args);
|
|
static int fd_isok (void *data);
|
|
static char *fd_status (void *data);
|
|
|
|
/*! Global data structure representing the operations for communicating
|
|
through a file descriptor channel */
|
|
struct channel_ops fd_channel_ops = {
|
|
.init = fd_init,
|
|
.open = generic_open,
|
|
.close = generic_close,
|
|
.read = fd_read,
|
|
.write = fd_write,
|
|
.free = generic_free,
|
|
.isok = fd_isok,
|
|
.status = fd_status,
|
|
};
|
|
|
|
|
|
static void *
|
|
fd_init (const char *args)
|
{
|
{
|
struct fd_channel * retval;
|
struct fd_channel * retval;
|
|
|
retval = (struct fd_channel*)calloc(1, sizeof(struct fd_channel));
|
retval = (struct fd_channel*)calloc(1, sizeof(struct fd_channel));
|
|
|
Line 65... |
Line 88... |
}
|
}
|
|
|
return (void*)retval;
|
return (void*)retval;
|
}
|
}
|
|
|
int fd_read(void * data, char * buffer, int size)
|
int
|
|
fd_read (void *data, char *buffer, int size)
|
{
|
{
|
struct fd_channel * fds = (struct fd_channel *)data;
|
struct fd_channel * fds = (struct fd_channel *)data;
|
struct timeval timeout = { 0, 0 };
|
struct timeval timeout = { 0, 0 };
|
fd_set rfds;
|
fd_set rfds;
|
int retval;
|
int retval;
|
Line 89... |
Line 113... |
return retval;
|
return retval;
|
|
|
return read(fds->fdin, buffer, size);
|
return read(fds->fdin, buffer, size);
|
}
|
}
|
|
|
int fd_write(void * data, const char * buffer, int size)
|
int
|
|
fd_write (void *data, const char *buffer, int size)
|
{
|
{
|
struct fd_channel * fds = (struct fd_channel *)data;
|
struct fd_channel * fds = (struct fd_channel *)data;
|
if(fds)
|
if(fds)
|
{
|
{
|
return write(fds->fdout, buffer, size);
|
return write(fds->fdout, buffer, size);
|
}
|
}
|
errno = ENODEV;
|
errno = ENODEV;
|
return -1;
|
return -1;
|
}
|
}
|
|
|
static int fd_isok(void * data)
|
static int
|
|
fd_isok (void *data)
|
{
|
{
|
struct fd_channel * fds = (struct fd_channel *)data;
|
struct fd_channel * fds = (struct fd_channel *)data;
|
if(fds)
|
if(fds)
|
{
|
{
|
return fds->fdout != -1 && fds->fdin != -1;
|
return fds->fdout != -1 && fds->fdin != -1;
|
}
|
}
|
return 0;
|
return 0;
|
}
|
}
|
|
|
static int fd_status_fd(int fd, char * str, int size)
|
static int
|
|
fd_status_fd (int fd, char *str, int size)
|
{
|
{
|
if(fd == -1)
|
if(fd == -1)
|
return snprintf(str, size, "closed");
|
return snprintf(str, size, "closed");
|
|
|
return snprintf(str, size, "opened(fd=%d)", fd);
|
return snprintf(str, size, "opened(fd=%d)", fd);
|
}
|
}
|
|
|
char * fd_status(void * data)
|
static char *
|
|
fd_status (void *data)
|
{
|
{
|
static char retval[256];
|
static char retval[256];
|
int index = 0;
|
int index = 0;
|
|
|
struct fd_channel * fds = (struct fd_channel *)data;
|
struct fd_channel * fds = (struct fd_channel *)data;
|
if(fds)
|
if(fds)
|
{
|
{
|
index += snprintf(retval + index, sizeof(retval) - index, "in ");
|
index += snprintf(retval + index, sizeof(retval) - index, "in ");
|
index += fd_status_fd(fds->fdin, retval + index, sizeof(retval) - index);
|
index +=
|
|
fd_status_fd (fds->fdin, retval + index, sizeof (retval) - index);
|
|
|
index += snprintf(retval + index, sizeof(retval) - index, "out ");
|
index += snprintf(retval + index, sizeof(retval) - index, "out ");
|
index += fd_status_fd(fds->fdout, retval + index, sizeof(retval) - index);
|
index +=
|
|
fd_status_fd (fds->fdout, retval + index, sizeof (retval) - index);
|
}
|
}
|
else
|
else
|
{
|
{
|
snprintf(retval, sizeof(retval), "(null)");
|
snprintf(retval, sizeof(retval), "(null)");
|
}
|
}
|
return retval;
|
return retval;
|
}
|
}
|
|
|
struct channel_ops fd_channel_ops =
|
|
{
|
|
init: fd_init,
|
|
open: generic_open,
|
|
close: generic_close,
|
|
read: fd_read,
|
|
write: fd_write,
|
|
free: generic_free,
|
|
isok: fd_isok,
|
|
status: fd_status,
|
|
};
|
|
|
|
/*
|
|
* Local variables:
|
|
* c-file-style: "linux"
|
|
* End:
|
|
*/
|
|
|
|
No newline at end of file
|
No newline at end of file
|