kstars
PPort.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "PPort.h"
00021 #include "port.h"
00022
00023 #include <unistd.h>
00024 #include <sys/types.h>
00025 #include <iostream>
00026
00027 using namespace std;
00028
00029 #define TEST_VALIDITY {if (this==NULL) return false;}
00030
00031 PPort::PPort() {
00032 reset();
00033 }
00034
00035 PPort::PPort(int ioPort) {
00036 reset();
00037 setPort(ioPort);
00038 }
00039
00040 void PPort::reset() {
00041 bitArray=0;
00042 for (int i=0;i<8;++i) {
00043 assignedBit[i]=NULL;
00044 }
00045 currentPort=NULL;
00046 }
00047
00048 bool PPort::setPort(int ioPort) {
00049 TEST_VALIDITY;
00050 if (geteuid() != 0) {
00051 cerr << "must be setuid root control parallel port"<<endl;
00052 return false;
00053 }
00054 if (currentPort) {
00055 delete currentPort;
00056 }
00057 reset();
00058 currentPort=new port_t(ioPort);
00059 return commit();
00060 }
00061
00062 bool PPort::commit() {
00063 TEST_VALIDITY;
00064 if (currentPort) {
00065 currentPort->write_data(bitArray);
00066 return true;
00067 } else {
00068 return false;
00069 }
00070 }
00071
00072 bool PPort::setBit(const void * ID,int bit,bool stat) {
00073 TEST_VALIDITY;
00074 if (ID != assignedBit[bit]) {
00075 return false;
00076 }
00077
00078 if (stat) {
00079 bitArray |= (1<<bit);
00080 } else {
00081 bitArray &= ~(1<<bit);
00082 }
00083 return true;
00084 }
00085
00086 bool PPort::registerBit(const void * ID,int bit) {
00087 TEST_VALIDITY;
00088 if (assignedBit[bit] || currentPort==NULL) {
00089 return false;
00090 }
00091 assignedBit[bit]=ID;
00092 return true;
00093 }
00094
00095 bool PPort::unregisterBit(const void * ID,int bit) {
00096 TEST_VALIDITY;
00097 if (assignedBit[bit] != ID) {
00098 return false;
00099 }
00100 assignedBit[bit]=NULL;
00101 return true;
00102 }
00103
00104 bool PPort::isRegisterBit(const void * ID,int bit) const {
00105 TEST_VALIDITY;
00106 return (assignedBit[bit] == ID);
00107 }