1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
/* $Id: Modem.c++,v 1.5 2001/06/17 17:57:55 darren Exp $ */
/*
* Copyright (c) 1990-1996 Sam Leffler
* Copyright (c) 1991-1996 Silicon Graphics, Inc.
* HylaFAX is a trademark of Silicon Graphics
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include "Sys.h"
#include <errno.h>
#include "Modem.h"
#include "UUCPLock.h"
#include "RegExDict.h"
#include "TriggerRef.h"
#include "Dispatcher.h"
#include "config.h"
RegExDict* ModemGroup::classes = NULL; // modem classes
RegEx*
ModemGroup::find(const char* name)
{
if (classes == NULL)
return (NULL);
const RegExPtr* re = classes->find(name);
return (re ? (RegEx*) *(RegExPtr*) re : (RegEx*) NULL);
}
void
ModemGroup::reset()
{
delete classes, classes = NULL;
}
void
ModemGroup::set(const fxStr& name, RegEx* re)
{
if (classes == NULL)
classes = new RegExDict;
(*classes)[name] = re;
}
ModemLockWaitHandler::ModemLockWaitHandler(Modem& m) : modem(m) {}
ModemLockWaitHandler::~ModemLockWaitHandler() {}
void ModemLockWaitHandler::timerExpired(long, long)
{ faxQueueApp::instance().pollForModemLock(modem); }
QLink Modem::list; // master list of known modems
Modem::Modem(const fxStr& id)
: devID(id)
, fifoName(FAX_FIFO "." | id)
, lockHandler(*this)
{
state = DOWN; // modem down until notified otherwise
canpoll = true; // be optimistic
fd = -1; // force open on first use
priority = 255; // lowest priority
insert(list); // place at end of master list
lock = faxQueueApp::instance().getUUCPLock(faxApp::idToDev(id));
}
Modem::~Modem()
{
stopLockPolling();
delete lock;
if (fd >= 0)
Sys::close(fd);
remove();
if (!triggers.isEmpty()) // purge trigger references
TriggerRef::purge(triggers);
}
Modem*
Modem::modemExists(const fxStr& id)
{
for (ModemIter iter(list); iter.notDone(); iter++) {
Modem& modem = iter;
if (modem.devID == id)
return (&modem);
}
return (NULL);
}
/*
* Given a modem device-id, return a reference
* to a Modem instance. If no instance exists,
* one is created and added to the list of known
* modems.
*/
Modem&
Modem::getModemByID(const fxStr& id)
{
Modem* modem = modemExists(id);
return *(modem ? modem : new Modem(id));
}
/*
* Is the modem capable of handling the job.
*/
bool
Modem::isCapable(const Job& job) const
{
if (job.willpoll && !canpoll)
return (false);
if (job.pagewidth && !supportsPageWidthInMM(job.pagewidth))
return (false);
if (job.pagelength && !supportsPageLengthInMM(job.pagelength))
return (false);
if (job.resolution && !supportsVRes(job.resolution))
return (false);
return (true);
}
/*
* Find a modem that is capable of handling
* work associated with the specified job.
*/
Modem*
Modem::findModem(const Job& job, const DestControlInfo& dci)
{
RegEx* c = ModemGroup::find(job.device);
if (c) {
const fxStr& mdci = dci.getModem();
RegEx* cdci = mdci != "" ? ModemGroup::find(mdci) : NULL;
int loops = 2;
/*
* At first try to find modem strictly (suitable to job and destination rules)
* Then try to find modem not strictly (suitable to job rules only)
*/
for (int i = 0 ; i < loops ; i++) {
/*
* Job is assigned to a class of modems; search
* the set of modems in the class according to
* the order specified (if any order is specified).
*/
for (ModemIter iter(list); iter.notDone(); iter++) {
Modem& modem = iter;
if (c->Find(modem.devID) && modem.isCapable(job)) {
if (i == 0) {
if (cdci) { // destination assigned to a class of modems
if (!cdci->Find(modem.devID))
continue;
} else if (mdci != "") { // destination assigned to an explicit modem
if (mdci != modem.devID)
continue;
}
loops = 1; // there is a strictly suitable modem
}
if (modem.getState() != Modem::READY) {
continue;
}
/*
* Move modem to the end of the priority group
*/
modem.remove();
if (!list.isEmpty()) {
ModemIter iter(list);
for ( ; iter.notDone(); iter++) {
if (iter.modem().priority > modem.priority)
break;
}
modem.insert(iter.modem());
} else
modem.insert(list);
return (&modem);
}
}
}
} else {
/*
* Job is assigned to an explicit modem or to an
* invalid class or modem. Look for the modem
* in the list of known modems.
*/
for (ModemIter iter(list); iter.notDone(); iter++) {
Modem& modem = iter;
if (modem.getState() != Modem::READY)
continue;
if (job.device != modem.devID)
continue;
return (modem.isCapable(job) ? &modem : (Modem*) NULL);
}
}
return (NULL);
}
/*
* Assign a modem for use by a job.
*/
bool
Modem::assign(Job& job)
{
if (lock->lock()) { // lock modem for use
state = BUSY; // mark in use
job.modem = this; // assign modem to job
return (true);
} else {
/*
* Modem is locked for use by an outbound task.
* This should only happen when operating in a
* send-only environment--a modem is presumed
* ready for use, only to discover when it's
* actually assigned that it's really busy.
* We mark the modem BUSY here so that if the
* caller requests another modem we won't try
* to re-assign it in findModem.
*/
state = BUSY; // mark in use
return (false);
}
}
/*
* Release a previously assigned modem.
*/
void
Modem::release()
{
lock->unlock();
/*
* We must mark the modem READY when releasing the lock
* because we cannot depend on the faxgetty process
* notifying us if/when the modem status changes. This
* may result in overzealous scheduling of the modem, but
* since sender apps are expected to stablize the modem
* before starting work it shouldn't be too bad.
*/
state = READY;
}
/*
* UUCP lock file polling support. When a modem is not
* monitored by a faxgetty process outbound modem usage
* is ``discovered'' when we attempt to assign a modem
* to a job. At that time we mark the modem BUSY and
* kick off a polling procedure to watch for when the
* lock file is removed; at which time we mark the modem
* READY again and poke the scheduler in case jobs are
* waiting for a modem to come ready again.
*/
void
Modem::startLockPolling(long sec)
{
Dispatcher::instance().startTimer(sec, 0, &lockHandler);
}
void
Modem::stopLockPolling()
{
Dispatcher::instance().stopTimer(&lockHandler);
}
void
Modem::setCapabilities(const char* s)
{
canpoll = (s[0] == 'P'); // P/p for polling/no polling
char* tp;
caps.decodeCaps((u_int) strtoul(s+1, &tp, 16));// fax capabilities
if (tp && *tp == ':') { // modem priority
u_int pri = (u_int) strtoul(tp+1, NULL, 16);
if (pri != priority) {
/*
* Priority changed, move modem so that the list remains
* sorted by priority (highest priority to lowest priority).
*/
remove();
priority = pri;
if (!list.isEmpty()) {
ModemIter iter(list);
do {
if (iter.modem().priority > pri)
break;
iter++;
} while (iter.notDone());
insert(iter.modem());
} else
insert(list);
}
}
setState(READY); // XXX needed for static configuration
}
void Modem::setNumber(const char* cp) { number = cp; }
void Modem::setCommID(const char* cp) { commid = cp; }
void Modem::setState(ModemState s) { state = s; }
/*
* Return whether or not the modem supports the
* specified page width. We perhaps should accept
* page width when large page sizes are supported
* (but then the caller would need to know in order
* to pad the image to the appropriate width).
*/
bool
Modem::supportsPageWidthInMM(u_int w) const
{
if (w <= 110) // 864 pixels + slop
return caps.wd & BIT(WD_864);
else if (w <= 154) // 1216 pixels + slop
return caps.wd & BIT(WD_1216);
else if (w <= 218) // 1728 pixels + slop
return caps.wd & BIT(WD_1728);
else if (w <= 258) // 2048 pixels + slop
return caps.wd & BIT(WD_2048);
else if (w <= 306) // 2432 pixels + slop
return caps.wd & BIT(WD_2432);
else
return false;
}
bool
Modem::supportsPageWidthInPixels(u_int w) const
{
if (w <= 880) // 864 pixels + slop
return caps.wd & BIT(WD_864);
else if (w <= 1232) // 1216 pixels + slop
return caps.wd & BIT(WD_1216);
else if (w <= 1744) // 1728 pixels + slop
return caps.wd & BIT(WD_1728);
else if (w <= 2064) // 2048 pixels + slop
return caps.wd & BIT(WD_2048);
else if (w <= 2448) // 2432 pixels + slop
return caps.wd & BIT(WD_2432);
else
return false;
}
/*
* Return whether or not the modem supports the
* specified vertical resolution. Note that we're
* rather tolerant because of potential precision
* problems and general sloppiness on the part of
* applications writing TIFF files.
*/
bool
Modem::supportsVRes(float res) const
{
if (75 <= res && res < 120)
return caps.vr & BIT(VR_NORMAL);
else if (150 <= res && res < 250)
return caps.vr & BIT(VR_FINE);
else
return false;
}
/*
* Return whether or not the modem supports 2DMR.
*/
bool
Modem::supports2D() const
{
return caps.df & BIT(DF_2DMR);
}
/*
* Return whether or not the modem supports the
* specified page length. As above for vertical
* resolution we're lenient in what we accept.
*/
bool
Modem::supportsPageLengthInMM(u_int l) const
{
// XXX probably need to be more forgiving with values
if (270 < l && l <= 330)
return caps.ln & (BIT(LN_A4)|BIT(LN_INF));
else if (330 < l && l <= 390)
return caps.ln & (BIT(LN_B4)|BIT(LN_INF));
else
return caps.ln & BIT(LN_INF);
}
/*
* Broadcast a message to all known modems.
*/
void
Modem::broadcast(const fxStr& msg)
{
for (ModemIter iter(list); iter.notDone(); iter++) {
/*
* NB: We rarely send msgs, so for now close after each use.
* +1 here is so the \0 is included in the message.
*/
iter.modem().send(msg, msg.length()+1, false);
}
}
/*
* Send a message to the process managing a modem.
*/
bool
Modem::send(const char* msg, u_int msgLen, bool cacheFd)
{
bool retry = true;
again:
if (fd < 0) {
fd = Sys::open(fifoName, O_WRONLY|O_NDELAY);
if (fd < 0) {
#ifdef notdef
/*
* NB: We don't generate a message here because this
* is expected when faxgetty is not running.
*/
logError("MODEM " | devID | ": Cannot open FIFO: %m");
#endif
return (false);
}
}
int n = Sys::write(fd, msg, msgLen);
if (n == -1) {
if (errno == EBADF && retry) { // cached descriptor bad, retry
retry = false;
Sys::close(fd), fd = -1;
goto again;
}
logError("MODEM " | devID | ": Cannot send msg \"%.*s\"", msgLen, msg);
}
if (!cacheFd)
Sys::close(fd), fd = -1;
return (n == msgLen);
}
#include "StackBuffer.h"
void
Modem::encode(fxStackBuffer& buf) const
{
buf.put(devID, devID.length()+1);
buf.put(number, number.length()+1);
buf.put(commid, commid.length()+1);
switch (state) {
case DOWN: buf.put('D'); break;
case BUSY: buf.put('B'); break;
case READY: buf.put('R'); break;
}
buf.put(canpoll ? 'P' : 'p');
u_int ec = caps.encodeCaps();
buf.put((const char*) &ec, sizeof (u_int));
buf.put((const char*) &priority, sizeof (u_short));
}
|