blob: 24577ea874a251b326a503805bacc36b56e7d09c [file] [log] [blame]
Harald Weltec12d52b2009-02-01 21:39:06 +00001/* OpenBSC BS-11 T-Link interface using POSIX serial port */
2
3/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <unistd.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <errno.h>
27#include <string.h>
28#include <termios.h>
29#include <fcntl.h>
30
31#include <openbsc/select.h>
32#include <openbsc/msgb.h>
33#include <openbsc/debug.h>
34#include <openbsc/gsm_data.h>
Harald Welte099d6102009-02-21 12:59:58 +000035#include <openbsc/rs232.h>
Harald Weltec12d52b2009-02-01 21:39:06 +000036
37/* adaption layer from GSM 08.59 + 12.21 to RS232 */
38
39struct serial_handle {
40 struct bsc_fd fd;
41 struct llist_head tx_queue;
42
43 struct msgb *rx_msg;
44 unsigned int rxmsg_bytes_missing;
45
46 unsigned int delay_ms;
47};
48
49/* FIXME: this needs to go */
50static struct serial_handle _ser_handle, *ser_handle = &_ser_handle;
51
52#define LAPD_HDR_LEN 10
53
Harald Weltef80b7d32009-02-19 03:11:13 +000054static int handle_ser_write(struct bsc_fd *bfd);
55
Harald Weltec12d52b2009-02-01 21:39:06 +000056/* callback from abis_nm */
57int _abis_nm_sendmsg(struct msgb *msg)
58{
59 struct serial_handle *sh = ser_handle;
60 u_int8_t *lapd;
61 unsigned int len;
62
63 msg->l2h = msg->data;
64
65 /* prepend LAPD header */
66 lapd = msgb_push(msg, LAPD_HDR_LEN);
67
68 len = msg->len - 2;
69
70 lapd[0] = (len >> 8) & 0xff;
71 lapd[1] = len & 0xff; /* length of bytes startign at lapd[2] */
72 lapd[2] = 0x00;
73 lapd[3] = 0x07;
74 lapd[4] = 0x01;
75 lapd[5] = 0x3e;
76 lapd[6] = 0x00;
77 lapd[7] = 0x00;
78 lapd[8] = msg->len - 10; /* length of bytes starting at lapd[10] */
79 lapd[9] = lapd[8] ^ 0x38;
80
81 msgb_enqueue(&sh->tx_queue, msg);
82 sh->fd.when |= BSC_FD_WRITE;
83
Harald Weltef80b7d32009-02-19 03:11:13 +000084 /* we try to immediately send */
85 handle_ser_write(&sh->fd);
86
Harald Weltec12d52b2009-02-01 21:39:06 +000087 return 0;
88}
89
90/* select.c callback in case we can write to the RS232 */
91static int handle_ser_write(struct bsc_fd *bfd)
92{
93 struct serial_handle *sh = bfd->data;
94 struct msgb *msg;
95 int written;
96
97 msg = msgb_dequeue(&sh->tx_queue);
98 if (!msg) {
99 bfd->when &= ~BSC_FD_WRITE;
100 return 0;
101 }
102
Harald Welte3cc4bf52009-02-28 13:08:01 +0000103 DEBUGP(DMI, "RS232 TX: %s", hexdump(msg->data, msg->len));
Harald Weltec12d52b2009-02-01 21:39:06 +0000104
105 /* send over serial line */
106 written = write(bfd->fd, msg->data, msg->len);
107 if (written < msg->len) {
108 perror("short write:");
109 msgb_free(msg);
110 return -1;
111 }
112
113 msgb_free(msg);
114 usleep(sh->delay_ms*1000);
115
116 return 0;
117}
118
119#define SERIAL_ALLOC_SIZE 300
120
121/* select.c callback in case we can read from the RS232 */
122static int handle_ser_read(struct bsc_fd *bfd)
123{
124 struct serial_handle *sh = bfd->data;
125 struct msgb *msg;
126 int rc = 0;
127
128 if (!sh->rx_msg) {
129 sh->rx_msg = msgb_alloc(SERIAL_ALLOC_SIZE);
130 sh->rx_msg->l2h = NULL;
131 }
132 msg = sh->rx_msg;
133
134 /* first read two byes to obtain length */
135 if (msg->len < 2) {
136 rc = read(sh->fd.fd, msg->tail, 2 - msg->len);
137 if (rc < 0) {
138 perror("ERROR reading from serial port");
139 msgb_free(msg);
140 return rc;
141 }
142 msgb_put(msg, rc);
143
144 if (msg->len >= 2) {
145 /* parse LAPD payload length */
146 if (msg->data[0] != 0)
147 fprintf(stderr, "Suspicious header byte 0: 0x%02x\n",
148 msg->data[0]);
149
150 sh->rxmsg_bytes_missing = msg->data[0] << 8;
151 sh->rxmsg_bytes_missing += msg->data[1];
152
153 if (sh->rxmsg_bytes_missing < LAPD_HDR_LEN -2)
154 fprintf(stderr, "Invalid length in hdr: %u\n",
155 sh->rxmsg_bytes_missing);
156 }
157 } else {
158 /* try to read as many of the missing bytes as are available */
159 rc = read(sh->fd.fd, msg->tail, sh->rxmsg_bytes_missing);
160 if (rc < 0) {
161 perror("ERROR reading from serial port");
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) {
169 /* we have one complete message now */
170 sh->rx_msg = NULL;
171
172 if (msg->len > LAPD_HDR_LEN)
173 msg->l2h = msg->data + LAPD_HDR_LEN;
174
Harald Welte3cc4bf52009-02-28 13:08:01 +0000175 DEBUGP(DMI, "RS232 RX: %s\n", hexdump(msg->data, msg->len));
Harald Weltec12d52b2009-02-01 21:39:06 +0000176 rc = handle_serial_msg(msg);
177 }
178 }
179
180 return rc;
181}
182
183/* select.c callback */
184static int serial_fd_cb(struct bsc_fd *bfd, unsigned int what)
185{
186 int rc = 0;
187
188 if (what & BSC_FD_READ)
189 rc = handle_ser_read(bfd);
190
191 if (rc < 0)
192 return rc;
193
194 if (what & BSC_FD_WRITE)
195 rc = handle_ser_write(bfd);
196
197 return rc;
198}
199
200int rs232_setup(const char *serial_port, unsigned int delay_ms)
201{
202 int rc, serial_fd;
203 struct termios tio;
204
205 serial_fd = open(serial_port, O_RDWR);
206 if (serial_fd < 0) {
207 perror("cannot open serial port:");
208 return serial_fd;
209 }
210
211 /* set baudrate */
212 rc = tcgetattr(serial_fd, &tio);
213 if (rc < 0) {
214 perror("tcgetattr()");
215 return rc;
216 }
217 cfsetispeed(&tio, B19200);
218 cfsetospeed(&tio, B19200);
219 tio.c_cflag |= (CREAD | CLOCAL | CS8);
220 tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
221 tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
222 tio.c_iflag |= (INPCK | ISTRIP);
223 tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR);
224 tio.c_oflag &= ~(OPOST);
225 rc = tcsetattr(serial_fd, TCSADRAIN, &tio);
226 if (rc < 0) {
227 perror("tcsetattr()");
228 return rc;
229 }
230
231 INIT_LLIST_HEAD(&ser_handle->tx_queue);
232 ser_handle->fd.fd = serial_fd;
233 ser_handle->fd.when = BSC_FD_READ;
234 ser_handle->fd.cb = serial_fd_cb;
235 ser_handle->fd.data = ser_handle;
236 ser_handle->delay_ms = delay_ms;
237 rc = bsc_register_fd(&ser_handle->fd);
238 if (rc < 0) {
239 fprintf(stderr, "could not register FD: %s\n",
240 strerror(rc));
241 return rc;
242 }
243
244 return 0;
245}