ParPort.C
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
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "Devices/ParPort.H"
00040
00041 #include <cstdlib>
00042 #include <cerrno>
00043 #include <csignal>
00044 #include <iostream>
00045
00046
00047 ParPort::ParPort(OptionManager& mgr, const std::string& descrName,
00048 const std::string& tagName) :
00049 ModelComponent(mgr, descrName, tagName),
00050 itsDevName(tagName+"DevName", this, "/dev/parport0"),
00051 itsFd(-1), itsData(0), itsNullMode(false)
00052 { }
00053
00054
00055 ParPort::~ParPort()
00056 { }
00057
00058
00059 void ParPort::start1()
00060 {
00061 if (itsDevName.getVal().compare("/dev/null") == 0)
00062 {
00063
00064
00065
00066 itsFd = -1;
00067 itsNullMode = true;
00068 return;
00069 }
00070
00071 #ifndef HAVE_LINUX_PARPORT_H
00072 LFATAL("Oops! I need to have <linux/parport.h>");
00073 #else
00074 const char *devnam = itsDevName.getVal().c_str();
00075
00076
00077 itsFd = open(devnam, O_RDWR);
00078 if (itsFd == -1)
00079 { PLERROR("Cannot open parallel port %s", devnam); return; }
00080
00081
00082 if (ioctl(itsFd, PPEXCL))
00083 {
00084 PLERROR("Cannot get exclusive access on %s", devnam);
00085 close(itsFd);
00086 itsFd = -1;
00087 return;
00088 }
00089
00090 if (ioctl(itsFd, PPCLAIM))
00091 {
00092 PLERROR("Cannot ioctl(PPCLAIM) on %s", devnam);
00093 close(itsFd);
00094 itsFd = -1;
00095 return;
00096 }
00097
00098
00099 int mode = IEEE1284_MODE_COMPAT;
00100 if (ioctl(itsFd, PPSETMODE, &mode))
00101 {
00102 PLERROR("Cannot switch to IEEE-1284 mode on %s", devnam);
00103 ioctl(itsFd, PPRELEASE);
00104 close(itsFd);
00105 itsFd = -1;
00106 return;
00107 }
00108 #endif // HAVE_LINUX_PARPORT_H
00109 }
00110
00111
00112 void ParPort::stop2()
00113 {
00114 #ifdef HAVE_LINUX_PARPORT_H
00115
00116 if (itsFd != -1)
00117 {
00118 if (ioctl(itsFd, PPRELEASE))
00119 PLFATAL("Cannot release parport %s", itsDevName.getVal().c_str());
00120
00121 close(itsFd);
00122 itsFd = -1;
00123 }
00124 #endif
00125
00126 itsNullMode = false;
00127 }
00128
00129
00130 void ParPort::WriteData(const byte mask, const byte newdata)
00131 {
00132 if (itsNullMode)
00133 {
00134 LERROR("Parallel port is disabled; "
00135 "attempt to write (data=0x%02x, mask=0x%02x) "
00136 "has been ignored", newdata, mask);
00137 return;
00138 }
00139
00140 if (itsFd < 0)
00141 LFATAL("Parallel port is not initialized");
00142
00143 #ifndef HAVE_LINUX_PARPORT_H
00144 LFATAL("Oops! I need to have <linux/parport.h>");
00145 #else
00146
00147 itsData &= ~mask;
00148
00149
00150 itsData |= (newdata & mask);
00151
00152
00153 if (ioctl(itsFd, PPWDATA, &itsData))
00154 PLFATAL("Writing to parport failed");
00155 #endif // HAVE_LINUX_PARPORT_H
00156 }
00157
00158
00159 byte ParPort::ReadStatus()
00160 {
00161 if (itsFd < 0)
00162 LFATAL("Parallel port is not initialized");
00163
00164 #ifndef HAVE_LINUX_PARPORT_H
00165 LFATAL("Oops! I need to have <linux/parport.h>");
00166 return 0;
00167 #else
00168 byte in;
00169 if (ioctl(itsFd, PPRSTATUS, &in))
00170 PLFATAL("Failed reading status of parallel port");
00171 return in;
00172 #endif // HAVE_LINUX_PARPORT_H
00173 }
00174
00175
00176 bool ParPort::ReadStatusPaperout()
00177 {
00178 #ifndef HAVE_LINUX_PARPORT_H
00179 LFATAL("Oops! I need to have <linux/parport.h>");
00180 return 0;
00181 #else
00182 return ( (this->ReadStatus() & PARPORT_STATUS_PAPEROUT) != 0 );
00183 #endif
00184 }
00185
00186
00187
00188
00189
00190