Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 1 | /* T-Link interface using POSIX serial port */ |
| 2 | |
| 3 | /* (C) 2008-2011 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * Authors: Harald Welte <laforge@gnumonks.org> |
| 8 | * Pablo Neira Ayuso <pablo@gnumonks.org> |
| 9 | * |
Harald Welte | 323d39d | 2017-11-13 01:09:21 +0900 | [diff] [blame] | 10 | * SPDX-License-Identifier: AGPL-3.0+ |
| 11 | * |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Affero General Public License as published by |
| 14 | * the Free Software Foundation; either version 3 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Affero General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Affero General Public License |
| 23 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <unistd.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | #include <errno.h> |
| 31 | #include <string.h> |
| 32 | #include <termios.h> |
| 33 | #include <fcntl.h> |
| 34 | |
| 35 | #include <osmocom/core/select.h> |
| 36 | #include <osmocom/core/msgb.h> |
| 37 | #include <osmocom/core/logging.h> |
| 38 | #include <osmocom/core/talloc.h> |
| 39 | #include <osmocom/abis/e1_input.h> |
| 40 | |
| 41 | static void *tall_rs232_ctx; |
| 42 | |
| 43 | struct serial_handle { |
| 44 | struct e1inp_line *line; |
| 45 | |
| 46 | struct msgb *rx_msg; |
| 47 | unsigned int rxmsg_bytes_missing; |
| 48 | |
| 49 | unsigned int delay_ms; |
| 50 | }; |
| 51 | |
| 52 | #define CRAPD_HDR_LEN 10 |
| 53 | |
| 54 | static int handle_ser_write(struct osmo_fd *bfd); |
| 55 | |
| 56 | static void rs232_build_msg(struct msgb *msg) |
| 57 | { |
| 58 | uint8_t *crapd; |
| 59 | unsigned int len; |
| 60 | |
| 61 | msg->l2h = msg->data; |
| 62 | |
| 63 | /* prepend CRAPD header */ |
| 64 | crapd = msgb_push(msg, CRAPD_HDR_LEN); |
| 65 | |
| 66 | len = msg->len - 2; |
| 67 | |
| 68 | crapd[0] = (len >> 8) & 0xff; |
| 69 | crapd[1] = len & 0xff; /* length of bytes startign at crapd[2] */ |
| 70 | crapd[2] = 0x00; |
| 71 | crapd[3] = 0x07; |
| 72 | crapd[4] = 0x01; |
| 73 | crapd[5] = 0x3e; |
| 74 | crapd[6] = 0x00; |
| 75 | crapd[7] = 0x00; |
| 76 | crapd[8] = msg->len - 10; /* length of bytes starting at crapd[10] */ |
| 77 | crapd[9] = crapd[8] ^ 0x38; |
| 78 | } |
| 79 | |
| 80 | /* select.c callback in case we can write to the rs232 */ |
| 81 | static int handle_ser_write(struct osmo_fd *bfd) |
| 82 | { |
| 83 | struct serial_handle *sh = bfd->data; |
| 84 | struct e1inp_ts *e1i_ts = &sh->line->ts[0]; |
| 85 | struct e1inp_sign_link *sign_link; |
| 86 | struct msgb *msg; |
| 87 | int written; |
| 88 | |
Harald Welte | 949b8a2 | 2020-10-18 23:01:53 +0200 | [diff] [blame] | 89 | osmo_fd_write_disable(bfd); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 90 | |
| 91 | /* get the next msg for this timeslot */ |
| 92 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 93 | if (!msg) { |
| 94 | /* no message after tx delay timer */ |
| 95 | return 0; |
| 96 | } |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 97 | LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "rs232 TX: %s\n", osmo_hexdump(msg->data, msg->len)); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 98 | |
| 99 | rs232_build_msg(msg); |
| 100 | |
| 101 | /* send over serial line */ |
| 102 | written = write(bfd->fd, msg->data, msg->len); |
| 103 | if (written < msg->len) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 104 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "rs232: short write\n"); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 105 | msgb_free(msg); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | msgb_free(msg); |
| 110 | usleep(sh->delay_ms*1000); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | #define SERIAL_ALLOC_SIZE 300 |
| 116 | |
| 117 | /* select.c callback in case we can read from the rs232 */ |
| 118 | static int handle_ser_read(struct osmo_fd *bfd) |
| 119 | { |
| 120 | struct serial_handle *sh = bfd->data; |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 121 | struct e1inp_ts *e1i_ts = &sh->line->ts[0]; |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 122 | struct msgb *msg; |
| 123 | int rc = 0; |
| 124 | |
| 125 | if (!sh->rx_msg) { |
| 126 | sh->rx_msg = msgb_alloc(SERIAL_ALLOC_SIZE, "rs232 Rx"); |
| 127 | sh->rx_msg->l2h = NULL; |
| 128 | } |
| 129 | msg = sh->rx_msg; |
| 130 | |
| 131 | /* first read two byes to obtain length */ |
| 132 | if (msg->len < 2) { |
| 133 | rc = read(bfd->fd, msg->tail, 2 - msg->len); |
| 134 | if (rc < 0) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 135 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "rs232: error reading from " |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 136 | "serial port: %s\n", strerror(errno)); |
| 137 | msgb_free(msg); |
| 138 | return rc; |
| 139 | } |
| 140 | msgb_put(msg, rc); |
| 141 | |
| 142 | if (msg->len >= 2) { |
| 143 | /* parse CRAPD payload length */ |
| 144 | if (msg->data[0] != 0) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 145 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "Suspicious header byte 0: 0x%02x\n", |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 146 | msg->data[0]); |
| 147 | } |
| 148 | sh->rxmsg_bytes_missing = msg->data[0] << 8; |
| 149 | sh->rxmsg_bytes_missing += msg->data[1]; |
| 150 | |
| 151 | if (sh->rxmsg_bytes_missing < CRAPD_HDR_LEN -2) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 152 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "Invalid length in hdr: %u\n", |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 153 | sh->rxmsg_bytes_missing); |
| 154 | } |
| 155 | } |
| 156 | } else { |
| 157 | /* try to read as many of the missing bytes as are available */ |
| 158 | rc = read(bfd->fd, msg->tail, sh->rxmsg_bytes_missing); |
| 159 | if (rc < 0) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 160 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "rs232: error reading from serial port: %s", |
| 161 | strerror(errno)); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 162 | msgb_free(msg); |
| 163 | return rc; |
| 164 | } |
| 165 | msgb_put(msg, rc); |
| 166 | sh->rxmsg_bytes_missing -= rc; |
| 167 | |
| 168 | if (sh->rxmsg_bytes_missing == 0) { |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 169 | |
| 170 | /* we have one complete message now */ |
| 171 | sh->rx_msg = NULL; |
| 172 | |
| 173 | if (msg->len > CRAPD_HDR_LEN) |
| 174 | msg->l2h = msg->data + CRAPD_HDR_LEN; |
| 175 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 176 | LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "rs232 RX: %s", osmo_hexdump(msg->data, msg->len)); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 177 | |
| 178 | /* don't use e1inp_tx_ts() here, this header does not |
| 179 | * contain any SAPI and TEI values. */ |
| 180 | if (!e1i_ts->line->ops->sign_link) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 181 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "rs232: no callback set, skipping message.\n"); |
| 182 | return -EINVAL; |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 183 | } |
| 184 | e1i_ts->line->ops->sign_link(msg); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return rc; |
| 189 | } |
| 190 | |
| 191 | /* select.c callback */ |
| 192 | static int serial_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 193 | { |
| 194 | int rc = 0; |
| 195 | |
Harald Welte | de3959e | 2020-10-18 22:28:50 +0200 | [diff] [blame] | 196 | if (what & OSMO_FD_READ) |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 197 | rc = handle_ser_read(bfd); |
| 198 | |
| 199 | if (rc < 0) |
| 200 | return rc; |
| 201 | |
Harald Welte | de3959e | 2020-10-18 22:28:50 +0200 | [diff] [blame] | 202 | if (what & OSMO_FD_WRITE) |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 203 | rc = handle_ser_write(bfd); |
| 204 | |
| 205 | return rc; |
| 206 | } |
| 207 | |
| 208 | static int rs232_want_write(struct e1inp_ts *e1i_ts) |
| 209 | { |
Harald Welte | 949b8a2 | 2020-10-18 23:01:53 +0200 | [diff] [blame] | 210 | osmo_fd_write_enable(&e1i_ts->driver.rs232.fd); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int |
| 216 | rs232_setup(struct e1inp_line *line, const char *serial_port, unsigned int delay_ms) |
| 217 | { |
| 218 | int rc; |
| 219 | struct osmo_fd *bfd = &line->ts[0].driver.rs232.fd; |
| 220 | struct serial_handle *ser_handle; |
| 221 | struct termios tio; |
| 222 | |
| 223 | rc = open(serial_port, O_RDWR); |
| 224 | if (rc < 0) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 225 | LOGPIL(line, DLMI, LOGL_ERROR, "rs232: cannot open serial port: %s", strerror(errno)); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 226 | return rc; |
| 227 | } |
| 228 | bfd->fd = rc; |
| 229 | |
| 230 | /* set baudrate */ |
| 231 | rc = tcgetattr(bfd->fd, &tio); |
| 232 | if (rc < 0) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 233 | LOGPIL(line, DLMI, LOGL_ERROR, "rs232: tcgetattr says: %s", strerror(errno)); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 234 | return rc; |
| 235 | } |
| 236 | cfsetispeed(&tio, B19200); |
| 237 | cfsetospeed(&tio, B19200); |
| 238 | tio.c_cflag |= (CREAD | CLOCAL | CS8); |
| 239 | tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS); |
| 240 | tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 241 | tio.c_iflag |= (INPCK | ISTRIP); |
| 242 | tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR); |
| 243 | tio.c_oflag &= ~(OPOST); |
| 244 | rc = tcsetattr(bfd->fd, TCSADRAIN, &tio); |
| 245 | if (rc < 0) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 246 | LOGPIL(line, DLMI, LOGL_ERROR, "rs232: tcsetattr says: %s", strerror(errno)); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 247 | return rc; |
| 248 | } |
| 249 | |
| 250 | ser_handle = talloc_zero(tall_rs232_ctx, struct serial_handle); |
| 251 | if (ser_handle == NULL) { |
| 252 | close(bfd->fd); |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 253 | LOGPIL(line, DLMI, LOGL_ERROR, "rs232: cannot allocate memory for serial handler\n"); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 254 | return -ENOMEM; |
| 255 | } |
| 256 | ser_handle->line = line; |
| 257 | ser_handle->delay_ms = delay_ms; |
| 258 | |
Harald Welte | 6e831b7 | 2020-10-18 22:59:58 +0200 | [diff] [blame] | 259 | osmo_fd_setup(bfd, bfd->fd, OSMO_FD_READ, serial_fd_cb, ser_handle, 0); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 260 | |
| 261 | rc = osmo_fd_register(bfd); |
| 262 | if (rc < 0) { |
| 263 | close(bfd->fd); |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 264 | LOGPIL(line, DLMI, LOGL_ERROR, "rs232: could not register FD: %s\n", strerror(-rc)); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 265 | return rc; |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 271 | static int rs232_line_update(struct e1inp_line *line); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 272 | |
| 273 | static struct e1inp_driver rs232_driver = { |
| 274 | .name = "rs232", |
| 275 | .want_write = rs232_want_write, |
| 276 | .line_update = rs232_line_update, |
| 277 | }; |
| 278 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 279 | static int rs232_line_update(struct e1inp_line *line) |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 280 | { |
| 281 | if (line->driver != &rs232_driver) |
| 282 | return -EINVAL; |
| 283 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 284 | return rs232_setup(line, line->ops->cfg.rs232.port, |
| 285 | line->ops->cfg.rs232.delay); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | int e1inp_rs232_init(void) |
| 289 | { |
| 290 | return e1inp_driver_register(&rs232_driver); |
| 291 | } |