ModemServer.h
上传用户:weiyuanprp
上传日期:2020-05-20
资源大小:1169k
文件大小:8k
源码类别:

传真(Fax)编程

开发平台:

C/C++

  1. /* $Id: ModemServer.h,v 1.6 2007/07/11 00:27:25 faxguy Exp $ */
  2. /*
  3.  * Copyright (c) 1990-1996 Sam Leffler
  4.  * Copyright (c) 1991-1996 Silicon Graphics, Inc.
  5.  * HylaFAX is a trademark of Silicon Graphics
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26. #ifndef _ModemServer_
  27. #define _ModemServer_
  28. /*
  29.  * Modem and Protocol Server.
  30.  */
  31. #include <stdarg.h>
  32. #include "ClassModem.h"
  33. #include "FaxRequest.h"
  34. #include "IOHandler.h"
  35. #include "ServerConfig.h"
  36. #include "Timeout.h"
  37. #include <sys/time.h>
  38. #ifdef tcsetattr
  39. #undef tcsetattr // workaround for SCO
  40. #endif
  41. #ifdef tcgetattr
  42. #undef tcgetattr // workaround for SCO
  43. #endif
  44. class FaxMachineInfo;
  45. class FaxMachineLog;
  46. /*
  47.  * This class defines the ``server process'' that manages
  48.  * a modem and implements the necessary protocol above the
  49.  * ClassModem driver interface.
  50.  */
  51. class ModemServer : public ServerConfig, public IOHandler {
  52. protected:
  53.     enum Parity {
  54. NONE            = 0,
  55. EVEN            = 1,
  56. ODD             = 2
  57.     };
  58. private:
  59.     FILE* statusFile; // server status file
  60.     bool deduceComplain; // if true, complain when can't deduce
  61.     bool changePriority; // change process priority by state
  62.     bool delayConfig; // suppress effects while reading config
  63.     fxStr dialRulesFile; // dial string rules filename
  64.     fxStr commid; // communication ID
  65. // generic modem-related stuff
  66.     int modemFd; // open modem file
  67.     fxStr modemDevice; // name of device to open
  68.     fxStr modemDevID; // device identifier
  69.     ClassModem* modem; // modem driver
  70.     Timeout timer; // timeout support class
  71.     bool timeout; // timeout during i/o operations
  72.     fxStr configFile; // pathname to configuration file
  73.     BaudRate curRate; // current termio baud rate
  74.     Parity curParity; // current termio parity setting
  75.     u_int curVMin; // current termio VMIN setting
  76.     u_int curVTime; // current termio VTIME setting
  77.     u_int setupAttempts; // consec. failed attempts to init modem
  78. // buffered i/o stuff
  79.     short rcvCC; // # bytes pending in rcvBuf
  80.     short rcvNext; // next available byte in rcvBuf
  81.     u_short rcvBit; // pending bit of rcvNext to send
  82.     int gotByte; // byte held for bit destruction
  83.     bool sawBlockEnd; // whether DLE+ETX has been seen
  84.     bool inputBuffered; // whether or not modem input is buffered
  85.     u_char rcvBuf[1024]; // receive buffering
  86.     friend class ClassModem;
  87. // configuration stuff
  88.     void setDialRules(const char* name);
  89.     void setModemSpeakerVolume(SpeakerVolume);
  90. // state-related stuff
  91.     static const char* stateNames[9];
  92.     static const char* stateStatus[9];
  93.     bool openDevice(const char* dev);
  94.     bool reopenDevice();
  95.     bool tcsetattr(int op, struct termios& term);
  96.     bool tcgetattr(const char* method, struct termios& term);
  97.     static void setFlow(struct termios&, FlowControl iflow, FlowControl oflow);
  98.     static void setParity(struct termios&, Parity);
  99. // general trace interface
  100.     void traceModemIO(const char* dir, const u_char* buf, u_int cc);
  101. protected:
  102.     enum ModemServerState {
  103. BASE = 0,
  104. RUNNING = 1,
  105. MODEMWAIT = 2,
  106. LOCKWAIT = 3,
  107. GETTYWAIT = 4,
  108. SENDING = 5,
  109. ANSWERING = 6,
  110. RECEIVING = 7,
  111. LISTENING = 8
  112.     };
  113.     ModemServerState state;
  114.     bool abortCall; // abort current send/receive
  115. // logging and tracing
  116.     FaxMachineLog* log; // current log device
  117.     ModemServer(const fxStr& deviceName, const fxStr& devID);
  118. // modem driver setup+teardown interfaces
  119.     virtual bool setupModem(bool isSend = true);
  120.     virtual bool readyModem();
  121.     virtual void discardModem(bool dropDTR);
  122.     virtual ClassModem* deduceModem(bool isSend = true);
  123. // low-level modem interfaces
  124.     int getModemFd(); // XXX
  125.     BaudRate getModemRate() const;
  126.     virtual fxStr getModemCapabilities() const;
  127. // modem i/o support
  128.     void timerExpired(long, long);
  129.     void sendDLEETX();
  130.     int getModemLine(char buf[], u_int bufSize, long ms = 0);
  131.     int getModemChar(long ms = 0, bool isquery = false);
  132.     int getModemBit(long ms = 0);
  133.     int getLastByte();
  134.     bool didBlockEnd();
  135.     void resetBlock();
  136.     bool isModemInput() const;
  137.     void flushModemInput();
  138.     bool putModem(const void* data, int n, long ms = 0);
  139.     bool putModem1(const void* data, int n, long ms = 0);
  140.     void startTimeout(long ms);
  141.     void stopTimeout(const char* whichdir);
  142. // modem line control
  143.     bool sendBreak(bool pause);
  144.     bool setBaudRate(BaudRate rate);
  145.     bool setBaudRate(BaudRate rate, FlowControl i, FlowControl o);
  146.     bool setParity(Parity parity);
  147.     bool setXONXOFF(FlowControl i, FlowControl o, SetAction act);
  148.     bool setDTR(bool on);
  149.     bool setInputBuffering(bool on);
  150.     bool modemStopOutput();
  151. // modem driver interfaces
  152.     bool modemWaitForRings(u_short rings, CallType&, CallID&);
  153.     CallType modemAnswerCall(AnswerType, fxStr&, const char* dialnumber = NULL);
  154.     void modemAnswerCallCmd(CallType);
  155.     void modemFlushInput();
  156.     void modemHangup();
  157. // server state and related control interfaces
  158.     void changeState(ModemServerState, long timeout = 0, const char* msg = NULL);
  159.     void setServerStatus(const char* fmt, ...);
  160.     void setProcessPriority(ModemServerState s);
  161. // system logging interfaces
  162.     void traceServer(const char* fmt ...);
  163.     void traceProtocol(const char* fmt ...);
  164.     void traceModemOp(const char* fmt ...);
  165.     void traceStatus(int kind, const char* fmt ...);
  166. // modem locking interfaces implemented in derived class
  167.     virtual bool canLockModem() = 0;
  168.     virtual bool lockModem() = 0;
  169.     virtual void unlockModem() = 0;
  170. // notification interfaces implemented in derived class
  171.     virtual void notifyModemReady() = 0;
  172.     virtual void notifyModemWedged() = 0;
  173. // configuration stuff
  174.     virtual void readConfig(const fxStr& filename);
  175.     virtual void resetConfig();
  176.     virtual void vconfigError(const char* fmt, va_list ap);
  177.     virtual void vconfigTrace(const char* fmt, va_list ap);
  178.     virtual void vdialrulesTrace(const char* fmt, va_list ap);
  179.     const fxStr& getConfigFile() const;
  180. // call/receive session start+end for controlling logging
  181.     void beginSession(const fxStr& number);
  182.     void endSession();
  183. // abort send/receive session
  184.     virtual void abortSession();
  185.     bool abortRequested();
  186. public:
  187.     const char* readyStateMsg;
  188.     virtual ~ModemServer();
  189.     virtual void open();
  190.     virtual void close();
  191.     virtual void initialize(int argc, char** argv);
  192.     ClassModem* getModem();
  193.     const fxStr& getModemDevice() const;
  194.     const fxStr& getModemDeviceID() const;
  195.     const fxStr& getModemNumber() const;
  196.     const fxStr& getCommID() const;
  197.     int getSessionTracing() const;
  198.     int getServerTracing() const;
  199.     void vtraceStatus(int kind, const char* fmt, va_list ap);
  200.     bool modemReady() const;
  201.     bool serverBusy() const;
  202. };
  203. inline ClassModem* ModemServer::getModem() { return modem; }
  204. inline int ModemServer::getModemFd() { return modemFd; }
  205. inline int ModemServer::getServerTracing() const
  206.     { return (tracingLevel); }
  207. inline int ModemServer::getSessionTracing() const
  208.     { return (logTracingLevel); }
  209. inline const fxStr& ModemServer::getModemDevice() const
  210.     { return modemDevice; }
  211. inline const fxStr& ModemServer::getModemDeviceID() const
  212.     { return modemDevID; }
  213. inline const fxStr& ModemServer::getModemNumber() const
  214.     { return FAXNumber; }
  215. inline const fxStr& ModemServer::getCommID() const { return commid; }
  216. inline bool ModemServer::isModemInput() const
  217.     { return rcvNext < rcvCC; }
  218. #endif /* _ModemServer_ */