Lines Matching +full:2 +full:- +full:byte
4 * SPDX-License-Identifier: Apache-2.0
16 #define MODEM_PPP_FRAME_TAIL_SIZE (2)
22 static uint16_t modem_ppp_fcs_init(uint8_t byte) in modem_ppp_fcs_init() argument
24 return crc16_ccitt(0xFFFF, &byte, 1); in modem_ppp_fcs_init()
27 static uint16_t modem_ppp_fcs_update(uint16_t fcs, uint8_t byte) in modem_ppp_fcs_update() argument
29 return crc16_ccitt(fcs, &byte, 1); in modem_ppp_fcs_update()
53 uint8_t byte; in modem_ppp_wrap_net_pkt_byte() local
55 switch (ppp->transmit_state) { in modem_ppp_wrap_net_pkt_byte()
62 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_HDR_FF; in modem_ppp_wrap_net_pkt_byte()
66 net_pkt_cursor_init(ppp->tx_pkt); in modem_ppp_wrap_net_pkt_byte()
67 ppp->tx_pkt_fcs = modem_ppp_fcs_init(0xFF); in modem_ppp_wrap_net_pkt_byte()
68 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_HDR_7D; in modem_ppp_wrap_net_pkt_byte()
72 ppp->tx_pkt_fcs = modem_ppp_fcs_update(ppp->tx_pkt_fcs, 0x03); in modem_ppp_wrap_net_pkt_byte()
73 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_HDR_23; in modem_ppp_wrap_net_pkt_byte()
77 if (net_pkt_is_ppp(ppp->tx_pkt) == true) { in modem_ppp_wrap_net_pkt_byte()
78 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_DATA; in modem_ppp_wrap_net_pkt_byte()
80 ppp->tx_pkt_protocol = modem_ppp_ppp_protocol(ppp->tx_pkt); in modem_ppp_wrap_net_pkt_byte()
81 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_PROTOCOL_HIGH; in modem_ppp_wrap_net_pkt_byte()
88 byte = (ppp->tx_pkt_protocol >> 8) & 0xFF; in modem_ppp_wrap_net_pkt_byte()
89 ppp->tx_pkt_fcs = modem_ppp_fcs_update(ppp->tx_pkt_fcs, byte); in modem_ppp_wrap_net_pkt_byte()
91 if ((byte == MODEM_PPP_CODE_DELIMITER) || (byte == MODEM_PPP_CODE_ESCAPE) || in modem_ppp_wrap_net_pkt_byte()
92 (byte < MODEM_PPP_VALUE_ESCAPE)) { in modem_ppp_wrap_net_pkt_byte()
93 ppp->tx_pkt_escaped = byte ^ MODEM_PPP_VALUE_ESCAPE; in modem_ppp_wrap_net_pkt_byte()
94 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_ESCAPING_PROTOCOL_HIGH; in modem_ppp_wrap_net_pkt_byte()
98 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_PROTOCOL_LOW; in modem_ppp_wrap_net_pkt_byte()
99 return byte; in modem_ppp_wrap_net_pkt_byte()
102 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_PROTOCOL_LOW; in modem_ppp_wrap_net_pkt_byte()
103 return ppp->tx_pkt_escaped; in modem_ppp_wrap_net_pkt_byte()
106 byte = ppp->tx_pkt_protocol & 0xFF; in modem_ppp_wrap_net_pkt_byte()
107 ppp->tx_pkt_fcs = modem_ppp_fcs_update(ppp->tx_pkt_fcs, byte); in modem_ppp_wrap_net_pkt_byte()
109 if ((byte == MODEM_PPP_CODE_DELIMITER) || (byte == MODEM_PPP_CODE_ESCAPE) || in modem_ppp_wrap_net_pkt_byte()
110 (byte < MODEM_PPP_VALUE_ESCAPE)) { in modem_ppp_wrap_net_pkt_byte()
111 ppp->tx_pkt_escaped = byte ^ MODEM_PPP_VALUE_ESCAPE; in modem_ppp_wrap_net_pkt_byte()
112 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_ESCAPING_PROTOCOL_LOW; in modem_ppp_wrap_net_pkt_byte()
116 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_DATA; in modem_ppp_wrap_net_pkt_byte()
117 return byte; in modem_ppp_wrap_net_pkt_byte()
120 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_DATA; in modem_ppp_wrap_net_pkt_byte()
121 return ppp->tx_pkt_escaped; in modem_ppp_wrap_net_pkt_byte()
125 net_pkt_read_u8(ppp->tx_pkt, &byte); in modem_ppp_wrap_net_pkt_byte()
126 ppp->tx_pkt_fcs = modem_ppp_fcs_update(ppp->tx_pkt_fcs, byte); in modem_ppp_wrap_net_pkt_byte()
128 if ((byte == MODEM_PPP_CODE_DELIMITER) || (byte == MODEM_PPP_CODE_ESCAPE) || in modem_ppp_wrap_net_pkt_byte()
129 (byte < MODEM_PPP_VALUE_ESCAPE)) { in modem_ppp_wrap_net_pkt_byte()
130 ppp->tx_pkt_escaped = byte ^ MODEM_PPP_VALUE_ESCAPE; in modem_ppp_wrap_net_pkt_byte()
131 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_ESCAPING_DATA; in modem_ppp_wrap_net_pkt_byte()
135 if (net_pkt_remaining_data(ppp->tx_pkt) == 0) { in modem_ppp_wrap_net_pkt_byte()
136 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_FCS_LOW; in modem_ppp_wrap_net_pkt_byte()
139 return byte; in modem_ppp_wrap_net_pkt_byte()
142 if (net_pkt_remaining_data(ppp->tx_pkt) == 0) { in modem_ppp_wrap_net_pkt_byte()
143 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_FCS_LOW; in modem_ppp_wrap_net_pkt_byte()
145 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_DATA; in modem_ppp_wrap_net_pkt_byte()
148 return ppp->tx_pkt_escaped; in modem_ppp_wrap_net_pkt_byte()
152 ppp->tx_pkt_fcs = modem_ppp_fcs_final(ppp->tx_pkt_fcs); in modem_ppp_wrap_net_pkt_byte()
153 byte = ppp->tx_pkt_fcs & 0xFF; in modem_ppp_wrap_net_pkt_byte()
155 if ((byte == MODEM_PPP_CODE_DELIMITER) || (byte == MODEM_PPP_CODE_ESCAPE) || in modem_ppp_wrap_net_pkt_byte()
156 (byte < MODEM_PPP_VALUE_ESCAPE)) { in modem_ppp_wrap_net_pkt_byte()
157 ppp->tx_pkt_escaped = byte ^ MODEM_PPP_VALUE_ESCAPE; in modem_ppp_wrap_net_pkt_byte()
158 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_ESCAPING_FCS_LOW; in modem_ppp_wrap_net_pkt_byte()
162 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_FCS_HIGH; in modem_ppp_wrap_net_pkt_byte()
163 return byte; in modem_ppp_wrap_net_pkt_byte()
166 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_FCS_HIGH; in modem_ppp_wrap_net_pkt_byte()
167 return ppp->tx_pkt_escaped; in modem_ppp_wrap_net_pkt_byte()
170 byte = (ppp->tx_pkt_fcs >> 8) & 0xFF; in modem_ppp_wrap_net_pkt_byte()
172 if ((byte == MODEM_PPP_CODE_DELIMITER) || (byte == MODEM_PPP_CODE_ESCAPE) || in modem_ppp_wrap_net_pkt_byte()
173 (byte < MODEM_PPP_VALUE_ESCAPE)) { in modem_ppp_wrap_net_pkt_byte()
174 ppp->tx_pkt_escaped = byte ^ MODEM_PPP_VALUE_ESCAPE; in modem_ppp_wrap_net_pkt_byte()
175 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_ESCAPING_FCS_HIGH; in modem_ppp_wrap_net_pkt_byte()
179 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_EOF; in modem_ppp_wrap_net_pkt_byte()
180 return byte; in modem_ppp_wrap_net_pkt_byte()
183 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_EOF; in modem_ppp_wrap_net_pkt_byte()
184 return ppp->tx_pkt_escaped; in modem_ppp_wrap_net_pkt_byte()
188 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_IDLE; in modem_ppp_wrap_net_pkt_byte()
195 static void modem_ppp_process_received_byte(struct modem_ppp *ppp, uint8_t byte) in modem_ppp_process_received_byte() argument
197 switch (ppp->receive_state) { in modem_ppp_process_received_byte()
199 if (byte == MODEM_PPP_CODE_DELIMITER) { in modem_ppp_process_received_byte()
200 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_FF; in modem_ppp_process_received_byte()
206 if (byte == MODEM_PPP_CODE_DELIMITER) { in modem_ppp_process_received_byte()
210 if (byte == 0xFF) { in modem_ppp_process_received_byte()
211 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_7D; in modem_ppp_process_received_byte()
213 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; in modem_ppp_process_received_byte()
219 if (byte == MODEM_PPP_CODE_ESCAPE) { in modem_ppp_process_received_byte()
220 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_23; in modem_ppp_process_received_byte()
222 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; in modem_ppp_process_received_byte()
228 if (byte == 0x23) { in modem_ppp_process_received_byte()
229 ppp->rx_pkt = net_pkt_rx_alloc_with_buffer(ppp->iface, in modem_ppp_process_received_byte()
232 if (ppp->rx_pkt == NULL) { in modem_ppp_process_received_byte()
234 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; in modem_ppp_process_received_byte()
239 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_WRITING; in modem_ppp_process_received_byte()
240 net_pkt_cursor_init(ppp->rx_pkt); in modem_ppp_process_received_byte()
243 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; in modem_ppp_process_received_byte()
249 if (byte == MODEM_PPP_CODE_DELIMITER) { in modem_ppp_process_received_byte()
253 net_pkt_remove_tail(ppp->rx_pkt, MODEM_PPP_FRAME_TAIL_SIZE); in modem_ppp_process_received_byte()
254 net_pkt_cursor_init(ppp->rx_pkt); in modem_ppp_process_received_byte()
255 net_pkt_set_ppp(ppp->rx_pkt, true); in modem_ppp_process_received_byte()
257 if (net_recv_data(ppp->iface, ppp->rx_pkt) < 0) { in modem_ppp_process_received_byte()
259 net_pkt_unref(ppp->rx_pkt); in modem_ppp_process_received_byte()
262 ppp->rx_pkt = NULL; in modem_ppp_process_received_byte()
263 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; in modem_ppp_process_received_byte()
267 if (net_pkt_available_buffer(ppp->rx_pkt) == 1) { in modem_ppp_process_received_byte()
268 if (net_pkt_alloc_buffer(ppp->rx_pkt, CONFIG_MODEM_PPP_NET_BUF_FRAG_SIZE, in modem_ppp_process_received_byte()
271 net_pkt_unref(ppp->rx_pkt); in modem_ppp_process_received_byte()
272 ppp->rx_pkt = NULL; in modem_ppp_process_received_byte()
273 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; in modem_ppp_process_received_byte()
278 if (byte == MODEM_PPP_CODE_ESCAPE) { in modem_ppp_process_received_byte()
279 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_UNESCAPING; in modem_ppp_process_received_byte()
283 if (net_pkt_write_u8(ppp->rx_pkt, byte) < 0) { in modem_ppp_process_received_byte()
285 net_pkt_unref(ppp->rx_pkt); in modem_ppp_process_received_byte()
286 ppp->rx_pkt = NULL; in modem_ppp_process_received_byte()
287 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; in modem_ppp_process_received_byte()
289 ppp->stats.drop++; in modem_ppp_process_received_byte()
296 if (net_pkt_write_u8(ppp->rx_pkt, (byte ^ MODEM_PPP_VALUE_ESCAPE)) < 0) { in modem_ppp_process_received_byte()
298 net_pkt_unref(ppp->rx_pkt); in modem_ppp_process_received_byte()
299 ppp->rx_pkt = NULL; in modem_ppp_process_received_byte()
300 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; in modem_ppp_process_received_byte()
302 ppp->stats.drop++; in modem_ppp_process_received_byte()
307 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_WRITING; in modem_ppp_process_received_byte()
318 k_work_submit(&ppp->process_work); in modem_ppp_pipe_callback()
325 uint8_t byte; in modem_ppp_send_handler() local
330 if (ppp->tx_pkt == NULL) { in modem_ppp_send_handler()
331 ppp->tx_pkt = k_fifo_get(&ppp->tx_pkt_fifo, K_NO_WAIT); in modem_ppp_send_handler()
334 if (ppp->tx_pkt != NULL) { in modem_ppp_send_handler()
336 if (ppp->transmit_state == MODEM_PPP_TRANSMIT_STATE_IDLE) { in modem_ppp_send_handler()
337 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_SOF; in modem_ppp_send_handler()
341 while (ring_buf_space_get(&ppp->transmit_rb) > 0) { in modem_ppp_send_handler()
342 byte = modem_ppp_wrap_net_pkt_byte(ppp); in modem_ppp_send_handler()
344 ring_buf_put(&ppp->transmit_rb, &byte, 1); in modem_ppp_send_handler()
346 if (ppp->transmit_state == MODEM_PPP_TRANSMIT_STATE_IDLE) { in modem_ppp_send_handler()
347 net_pkt_unref(ppp->tx_pkt); in modem_ppp_send_handler()
348 ppp->tx_pkt = k_fifo_get(&ppp->tx_pkt_fifo, K_NO_WAIT); in modem_ppp_send_handler()
354 reserved_size = ring_buf_get_claim(&ppp->transmit_rb, &reserved, UINT32_MAX); in modem_ppp_send_handler()
356 ring_buf_get_finish(&ppp->transmit_rb, 0); in modem_ppp_send_handler()
360 ret = modem_pipe_transmit(ppp->pipe, reserved, reserved_size); in modem_ppp_send_handler()
362 ring_buf_get_finish(&ppp->transmit_rb, 0); in modem_ppp_send_handler()
364 ring_buf_get_finish(&ppp->transmit_rb, (uint32_t)ret); in modem_ppp_send_handler()
368 if ((ring_buf_is_empty(&ppp->transmit_rb) == false) || (ppp->tx_pkt != NULL)) { in modem_ppp_send_handler()
369 k_work_submit(&ppp->send_work); in modem_ppp_send_handler()
378 ret = modem_pipe_receive(ppp->pipe, ppp->receive_buf, ppp->buf_size); in modem_ppp_process_handler()
384 modem_ppp_process_received_byte(ppp, ppp->receive_buf[i]); in modem_ppp_process_handler()
387 k_work_submit(&ppp->process_work); in modem_ppp_process_handler()
393 struct modem_ppp *ppp = (struct modem_ppp *)dev->data; in modem_ppp_ppp_api_init()
399 if (ppp->init_iface != NULL) { in modem_ppp_ppp_api_init()
400 ppp->init_iface(iface); in modem_ppp_ppp_api_init()
403 ppp->iface = iface; in modem_ppp_ppp_api_init()
418 struct modem_ppp *ppp = (struct modem_ppp *)dev->data; in modem_ppp_ppp_api_send()
420 if (atomic_test_bit(&ppp->state, MODEM_PPP_STATE_ATTACHED_BIT) == false) { in modem_ppp_ppp_api_send()
421 return -EPERM; in modem_ppp_ppp_api_send()
427 return -EPROTONOSUPPORT; in modem_ppp_ppp_api_send()
431 if (((net_pkt_get_len(pkt) < 2) && (net_pkt_is_ppp(pkt) == true)) || in modem_ppp_ppp_api_send()
433 return -ENODATA; in modem_ppp_ppp_api_send()
437 k_fifo_put(&ppp->tx_pkt_fifo, pkt); in modem_ppp_ppp_api_send()
438 k_work_submit(&ppp->send_work); in modem_ppp_ppp_api_send()
445 struct modem_ppp *ppp = (struct modem_ppp *)dev->data; in modem_ppp_ppp_get_stats()
447 return &ppp->stats; in modem_ppp_ppp_get_stats()
463 if (atomic_test_and_set_bit(&ppp->state, MODEM_PPP_STATE_ATTACHED_BIT) == true) { in modem_ppp_attach()
468 ppp->pipe = pipe; in modem_ppp_attach()
474 return ppp->iface; in modem_ppp_get_iface()
482 if (atomic_test_and_clear_bit(&ppp->state, MODEM_PPP_STATE_ATTACHED_BIT) == false) { in modem_ppp_release()
486 modem_pipe_release(ppp->pipe); in modem_ppp_release()
487 k_work_cancel_sync(&ppp->send_work, &sync); in modem_ppp_release()
488 k_work_cancel_sync(&ppp->process_work, &sync); in modem_ppp_release()
489 ppp->pipe = NULL; in modem_ppp_release()
490 ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; in modem_ppp_release()
492 if (ppp->rx_pkt != NULL) { in modem_ppp_release()
493 net_pkt_unref(ppp->rx_pkt); in modem_ppp_release()
494 ppp->rx_pkt = NULL; in modem_ppp_release()
497 ppp->transmit_state = MODEM_PPP_TRANSMIT_STATE_IDLE; in modem_ppp_release()
499 if (ppp->tx_pkt != NULL) { in modem_ppp_release()
500 net_pkt_unref(ppp->tx_pkt); in modem_ppp_release()
501 ppp->tx_pkt = NULL; in modem_ppp_release()
505 pkt = k_fifo_get(&ppp->tx_pkt_fifo, K_NO_WAIT); in modem_ppp_release()
516 struct modem_ppp *ppp = (struct modem_ppp *)dev->data; in modem_ppp_init_internal()
518 atomic_set(&ppp->state, 0); in modem_ppp_init_internal()
519 ring_buf_init(&ppp->transmit_rb, ppp->buf_size, ppp->transmit_buf); in modem_ppp_init_internal()
520 k_work_init(&ppp->send_work, modem_ppp_send_handler); in modem_ppp_init_internal()
521 k_work_init(&ppp->process_work, modem_ppp_process_handler); in modem_ppp_init_internal()
522 k_fifo_init(&ppp->tx_pkt_fifo); in modem_ppp_init_internal()