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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_44/] [or1ksim/] [peripheral/] [channels/] [fd.c] - Diff between revs 1413 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1413 Rev 1765
/* 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>
 
 
This file is part of OpenRISC 1000 Architectural Simulator.
This file is part of OpenRISC 1000 Architectural Simulator.
 
 
This program is free software; you can redistribute it and/or modify
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
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
(at your option) any later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
 
 
#if HAVE_CONFIG_H
#if HAVE_CONFIG_H
#include <config.h>
#include <config.h>
#endif
#endif
 
 
#include <sys/time.h>   /* struct timeval */
#include <sys/time.h>   /* struct timeval */
#include <sys/types.h>  /* fd_set */
#include <sys/types.h>  /* fd_set */
#include <stdio.h>      /* perror */
#include <stdio.h>      /* perror */
#include <stdlib.h>     /* atoi */
#include <stdlib.h>     /* atoi */
#include <unistd.h>     /* read, write, select */
#include <unistd.h>     /* read, write, select */
#if HAVE_MALLOC_H
#if HAVE_MALLOC_H
#include <malloc.h>     /* calloc, free */
#include <malloc.h>     /* calloc, free */
#endif
#endif
#include <string.h>     /* strchr */
#include <string.h>     /* strchr */
#include <errno.h>      /* errno */
#include <errno.h>      /* errno */
 
 
#include "channel.h"
#include "channel.h"
#include "generic.h"
#include "generic.h"
#include "fd.h"
#include "fd.h"
 
 
static void * fd_init(const char * args)
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));
 
 
        if(!retval)
        if(!retval)
        {
        {
                return NULL;
                return NULL;
        }
        }
 
 
 
 
        retval->fdin  = atoi(args);     /* so 0 if garbage */
        retval->fdin  = atoi(args);     /* so 0 if garbage */
                                        /* TODO: strtoul */
                                        /* TODO: strtoul */
 
 
        args = strchr(args, ',');
        args = strchr(args, ',');
 
 
        if(args)
        if(args)
        {
        {
                retval->fdout  = atoi(args+1);
                retval->fdout  = atoi(args+1);
        }
        }
        else
        else
        {
        {
                retval->fdout  = retval->fdin;
                retval->fdout  = retval->fdin;
        }
        }
 
 
        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;
 
 
        if(!fds)
        if(!fds)
        {
        {
                errno = ENODEV;
                errno = ENODEV;
                return -1;
                return -1;
        }
        }
 
 
        FD_ZERO(&rfds);
        FD_ZERO(&rfds);
        FD_SET(fds->fdin, &rfds);
        FD_SET(fds->fdin, &rfds);
 
 
        retval = select(fds->fdin+1, &rfds, NULL, NULL, &timeout);
        retval = select(fds->fdin+1, &rfds, NULL, NULL, &timeout);
 
 
        if(retval <= 0)
        if(retval <= 0)
                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)
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 =
struct channel_ops fd_channel_ops =
{
{
        init:   fd_init,
        init:   fd_init,
        open:   generic_open,
        open:   generic_open,
        close:  generic_close,
        close:  generic_close,
        read:   fd_read,
        read:   fd_read,
        write:  fd_write,
        write:  fd_write,
        free:   generic_free,
        free:   generic_free,
        isok:   fd_isok,
        isok:   fd_isok,
        status: fd_status,
        status: fd_status,
};
};
 
 
/*
/*
 * Local variables:
 * Local variables:
 * c-file-style: "linux"
 * c-file-style: "linux"
 * End:
 * End:
 */
 */
 
 

powered by: WebSVN 2.1.0

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