3 * AT chat library with GLib integration
5 * Copyright (C) 2008-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 static gboolean set_baud(const char *baud, struct termios *ti)
39 if (g_str_equal(baud, "300"))
41 else if (g_str_equal(baud, "1200"))
43 else if (g_str_equal(baud, "2400"))
45 else if (g_str_equal(baud, "4800"))
47 else if (g_str_equal(baud, "9600"))
49 else if (g_str_equal(baud, "19200"))
51 else if (g_str_equal(baud, "38400"))
53 else if (g_str_equal(baud, "57600"))
55 else if (g_str_equal(baud, "115200"))
57 else if (g_str_equal(baud, "230400"))
59 else if (g_str_equal(baud, "460800"))
61 else if (g_str_equal(baud, "500000"))
63 else if (g_str_equal(baud, "576000"))
65 else if (g_str_equal(baud, "921600"))
67 else if (g_str_equal(baud, "1000000"))
69 else if (g_str_equal(baud, "1152000"))
71 else if (g_str_equal(baud, "1500000"))
73 else if (g_str_equal(baud, "2000000"))
76 else if (g_str_equal(baud, "2500000"))
80 else if (g_str_equal(baud, "3000000"))
84 else if (g_str_equal(baud, "3500000"))
88 else if (g_str_equal(baud, "4000000"))
94 cfsetospeed(ti, speed);
95 cfsetispeed(ti, speed);
100 static gboolean set_read(const char *bits, struct termios *ti)
102 if (g_str_equal(bits, "off"))
103 ti->c_cflag &= ~(CREAD);
104 else if (g_str_equal(bits, "on"))
105 ti->c_cflag |= CREAD;
112 static gboolean set_stop_bits(const char *bits, struct termios *ti)
114 if (g_str_equal(bits, "1"))
115 ti->c_cflag &= ~(CSTOPB);
116 else if (g_str_equal(bits, "2"))
117 ti->c_cflag |= CSTOPB;
124 static gboolean set_data_bits(const char *bits, struct termios *ti)
126 if (g_str_equal(bits, "7")) {
127 ti->c_cflag &= ~(CSIZE);
129 } else if (g_str_equal(bits, "8")) {
130 ti->c_cflag &= ~(CSIZE);
138 static gboolean set_parity(const char *parity, struct termios *ti)
140 if (g_str_equal(parity, "none"))
141 ti->c_cflag &= ~(PARENB);
142 else if (g_str_equal(parity, "even")) {
143 ti->c_cflag |= PARENB;
144 ti->c_cflag &= ~(PARODD);
145 } else if (g_str_equal(parity, "odd")) {
146 ti->c_cflag |= PARENB;
147 ti->c_cflag |= PARODD;
154 static gboolean set_xonxoff(const char *xonxoff, struct termios *ti)
156 if (g_str_equal(xonxoff, "on")) {
157 ti->c_iflag |= (IXON | IXOFF | IXANY);
158 ti->c_cc[VSTART] = 17;
159 ti->c_cc[VSTOP] = 19;
160 } else if (g_str_equal(xonxoff, "off"))
161 ti->c_iflag &= ~(IXON | IXOFF | IXANY);
168 static gboolean set_rtscts(const char *rtscts, struct termios *ti)
170 if (g_str_equal(rtscts, "on"))
171 ti->c_cflag |= CRTSCTS;
172 else if (g_str_equal(rtscts, "off"))
173 ti->c_cflag &= ~(CRTSCTS);
180 static gboolean set_local(const char *local, struct termios *ti)
182 if (g_str_equal(local, "on"))
183 ti->c_cflag |= CLOCAL;
184 else if (g_str_equal(local, "off"))
185 ti->c_cflag &= ~(CLOCAL);
192 static int open_device(const char *tty, GHashTable *options)
197 /* Switch TTY to raw mode */
198 memset(&ti, 0, sizeof(ti));
206 g_hash_table_iter_init (&iter, options);
207 while (g_hash_table_iter_next(&iter, (void *) &key,
211 if (g_str_equal(key, "Baud"))
212 ok = set_baud(value, &ti);
213 else if (g_str_equal(key, "StopBits"))
214 ok = set_stop_bits(value, &ti);
215 else if (g_str_equal(key, "DataBits"))
216 ok = set_data_bits(value, &ti);
217 else if (g_str_equal(key, "Parity"))
218 ok = set_parity(value, &ti);
219 else if (g_str_equal(key, "XonXoff"))
220 ok = set_xonxoff(value, &ti);
221 else if (g_str_equal(key, "RtsCts"))
222 ok = set_rtscts(value, &ti);
223 else if (g_str_equal(key, "Local"))
224 ok = set_local(value, &ti);
225 else if (g_str_equal(key, "Read"))
226 ok = set_read(value, &ti);
233 fd = open(tty, O_RDWR | O_NOCTTY | O_NONBLOCK);
237 tcflush(fd, TCIOFLUSH);
238 tcsetattr(fd, TCSANOW, &ti);
243 GIOChannel *g_at_tty_open(const char *tty, GHashTable *options)
248 fd = open_device(tty, options);
252 channel = g_io_channel_unix_new(fd);
254 if (channel == NULL) {
259 g_io_channel_set_close_on_unref(channel, TRUE);