xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/imxx11/gprs/gprs.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 
2 #include "gprs.h"
3 #include "ui_gprs.h"
4 #include"stdio.h"
5 #include <fcntl.h>
6 #include <qmessagebox.h>
7 #include <termio.h>
8 #include <unistd.h>
9 #include <QFile>
10 
11 int m_fd=-1;
12 
13 
Gprs(QWidget * parent)14 Gprs::Gprs(QWidget *parent) :
15     QWidget(parent),
16     ui(new Ui::Gprs)
17 {
18     ui->setupUi(this);
19 
20     myprocess = NULL;
21     m_startgprs = new QProcess;
22     connect(m_startgprs, SIGNAL(readyRead()), this, SLOT(readOutput()));
23 }
24 
~Gprs()25 Gprs::~Gprs()
26 {
27     ::system("killall pppd");
28 
29     if (m_fd >= 0)
30     {
31        ::close(m_fd);
32        m_fd = -1;
33      }
34     delete ui;
35 
36     if(myprocess)
37         delete myprocess;
38 }
39 
on_pbt_set_clicked()40 void Gprs::on_pbt_set_clicked()
41 {
42     if(ui->pbt_set->text()=="disconnect")
43     {
44         if (m_fd >= 0)
45         {
46            ::close(m_fd);
47            m_fd = -1;
48          }
49         ui->pbt_set->setText("set");
50         return;
51     }
52     m_fd = openSerialPort();
53     if (m_fd < 0)
54     {
55         QMessageBox::warning(this, tr("Error"), tr("Fail to open serial port!"));
56         return ;
57     }
58     m_notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
59     connect (m_notifier, SIGNAL(activated(int)), this, SLOT(remoteDataIncoming()));
60     ui->pbt_set->setText("disconnect");
61     ::write(m_fd,"AT+CNMI=2,1,0,0,0\r",strlen("AT+CNMI=2,1,0,0,0\r"));
62     sleep(1);
63 }
64 
result()65 void Gprs::result()
66 {
67     QString abc = myprocess->readAllStandardOutput();
68     ui->Edit_info->append(abc.trimmed());
69     QString efg = myprocess->readAllStandardError();
70     if(efg.length()>1)ui->Edit_info->append(efg.trimmed());
71 }
72 
73 
on_pbt_call_clicked()74 void Gprs::on_pbt_call_clicked()
75 {
76     if (m_fd < 0)
77     {
78         QMessageBox::warning(this, tr("Error"), tr("Fail to set serial port!"));
79         return ;
80     }
81     if(ui->pbt_call->text()=="stop")
82     {
83        ::write(m_fd,"ATH\r",strlen("ATH\r"));
84         sleep(1);
85         ui->Edit_info->clear();
86         ui->pbt_call->setText("call");
87         return;
88 
89     }
90     if (ui->Edit_phone->text().length() < 11)
91     {
92         ui->Edit_info->setText("输入的电话号码错误。\n");
93     }
94     else
95     {
96         telnum=ui->Edit_phone->text();
97         if (telnum.length()!=11)
98         {
99              QMessageBox::warning(this, tr("warming"), tr("phono number set error!"));
100         }
101         QString send="ATD"+telnum+";\r";
102 
103        if(::write(m_fd, send.toLatin1(), send.length()))
104        {
105            ui->pbt_call->setText("stop");
106            ui->Edit_info->setText("calling "+telnum);
107 
108        }
109 
110     }
111 }
112 
on_pbt_send_clicked()113 void Gprs::on_pbt_send_clicked()
114 {
115     if (m_fd < 0)
116     {
117         QMessageBox::warning(this, tr("Error"), tr("Fail to set serial port!"));
118         return ;
119     }
120 
121     QString mess=ui->Line_msg->text()+"\x1a";
122 
123     QString tell = "AT+CMGS=\"" +  ui->Edit_phone->text() + "\"\r";
124 
125     if (ui->Edit_phone->text().length()!=11)
126     {
127          QMessageBox::warning(this, tr("warming"), tr("phono number set error!"));
128     }
129     char getch[15];
130 
131     if(::write(m_fd,"AT+CSCS=\"GSM\"\r",strlen("AT+CSCS=\"GSM\"\r")))
132     {
133         sleep(1);
134 
135     }
136     if(::write(m_fd,tell.toLatin1(),tell.length()))
137     {
138         sleep(1);
139 
140     }
141 
142     if(::write(m_fd,mess.toLatin1(),mess.length()))
143     {
144         sleep(3);
145        // QMessageBox::information(this,"gprs","send message OK",0);
146     }
147 
148     if(::read(m_fd,getch,strlen(getch)))
149     {
150         sleep(1);
151        // QMessageBox::information(this,tr(""),tr(getch));
152     }
153 }
154 
155 
on_pbt_gprs_clicked()156 void Gprs::on_pbt_gprs_clicked()
157 {
158     if(ui->pbt_gprs->text()=="stop")
159     {
160        ::system("killall pppd");
161         ui->pbt_gprs->setText("gprs");
162         return;
163     }
164     ::system("ifconfig eth0 down");
165     //::system("ifconfig eth1 down");
166     //::system("rm /etc/resolv.conf");
167     //::system("ln -s /etc/ppp/resolv.conf /etc/resolv.conf");
168 
169      m_startgprs->terminate();
170 
171      QString dialcmd= "pppd call gprs /dev/";
172 
173      dialcmd+=ui->cb_com->currentText();
174 
175      m_startgprs->start(dialcmd);
176 
177      ui->pbt_gprs->setText("stop");
178 
179 }
180 
openSerialPort()181 int Gprs::openSerialPort()
182 {
183   int fd = -1;
184 
185   portName="/dev/"+ui->cb_com->currentText();
186   fd = ::open(portName.toStdString().data(), O_RDWR|O_NONBLOCK);
187   if (fd < 0)
188   {
189     QMessageBox::warning(this, tr("Error"), tr("fd1"));
190     return -1;
191   }
192 
193   termios serialAttr;
194   memset(&serialAttr, 0, sizeof serialAttr);
195 /*
196 serialAttr.c_iflag = IGNPAR;
197   serialAttr.c_cflag = B115200 | HUPCL | CS8 | CREAD | CLOCAL;
198   serialAttr.c_cc[VMIN] = 1;
199 */
200 if(ui->cb_bound->currentText() == tr("9600"))
201     {
202         cfsetispeed(&serialAttr,B9600);
203         cfsetospeed(&serialAttr,B9600);
204     }
205      else if(ui->cb_bound->currentText() == tr("115200"))
206     {
207         cfsetispeed(&serialAttr,B9600);
208         cfsetospeed(&serialAttr,B115200);
209     }
210      if(ui->cb_data->currentText() == tr("8"))
211     {
212         serialAttr.c_cflag &= ~CSIZE;
213         serialAttr.c_cflag |= CS8;
214     }
215      else if(ui->cb_data->currentText() == tr("7"))
216     {
217         serialAttr.c_cflag &= ~CSIZE;
218         serialAttr.c_cflag |= CS7;
219     }
220     if(ui->cb_partity->currentText() == tr("No"))
221         serialAttr.c_iflag = IGNPAR;
222     else if(ui->cb_partity->currentText() == tr("Odd"))
223     {
224         serialAttr.c_cflag |= (PARODD | PARENB);
225         serialAttr.c_iflag |= INPCK;
226     }
227     else if(ui->cb_partity->currentText() == tr("Even"))
228     {
229         serialAttr.c_cflag |= PARENB;
230         serialAttr.c_cflag &= ~PARODD;
231         serialAttr.c_iflag |= INPCK;
232     }
233   if (tcsetattr(fd, TCSANOW, &serialAttr) != 0)
234   {
235     QMessageBox::warning(this, tr("Error"), tr("fd2"));
236     return -1;
237   }
238   return fd;
239 }
240 
241 
242 
remoteDataIncoming()243 void Gprs::remoteDataIncoming()
244 {
245     char getch[30];
246     if(::read(m_fd,getch,30))
247     {
248         sleep(1);
249         QString mes_or_tell = QString(getch);
250 
251         int tell_iscome=mes_or_tell.indexOf(QRegExp("RING"), 0);
252         int mes_iscome=mes_or_tell.indexOf(QRegExp("CMTI"), 0);
253         if(mes_iscome>=0)
254         {
255           QMessageBox::information(this,"gprs","message is received",0);
256         }
257         if(tell_iscome>=0)
258         {
259            QMessageBox::information(this,"gprs","tell is coming",0);
260         }
261     }
262 }
263 
264 
265 
readOutput()266 void Gprs::readOutput()
267 {
268     while (m_startgprs->canReadLine())
269      {
270         QString gprsInfo = m_startgprs->readAll();
271         ui->Edit_info->append(gprsInfo);
272     }
273 
274 }
275 
closeEvent(QCloseEvent *)276 void Gprs::closeEvent(QCloseEvent *)
277 {
278     destroy();
279     exit(0);
280 }
281 
moveEvent(QMoveEvent *)282 void Gprs::moveEvent(QMoveEvent *)
283 {
284     this->move(QPoint(0,0));
285 }
286 
resizeEvent(QResizeEvent *)287 void Gprs::resizeEvent(QResizeEvent *)
288 {
289     this->showMaximized();
290 }
291 
292 
on_ping_clicked()293 void Gprs::on_ping_clicked()
294 {
295     if(ui->hostname->text() == QString(""))
296     {
297         QMessageBox::about(this,"error","hostname cannot be empty!");
298         return;
299     }
300 
301     if(myprocess)
302         delete myprocess;
303 
304     myprocess = new QProcess(this);
305     connect(myprocess, SIGNAL(readyReadStandardOutput()),this, SLOT(result()));
306     connect(myprocess, SIGNAL(readyReadStandardError()),this, SLOT(result()));
307     myprocess->start(QString("ping -c 5 ")+ui->hostname->text());
308     ui->Edit_info->append(QString("ping -c 5 ")+ui->hostname->text());
309 }
310