Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file serial.c |
| 2 | * Utility functions to deal with serial ports */ |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 3 | /* |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 4 | * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | /*! \addtogroup serial |
| 24 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 25 | * Osmocom serial port helpers |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 26 | * |
| 27 | * \file serial.c */ |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 28 | |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <stdio.h> |
| 32 | #include <termios.h> |
| 33 | #include <unistd.h> |
| 34 | #include <sys/ioctl.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 37 | #ifdef __linux__ |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 38 | #include <linux/serial.h> |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 39 | #endif |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 40 | |
| 41 | #include <osmocom/core/serial.h> |
| 42 | |
| 43 | |
| 44 | #if 0 |
| 45 | # define dbg_perror(x) perror(x) |
| 46 | #else |
| 47 | # define dbg_perror(x) do { } while (0) |
| 48 | #endif |
| 49 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 50 | /*! Open serial device and does base init |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 51 | * \param[in] dev Path to the device node to open |
| 52 | * \param[in] baudrate Baudrate constant (speed_t: B9600, B...) |
| 53 | * \returns >=0 file descriptor in case of success or negative errno. |
| 54 | */ |
| 55 | int |
| 56 | osmo_serial_init(const char *dev, speed_t baudrate) |
| 57 | { |
Harald Welte | 1514f34 | 2016-12-24 17:59:36 +0100 | [diff] [blame] | 58 | int rc, fd=-1, v24, flags; |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 59 | struct termios tio; |
| 60 | |
Harald Welte | 1db3782 | 2016-12-23 22:49:39 +0100 | [diff] [blame] | 61 | /* Use nonblock as the device might block otherwise */ |
| 62 | fd = open(dev, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK); |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 63 | if (fd < 0) { |
| 64 | dbg_perror("open"); |
| 65 | return -errno; |
| 66 | } |
| 67 | |
Harald Welte | 1db3782 | 2016-12-23 22:49:39 +0100 | [diff] [blame] | 68 | /* now put it into blcoking mode */ |
| 69 | flags = fcntl(fd, F_GETFL, 0); |
| 70 | if (flags < 0) { |
| 71 | dbg_perror("fcntl get flags"); |
Harald Welte | d251045 | 2016-12-24 17:58:13 +0100 | [diff] [blame] | 72 | rc = -errno; |
| 73 | goto error; |
Harald Welte | 1db3782 | 2016-12-23 22:49:39 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | flags &= ~O_NONBLOCK; |
| 77 | rc = fcntl(fd, F_SETFL, flags); |
| 78 | if (rc != 0) { |
| 79 | dbg_perror("fcntl set flags"); |
Harald Welte | d251045 | 2016-12-24 17:58:13 +0100 | [diff] [blame] | 80 | rc = -errno; |
| 81 | goto error; |
Harald Welte | 1db3782 | 2016-12-23 22:49:39 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 84 | /* Configure serial interface */ |
| 85 | rc = tcgetattr(fd, &tio); |
| 86 | if (rc < 0) { |
| 87 | dbg_perror("tcgetattr()"); |
| 88 | rc = -errno; |
| 89 | goto error; |
| 90 | } |
| 91 | |
| 92 | cfsetispeed(&tio, baudrate); |
| 93 | cfsetospeed(&tio, baudrate); |
| 94 | |
| 95 | tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS); |
| 96 | tio.c_cflag |= (CREAD | CLOCAL | CS8); |
| 97 | tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 98 | tio.c_iflag |= (INPCK | ISTRIP); |
| 99 | tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR); |
| 100 | tio.c_oflag &= ~(OPOST | ONLCR); |
| 101 | |
| 102 | rc = tcsetattr(fd, TCSANOW, &tio); |
| 103 | if (rc < 0) { |
| 104 | dbg_perror("tcsetattr()"); |
| 105 | rc = -errno; |
| 106 | goto error; |
| 107 | } |
| 108 | |
| 109 | /* Set ready to read/write */ |
| 110 | v24 = TIOCM_DTR | TIOCM_RTS; |
| 111 | rc = ioctl(fd, TIOCMBIS, &v24); |
| 112 | if (rc < 0) { |
| 113 | dbg_perror("ioctl(TIOCMBIS)"); |
Harald Welte | c68ce3b | 2016-12-23 22:59:27 +0100 | [diff] [blame] | 114 | /* some serial porst don't support this, so let's not |
| 115 | * return an error here */ |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return fd; |
| 119 | |
| 120 | error: |
Harald Welte | 1514f34 | 2016-12-24 17:59:36 +0100 | [diff] [blame] | 121 | if (fd >= 0) |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 122 | close(fd); |
| 123 | return rc; |
| 124 | } |
| 125 | |
| 126 | static int |
| 127 | _osmo_serial_set_baudrate(int fd, speed_t baudrate) |
| 128 | { |
| 129 | int rc; |
| 130 | struct termios tio; |
| 131 | |
| 132 | rc = tcgetattr(fd, &tio); |
| 133 | if (rc < 0) { |
| 134 | dbg_perror("tcgetattr()"); |
| 135 | return -errno; |
| 136 | } |
| 137 | cfsetispeed(&tio, baudrate); |
| 138 | cfsetospeed(&tio, baudrate); |
| 139 | |
| 140 | rc = tcsetattr(fd, TCSANOW, &tio); |
| 141 | if (rc < 0) { |
| 142 | dbg_perror("tcgetattr()"); |
| 143 | return -errno; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 149 | /*! Change current baudrate |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 150 | * \param[in] fd File descriptor of the open device |
| 151 | * \param[in] baudrate Baudrate constant (speed_t: B9600, B...) |
| 152 | * \returns 0 for success or negative errno. |
| 153 | */ |
| 154 | int |
| 155 | osmo_serial_set_baudrate(int fd, speed_t baudrate) |
| 156 | { |
| 157 | osmo_serial_clear_custom_baudrate(fd); |
| 158 | return _osmo_serial_set_baudrate(fd, baudrate); |
| 159 | } |
| 160 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 161 | /*! Change current baudrate to a custom one using OS specific method |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 162 | * \param[in] fd File descriptor of the open device |
| 163 | * \param[in] baudrate Baudrate as integer |
| 164 | * \returns 0 for success or negative errno. |
| 165 | * |
| 166 | * This function might not work on all OS or with all type of serial adapters |
| 167 | */ |
| 168 | int |
| 169 | osmo_serial_set_custom_baudrate(int fd, int baudrate) |
| 170 | { |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 171 | #ifdef __linux__ |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 172 | int rc; |
| 173 | struct serial_struct ser_info; |
| 174 | |
| 175 | rc = ioctl(fd, TIOCGSERIAL, &ser_info); |
| 176 | if (rc < 0) { |
| 177 | dbg_perror("ioctl(TIOCGSERIAL)"); |
| 178 | return -errno; |
| 179 | } |
| 180 | |
| 181 | ser_info.flags = ASYNC_SPD_CUST | ASYNC_LOW_LATENCY; |
| 182 | ser_info.custom_divisor = ser_info.baud_base / baudrate; |
| 183 | |
| 184 | rc = ioctl(fd, TIOCSSERIAL, &ser_info); |
| 185 | if (rc < 0) { |
| 186 | dbg_perror("ioctl(TIOCSSERIAL)"); |
| 187 | return -errno; |
| 188 | } |
| 189 | |
| 190 | return _osmo_serial_set_baudrate(fd, B38400); /* 38400 is a kind of magic ... */ |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 191 | #elif defined(__APPLE__) |
| 192 | #ifndef IOSSIOSPEED |
| 193 | #define IOSSIOSPEED _IOW('T', 2, speed_t) |
| 194 | #endif |
| 195 | int rc; |
| 196 | |
| 197 | unsigned int speed = baudrate; |
| 198 | rc = ioctl(fd, IOSSIOSPEED, &speed); |
| 199 | if (rc < 0) { |
Sylvain Munaut | e40549a | 2011-09-28 10:55:22 +0200 | [diff] [blame] | 200 | dbg_perror("ioctl(IOSSIOSPEED)"); |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 201 | return -errno; |
| 202 | } |
| 203 | return 0; |
| 204 | #else |
Pau Espin Pedrol | ea809df | 2017-06-23 11:03:55 +0200 | [diff] [blame] | 205 | #pragma message ("osmo_serial_set_custom_baudrate: unsupported platform") |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 206 | return 0; |
| 207 | #endif |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 210 | /*! Clear any custom baudrate |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 211 | * \param[in] fd File descriptor of the open device |
| 212 | * \returns 0 for success or negative errno. |
| 213 | * |
| 214 | * This function might not work on all OS or with all type of serial adapters |
| 215 | */ |
| 216 | int |
| 217 | osmo_serial_clear_custom_baudrate(int fd) |
| 218 | { |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 219 | #ifdef __linux__ |
Sylvain Munaut | 31d3de9 | 2011-11-20 08:55:11 +0100 | [diff] [blame] | 220 | int rc; |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 221 | struct serial_struct ser_info; |
| 222 | |
| 223 | rc = ioctl(fd, TIOCGSERIAL, &ser_info); |
| 224 | if (rc < 0) { |
| 225 | dbg_perror("ioctl(TIOCGSERIAL)"); |
| 226 | return -errno; |
| 227 | } |
| 228 | |
| 229 | ser_info.flags = ASYNC_LOW_LATENCY; |
| 230 | ser_info.custom_divisor = 0; |
| 231 | |
| 232 | rc = ioctl(fd, TIOCSSERIAL, &ser_info); |
| 233 | if (rc < 0) { |
| 234 | dbg_perror("ioctl(TIOCSSERIAL)"); |
| 235 | return -errno; |
| 236 | } |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 237 | #endif |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 241 | /*! @} */ |