181
178
// switch off non-blocking mode
183
if ((fdFlags = fcntl(_fd, F_GETFL)) == -1) {
185
throwModemException(_("getting file status flags failed"));
180
if ((fdFlags = fcntl(_fd, F_GETFL)) == -1)
183
throwModemException(_("getting file status flags failed"));
187
185
fdFlags &= ~O_NONBLOCK;
188
if (fcntl(_fd, F_SETFL, fdFlags) == -1) {
190
throwModemException(_("switching of non-blocking mode failed"));
186
if (fcntl(_fd, F_SETFL, fdFlags) == -1)
189
throwModemException(_("switching of non-blocking mode failed"));
193
192
long int saveTimeoutVal = _timeoutVal;
195
194
int initTries = holdoffArraySize;
196
195
while (initTries-- > 0)
198
// flush all pending output
199
tcflush(_fd, TCOFLUSH);
201
// toggle DTR to reset modem
202
int mctl = TIOCM_DTR;
203
if (ioctl(_fd, TIOCMBIC, &mctl) < 0) {
205
throwModemException(_("clearing DTR failed"));
207
// the waiting time for DTR toggling is increased with each loop
208
usleep(holdoff[initTries]);
209
if (ioctl(_fd, TIOCMBIS, &mctl) < 0) {
211
throwModemException(_("setting DTR failed"));
214
if (tcgetattr(_fd, &t) < 0) {
216
throwModemException(stringPrintf(_("tcgetattr device '%s'"),
221
cfsetispeed(&t, lineSpeed);
222
cfsetospeed(&t, lineSpeed);
224
// set the device to a sane state
225
t.c_iflag |= IGNPAR | (swHandshake ? IXON | IXOFF : 0);
226
t.c_iflag &= ~(INPCK | ISTRIP | IMAXBEL |
227
(swHandshake ? 0 : IXON | IXOFF)
228
| IXANY | IGNCR | ICRNL | IMAXBEL | INLCR | IGNBRK);
229
t.c_oflag &= ~(OPOST);
230
// be careful, only touch "known" flags
231
t.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD |
232
(swHandshake ? CRTSCTS : 0 ));
233
t.c_cflag |= CS8 | CREAD | HUPCL | (swHandshake ? 0 : CRTSCTS) | CLOCAL;
234
t.c_lflag &= ~(ECHO | ECHOE | ECHOPRT | ECHOK | ECHOKE | ECHONL |
235
ECHOCTL | ISIG | IEXTEN | TOSTOP | FLUSHO | ICANON);
243
if(tcsetattr (_fd, TCSANOW, &t) < 0) {
245
throwModemException(stringPrintf(_("tcsetattr device '%s'"),
248
// the waiting time for writing to the ME/TA is increased with each loop
249
usleep(holdoff[initTries]);
251
// flush all pending input
252
tcflush(_fd, TCIFLUSH);
258
bool foundOK = false;
260
while (readTries-- > 0)
262
// for the first call getLine() waits only 3 seconds
263
// because of _timeoutVal = 3
264
string s = getLine();
265
if (s.find("OK") != string::npos ||
266
s.find("CABLE: GSM") != string::npos)
269
readTries = 0; // found OK, exit loop
271
else if (s.find("ERROR") != string::npos)
272
readTries = 0; // error, exit loop
275
// set getLine/putLine timeout back to old value
276
_timeoutVal = saveTimeoutVal;
282
putLine("AT" + initString);
283
while (readTries-- > 0)
285
string s = getLine();
286
if (s.find("OK") != string::npos ||
287
s.find("CABLE: GSM") != string::npos)
288
return; // found OK, return
292
catch (GsmException &e)
294
_timeoutVal = saveTimeoutVal;
295
if (initTries == 0) {
197
// flush all pending output
198
tcflush(_fd, TCOFLUSH);
200
// toggle DTR to reset modem
201
int mctl = TIOCM_DTR;
202
if (ioctl(_fd, TIOCMBIC, &mctl) < 0) {
204
throwModemException(_("clearing DTR failed"));
206
// the waiting time for DTR toggling is increased with each loop
207
usleep(holdoff[initTries]);
208
if (ioctl(_fd, TIOCMBIS, &mctl) < 0) {
210
throwModemException(_("setting DTR failed"));
213
if (tcgetattr(_fd, &t) < 0) {
215
throwModemException(stringPrintf(_("tcgetattr device '%s'"),
220
cfsetispeed(&t, lineSpeed);
221
cfsetospeed(&t, lineSpeed);
223
// set the device to a sane state
224
t.c_iflag |= IGNPAR | (swHandshake ? IXON | IXOFF : 0);
225
t.c_iflag &= ~(INPCK | ISTRIP | IMAXBEL |
226
(swHandshake ? 0 : IXON | IXOFF)
227
| IXANY | IGNCR | ICRNL | IMAXBEL | INLCR | IGNBRK);
228
t.c_oflag &= ~(OPOST);
229
// be careful, only touch "known" flags
230
t.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD |
231
(swHandshake ? CRTSCTS : 0 ));
232
t.c_cflag |= CS8 | CREAD | HUPCL | (swHandshake ? 0 : CRTSCTS) | CLOCAL;
233
t.c_lflag &= ~(ECHO | ECHOE | ECHOPRT | ECHOK | ECHOKE | ECHONL |
234
ECHOCTL | ISIG | IEXTEN | TOSTOP | FLUSHO | ICANON);
242
if(tcsetattr (_fd, TCSANOW, &t) < 0)
245
throwModemException(stringPrintf(_("tcsetattr device '%s'"),
248
// the waiting time for writing to the ME/TA is increased with each loop
249
usleep(holdoff[initTries]);
251
// flush all pending input
252
tcflush(_fd, TCIFLUSH);
258
bool foundOK = false;
260
while (readTries-- > 0)
262
// for the first call getLine() waits only 3 seconds
263
// because of _timeoutVal = 3
264
std::string s = getLine();
265
if (s.find("OK") != std::string::npos ||
266
s.find("CABLE: GSM") != std::string::npos)
269
readTries = 0; // found OK, exit loop
271
else if (s.find("ERROR") != std::string::npos)
272
readTries = 0; // error, exit loop
275
// set getLine/putLine timeout back to old value
276
_timeoutVal = saveTimeoutVal;
282
putLine("AT" + initString);
283
while (readTries-- > 0)
285
std::string s = getLine();
286
if (s.find("OK") != std::string::npos ||
287
s.find("CABLE: GSM") != std::string::npos)
288
return; // found OK, return
292
catch (GsmException &e)
294
_timeoutVal = saveTimeoutVal;
295
if (initTries == 0) {
301
301
// no response after 3 tries
303
303
throw GsmException(stringPrintf(_("reset modem failed '%s'"),
304
304
device.c_str()), OtherError);
307
string UnixSerialPort::getLine() throw(GsmException)
307
std::string UnixSerialPort::getLine() throw(GsmException)
311
311
while ((c = readByte()) >= 0)