⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modem.c

📁 C++ SOCKET 类
💻 C
字号:
#include <thread_io.h>#include <queue>extern "C" {# include <stdarg.h># include <fcntl.h># include <termios.h># include <unistd.h># include <sys/syslog.h># include <sys/types.h># include <sys/stat.h>};#include "LockFile.h"#define _(t)  tusing namespace thread;class event_data {public:  enum {    OK,    CONNECT,    BUSY,    RING,    NOCARRIER,    ERROR,    NODIALTONE,    NOANSWER,    DLE,    PACKAGE,    STRING  };private:  int     _type;  int     _pri;  string  _s;public:  event_data() { };  ~event_data() { };  string& s() { return _s; };  int& type() { return _type; };  int& pri()  { return _pri; };  bool operator< (event_data& e) { return _pri < e.pri(); };  bool operator> (event_data& e) { return _pri > e.pri(); };  bool operator== (event_data& e) { return _pri == e.pri(); };  bool operator>= (event_data& e) { return _pri >= e.pri(); };  bool operator<= (event_data& e) { return _pri <= e.pri(); };};class modem_io : public asynchronous_input {public:  enum __state {    Command,    Text,    Direct  };private:  priority_queue<event_data*> _events;  __state                     _state;protected:  void got(const char *buf, int len) {    event_data *ep;    int i=0;    while ( i < len ) {      ep = new event_data;      if ( _state != Direct ) {	while ( i < len && (buf[i] == '\r' || buf[i] == '\n') )	  i++;	while ( i < len && buf[i] != '\r' && buf[i] != '\n' )	  ep->s() += buf[i++];	while ( i < len && (buf[i] == '\r' || buf[i] == '\n') )	  i++;	ep->type() = event_data::STRING;	ep->pri()  = 2;	if ( _state == Command ) {	  ep->pri() = 0;	  if ( strcasecmp(ep->s().c_str(),"OK") == 0 )	    ep->type() = event_data::OK;	  else if ( strcasecmp(ep->s().c_str(),"CONNECT") == 0 )	    ep->type() = event_data::CONNECT;	  else if ( strcasecmp(ep->s().c_str(),"BUSY") == 0 )	    ep->type() = event_data::BUSY;	  else if ( strcasecmp(ep->s().c_str(),"ERROR") == 0 )	    ep->type() = event_data::ERROR;	  else if ( strcasecmp(ep->s().c_str(),"NO DIALTONE") == 0 )	    ep->type() = event_data::NODIALTONE;	  else if ( strcasecmp(ep->s().c_str(),"NO CARRIER") == 0 )	    ep->type() = event_data::NOCARRIER;	  else if ( strcasecmp(ep->s().c_str(),"NO ANSWER") == 0 )	    ep->type() = event_data::NOANSWER;	  else	    ep->pri() = 1;	}      } else {      }      _events.push(ep);    }  }public:  modem_io(int p_fd) : asynchronous_input(p_fd) { };  ~modem_io() { close(fd()); };  bool empty() { return _events.empty(); };  event_data *top() { return _events.top(); };  void pop() { _events.pop(); };};main(){  pthread_io io;  LockFile lockf;  modem_io *p;  int fd, speed;  struct termios tio;  char buf[80];  event_data *ep;  char *line = "/dev/modem";  if (lockf.Lock(line) == false ) {    cout << "Can't get exclusive line access" << endl;    exit(-1);  }  if ((fd = open(line,O_RDWR|O_NONBLOCK)) <= 0) {    cout << "Couldn't open serial line" << endl;    lockf.UnLock();    exit(-1);  }  tcgetattr(fd, &tio);  cfmakeraw(&tio);  tio.c_cc[VMIN] = 1;  tio.c_cc[VTIME] = 5;  tcsetattr(fd, TCSANOW, &tio);  speed = cfgetospeed(&tio);  cfsetispeed(&tio,speed);  strcpy(buf,"ATZ");  p = new modem_io(fd);  io.register_fd(p);  do {    if ( buf[0] == 'a' && buf[1] == 't' ) {      io.put(fd,buf);      io.put(fd,'\r');      usleep(10);    }    while( p->empty() == false ) {      ep = p->top(); p->pop();      cout << ep->type() << ":'" << ep->s().c_str() << "'" << endl;    }    cin.getline(buf,80);  } while( strcmp(buf,".") );  delete p;  lockf.UnLock();}voidLog(int t,char *f,...){  va_list ap;  va_start(ap, f);  vsyslog(t, f, ap);  va_end(ap);}voidFatal(char *f,...){  char pbuf[64];  va_list ap;  va_start(ap, f);  vsprintf(pbuf, f, ap);  Log(LOG_CRIT, _("Fatal called: %s"), pbuf);  va_end(ap);  exit(4);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -