]> sigrok.org Git - libsigrok.git/blob - hardware/common/serial.c
dfc6d5578c87b03c998a4b688dd50f99e0025da6
[libsigrok.git] / hardware / common / serial.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <glob.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <termios.h>
27 #include <stdlib.h>
28 #include <glib.h>
29
30 #include "sigrok.h"
31
32
33
34 char *serial_port_glob[] = {
35         /* Linux */
36         "/dev/ttyS*",
37         "/dev/ttyUSB*",
38         "/dev/ttyACM*",
39         /* MacOS X */
40         "/dev/ttys*",
41         "/dev/tty.USB-*",
42         "/dev/tty.Modem-*",
43         NULL
44 };
45
46
47 GSList *list_serial_ports(void)
48 {
49         glob_t g;
50         GSList *ports;
51         unsigned int i, j;
52
53         ports = NULL;
54         for(i = 0; serial_port_glob[i]; i++)
55         {
56                 if(!glob(serial_port_glob[i], 0, NULL, &g))
57                 {
58                         for(j = 0; j < g.gl_pathc; j++)
59                                 ports = g_slist_append(ports, strdup(g.gl_pathv[j]));
60                         globfree(&g);
61                 }
62         }
63
64         return ports;
65 }
66
67
68 int serial_open(const char *pathname, int flags)
69 {
70
71         return open(pathname, flags);
72 }
73
74
75 int serial_close(int fd)
76 {
77
78         return close(fd);
79 }
80
81
82 void *serial_backup_params(int fd)
83 {
84         struct termios *term;
85
86         term = malloc(sizeof(struct termios));
87         tcgetattr(fd, term);
88
89         return term;
90 }
91
92
93 void serial_restore_params(int fd, void *backup)
94 {
95
96         tcsetattr(fd, TCSADRAIN, (struct termios *) backup);
97
98 }
99
100
101 /* flowcontrol 1 = rts/cts  2 = xon/xoff */
102 int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, int flowcontrol)
103 {
104         struct termios term;
105
106         /* only supporting what we need really -- currently just the OLS driver */
107         if(speed != 115200 || bits != 8 || parity != 0 || stopbits != 1 || flowcontrol != 2)
108                 return SIGROK_ERR;
109
110         if(tcgetattr(fd, &term) < 0)
111                 return SIGROK_ERR;
112         if(cfsetispeed(&term, B115200) < 0)
113                 return SIGROK_ERR;
114         term.c_cflag &= ~CSIZE;
115         term.c_cflag |= CS8;
116         term.c_cflag &= ~CSTOPB;
117         term.c_cflag |= IXON | IXOFF;
118         term.c_iflag |= IGNPAR;
119         if(tcsetattr(fd, TCSADRAIN, &term) < 0)
120                 return SIGROK_ERR;
121
122         return SIGROK_OK;
123 }
124
125