Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 1 | /* Serial communications layer, based on HDLC */ |
| 2 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 3 | /* (C) 2010,2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 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 <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | #include <osmocom/core/msgb.h> |
Harald Welte | 1a97e2c | 2017-05-01 00:19:38 +0200 | [diff] [blame^] | 28 | #include <osmocom/core/sercomm.h> |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 29 | |
| 30 | #ifdef HOST_BUILD |
| 31 | |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 32 | # define DEFAULT_RX_MSG_SIZE 2048 |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 33 | # ifndef ARRAY_SIZE |
| 34 | # define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
| 35 | # endif |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 36 | |
| 37 | static inline void sercomm_lock(unsigned long __attribute__((unused)) *flags) {} |
| 38 | static inline void sercomm_unlock(unsigned long __attribute__((unused)) *flags) {} |
| 39 | |
| 40 | #else |
| 41 | |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 42 | # define DEFAULT_RX_MSG_SIZE 256 |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 43 | # include <debug.h> |
| 44 | # include <osmocom/core/linuxlist.h> |
| 45 | # include <asm/system.h> |
| 46 | |
| 47 | static inline void sercomm_lock(unsigned long *flags) |
| 48 | { |
| 49 | local_firq_save(*flags); |
| 50 | } |
| 51 | |
| 52 | static inline void sercomm_unlock(unsigned long *flags) |
| 53 | { |
| 54 | local_irq_restore(*flags); |
| 55 | } |
| 56 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 57 | # include <uart.h> |
| 58 | |
| 59 | #endif |
| 60 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 61 | enum rx_state { |
| 62 | RX_ST_WAIT_START, |
| 63 | RX_ST_ADDR, |
| 64 | RX_ST_CTRL, |
| 65 | RX_ST_DATA, |
| 66 | RX_ST_ESCAPE, |
| 67 | }; |
| 68 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 69 | |
| 70 | #ifndef HOST_BUILD |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 71 | void osmo_sercomm_bind_uart(struct osmo_sercomm_inst *sercomm, int uart) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 72 | { |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 73 | sercomm->uart_id = uart; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 76 | int osmo_sercomm_get_uart(struct osmo_sercomm_inst *sercomm) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 77 | { |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 78 | return sercomm->uart_id; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 79 | } |
| 80 | #endif |
| 81 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 82 | void osmo_sercomm_init(struct osmo_sercomm_inst *sercomm) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 83 | { |
| 84 | unsigned int i; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 85 | for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) |
| 86 | INIT_LLIST_HEAD(&sercomm->tx.dlci_queues[i]); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 87 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 88 | sercomm->rx.msg = NULL; |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 89 | if (!sercomm->rx.msg_size) |
| 90 | sercomm->rx.msg_size = DEFAULT_RX_MSG_SIZE; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 91 | sercomm->initialized = 1; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 92 | |
| 93 | /* set up the echo dlci */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 94 | osmo_sercomm_register_rx_cb(sercomm, SC_DLCI_ECHO, &osmo_sercomm_sendmsg); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 97 | int osmo_sercomm_initialized(struct osmo_sercomm_inst *sercomm) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 98 | { |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 99 | return sercomm->initialized; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* user interface for transmitting messages for a given DLCI */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 103 | void osmo_sercomm_sendmsg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 104 | { |
| 105 | unsigned long flags; |
| 106 | uint8_t *hdr; |
| 107 | |
| 108 | /* prepend address + control octet */ |
| 109 | hdr = msgb_push(msg, 2); |
| 110 | hdr[0] = dlci; |
| 111 | hdr[1] = HDLC_C_UI; |
| 112 | |
| 113 | /* This functiion can be called from any context: FIQ, IRQ |
| 114 | * and supervisor context. Proper locking is important! */ |
| 115 | sercomm_lock(&flags); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 116 | msgb_enqueue(&sercomm->tx.dlci_queues[dlci], msg); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 117 | sercomm_unlock(&flags); |
| 118 | |
| 119 | #ifndef HOST_BUILD |
| 120 | /* tell UART that we have something to send */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 121 | uart_irq_enable(sercomm->uart_id, UART_IRQ_TX_EMPTY, 1); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 122 | #endif |
| 123 | } |
| 124 | |
| 125 | /* how deep is the Tx queue for a given DLCI */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 126 | unsigned int osmo_sercomm_tx_queue_depth(struct osmo_sercomm_inst *sercomm, uint8_t dlci) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 127 | { |
| 128 | struct llist_head *le; |
| 129 | unsigned int num = 0; |
| 130 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 131 | llist_for_each(le, &sercomm->tx.dlci_queues[dlci]) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 132 | num++; |
| 133 | } |
| 134 | |
| 135 | return num; |
| 136 | } |
| 137 | |
| 138 | #ifndef HOST_BUILD |
| 139 | /* wait until everything has been transmitted, then grab the lock and |
| 140 | * change the baud rate as requested */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 141 | void osmo_sercomm_change_speed(struct osmo_sercomm_inst *sercomm, enum uart_baudrate bdrt) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 142 | { |
| 143 | unsigned int i, count; |
| 144 | unsigned long flags; |
| 145 | |
| 146 | while (1) { |
| 147 | /* count the number of pending messages */ |
| 148 | count = 0; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 149 | for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 150 | count += sercomm_tx_queue_depth(i); |
| 151 | /* if we still have any in the queue, restart */ |
| 152 | if (count == 0) |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | while (1) { |
| 157 | /* no messages in the queue, grab the lock to ensure it |
| 158 | * stays that way */ |
| 159 | sercomm_lock(&flags); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 160 | if (!sercomm->tx.msg && !sercomm->tx.next_char) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 161 | /* change speed */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 162 | uart_baudrate(sercomm->uart_id, bdrt); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 163 | sercomm_unlock(&flags); |
| 164 | break; |
| 165 | } |
| 166 | sercomm_unlock(&flags); |
| 167 | } |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | /* fetch one octet of to-be-transmitted serial data */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 172 | int osmo_sercomm_drv_pull(struct osmo_sercomm_inst *sercomm, uint8_t *ch) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 173 | { |
| 174 | unsigned long flags; |
| 175 | |
| 176 | /* we may be called from interrupt context, but we stiff need to lock |
| 177 | * because sercomm could be accessed from a FIQ context ... */ |
| 178 | |
| 179 | sercomm_lock(&flags); |
| 180 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 181 | if (!sercomm->tx.msg) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 182 | unsigned int i; |
| 183 | /* dequeue a new message from the queues */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 184 | for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) { |
| 185 | sercomm->tx.msg = msgb_dequeue(&sercomm->tx.dlci_queues[i]); |
| 186 | if (sercomm->tx.msg) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 187 | break; |
| 188 | } |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 189 | if (sercomm->tx.msg) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 190 | /* start of a new message, send start flag octet */ |
| 191 | *ch = HDLC_FLAG; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 192 | sercomm->tx.next_char = sercomm->tx.msg->data; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 193 | sercomm_unlock(&flags); |
| 194 | return 1; |
| 195 | } else { |
| 196 | /* no more data avilable */ |
| 197 | sercomm_unlock(&flags); |
| 198 | return 0; |
| 199 | } |
| 200 | } |
| 201 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 202 | if (sercomm->tx.state == RX_ST_ESCAPE) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 203 | /* we've already transmitted the ESCAPE octet, |
| 204 | * we now need to transmit the escaped data */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 205 | *ch = *sercomm->tx.next_char++; |
| 206 | sercomm->tx.state = RX_ST_DATA; |
| 207 | } else if (sercomm->tx.next_char >= sercomm->tx.msg->tail) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 208 | /* last character has already been transmitted, |
| 209 | * send end-of-message octet */ |
| 210 | *ch = HDLC_FLAG; |
| 211 | /* we've reached the end of the message buffer */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 212 | msgb_free(sercomm->tx.msg); |
| 213 | sercomm->tx.msg = NULL; |
| 214 | sercomm->tx.next_char = NULL; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 215 | /* escaping for the two control octets */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 216 | } else if (*sercomm->tx.next_char == HDLC_FLAG || |
| 217 | *sercomm->tx.next_char == HDLC_ESCAPE || |
| 218 | *sercomm->tx.next_char == 0x00) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 219 | /* send an escape octet */ |
| 220 | *ch = HDLC_ESCAPE; |
| 221 | /* invert bit 5 of the next octet to be sent */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 222 | *sercomm->tx.next_char ^= (1 << 5); |
| 223 | sercomm->tx.state = RX_ST_ESCAPE; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 224 | } else { |
| 225 | /* standard case, simply send next octet */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 226 | *ch = *sercomm->tx.next_char++; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | sercomm_unlock(&flags); |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | /* register a handler for a given DLCI */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 234 | int osmo_sercomm_register_rx_cb(struct osmo_sercomm_inst *sercomm, uint8_t dlci, dlci_cb_t cb) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 235 | { |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 236 | if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler)) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 237 | return -EINVAL; |
| 238 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 239 | if (sercomm->rx.dlci_handler[dlci]) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 240 | return -EBUSY; |
| 241 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 242 | sercomm->rx.dlci_handler[dlci] = cb; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /* dispatch an incoming message once it is completely received */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 247 | static void dispatch_rx_msg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 248 | { |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 249 | if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler) || |
| 250 | !sercomm->rx.dlci_handler[dlci]) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 251 | msgb_free(msg); |
| 252 | return; |
| 253 | } |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 254 | sercomm->rx.dlci_handler[dlci](sercomm, dlci, msg); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* the driver has received one byte, pass it into sercomm layer */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 258 | int osmo_sercomm_drv_rx_char(struct osmo_sercomm_inst *sercomm, uint8_t ch) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 259 | { |
| 260 | uint8_t *ptr; |
| 261 | |
| 262 | /* we are always called from interrupt context in this function, |
| 263 | * which means that any data structures we use need to be for |
| 264 | * our exclusive access */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 265 | if (!sercomm->rx.msg) |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 266 | sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 267 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 268 | if (msgb_tailroom(sercomm->rx.msg) == 0) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 269 | //cons_puts("sercomm_drv_rx_char() overflow!\n"); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 270 | msgb_free(sercomm->rx.msg); |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 271 | sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 272 | sercomm->rx.state = RX_ST_WAIT_START; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 276 | switch (sercomm->rx.state) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 277 | case RX_ST_WAIT_START: |
| 278 | if (ch != HDLC_FLAG) |
| 279 | break; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 280 | sercomm->rx.state = RX_ST_ADDR; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 281 | break; |
| 282 | case RX_ST_ADDR: |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 283 | sercomm->rx.dlci = ch; |
| 284 | sercomm->rx.state = RX_ST_CTRL; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 285 | break; |
| 286 | case RX_ST_CTRL: |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 287 | sercomm->rx.ctrl = ch; |
| 288 | sercomm->rx.state = RX_ST_DATA; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 289 | break; |
| 290 | case RX_ST_DATA: |
| 291 | if (ch == HDLC_ESCAPE) { |
| 292 | /* drop the escape octet, but change state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 293 | sercomm->rx.state = RX_ST_ESCAPE; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 294 | break; |
| 295 | } else if (ch == HDLC_FLAG) { |
| 296 | /* message is finished */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 297 | dispatch_rx_msg(sercomm, sercomm->rx.dlci, sercomm->rx.msg); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 298 | /* allocate new buffer */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 299 | sercomm->rx.msg = NULL; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 300 | /* start all over again */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 301 | sercomm->rx.state = RX_ST_WAIT_START; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 302 | |
| 303 | /* do not add the control char */ |
| 304 | break; |
| 305 | } |
| 306 | /* default case: store the octet */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 307 | ptr = msgb_put(sercomm->rx.msg, 1); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 308 | *ptr = ch; |
| 309 | break; |
| 310 | case RX_ST_ESCAPE: |
| 311 | /* store bif-5-inverted octet in buffer */ |
| 312 | ch ^= (1 << 5); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 313 | ptr = msgb_put(sercomm->rx.msg, 1); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 314 | *ptr = ch; |
| 315 | /* transition back to normal DATA state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 316 | sercomm->rx.state = RX_ST_DATA; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | |
| 320 | return 1; |
| 321 | } |