aboutsummaryrefslogtreecommitdiffstats
path: root/src/rs232.c
blob: 865ec9771bbc8940fff0f9f9392304b95ef0195a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* T-Link interface using POSIX serial port */

/* (C) 2008-2011 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * Authors:	Harald Welte <laforge@gnumonks.org>
 *		Pablo Neira Ayuso <pablo@gnumonks.org>
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <limits.h>

#include <osmocom/core/select.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/timer.h>

struct osmo_rs232 {
	struct osmo_fd		ofd;
	struct llist_head	tx_queue;

	struct {
		int (*read)(struct osmo_rs232 *);
	} cb;

	/* sometimes we want to delay the transmission. */
	struct osmo_timer_list	tx_timer;

	struct {
		char		serial_port[PATH_MAX];
		int		baudrate;
		unsigned int	delay_us;
	} cfg;
};

void rs232_tx_timer_cb(void *ptr)
{
	struct osmo_rs232 *r = ptr;

	/* we're again ready to transmit. */
	r->ofd.when |= OSMO_FD_WRITE;
}

static int handle_ser_write(struct osmo_fd *bfd)
{
	struct osmo_rs232 *r = bfd->data;
	struct llist_head *lh;
	struct msgb *msg;
	int written;

	LOGP(DLINP, LOGL_DEBUG, "writing data to rs232\n");

	if (llist_empty(&r->tx_queue)) {
		r->ofd.when &= ~OSMO_FD_WRITE;
		return 0;
	}
	lh = r->tx_queue.next;
	llist_del(lh);
	msg = llist_entry(lh, struct msgb, list);

	written = write(bfd->fd, msg->data, msg->len);
	if (written < msg->len) {
		LOGP(DLINP, LOGL_ERROR, "rs232: short write\n");
		msgb_free(msg);
		return -1;
	}
	msgb_free(msg);

	/* We've got more data to write, but we have to wait to make it. */
	if (!llist_empty(&r->tx_queue) && r->cfg.delay_us) {
		r->ofd.when &= ~OSMO_FD_WRITE;
		osmo_timer_schedule(&r->tx_timer, 0, r->cfg.delay_us);
	}
	return 0;
}

static int handle_ser_read(struct osmo_fd *bfd)
{
	struct osmo_rs232 *r = bfd->data;

	LOGP(DLINP, LOGL_DEBUG, "data to be read in rs232\n");

	if (r->cb.read)
		r->cb.read(r);

	return 0;
}

static int serial_fd_cb(struct osmo_fd *bfd, unsigned int what)
{
	int rc = 0;

	if (what & OSMO_FD_READ)
		rc = handle_ser_read(bfd);

	if (rc < 0)
		return rc;

	if (what & OSMO_FD_WRITE)
		rc = handle_ser_write(bfd);

	return rc;
}

struct osmo_rs232 *osmo_rs232_create(void *ctx)
{
	struct osmo_rs232 *r;

	r = talloc_zero(ctx, struct osmo_rs232);
	if (r == NULL)
		return NULL;

	INIT_LLIST_HEAD(&r->tx_queue);

	return r;
}

void osmo_rs232_set_serial_port(struct osmo_rs232 *r, char *serial_port)
{
	osmo_strlcpy(r->cfg.serial_port, serial_port, PATH_MAX);
}

void osmo_rs232_set_baudrate(struct osmo_rs232 *r, int baudrate)
{
	r->cfg.baudrate = baudrate;
}

void osmo_rs232_set_delay_us(struct osmo_rs232 *r, int delay_us)
{
	r->cfg.delay_us = delay_us;
}

void osmo_rs232_set_read_cb(struct osmo_rs232 *r,
			    int (*read_cb)(struct osmo_rs232 *r))
{
	r->cb.read = read_cb;
}

/* XXX: Better use TIOCGSERIAL / TIOCSSERIAL to allow setting non-standard. */
static struct baudrate2termbits {
	int rate;
	int def;
} baudrate2termbits[] = {
	{ 9600, B9600 },
	{ 19200, B19200 },
	{ 38400, B38400 },
	{ 115200, B115200 },
	{ -1, -1 },
};

int osmo_rs232_open(struct osmo_rs232 *r)
{
	int rc, i, speed = 0;
	struct osmo_fd *bfd = &r->ofd;
	struct termios tio;

	rc = open(r->cfg.serial_port, O_RDWR);
	if (rc < 0) {
		LOGP(DLINP, LOGL_ERROR, "rs232: cannot open serial port: %s",
			strerror(errno));
		return rc;
	}
	bfd->fd = rc;

	/* set baudrate */
	rc = tcgetattr(bfd->fd, &tio);
	if (rc < 0) {
		LOGP(DLINP, LOGL_ERROR, "rs232: tcgetattr says: %s",
			strerror(errno));
		return rc;
	}
	for (i=0; i<baudrate2termbits[i].rate; i++) {
		if (baudrate2termbits[i].rate == -1)
			break;

		if (baudrate2termbits[i].rate == r->cfg.baudrate) {
			speed = baudrate2termbits[i].def;
			break;
		}
	}
	if (speed == 0) {
		close(rc);
		bfd->fd = -1;
		return -1;
	}

	cfsetispeed(&tio, speed);
	cfsetospeed(&tio, speed);
	tio.c_cflag |=  (CREAD | CLOCAL | CS8);
	tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
	tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
	tio.c_iflag |=  (INPCK | ISTRIP);
	tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR);
	tio.c_oflag &= ~(OPOST);
	rc = tcsetattr(bfd->fd, TCSADRAIN, &tio);
	if (rc < 0) {
		LOGP(DLINP, LOGL_ERROR, "rs232: tcsetattr says: %s",
			strerror(errno));
		return rc;
	}

	osmo_fd_setup(bfd, bfd->fd, OSMO_FD_READ, serial_fd_cb, r, 0);

	rc = osmo_fd_register(bfd);
	if (rc < 0) {
		close(bfd->fd);
		LOGP(DLINP, LOGL_ERROR, "rs232: could not register FD: %s\n",
			strerror(-rc));
		return rc;
	}

	if (r->cfg.delay_us)
		osmo_timer_setup(&r->tx_timer, rs232_tx_timer_cb, r);

	return 0;
}

int osmo_rs232_read(struct osmo_rs232 *r, struct msgb *msg)
{
	int ret;

	ret = read(r->ofd.fd, msg->data, msg->data_len);
	if (ret < 0) {
		LOGP(DLINP, LOGL_ERROR, "read error: %s\n", strerror(errno));
		return -EIO;
	}
	msgb_put(msg, ret);
	return ret;
}

void osmo_rs232_write(struct osmo_rs232 *r, struct msgb *msg)
{
	msgb_enqueue(&r->tx_queue, msg);
	r->ofd.when |= OSMO_FD_WRITE;
}

void osmo_rs232_close(struct osmo_rs232 *r)
{
	close(r->ofd.fd);
	r->ofd.fd = -1;
}

void osmo_rs232_destroy(struct osmo_rs232 *r)
{
	talloc_free(r);
}