~bilalakhtar/ubuntu/maverick/gpsdrive/gpsdrive-fix-325288

« back to all changes in this revision

Viewing changes to src/serial.c

  • Committer: Bazaar Package Importer
  • Author(s): Frank Kirschner
  • Date: 2004-05-25 11:44:03 UTC
  • Revision ID: james.westby@ubuntu.com-20040525114403-j3rsu57cavfax6z8
Tags: upstream-2.09
ImportĀ upstreamĀ versionĀ 2.09

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "config.h"
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
#include <unistd.h>
 
5
#include <string.h>
 
6
#include <sys/types.h>
 
7
#include <sys/socket.h>
 
8
#include <fcntl.h>
 
9
 
 
10
#if defined (HAVE_SYS_TERMIOS_H)
 
11
#include <sys/termios.h>
 
12
#else
 
13
#if defined (HAVE_TERMIOS_H)
 
14
#include <termios.h>
 
15
#endif
 
16
#endif
 
17
 
 
18
#if defined (HAVE_TERMIO_H)
 
19
#include <termio.h>
 
20
#define USE_TERMIO 1
 
21
#define TIOCGETA        TCGETA
 
22
#define TIOCSETAF       TCSETAF
 
23
 
 
24
#ifndef ONLCR
 
25
#define ONLCR           ONLRET
 
26
#endif
 
27
 
 
28
#define termios         termio
 
29
#define tcflag_t        ushort
 
30
#endif
 
31
 
 
32
#ifdef HAVE_SYS_IOCTL_H
 
33
#include <sys/ioctl.h>
 
34
#endif
 
35
 
 
36
#include "gpsd.h"
 
37
 
 
38
#define DEFAULTPORT "2947"
 
39
 
 
40
extern int debug;
 
41
extern char *device_name;
 
42
extern int device_speed;
 
43
 
 
44
 
 
45
/* define global variables */
 
46
int ttyfd = -1;
 
47
struct termios ttyset, ttyset_old;
 
48
 
 
49
int
 
50
serial_open ()
 
51
{
 
52
  char *temp;
 
53
  char *p;
 
54
 
 
55
  temp = malloc (strlen (device_name) + 1);
 
56
  strcpy (temp, device_name);
 
57
 
 
58
  if ((p = strchr (temp, ':')))
 
59
    {
 
60
      char *port = DEFAULTPORT;
 
61
      int one = 1;
 
62
 
 
63
      if (*(p + 1))
 
64
        port = p + 1;
 
65
      *p = '\0';
 
66
 
 
67
      /* temp now holds the HOSTNAME portion and port the port number. */
 
68
      if (debug > 5)
 
69
        fprintf (stderr, "Host: %s  Port: %s\n", temp, port);
 
70
      ttyfd = connectTCP (temp, port);
 
71
      free (temp);
 
72
      port = 0;
 
73
 
 
74
      setsockopt (ttyfd, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
 
75
                  sizeof (one));
 
76
 
 
77
      if (write (ttyfd, "r\n", 2) != 2)
 
78
        errexit ("Can't write to socket");
 
79
    }
 
80
  else
 
81
    {
 
82
      ttyfd = open (temp, O_RDWR | O_NONBLOCK);
 
83
      free (temp);
 
84
 
 
85
      if (ttyfd < 0)
 
86
        return (-1);
 
87
 
 
88
      if (isatty (ttyfd))
 
89
        {
 
90
 
 
91
          /* Save original terminal parameters */
 
92
          if (tcgetattr (ttyfd, &ttyset_old) != 0)
 
93
            return (-1);
 
94
 
 
95
          if (ioctl (ttyfd, TIOCGETA, &ttyset) < 0)
 
96
            return (-1);
 
97
 
 
98
#if defined (USE_TERMIO)
 
99
          ttyset.c_cflag = CBAUD & device_speed;
 
100
#else
 
101
          ttyset.c_ispeed = device_speed;
 
102
          ttyset.c_ospeed = device_speed;
 
103
#endif
 
104
          ttyset.c_cflag &= ~(PARENB | CRTSCTS);
 
105
          ttyset.c_cflag |= (CSIZE & CS8) | CREAD | CLOCAL;
 
106
          ttyset.c_iflag = ttyset.c_oflag = ttyset.c_lflag = (tcflag_t) 0;
 
107
          ttyset.c_oflag = (ONLCR);
 
108
          if (ioctl (ttyfd, TIOCSETAF, &ttyset) < 0)
 
109
            return (-1);
 
110
        }
 
111
    }
 
112
  return ttyfd;
 
113
}
 
114
 
 
115
void
 
116
serial_close ()
 
117
{
 
118
  if (ttyfd != -1)
 
119
    {
 
120
      if (isatty (ttyfd))
 
121
        {
 
122
#if defined (USE_TERMIO)
 
123
          ttyset.c_cflag = CBAUD & B0;
 
124
#else
 
125
          ttyset.c_ispeed = B0;
 
126
          ttyset.c_ospeed = B0;
 
127
#endif
 
128
          ioctl (ttyfd, TIOCSETAF, &ttyset);
 
129
        }
 
130
      /* Restore original terminal parameters */
 
131
      tcsetattr (ttyfd, TCSANOW, &ttyset_old);
 
132
 
 
133
      close (ttyfd);
 
134
      ttyfd = -1;
 
135
    }
 
136
}