| 1 | /*
|
|---|
| 2 | Copyright (C) 1996-1998 by Patrick Reynolds
|
|---|
| 3 | Email: <no.email@noemail.com>
|
|---|
| 4 |
|
|---|
| 5 | This library is free software; you can redistribute it and/or
|
|---|
| 6 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 7 | License as published by the Free Software Foundation; either
|
|---|
| 8 | version 2.1 of the License, or (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This library is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | Lesser General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 16 | License along with this library; if not, write to the Free Software
|
|---|
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 18 |
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "PPort.h"
|
|---|
| 22 | #include "port.h"
|
|---|
| 23 |
|
|---|
| 24 | #include <unistd.h>
|
|---|
| 25 | #include <sys/types.h>
|
|---|
| 26 | #include <iostream>
|
|---|
| 27 |
|
|---|
| 28 | using namespace std;
|
|---|
| 29 |
|
|---|
| 30 | #define TEST_VALIDITY {if (this==NULL) return false;}
|
|---|
| 31 |
|
|---|
| 32 | PPort::PPort() {
|
|---|
| 33 | reset();
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | PPort::PPort(int ioPort) {
|
|---|
| 37 | reset();
|
|---|
| 38 | setPort(ioPort);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | PPort::~PPort()
|
|---|
| 42 | {
|
|---|
| 43 | delete currentPort;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | void PPort::reset() {
|
|---|
| 47 | bitArray=0;
|
|---|
| 48 | for (int i=0;i<8;++i) {
|
|---|
| 49 | assignedBit[i]=NULL;
|
|---|
| 50 | }
|
|---|
| 51 | currentPort=NULL;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | bool PPort::setPort(int ioPort) {
|
|---|
| 55 | TEST_VALIDITY;
|
|---|
| 56 | if (geteuid() != 0) {
|
|---|
| 57 | cerr << "must be setuid root control parallel port"<<endl;
|
|---|
| 58 | return false;
|
|---|
| 59 | }
|
|---|
| 60 | delete currentPort;
|
|---|
| 61 | reset();
|
|---|
| 62 | currentPort=new port_t(ioPort);
|
|---|
| 63 | return commit();
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | bool PPort::commit() {
|
|---|
| 67 | TEST_VALIDITY;
|
|---|
| 68 | if (currentPort) {
|
|---|
| 69 | currentPort->write_data(bitArray);
|
|---|
| 70 | return true;
|
|---|
| 71 | } else {
|
|---|
| 72 | return false;
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | bool PPort::setBit(const void * ID,int bit,bool stat) {
|
|---|
| 77 | TEST_VALIDITY;
|
|---|
| 78 | if (ID != assignedBit[bit]) {
|
|---|
| 79 | return false;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | if (stat) {
|
|---|
| 83 | bitArray |= (1<<bit);
|
|---|
| 84 | } else {
|
|---|
| 85 | bitArray &= ~(1<<bit);
|
|---|
| 86 | }
|
|---|
| 87 | return true;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | bool PPort::registerBit(const void * ID,int bit) {
|
|---|
| 91 | TEST_VALIDITY;
|
|---|
| 92 | if (assignedBit[bit] || currentPort==NULL) {
|
|---|
| 93 | return false;
|
|---|
| 94 | }
|
|---|
| 95 | assignedBit[bit]=ID;
|
|---|
| 96 | return true;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | bool PPort::unregisterBit(const void * ID,int bit) {
|
|---|
| 100 | TEST_VALIDITY;
|
|---|
| 101 | if (assignedBit[bit] != ID) {
|
|---|
| 102 | return false;
|
|---|
| 103 | }
|
|---|
| 104 | assignedBit[bit]=NULL;
|
|---|
| 105 | return true;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | bool PPort::isRegisterBit(const void * ID,int bit) const {
|
|---|
| 109 | TEST_VALIDITY;
|
|---|
| 110 | return (assignedBit[bit] == ID);
|
|---|
| 111 | }
|
|---|