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