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