File: gsm_win32_serial.cc

package info (click to toggle)
gsmlib 1.10+20120414.gita5e5ae9a-0.3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 3,468 kB
  • ctags: 1,907
  • sloc: cpp: 12,315; sh: 11,376; ansic: 2,366; makefile: 263; sed: 93
file content (506 lines) | stat: -rw-r--r-- 12,223 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// *************************************************************************
// * GSM TA/ME library
// *
// * File:    gsm_win32_port.cc
// *
// * Purpose: WIN32 serial port implementation
// *
// * Author:  Frediano Ziglio (freddy77@angelfire.com)
// *
// * Created: 25.10.2000
// *************************************************************************

#ifdef HAVE_CONFIG_H
#include <gsm_config.h>
#endif
#include <winsock.h>
#include <gsmlib/gsm_nls.h>
#include <gsmlib/gsm_win32_serial.h>
#include <gsmlib/gsm_util.h>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <signal.h>

using namespace gsmlib;

static long int timeoutVal = TIMEOUT_SECS;

struct ExceptionSafeOverlapped: public OVERLAPPED
{
  ExceptionSafeOverlapped()
  {
    memset((OVERLAPPED*)this,0,sizeof(OVERLAPPED));
    hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); 
    if (hEvent == INVALID_HANDLE_VALUE) 
      throw GsmException(_("error creating event"),OSError,GetLastError());
  }
  ~ExceptionSafeOverlapped()
  { CloseHandle(hEvent); }
};

typedef BOOL (WINAPI *TCancelIoProc)(HANDLE file);
TCancelIoProc CancelIoProc = NULL;
BOOL CancelIoHook(HANDLE file)
{
  if (CancelIoProc)
    return CancelIoProc(file);

  HMODULE hmodule = GetModuleHandle("KERNEL32");
  if (hmodule)
  {
    CancelIoProc = (TCancelIoProc)GetProcAddress(hmodule,"CancelIo");
    if (CancelIoProc)
      return CancelIoProc(file);
  }

  return TRUE;
}
#define CancelIo CancelIoHook

// Win32SerialPort members

void Win32SerialPort::throwModemException(std::string message) throw(GsmException)
{
  ostrstream os;
  os << message << " (errno: " << errno << "/" << strerror(errno) << ")"
     << ends;
  char *ss = os.str();
  std::string s(ss);
  delete[] ss;
  throw GsmException(s, OSError, errno);
}

void Win32SerialPort::putBack(unsigned char c)
{
  assert(_oldChar == -1);
  _oldChar = c;
}

int Win32SerialPort::readByte() throw(GsmException)
{
  if (_oldChar != -1)
  {
    int result = _oldChar;
    _oldChar = -1;
    return result;
  }

  unsigned char c;
  int timeElapsed = 0;
  bool readDone = true;
  ExceptionSafeOverlapped  over;

  DWORD initTime = GetTickCount();
  DWORD dwReaded;
  if (!ReadFile(_file,&c,1,&dwReaded,&over))
  {
    readDone = false;
    if (GetLastError() != ERROR_IO_PENDING)
    {
      throwModemException(_("reading from TA"));
    }

    while(!readDone)
    {
      if (interrupted())
        throwModemException(_("interrupted when reading from TA"));

      // wait another second
      switch(WaitForSingleObject(over.hEvent,1000))
      {
      case WAIT_TIMEOUT:
        break;
      case WAIT_OBJECT_0:
      case WAIT_ABANDONED:
	      // !!! do a infinite loop if (bytesWritten < lenght) ?
        GetOverlappedResult(_file,&over,&dwReaded,TRUE);
        readDone = true;
        break;
      case WAIT_FAILED:
        throwModemException(_("reading from TA"));
      }

      timeElapsed = (GetTickCount() - initTime)/1000U;

      // timeout elapsed ?
      if (timeElapsed >= timeoutVal)
      {
        CancelIo(_file);
        break;
      }

    }
  }
  
  if (! readDone)
    throwModemException(_("timeout when reading from TA"));

#ifndef NDEBUG
  if (debugLevel() >= 2)
  {
    // some useful debugging code
    if (c == LF)
      cerr << "<LF>";
    else if (c == CR)
      cerr << "<CR>";
    else cerr << "<'" << (char) c << "'>";
    cerr.flush();
  }
#endif
  return c;
}

