blob: 92e2e9b23fbabd314a4cacaf185965c45cdac1d2 [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>
35
36/* adaption layer from GSM 08.59 + 12.21 to RS232 */
37
38struct serial_handle {
39 struct bsc_fd fd;
40 struct llist_head tx_queue;
41
42 struct msgb *rx_msg;
43 unsigned int rxmsg_bytes_missing;
44
45 unsigned int delay_ms;
46};
47
48/* FIXME: this needs to go */
49static struct serial_handle _ser_handle, *ser_handle = &_ser_handle;
50
51#define LAPD_HDR_LEN 10
52
Harald Weltef80b7d32009-02-19 03:11:13 +000053static int handle_ser_write(struct bsc_fd *bfd);
54
Harald Weltec12d52b2009-02-01 21:39:06 +000055/* callback from abis_nm */
56int _abis_nm_sendmsg(struct msgb *msg)
57{
58 struct serial_handle *sh = ser_handle;
59 u_int8_t *lapd;
60 unsigned int len;
61
62 msg->l2h = msg->data;
63
64 /* prepend LAPD header */
65 lapd = msgb_push(msg, LAPD_HDR_LEN);
66
67 len = msg->len - 2;
68
69 lapd[0] = (len >> 8) & 0xff;
70 lapd[1] = len & 0xff; /* length of bytes startign at lapd[2] */
71 lapd[2] = 0x00;
72 lapd[3] = 0x07;
73 lapd[4] = 0x01;
74 lapd[5] = 0x3e;
75 lapd[6] = 0x00;
76 lapd[7] = 0x00;
77 lapd[8] = msg->len - 10; /* length of bytes starting at lapd[10] */
78 lapd[9] = lapd[8] ^ 0x38;
79
80 msgb_enqueue(&sh->tx_queue, msg);
81 sh->fd.when |= BSC_FD_WRITE;
82
Harald Weltef80b7d32009-02-19 03:11:13 +000083 /* we try to immediately send */
84 handle_ser_write(&sh->fd);
85
Harald Weltec12d52b2009-02-01 21:39:06 +000086 return 0;
87}
88
89/* select.c callback in case we can write to the RS232 */
90static int handle_ser_write(struct bsc_fd *bfd)
91{
92 struct serial_handle *sh = bfd->data;
93 struct msgb *msg;
94 int written;
95
96 msg = msgb_dequeue(&sh->tx_queue);
97 if (!msg) {
98 bfd->when &= ~BSC_FD_WRITE;
99 return 0;
100 }
101
102 if (debug_mask & DMI) {
103 fprintf(stdout, "RS232 TX: ");
104 hexdump(msg->data, msg->len);
105 }
106
107 /* send over serial line */
108 written = write(bfd->fd, msg->data, msg->len);
109 if (written < msg->len) {
110 perror("short write:");
111 msgb_free(msg);
112 return -1;
113 }
114
115 msgb_free(msg);
116 usleep(sh->delay_ms*1000);
117
118 return 0;
119}
120
121#define SERIAL_ALLOC_SIZE 300
122
123/* select.c callback in case we can read from the RS232 */
124static int handle_ser_read(struct bsc_fd *bfd)
125{
126 struct serial_handle *sh = bfd->data;
127 struct msgb *msg;
128 int rc = 0;
129
130 if (!sh->rx_msg) {
131 sh->rx_msg = msgb_alloc(SERIAL_ALLOC_SIZE);
132 sh->rx_msg->l2h = NULL;
133 }
134 msg = sh->rx_msg;
135
136 /* first read two byes to obtain length */
137 if (msg->len < 2) {
138 rc = read(sh->fd.fd, msg->tail, 2 - msg->len);
139 if (rc < 0) {
140 perror("ERROR reading from serial port");
141 msgb_free(msg);
142 return rc;
143 }
144 msgb_put(msg, rc);
145
146 if (msg->len >= 2) {
147 /* parse LAPD payload length */
148 if (msg->data[0] != 0)
149 fprintf(stderr, "Suspicious header byte 0: 0x%02x\n",
150 msg->data[0]);
151
152 sh->rxmsg_bytes_missing = msg->data[0] << 8;
153 sh->rxmsg_bytes_missing += msg->data[1];
154
155 if (sh->rxmsg_bytes_missing < LAPD_HDR_LEN -2)
156 fprintf(stderr, "Invalid length in hdr: %u\n",
157 sh->rxmsg_bytes_missing);
158 }
159 } else {
160 /* try to read as many of the missing bytes as are available */
161 rc = read(sh->fd.fd, msg->tail, sh->rxmsg_bytes_missing);
162 if (rc < 0) {
163 perror("ERROR reading from serial port");
164 msgb_free(msg);
165 return rc;
166 }
167 msgb_put(msg, rc);
168 sh->rxmsg_bytes_missing -= rc;
169
170 if (sh->rxmsg_bytes_missing == 0) {
171 /* we have one complete message now */
172 sh->rx_msg = NULL;
173
174 if (msg->len > LAPD_HDR_LEN)
175 msg->l2h = msg->data + LAPD_HDR_LEN;
176
177 if (debug_mask & DMI) {
178 fprintf(stdout, "RS232 RX: ");
179 hexdump(msg->data, msg->len);
180 }
181 rc = handle_serial_msg(msg);
182 }
183 }
184
185 return rc;
186}
187
188/* select.c callback */
189static int serial_fd_cb(struct bsc_fd *bfd, unsigned int what)
190{
191 int rc = 0;
192
193 if (what & BSC_FD_READ)
194 rc = handle_ser_read(bfd);
195
196 if (rc < 0)
197 return rc;
198
199 if (what & BSC_FD_WRITE)
200 rc = handle_ser_write(bfd);
201
202 return rc;
203}
204
205int rs232_setup(const char *serial_port, unsigned int delay_ms)
206{
207 int rc, serial_fd;
208 struct termios tio;
209
210 serial_fd = open(serial_port, O_RDWR);
211 if (serial_fd < 0) {
212 perror("cannot open serial port:");
213 return serial_fd;
214 }
215
216 /* set baudrate */
217 rc = tcgetattr(serial_fd, &tio);
218 if (rc < 0) {
219 perror("tcgetattr()");
220 return rc;
221 }
222 cfsetispeed(&tio, B19200);
223 cfsetospeed(&tio, B19200);
224 tio.c_cflag |= (CREAD | CLOCAL | CS8);
225 tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
226 tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
227 tio.c_iflag |= (INPCK | ISTRIP);
228 tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR);
229 tio.c_oflag &= ~(OPOST);
230 rc = tcsetattr(serial_fd, TCSADRAIN, &tio);
231 if (rc < 0) {
232 perror("tcsetattr()");
233 return rc;
234 }
235
236 INIT_LLIST_HEAD(&ser_handle->tx_queue);
237 ser_handle->fd.fd = serial_fd;
238 ser_handle->fd.when = BSC_FD_READ;
239 ser_handle->fd.cb = serial_fd_cb;
240 ser_handle->fd.data = ser_handle;
241 ser_handle->delay_ms = delay_ms;
242 rc = bsc_register_fd(&ser_handle->fd);
243 if (rc < 0) {
244 fprintf(stderr, "could not register FD: %s\n",
245 strerror(rc));
246 return rc;
247 }
248
249 return 0;
250}