Tscope5
cedrusbox_posix_internal.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file cedrusbox_posix_internal.c
11 /// Definitions of Posix-specific (Mac OS X, Linux) cedrusbox functions.
12 ////////////////////////////////////////////////////////////////////////////////
13 
14 ////////////////////////////////////////////////////////////////////////////////
15 /// @name Posix-specific (Mac OS X, Linux) cedrusbox functions
16 //@{
17 ////////////////////////////////////////////////////////////////////////////////
18 
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <dirent.h>
22 #include <termios.h>
23 #include <errno.h>
24 
25 #include "../include/tscope5/timer.h"
26 
27 int _ts5_cedrusbox_fd[TS5_CEDRUSBOX_MAXPORT] = {TS5_CEDRUSBOX_NODEVICE};
28 struct termios _ts5_cedrusbox_oldoptions[TS5_CEDRUSBOX_MAXPORT];
29 
30 int ts5_install_cedrusbox_posix(char *calling_function);
32 unsigned int ts5_read_cedrusbox_posix(int port, char *buff, unsigned long bytes_to_read);
33 unsigned int ts5_write_cedrusbox_posix(int port, char *buff, unsigned long bytes_to_write);
34 void ts5_fflush_cedrusbox_posix(int port);
35 
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// Posix-specific (Mac OS X, Linux) cedrusbox function.
39 ///
40 /// \param calling_function Name the function that calls for this check or installation.
41 ///
42 /// \warning This is an internal function. Do not call this function directly.
43 ////////////////////////////////////////////////////////////////////////////////
44 int ts5_install_cedrusbox_posix(char *calling_function)
45 {
46  int num_serial_devices = 0;
47 
48  // get directory listing of /dev
49  DIR *dp;
50  struct dirent *ep;
51  dp = opendir ("/dev");
52 
53  if (!dp) {
54  ts5_fatal("%s: could not get directory listing\n", calling_function);
55  }
56 
57  // look for a tty.usbserial entry in the /dev directory
58  char device_path[TS5_CEDRUSBOX_MAXPORT][TS5_MAX_CHAR];
59 
60  while ((ep = readdir (dp)) && num_serial_devices<TS5_CEDRUSBOX_MAXPORT) {
61 
62  char devicename[TS5_MAX_CHAR];
63 
64  #ifdef TS5_MACOSX
65  strcpy(devicename, "tty.usbserial");
66 
67  #else
68  strcpy(devicename, "ttyUSB");
69 
70  #endif
71 
72  if (strstr(ep->d_name, devicename)!=0) {
73 
74  sprintf(device_path[num_serial_devices], "/dev/%s", ep->d_name);
75  ts5_log(TS5_LOGLEVEL_1,"%s: found a usb serial device at %s\n",
76  calling_function, device_path[num_serial_devices]);
77 
78  // open the device
79  _ts5_cedrusbox_fd[num_serial_devices] = open(device_path[num_serial_devices],
80  O_RDWR | O_NOCTTY | O_NDELAY);
81 
82  if (_ts5_cedrusbox_fd[num_serial_devices] == -1) {
83  ts5_fatal("%s: could not open usb serial device at %s\n",
84  calling_function, device_path[num_serial_devices]);
85  }
86 
87  // get exclusive io
88  if (ioctl(_ts5_cedrusbox_fd[num_serial_devices], TIOCEXCL) == -1) {
89  ts5_fatal("%s: could not get exclusivity on usb serial device at %s\n",
90  calling_function, device_path[num_serial_devices]);
91  }
92 
93  // set options for the connection
94  struct termios options;
95  tcgetattr(_ts5_cedrusbox_fd[num_serial_devices] ,
96  &_ts5_cedrusbox_oldoptions[num_serial_devices]);
97  bzero (&options, sizeof (options));
98 
99  cfsetispeed(&options, B115200);
100  cfsetospeed(&options, B115200);
101 
102  options.c_iflag = IGNBRK;
103  options.c_lflag = 0;
104  options.c_oflag = 0;
105  options.c_cflag |= CLOCAL | CREAD;
106  options.c_iflag = IGNPAR;
107  options.c_oflag = 0;
108  options.c_cflag &= ~(PARENB|PARODD) ;
109  options.c_cflag &= ~CSTOPB ;
110  options.c_cflag &= ~CSIZE;
111  options.c_cflag |= CS8;
112  options.c_cflag &= ~CRTSCTS;
113  options.c_iflag &= ~(IXON | IXOFF | IXANY);
114 
115  if (tcsetattr (_ts5_cedrusbox_fd[num_serial_devices], TCSANOW, &options) == -1) {
116  ts5_fatal("%s: could not set options on usb serial device at %s\n",
117  calling_function, device_path[num_serial_devices]);
118  }
119 
120  if (fcntl(_ts5_cedrusbox_fd[num_serial_devices], F_SETFL, FNDELAY) == -1) {
121  ts5_fatal("%s: could not set delay on usb serial device at %s\n",
122  calling_function, device_path[num_serial_devices]);
123  }
124 
125  // if we get here mark it as a possible cedrusbox
126  num_serial_devices++;
127  }
128  }
129  al_rest(0.2);
130 
131  return num_serial_devices;
132 }
133 
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// Posix-specific (Mac OS X, Linux) cedrusbox function.
137 ///
138 /// \warning This is an internal function. Do not call this function directly.
139 ////////////////////////////////////////////////////////////////////////////////
141 {
142  int i;
143  for (i=0; i<_ts5_status.timer.num_cedrusboxes; i++) {
144 
145  if (_ts5_cedrusbox_fd[i] > 0) {
146 
147  tcdrain(_ts5_cedrusbox_fd[i]);
148  tcsetattr(_ts5_cedrusbox_fd[i], TCSANOW, &_ts5_cedrusbox_oldoptions[i]);
149 
150  if (close(_ts5_cedrusbox_fd[i]) == -1) {
151  ts5_log(TS5_LOGLEVEL_1, "ts5_uninstall_cedrusbox_posix:\n");
152  ts5_log(TS5_LOGLEVEL_1, "\tcould not uninstall cedrusbox %d\n", i);
153  }
154 
155  ts5_log(TS5_LOGLEVEL_1, "ts5_uninstall_cedrusbox_posix: uninstalled cedrusbox %d\n", i);
156  }
157  }
158 }
159 
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// Posix-specific (Mac OS X, Linux) cedrusbox function.
163 ///
164 /// \warning This is an internal function. Do not call this function directly.
165 ////////////////////////////////////////////////////////////////////////////////
166 unsigned int ts5_read_cedrusbox_posix(int port, char *buff, unsigned long bytes_to_read)
167 {
168  unsigned int bytes_read;
169  bytes_read = read(_ts5_cedrusbox_fd[port], buff, bytes_to_read);
170 
171  if ((int)bytes_read == -1) {
172  ts5_fatal("ts5_read_cedrusbox_posix: read error\n");
173  }
174 
175  return bytes_read;
176 }
177 
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// Posix-specific (Mac OS X, Linux) cedrusbox function.
181 ///
182 /// \warning This is an internal function. Do not call this function directly.
183 ////////////////////////////////////////////////////////////////////////////////
184 unsigned int ts5_write_cedrusbox_posix(int port, char *buff, unsigned long bytes_to_write)
185 {
186  unsigned int bytes_written;
187  bytes_written = write(_ts5_cedrusbox_fd[port], buff, bytes_to_write);
188 
189  if (bytes_written != bytes_to_write) {
190  ts5_fatal("ts5_write_cedrusbox_posix: write error\n");
191  }
192 
193  ts5_wait(0.10);
194 
195  return bytes_written;
196 }
197 
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 /// Posix-specific (Mac OS X, Linux) cedrusbox function.
201 ///
202 /// \warning This is an internal function. Do not call this function directly.
203 ////////////////////////////////////////////////////////////////////////////////
205 {
206  char buff[TS5_MAX_CHAR];
207  unsigned int bytes_read;
208 
209  do {
210  bytes_read = read (_ts5_cedrusbox_fd[port], buff, 6);
211  } while (bytes_read > 0);
212 }
213 
214 //@}
int ts5_install_cedrusbox_posix(char *calling_function)
Posix-specific (Mac OS X, Linux) cedrusbox function.
void ts5_wait(double waittime)
Wait for a number of seconds.
Definition: timer.c:220
void ts5_uninstall_cedrusbox_posix()
Posix-specific (Mac OS X, Linux) cedrusbox function.
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
Definition: system.c:45
unsigned int ts5_read_cedrusbox_posix(int port, char *buff, unsigned long bytes_to_read)
Posix-specific (Mac OS X, Linux) cedrusbox function.
void ts5_fflush_cedrusbox_posix(int port)
Posix-specific (Mac OS X, Linux) cedrusbox function.
void ts5_fatal(const char *format,...)
Exit safely with an error message.
Definition: system.c:533
unsigned int ts5_write_cedrusbox_posix(int port, char *buff, unsigned long bytes_to_write)
Posix-specific (Mac OS X, Linux) cedrusbox function.