📄 atphone.cpp
字号:
/*************************************************************************** * Copyright (C) 2004 by Igor Vlassov * * iev@iev.cor.ru * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/#define _XOPEN_SOURCE#define _XOPEN_SOURCE_EXTENDED#include "atphone.h"// ATPhone.cpp: implementation of the CATPhone class.////////////////////////////////////////////////////////////////////////// Constantsconst char *sTimeoutMsg = "Timeout! The modem's answer is not received!";//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CATPhone::CATPhone(){ fiLog = NULL; CtrlZ[0] = 26; CtrlZ[1] = 0;}CATPhone::~CATPhone(){}int CATPhone::Init(char *sTty, char *speed){ int baud=0; struct termio term, tstdin; puts("Port initialisation..."); if ((fd = open(sTty, O_RDWR | O_NDELAY)) < 0) { perror(sTty); exit(errno); } if (speed!=NULL) { switch(atoi(speed)) { case 300: baud = B300; break; case 1200: baud = B1200; break; case 2400: baud = B2400; break; case 4800: baud = B4800; break; case 9600: baud = B9600; break; case 19200: baud = B19200; break; case 38400: baud = B38400; break; default: baud = 0; fprintf(stderr, "%s: скорость %s не поддерживается\n", sTty,speed); exit(1); } } /* Сохранить состояние stdin и tty; отслеживание некоторых сигналов */ ioctl(fd, TCGETA, &term_save); ioctl(fileno(stdin), TCGETA, &stdin_save);// signal(SIGHUP, Exit);// signal(SIGINT, Exit);// signal(SIGQUIT, Exit);// signal(SIGTERM, Exit); /* Перевести stdin в линейный режим, выключить эхо */ ioctl(fileno(stdin), TCGETA, &tstdin); tstdin.c_iflag = 0; tstdin.c_lflag &= ~(ICANON | ECHO); tstdin.c_cc[VMIN] = 0; tstdin.c_cc[VTIME] = 0; ioctl(fileno(stdin), TCSETA, &tstdin); /* Задать состояние tty */ ioctl(fd, TCGETA, &term); term.c_cflag |= CLOCAL|HUPCL; if (baud > 0) { term.c_cflag &= ~CBAUD; term.c_cflag |= baud; } term.c_lflag &= ~(ICANON | ECHO); /* линейный режим */ term.c_iflag &= ~ICRNL; /* исключить пустые строки */ term.c_cc[VMIN] = 0; term.c_cc[VTIME] = 10; ioctl(fd, TCSETA, &term); fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NDELAY); /* Открыть tty для чтения и записи */ if ((fdr = fopen(sTty, "r")) == NULL ) { perror(sTty); exit(errno); } if ((fdw = fopen(sTty, "w")) == NULL ) { perror(sTty); exit(errno); } puts("Port is opened..."); return 0;}void CATPhone::Exit(int sig){ StopLog(); if (fdr) fclose(fdr); if (fdw) fclose(fdw); ioctl(fd, TCSETA, &term_save); close(fd); ioctl(fileno(stdin), TCSETA, &stdin_save); exit(sig);}CATPhone::ERetCodes CATPhone::Wait(char *good, char *bad, int timeout){ int num = 0, num0=0; for (int i=0; i<timeout; i++) { sleep(1); if ((num = read(fileno(fdr), &buffer[num0], sizeof(buffer)-sizeof(char))) > 0) { buffer[num+num0]=0; if (strstr(buffer,good)!=NULL) return CATPhone::GOOD; if (strstr(buffer,bad)!=NULL) return CATPhone::BAD; num0+=num; } } return CATPhone::TIMEOUT;}// Is modem GSM-modem?bool CATPhone::isGSMmodem(){ int num; char *p; ERetCodes code; bool ret = false; int bufPos = 0; OutLog("GSM-modem type determination: "); strcpy(buffer,"AT+CGMI\n"); write(fileno(fdw), buffer, strlen(buffer)); code = Wait("OK", "ERROR", 1); switch(code) { case CATPhone::GOOD: if((p=strstr(buffer,"CGMI:"))!=NULL) { OutLog(p+5*sizeof(char)); } else OutLog(buffer); ret = true; break; case CATPhone::BAD: OutLog("Device is not GSM-modem!"); break; case CATPhone::TIMEOUT: OutLog("Timeout!"); break; } return ret;}// Set text modebool CATPhone::SetSMStextMode(){ CATPhone::ERetCodes code; bool ret = false; OutLog("Text type of message setup: "); strcpy(buffer,"AT+CMGF=1\n"); write(fileno(fdw), buffer, strlen(buffer)); code = Wait("OK", "ERROR", 1); switch(code) { case CATPhone::GOOD: OutLog("Text mode is installed!"); ret = true; break; case CATPhone::BAD: OutLog("Text mode is not supported!"); break; case CATPhone::TIMEOUT: OutLog("Timeout!"); break; } return ret;}//// Store SMS into phone memoryint CATPhone::StoreSMS2mem(const char *sPhone, const char *sText){ CATPhone::ERetCodes code; int ret = -1; char cmdPhone[128]; char cText[256]; char *p; strcat(strcat(strcpy(cmdPhone,"AT+CMGW=\""),sPhone),"\"\n"); strcat(strcat(strcat(strcpy(cText,"\""), sText), "\""),CtrlZ); currMemCell = 0; OutLog("Storing SMS message in phone memory..."); sprintf(buffer,"Phone: %s Text: \"%s\"", sPhone, sText); OutLog(buffer); for( int i = 0; i<3; i++) { write(fileno(fdw), cmdPhone, strlen(cmdPhone)); write(fileno(fdw), cText, strlen(cText)); code = Wait("OK", "ERROR", 5); if (code == CATPhone::GOOD) break; } switch(code) { case CATPhone::GOOD: if((p=strstr(buffer,"CMGW:"))!=NULL) { ret = atoi(p+5*sizeof(char)); sprintf(buffer,"OK, Slot: %d",ret); OutLog(buffer); currMemCell = ret; } break; case CATPhone::BAD: OutLog("Impossible to store SMS into memory!\n"); break; case CATPhone::TIMEOUT: OutLog("TIMEOUT!"); break; } return ret;}// Send SMS from memorybool CATPhone::SendSMSFromMem(int cell){ CATPhone::ERetCodes code; char buf[10]; bool ret = false;// itoa((cell!=0 ? cell : currMemCell),buf,10); sprintf(buf,"%d",(cell!=0 ? cell : currMemCell)); strcat(strcat(strcpy(buffer,"AT+CMSS="),buf),"\n"); OutLog("SMS sending from memory"); write(fileno(fdw), buffer, strlen(buffer)); code = Wait("OK", "ERROR", 20); switch(code) { case CATPhone::GOOD: OutLog("Message is successfully sent!"); ret = true; break; case CATPhone::BAD: OutLog("Impossible to send message!"); break; case CATPhone::TIMEOUT: OutLog("Timeout!"); break; } return ret;}// Deleting SMS from memorybool CATPhone::DelSMSFromMem(int cell){ CATPhone::ERetCodes code; char buf[10]; bool ret = false;// itoa((cell!=0 ? cell : currMemCell),buf,10); sprintf(buf,"%d",(cell!=0 ? cell : currMemCell)); strcat(strcat(strcpy(buffer,"AT+CMGD="),buf),"\n"); OutLog("Deleting SMS from memory");// com.sReceiveBuffer = ""; write(fileno(fdw), buffer, strlen(buffer)); code = Wait("OK", "ERROR", 2); switch(code) { case CATPhone::GOOD: OutLog("Message is successfully deleted from phone memory!"); ret = true; break; case CATPhone::BAD: OutLog("Impossible to delete message from phone memory!"); break; case CATPhone::TIMEOUT: OutLog("Timeout!"); break; } return ret;}bool CATPhone::StartLog(char *strLogFiName){ if((fiLog = fopen(strLogFiName,"a+t"))==NULL) { printf("Impossible to create/open log file %s!\n",strLogFiName); return false; } return true;}void CATPhone::StopLog(){// fiLog.Close(); if (fiLog) fclose(fiLog);}void CATPhone::OutLog(const char *sMsg){ time_t tt; tm *tLocalTime = NULL; time(&tt); tLocalTime = localtime(&tt); printf("%02d.%02d.%04d %02d:%02d %s\n",tLocalTime->tm_mon, tLocalTime->tm_mday,tLocalTime->tm_year, tLocalTime->tm_hour, tLocalTime->tm_min, sMsg); if (fiLog) fprintf(fiLog,"%02d.%02d.%04d %02d:%02d %s\n",tLocalTime->tm_mon, tLocalTime->tm_mday, tLocalTime->tm_year, tLocalTime->tm_hour, tLocalTime->tm_min, sMsg);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -