blob: c801ab27601e1290441cdd08884cef265c2116d9 [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 }
97 DEBUGP(DLMI, "rs232 TX: %s\n", osmo_hexdump(msg->data, msg->len));
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) {
104 LOGP(DLMI, LOGL_ERROR, "rs232: short write\n");
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 */
118static int handle_ser_read(struct osmo_fd *bfd)
119{
120 struct serial_handle *sh = bfd->data;
121 struct msgb *msg;
122 int rc = 0;
123
124 if (!sh->rx_msg) {
125 sh->rx_msg = msgb_alloc(SERIAL_ALLOC_SIZE, "rs232 Rx");
126 sh->rx_msg->l2h = NULL;
127 }
128 msg = sh->rx_msg;
129
130 /* first read two byes to obtain length */
131 if (msg->len < 2) {
132 rc = read(bfd->fd, msg->tail, 2 - msg->len);
133 if (rc < 0) {
134 LOGP(DLMI, LOGL_ERROR, "rs232: error reading from "
135 "serial port: %s\n", strerror(errno));
136 msgb_free(msg);
137 return rc;
138 }
139 msgb_put(msg, rc);
140
141 if (msg->len >= 2) {
142 /* parse CRAPD payload length */
143 if (msg->data[0] != 0) {
144 LOGP(DLMI, LOGL_ERROR,
145 "Suspicious header byte 0: 0x%02x\n",
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) {
152 LOGP(DLMI, LOGL_ERROR,
153 "Invalid length in hdr: %u\n",
154 sh->rxmsg_bytes_missing);
155 }
156 }
157 } else {
158 /* try to read as many of the missing bytes as are available */
159 rc = read(bfd->fd, msg->tail, sh->rxmsg_bytes_missing);
160 if (rc < 0) {
161 LOGP(DLMI, LOGL_ERROR, "rs232: error reading from "
162 "serial port: %s", strerror(errno));
163 msgb_free(msg);
164 return rc;
165 }
166 msgb_put(msg, rc);
167 sh->rxmsg_bytes_missing -= rc;
168
169 if (sh->rxmsg_bytes_missing == 0) {
170 struct e1inp_ts *e1i_ts = &sh->line->ts[0];
171
172 /* we have one complete message now */
173 sh->rx_msg = NULL;
174
175 if (msg->len > CRAPD_HDR_LEN)
176 msg->l2h = msg->data + CRAPD_HDR_LEN;
177
178 DEBUGP(DLMI, "rs232 RX: %s",
179 osmo_hexdump(msg->data, msg->len));
180
181 /* don't use e1inp_tx_ts() here, this header does not
182 * contain any SAPI and TEI values. */
183 if (!e1i_ts->line->ops->sign_link) {
184 LOGP(DLMI, LOGL_ERROR, "rs232: no callback set, "
185 "skipping message.\n");
186 return -EINVAL;
187 }
188 e1i_ts->line->ops->sign_link(msg);
189 }
190 }
191
192 return rc;
193}
194
195/* select.c callback */
196static int serial_fd_cb(struct osmo_fd *bfd, unsigned int what)
197{
198 int rc = 0;
199
200 if (what & BSC_FD_READ)
201 rc = handle_ser_read(bfd);
202
203 if (rc < 0)
204 return rc;
205
206 if (what & BSC_FD_WRITE)
207 rc = handle_ser_write(bfd);
208
209 return rc;
210}
211
212static int rs232_want_write(struct e1inp_ts *e1i_ts)
213{
214 e1i_ts->driver.rs232.fd.when |= BSC_FD_WRITE;
215
216 return 0;
217}
218
219static int
220rs232_setup(struct e1inp_line *line, const char *serial_port, unsigned int delay_ms)
221{
222 int rc;
223 struct osmo_fd *bfd = &line->ts[0].driver.rs232.fd;
224 struct serial_handle *ser_handle;
225 struct termios tio;
226
227 rc = open(serial_port, O_RDWR);
228 if (rc < 0) {
229 LOGP(DLMI, LOGL_ERROR, "rs232: cannot open serial port: %s",
230 strerror(errno));
231 return rc;
232 }
233 bfd->fd = rc;
234
235 /* set baudrate */
236 rc = tcgetattr(bfd->fd, &tio);
237 if (rc < 0) {
238 LOGP(DLMI, LOGL_ERROR, "rs232: tcgetattr says: %s",
239 strerror(errno));
240 return rc;
241 }
242 cfsetispeed(&tio, B19200);
243 cfsetospeed(&tio, B19200);
244 tio.c_cflag |= (CREAD | CLOCAL | CS8);
245 tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
246 tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
247 tio.c_iflag |= (INPCK | ISTRIP);
248 tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR);
249 tio.c_oflag &= ~(OPOST);
250 rc = tcsetattr(bfd->fd, TCSADRAIN, &tio);
251 if (rc < 0) {
252 LOGP(DLMI, LOGL_ERROR, "rs232: tcsetattr says: %s",
253 strerror(errno));
254 return rc;
255 }
256
257 ser_handle = talloc_zero(tall_rs232_ctx, struct serial_handle);
258 if (ser_handle == NULL) {
259 close(bfd->fd);
260 LOGP(DLMI, LOGL_ERROR, "rs232: cannot allocate memory for "
261 "serial handler\n");
262 return -ENOMEM;
263 }
264 ser_handle->line = line;
265 ser_handle->delay_ms = delay_ms;
266
267 bfd->when = BSC_FD_READ;
268 bfd->cb = serial_fd_cb;
269 bfd->data = ser_handle;
270
271 rc = osmo_fd_register(bfd);
272 if (rc < 0) {
273 close(bfd->fd);
274 LOGP(DLMI, LOGL_ERROR, "rs232: could not register FD: %s\n",
Harald Welteb0a42352016-11-26 09:44:33 +0100275 strerror(-rc));
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200276 return rc;
277 }
278
279 return 0;
280}
281
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200282static int rs232_line_update(struct e1inp_line *line);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200283
284static struct e1inp_driver rs232_driver = {
285 .name = "rs232",
286 .want_write = rs232_want_write,
287 .line_update = rs232_line_update,
288};
289
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200290static int rs232_line_update(struct e1inp_line *line)
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200291{
292 if (line->driver != &rs232_driver)
293 return -EINVAL;
294
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200295 return rs232_setup(line, line->ops->cfg.rs232.port,
296 line->ops->cfg.rs232.delay);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200297}
298
299int e1inp_rs232_init(void)
300{
301 return e1inp_driver_register(&rs232_driver);
302}