Harald Welte | c12d52b | 2009-02-01 21:39:06 +0000 | [diff] [blame] | 1 | /* 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 Welte | 099d610 | 2009-02-21 12:59:58 +0000 | [diff] [blame] | 35 | #include <openbsc/rs232.h> |
Harald Welte | c12d52b | 2009-02-01 21:39:06 +0000 | [diff] [blame] | 36 | |
| 37 | /* adaption layer from GSM 08.59 + 12.21 to RS232 */ |
| 38 | |
| 39 | struct 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 */ |
| 50 | static struct serial_handle _ser_handle, *ser_handle = &_ser_handle; |
| 51 | |
| 52 | #define LAPD_HDR_LEN 10 |
| 53 | |
Harald Welte | f80b7d3 | 2009-02-19 03:11:13 +0000 | [diff] [blame] | 54 | static int handle_ser_write(struct bsc_fd *bfd); |
| 55 | |
Harald Welte | c12d52b | 2009-02-01 21:39:06 +0000 | [diff] [blame] | 56 | /* callback from abis_nm */ |
| 57 | int _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 Welte | f80b7d3 | 2009-02-19 03:11:13 +0000 | [diff] [blame] | 84 | /* we try to immediately send */ |
| 85 | handle_ser_write(&sh->fd); |
| 86 | |
Harald Welte | c12d52b | 2009-02-01 21:39:06 +0000 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /* select.c callback in case we can write to the RS232 */ |
| 91 | static 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 | |
| 103 | if (debug_mask & DMI) { |
| 104 | fprintf(stdout, "RS232 TX: "); |
| 105 | hexdump(msg->data, msg->len); |
| 106 | } |
| 107 | |
| 108 | /* send over serial line */ |
| 109 | written = write(bfd->fd, msg->data, msg->len); |
| 110 | if (written < msg->len) { |
| 111 | perror("short write:"); |
| 112 | msgb_free(msg); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | msgb_free(msg); |
| 117 | usleep(sh->delay_ms*1000); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | #define SERIAL_ALLOC_SIZE 300 |
| 123 | |
| 124 | /* select.c callback in case we can read from the RS232 */ |
| 125 | static int handle_ser_read(struct bsc_fd *bfd) |
| 126 | { |
| 127 | struct serial_handle *sh = bfd->data; |
| 128 | struct msgb *msg; |
| 129 | int rc = 0; |
| 130 | |
| 131 | if (!sh->rx_msg) { |
| 132 | sh->rx_msg = msgb_alloc(SERIAL_ALLOC_SIZE); |
| 133 | sh->rx_msg->l2h = NULL; |
| 134 | } |
| 135 | msg = sh->rx_msg; |
| 136 | |
| 137 | /* first read two byes to obtain length */ |
| 138 | if (msg->len < 2) { |
| 139 | rc = read(sh->fd.fd, msg->tail, 2 - msg->len); |
| 140 | if (rc < 0) { |
| 141 | perror("ERROR reading from serial port"); |
| 142 | msgb_free(msg); |
| 143 | return rc; |
| 144 | } |
| 145 | msgb_put(msg, rc); |
| 146 | |
| 147 | if (msg->len >= 2) { |
| 148 | /* parse LAPD payload length */ |
| 149 | if (msg->data[0] != 0) |
| 150 | fprintf(stderr, "Suspicious header byte 0: 0x%02x\n", |
| 151 | msg->data[0]); |
| 152 | |
| 153 | sh->rxmsg_bytes_missing = msg->data[0] << 8; |
| 154 | sh->rxmsg_bytes_missing += msg->data[1]; |
| 155 | |
| 156 | if (sh->rxmsg_bytes_missing < LAPD_HDR_LEN -2) |
| 157 | fprintf(stderr, "Invalid length in hdr: %u\n", |
| 158 | sh->rxmsg_bytes_missing); |
| 159 | } |
| 160 | } else { |
| 161 | /* try to read as many of the missing bytes as are available */ |
| 162 | rc = read(sh->fd.fd, msg->tail, sh->rxmsg_bytes_missing); |
| 163 | if (rc < 0) { |
| 164 | perror("ERROR reading from serial port"); |
| 165 | msgb_free(msg); |
| 166 | return rc; |
| 167 | } |
| 168 | msgb_put(msg, rc); |
| 169 | sh->rxmsg_bytes_missing -= rc; |
| 170 | |
| 171 | if (sh->rxmsg_bytes_missing == 0) { |
| 172 | /* we have one complete message now */ |
| 173 | sh->rx_msg = NULL; |
| 174 | |
| 175 | if (msg->len > LAPD_HDR_LEN) |
| 176 | msg->l2h = msg->data + LAPD_HDR_LEN; |
| 177 | |
| 178 | if (debug_mask & DMI) { |
| 179 | fprintf(stdout, "RS232 RX: "); |
| 180 | hexdump(msg->data, msg->len); |
| 181 | } |
| 182 | rc = handle_serial_msg(msg); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return rc; |
| 187 | } |
| 188 | |
| 189 | /* select.c callback */ |
| 190 | static int serial_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 191 | { |
| 192 | int rc = 0; |
| 193 | |
| 194 | if (what & BSC_FD_READ) |
| 195 | rc = handle_ser_read(bfd); |
| 196 | |
| 197 | if (rc < 0) |
| 198 | return rc; |
| 199 | |
| 200 | if (what & BSC_FD_WRITE) |
| 201 | rc = handle_ser_write(bfd); |
| 202 | |
| 203 | return rc; |
| 204 | } |
| 205 | |
| 206 | int rs232_setup(const char *serial_port, unsigned int delay_ms) |
| 207 | { |
| 208 | int rc, serial_fd; |
| 209 | struct termios tio; |
| 210 | |
| 211 | serial_fd = open(serial_port, O_RDWR); |
| 212 | if (serial_fd < 0) { |
| 213 | perror("cannot open serial port:"); |
| 214 | return serial_fd; |
| 215 | } |
| 216 | |
| 217 | /* set baudrate */ |
| 218 | rc = tcgetattr(serial_fd, &tio); |
| 219 | if (rc < 0) { |
| 220 | perror("tcgetattr()"); |
| 221 | return rc; |
| 222 | } |
| 223 | cfsetispeed(&tio, B19200); |
| 224 | cfsetospeed(&tio, B19200); |
| 225 | tio.c_cflag |= (CREAD | CLOCAL | CS8); |
| 226 | tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS); |
| 227 | tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 228 | tio.c_iflag |= (INPCK | ISTRIP); |
| 229 | tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR); |
| 230 | tio.c_oflag &= ~(OPOST); |
| 231 | rc = tcsetattr(serial_fd, TCSADRAIN, &tio); |
| 232 | if (rc < 0) { |
| 233 | perror("tcsetattr()"); |
| 234 | return rc; |
| 235 | } |
| 236 | |
| 237 | INIT_LLIST_HEAD(&ser_handle->tx_queue); |
| 238 | ser_handle->fd.fd = serial_fd; |
| 239 | ser_handle->fd.when = BSC_FD_READ; |
| 240 | ser_handle->fd.cb = serial_fd_cb; |
| 241 | ser_handle->fd.data = ser_handle; |
| 242 | ser_handle->delay_ms = delay_ms; |
| 243 | rc = bsc_register_fd(&ser_handle->fd); |
| 244 | if (rc < 0) { |
| 245 | fprintf(stderr, "could not register FD: %s\n", |
| 246 | strerror(rc)); |
| 247 | return rc; |
| 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |