UniSet  1.4.0
IOAccess.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Vitaly Lipatov
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00027 // --------------------------------------------------------------------------
00028 #ifndef _IOACCESS_H_
00029 #define _IOACCESS_H_
00030 // --------------------------------------------------------------------------
00031 #include <unistd.h>
00032 #include <fcntl.h>
00033 #include <iostream>
00034 
00035 #include "Exceptions.h"
00036 
00045 class IOAccess
00046 {
00047 public:
00048 
00050     IOAccess()
00051     {
00052         fd_port = open("/dev/port", O_RDWR | O_NDELAY);
00053         if ( fd_port == -1 )
00054             throw UniSetTypes::IOBadParam();
00055     }
00056     
00057     ~IOAccess()
00058     {
00059         close(fd_port);
00060     }
00061     
00065     void get(int port, void* buf, int size) const
00066     {
00067         if ( lseek(fd_port, port, SEEK_SET) == -1 )
00068             throw UniSetTypes::IOBadParam();
00069         ssize_t s = read(fd_port, buf, size);
00070         if ( s != size )
00071             throw UniSetTypes::IOBadParam();
00072     }
00073     
00075     int in(int port) const
00076     {
00077         char input;
00078         get(port, &input, 1);
00079         return input;
00080     }
00081     
00085     void put(int port, const void* buf, int size) const
00086     {
00087         if ( lseek(fd_port, port, SEEK_SET) == -1 )
00088             throw UniSetTypes::IOBadParam();
00089         ssize_t s = write(fd_port, buf, size);
00090         if ( s != size )
00091             throw UniSetTypes::IOBadParam();
00092     }
00093 
00095     void out(int port, int value) const
00096     {
00097         char output = value;
00098         put(port,&output,1);
00099     }
00100 
00101 private:
00102 
00104     int fd_port;
00105     
00106 };
00107 
00108 #endif