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