Win32SerialPort::Win32SerialPort(std::string device, int lineSpeed,
                               std::string initString, bool swHandshake)
  throw(GsmException) :
  _oldChar(-1)
{
 try
 {
  int holdoff[] = {2000, 1000, 400};

  // open device
  _file = CreateFile(device.c_str(),GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
  if (_file == INVALID_HANDLE_VALUE)
    throwModemException(stringPrintf(_("opening device '%s'"),
                                     device.c_str()));

  int initTries = 3;
  while (initTries-- > 0)
  {
    // flush all pending output
    FlushFileBuffers(_file);

    // toggle DTR to reset modem
    if (!EscapeCommFunction(_file,CLRDTR))
      throwModemException(_("clearing DTR failed"));
    Sleep(holdoff[initTries]);
    if (!EscapeCommFunction(_file,SETDTR))
      throwModemException(_("setting DTR failed"));

    DCB dcb;
    // get line modes
    if (!GetCommState(_file,&dcb))
      throwModemException(stringPrintf(_("GetCommState device '%s'"),
                                       device.c_str()));

//    if (tcgetattr(_fd, &t) < 0)
//      throwModemException(stringPrintf(_("tcgetattr device '%s'"),
//                                       device.c_str()));

    // set the device to a sane state
    dcb.fBinary = TRUE;
    dcb.BaudRate = lineSpeed;

    // n,8,1
    dcb.fParity = FALSE;
    dcb.Parity = 0;
    dcb.ByteSize = 8;
    dcb.StopBits = 0;

    if (!swHandshake)
    {
      dcb.fInX = FALSE;
      dcb.fOutX = FALSE;
      dcb.fOutxDsrFlow = FALSE;
      dcb.fOutxCtsFlow = FALSE;
    }
    else
    {
      dcb.fInX  = TRUE;
      dcb.fOutX = TRUE;
      dcb.fOutxDsrFlow = FALSE;
      dcb.fOutxCtsFlow = FALSE;
    }
    dcb.fDtrControl = DTR_CONTROL_ENABLE;
    dcb.fRtsControl = RTS_CONTROL_ENABLE;
    
//    t.c_iflag |= IGNPAR;
//    t.c_iflag &= ~(INPCK | ISTRIP | IMAXBEL |
//                   (swHandshake ? CRTSCTS : IXON |  IXOFF)
//                   | IXANY | IGNCR | ICRNL | IMAXBEL | INLCR | IGNBRK);
//    t.c_oflag &= ~(OPOST);
//    // be careful, only touch "known" flags
//    t.c_cflag&= ~(CSIZE | CSTOPB | PARENB | PARODD);
//    t.c_cflag|= CS8 | CREAD | HUPCL |
//      (swHandshake ? IXON |  IXOFF : CRTSCTS) |
//      CLOCAL;
//    t.c_lflag &= ~(ECHO | ECHOE | ECHOPRT | ECHOK | ECHOKE | ECHONL |
//                   ECHOCTL | ISIG | IEXTEN | TOSTOP | FLUSHO | ICANON);
//    t.c_lflag |= NOFLSH;
//
//    t.c_cc[VMIN] = 1;
//    t.c_cc[VTIME] = 0;
//
//    t.c_cc[VSUSP] = 0;

    // write back
    if (!SetCommState(_file,&dcb))
      throwModemException(stringPrintf(_("SetCommState device '%s'"),
                                       device.c_str()));

    Sleep(holdoff[initTries]);

    if (!SetupComm(_file,1024,1024))
      throwModemException(stringPrintf(_("SetupComm device '%s'"),
                                       device.c_str()));
 

    // flush all pending input
    PurgeComm(_file,PURGE_RXABORT|PURGE_RXCLEAR);

    try
    {
      // reset modem
      putLine("ATZ");
      bool foundOK = false;
      int readTries = 5;
      while (readTries-- > 0)
      {
        std::string s = getLine();
        if (s.find("OK") != string::npos ||
            s.find("CABLE: GSM") != string::npos)
        {
          foundOK = true;
          readTries = 0;           // found OK, exit loop
        }
      }

      if (foundOK)
      {
        // init modem
        readTries = 5;
        // !!! no not declare this in loop, compiler error on Visual C++
        // (without SP and with SP4)
        std::string s; 
        putLine("AT" + initString);
        do
        {
          s = getLine();
          if (s.find("OK") != string::npos ||
              s.find("CABLE: GSM") != string::npos)
            return;                 // found OK, return
        } while(--readTries);
      }
    }
    catch (GsmException &e)
    {
      if (initTries == 0)
        throw e;
    }
  }
  // no response after 3 tries
  throw GsmException(stringPrintf(_("reset modem failed '%s'"),
                                  device.c_str()), OtherError);
 }
 catch (GsmException &e)
 {
  if ( _file != INVALID_HANDLE_VALUE)
   CloseHandle(_file);  
  throw e;
 }
}

string Win32SerialPort::getLine() throw(GsmException)
{
  std::string result;
  int c;
  while ((c = readByte()) > 0)
  {
    while (c == CR)
    {
      c = readByte();
    }
    if (c == LF)
      break;
    result += c;
  }

#ifndef NDEBUG
  if (debugLevel() >= 1)
    cerr << "<-- " << result << endl;
#endif

  return result;
}

void Win32SerialPort::putLine(std::string line,
                             bool carriageReturn) throw(GsmException)
{
#ifndef NDEBUG
  if (debugLevel() >= 1)
    cerr << "--> " << line << endl;
#endif

  if (carriageReturn) line += CR;
  // !!! BUG, mantain this pointer isn't corrent, use iterator !!!
  const char *l = line.c_str();
  
  FlushFileBuffers(_file);      // flush all pending input and output

  int timeElapsed = 0;

  DWORD bytesWritten = 0;

  ExceptionSafeOverlapped over;

  DWORD initTime = GetTickCount();
  if (!WriteFile(_file,l,line.length(),&bytesWritten,&over))
  {
    if (GetLastError() != ERROR_IO_PENDING)
    {
      throwModemException(_("writing to TA"));
    }

    while(bytesWritten < (DWORD)line.length())
    {
      if (interrupted())
        throwModemException(_("interrupted when writing to TA"));

      // wait another second
      switch(WaitForSingleObject(over.hEvent,1000))
      {
      case WAIT_TIMEOUT:
        break;
      case WAIT_OBJECT_0:
      case WAIT_ABANDONED:
	// !!! do a infinite loop if (bytesWritten < lenght) ?
        GetOverlappedResult(_file,&over,&bytesWritten,TRUE);
        break;
      case WAIT_FAILED:
        throwModemException(_("writing to TA"));
      }

      timeElapsed = (GetTickCount() - initTime)/1000U;

      // timeout elapsed ?
      if (timeElapsed >= timeoutVal)
      {
        CancelIo(_file);
        throwModemException(_("timeout when writing to TA"));
      }

    }
  }

  return;
/*
  // empty buffer
  SetCommMask(_file,EV_TXEMPTY);
  DWORD dwEvent;
  ResetEvent(over.hEvent);
  if( WaitCommEvent(_file,&dwEvent,&over) )
    return; // already empty

  // check true errors
  if (GetLastError() != ERROR_IO_PENDING)
    throwModemException(_("error comm waiting"));

  while(timeElapsed < timeoutVal)
  {
    if (interrupted())
      throwModemException(_("interrupted when flushing to TA"));

    switch( WaitForSingleObject( over.hEvent, 1000 ) ) 
    {
    case WAIT_TIMEOUT:
      break;

    // successfully flushed
    case WAIT_ABANDONED:
    case WAIT_OBJECT_0:
      return;

    default:
      throwModemException(_("error waiting"));
    }
    timeElapsed = (GetTickCount() - initTime)/1000U;
  }

  CancelIo(_file);
  throwModemException(_("timeout when writing to TA"));
*/

  // echo CR LF must be removed by higher layer functions in gsm_at because
  // in order to properly handle unsolicited result codes from the ME/TA
}

bool Win32SerialPort::wait(GsmTime timeout) throw(GsmException)
{
  // See differences from UNIX
  // Why do I use Windows ?
  DWORD dwEvent;
  SetCommMask(_file,EV_RXCHAR);
  if (!timeout)
  {
    if( !WaitCommEvent(_file,&dwEvent,NULL) )
      throwModemException(_("error comm waiting"));
    return true;
  }
  
  ExceptionSafeOverlapped over;
  if( !WaitCommEvent(_file,&dwEvent,&over) )
  {
    // check true errors
    if (GetLastError() != ERROR_IO_PENDING)
      throwModemException(_("error comm waiting"));

    switch( WaitForSingleObject( over.hEvent, timeout->tv_sec*1000U+(timeout->tv_usec/1000U) ) ) 
    {
    case WAIT_TIMEOUT:
      CancelIo(_file);
      return false;

    case WAIT_ABANDONED:
    case WAIT_OBJECT_0:
      return true;

    default:
      throwModemException(_("error waiting"));
    }
  }

  return true;
}

void Win32SerialPort::setTimeOut(unsigned int timeout)
{
  timeoutVal = timeout;
}

Win32SerialPort::~Win32SerialPort()
{
  if ( _file != INVALID_HANDLE_VALUE)
    CloseHandle(_file);
}

int gsmlib::baudRateStrToSpeed(std::string baudrate) throw(GsmException)
{
  if (baudrate == "300")
    return 300;
  else if (baudrate == "600")
    return 600;
  else if (baudrate == "1200")
    return 1200;
  else if (baudrate == "2400")
    return 2400;
  else if (baudrate == "4800")
    return 4800;
  else if (baudrate == "9600")
    return 9600;
  else if (baudrate == "19200")
    return 19200;
  else if (baudrate == "38400")
    return 38400;
  else if (baudrate == "57600")
    return 57600;
  else if (baudrate == "115200")
    return 115200;
  else
    throw GsmException(stringPrintf(_("unknown baudrate '%s'"),
                                    baudrate.c_str()), ParameterError);
}