blob: dfbf97c44bebec1a273f004f6ba931d99c242e06 [file] [log] [blame]
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +02001/* 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 Welte323d39d2017-11-13 01:09:21 +090010 * SPDX-License-Identifier: AGPL-3.0+
11 *
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +020012 * 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
41static void *tall_rs232_ctx;
42
43struct 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
54static int handle_ser_write(struct osmo_fd *bfd);
55
56static 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 */
81static 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
89 bfd->when &= ~BSC_FD_WRITE;
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 Welteb5af0992020-01-12 12:59:52 +010097 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "rs232 TX: %s\n", osmo_hexdump(msg->data, msg->len));
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +020098
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 Welteb5af0992020-01-12 12:59:52 +0100104 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "rs232: short write\n");
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200105 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 */
118static int handle_ser_read(struct osmo_fd *bfd)
119{
120 struct serial_handle *sh = bfd->data;
Harald Welteb5af0992020-01-12 12:59:52 +0100121 struct e1inp_ts *e1i_ts = &sh->line->ts[0];
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200122 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 Welteb5af0992020-01-12 12:59:52 +0100135 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "rs232: error reading from "
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200136 "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 Welteb5af0992020-01-12 12:59:52 +0100145 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "Suspicious header byte 0: 0x%02x\n",
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200146 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 Welteb5af0992020-01-12 12:59:52 +0100152 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "Invalid length in hdr: %u\n",
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200153 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 Welteb5af0992020-01-12 12:59:52 +0100160 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "rs232: error reading from serial port: %s",
161 strerror(errno));
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200162 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 Ayuso7e0d0062011-08-19 11:36:15 +0200169
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 Welteb5af0992020-01-12 12:59:52 +0100176 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "rs232 RX: %s", osmo_hexdump(msg->data, msg->len));
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200177
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 Welteb5af0992020-01-12 12:59:52 +0100181 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "rs232: no callback set, skipping message.\n");
182 return -EINVAL;
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200183 }
184 e1i_ts->line->ops->sign_link(msg);
185 }
186 }
187
188 return rc;
189}
190
191/* select.c callback */
192static int serial_fd_cb(struct osmo_fd *bfd, unsigned int what)
193{
194 int rc = 0;
195
196 if (what & BSC_FD_READ)
197 rc = handle_ser_read(bfd);
198
199 if (rc < 0)
200 return rc;
201
202 if (what & BSC_FD_WRITE)
203 rc = handle_ser_write(bfd);
204
205 return rc;
206}
207
208static int rs232_want_write(struct e1inp_ts *e1i_ts)
209{
210 e1i_ts->driver.rs232.fd.when |= BSC_FD_WRITE;
211
212 return 0;
213}
214
215static int
216rs232_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 Welteb5af0992020-01-12 12:59:52 +0100225 LOGPIL(line, DLMI, LOGL_ERROR, "rs232: cannot open serial port: %s", strerror(errno));
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200226 return rc;
227 }
228 bfd->fd = rc;
229
230 /* set baudrate */
231 rc = tcgetattr(bfd->fd, &tio);
232 if (rc < 0) {
Harald Welteb5af0992020-01-12 12:59:52 +0100233 LOGPIL(line, DLMI, LOGL_ERROR, "rs232: tcgetattr says: %s", strerror(errno));
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200234 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 Welteb5af0992020-01-12 12:59:52 +0100246 LOGPIL(line, DLMI, LOGL_ERROR, "rs232: tcsetattr says: %s", strerror(errno));
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200247 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 Welteb5af0992020-01-12 12:59:52 +0100253 LOGPIL(line, DLMI, LOGL_ERROR, "rs232: cannot allocate memory for serial handler\n");
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200254 return -ENOMEM;
255 }
256 ser_handle->line = line;
257 ser_handle->delay_ms = delay_ms;
258
259 bfd->when = BSC_FD_READ;
260 bfd->cb = serial_fd_cb;
261 bfd->data = ser_handle;
262
263 rc = osmo_fd_register(bfd);
264 if (rc < 0) {
265 close(bfd->fd);
Harald Welteb5af0992020-01-12 12:59:52 +0100266 LOGPIL(line, DLMI, LOGL_ERROR, "rs232: could not register FD: %s\n", strerror(-rc));
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200267 return rc;
268 }
269
270 return 0;
271}
272
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200273static int rs232_line_update(struct e1inp_line *line);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200274
275static struct e1inp_driver rs232_driver = {
276 .name = "rs232",
277 .want_write = rs232_want_write,
278 .line_update = rs232_line_update,
279};
280
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200281static int rs232_line_update(struct e1inp_line *line)
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200282{
283 if (line->driver != &rs232_driver)
284 return -EINVAL;
285
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200286 return rs232_setup(line, line->ops->cfg.rs232.port,
287 line->ops->cfg.rs232.delay);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200288}
289
290int e1inp_rs232_init(void)
291{
292 return e1inp_driver_register(&rs232_driver);
293}