📄 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(){ 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);}void CATPhone::InitLogs(CLog *_log, CLog *_sms){ pLog = _log; pSms = _sms;}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;}CATPhone::ERetCodes CATPhone::strWait(char *good, char *bad, int timeout){ int num = 0; strBuf = ""; for (int i=0; i<timeout; i++) { sleep(1); if ((num = read(fileno(fdr), buffer, sizeof(buffer)-sizeof(char))) > 0) { buffer[num]=0; strBuf+=buffer; if (strstr(buffer,good)!=NULL) return CATPhone::GOOD; if (strstr(buffer,bad)!=NULL) return CATPhone::BAD; } } return CATPhone::TIMEOUT;}// Is modem GSM-modem?bool CATPhone::isGSMmodem(){ int num; char *p; ERetCodes code; bool ret = false; int bufPos = 0; pLog->Out("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) { pLog->Out(p+5*sizeof(char)); } else pLog->Out(buffer); ret = true; break; case CATPhone::BAD: pLog->Out("Device is not GSM-modem!"); break; case CATPhone::TIMEOUT: pLog->Out("Timeout!"); break; } return ret;}// Set text modebool CATPhone::SetSMStextMode(){ CATPhone::ERetCodes code; bool ret = false; pLog->Out("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: pLog->Out("Text mode is installed!"); ret = true; break; case CATPhone::BAD: pLog->Out("Text mode is not supported!"); break; case CATPhone::TIMEOUT: pLog->Out("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; pLog->Out("Storing SMS message in phone memory..."); sprintf(buffer,"Phone: %s Text: \"%s\"", sPhone, sText); pLog->Out(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); pLog->Out(buffer); currMemCell = ret; } break; case CATPhone::BAD: pLog->Out("Impossible to store SMS into memory!\n"); break; case CATPhone::TIMEOUT: pLog->Out("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"); pLog->Out("SMS sending from memory"); write(fileno(fdw), buffer, strlen(buffer)); code = Wait("OK", "ERROR", 20); switch(code) { case CATPhone::GOOD: pLog->Out("Message is successfully sent!"); ret = true; break; case CATPhone::BAD: pLog->Out("Impossible to send message!"); break; case CATPhone::TIMEOUT: pLog->Out("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"); pLog->Out("Deleting SMS from memory");// com.sReceiveBuffer = ""; write(fileno(fdw), buffer, strlen(buffer)); code = Wait("OK", "ERROR", 2); switch(code) { case CATPhone::GOOD: pLog->Out("Message is successfully deleted from phone memory!"); ret = true; break; case CATPhone::BAD: pLog->Out("Impossible to delete message from phone memory!"); break; case CATPhone::TIMEOUT: pLog->Out("Timeout!"); break; } return ret;}bool CATPhone::ListSMS(){ CATPhone::ERetCodes code; bool ret = false;// BYTE *sAtz; QString s = "Retrieve list of messages error! "; strcpy(buffer,"AT+CMGL\n"); write(fileno(fdw), buffer, strlen(buffer)); code = strWait("OK", "ERROR", 10); switch(code) { case CATPhone::GOOD: ParceSMSList(); ret = true; break; case CATPhone::BAD: pLog->Out(s); break; case CATPhone::TIMEOUT: s+=sTimeoutMsg; pLog->Out(s); break; } return ret;}void CATPhone::ParceSMSList(){ int posCMGL = 0, pos1 = 0, nCell = 0, pos4sign = 0; QString strList=strBuf, strTmp="", strPhoneNo="", text="";// if (com.ReadReceiveBuffer(strList)==CComm::TIMEOUT) return; while (posCMGL>=0) {// Record start if ((posCMGL = strList.find("+CMGL:", posCMGL))>=0) { nCell = atoi(&((const char *)strList)[posCMGL+6]); posCMGL+=6; if ((posCMGL = strList.find(',', posCMGL))>=0) { pos1 = posCMGL+1; if ((pos1 = strList.find(',', pos1))>=0) strTmp = strList.mid(posCMGL+1, pos1 - posCMGL -1); if (!strTmp.find("\"REC ")) { posCMGL = strList.find('\n', posCMGL); posCMGL = strList.find('\n', posCMGL+1); } else { posCMGL = strList.find('\"', pos1+1); pos1 = strList.find('\"', posCMGL+1); strPhoneNo = strList.mid(posCMGL+1, pos1 - posCMGL -1); posCMGL = strList.find('\n', pos1); pos1 = strList.find('\n', posCMGL+1); if (pos1<0) pos1 = strList.length(); text = strList.mid(posCMGL+1, pos1 - posCMGL - 2); posCMGL = pos1; pLog->Out((strPhoneNo+" "+text)); pSms->Out((strPhoneNo+" "+text)); } // Delete record from phone DelSMSFromMem(nCell); }// str.Format(" � �:%d\n",ret);// theApp.dlg->m_lstAT.AddString(str); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -