Lines Matching +full:2 +full:- +full:byte

4  * SPDX-License-Identifier: Apache-2.0
66 /* with async we use 2 rx buffers */
122 switch (evt->type) { in uart_callback()
124 LOG_DBG("UART_TX_DONE: sent %d bytes", evt->data.tx.len); in uart_callback()
134 len = evt->data.rx.len; in uart_callback()
135 p = evt->data.rx.buf + evt->data.rx.offset; in uart_callback()
139 ret = ring_buf_put(&context->rx_ringbuf, p, len); in uart_callback()
140 if (ret < evt->data.rx.len) { in uart_callback()
144 evt->data.rx.len, ret); in uart_callback()
148 * rx off for now and re-enabling that later. in uart_callback()
156 space_left = ring_buf_space_get(&context->rx_ringbuf); in uart_callback()
157 if (!rx_retry_pending && space_left < (sizeof(context->rx_buf) / 8)) { in uart_callback()
169 k_work_submit_to_queue(&context->cb_workq, &context->cb_work); in uart_callback()
177 err = uart_rx_buf_rsp(dev, next_buf, sizeof(context->buf)); in uart_callback()
187 next_buf = evt->data.rx_buf.buf; in uart_callback()
192 LOG_DBG("UART_RX_DISABLED - re-enabling in a while"); in uart_callback()
195 k_work_schedule(&context->uart_recovery_work, in uart_callback()
203 LOG_DBG("UART_RX_STOPPED: stop reason %d", evt->data.rx_stop.reason); in uart_callback()
205 if (evt->data.rx_stop.reason != 0) { in uart_callback()
216 next_buf = context->buf2; in ppp_async_uart_rx_enable()
217 err = uart_callback_set(context->dev, uart_callback, (void *)context); in ppp_async_uart_rx_enable()
222 err = uart_rx_enable(context->dev, context->buf, sizeof(context->buf), in ppp_async_uart_rx_enable()
240 ret = ring_buf_space_get(&ppp->rx_ringbuf); in uart_recovery()
241 if (ret >= (sizeof(ppp->rx_buf) / 2)) { in uart_recovery()
250 LOG_ERR("Rx buffer still doesn't have enough room %d to be re-enabled", ret); in uart_recovery()
251 k_work_schedule(&ppp->uart_recovery_work, in uart_recovery()
257 static int ppp_save_byte(struct ppp_driver_context *ppp, uint8_t byte) in ppp_save_byte() argument
261 if (!ppp->pkt) { in ppp_save_byte()
262 ppp->pkt = net_pkt_rx_alloc_with_buffer( in ppp_save_byte()
263 ppp->iface, in ppp_save_byte()
266 if (!ppp->pkt) { in ppp_save_byte()
268 return -ENOMEM; in ppp_save_byte()
271 net_pkt_cursor_init(ppp->pkt); in ppp_save_byte()
273 ppp->available = net_pkt_available_buffer(ppp->pkt); in ppp_save_byte()
280 LOG_DBG("Saving byte %02x", byte); in ppp_save_byte()
284 * before we write a byte to last available cursor position. in ppp_save_byte()
286 if (ppp->available == 1) { in ppp_save_byte()
287 ret = net_pkt_alloc_buffer(ppp->pkt, in ppp_save_byte()
295 ppp->available = net_pkt_available_buffer(ppp->pkt); in ppp_save_byte()
298 if (ppp->available) { in ppp_save_byte()
299 ret = net_pkt_write_u8(ppp->pkt, byte); in ppp_save_byte()
302 ppp, ppp->pkt, ret); in ppp_save_byte()
306 ppp->available--; in ppp_save_byte()
312 net_pkt_unref(ppp->pkt); in ppp_save_byte()
313 ppp->pkt = NULL; in ppp_save_byte()
314 return -ENOMEM; in ppp_save_byte()
340 if (ctx->state == new_state) { in ppp_change_state()
348 ctx, ppp_driver_state_str(ctx->state), ctx->state, in ppp_change_state()
351 ctx->state = new_state; in ppp_change_state()
359 uint8_t *buf = ppp->send_buf; in ppp_send_flush()
362 * one byte at a time causes each byte to get wrapped in muxing headers. in ppp_send_flush()
367 (void)uart_fifo_fill(ppp->dev, buf, off); in ppp_send_flush()
374 ret = uart_tx(ppp->dev, buf, off, in ppp_send_flush()
382 while (off--) { in ppp_send_flush()
383 uart_poll_out(ppp->dev, *buf++); in ppp_send_flush()
396 ppp->send_buf[off++] = data[i]; in ppp_send_bytes()
398 if (off >= sizeof(ppp->send_buf)) { in ppp_send_bytes()
411 static void ppp_handle_client(struct ppp_driver_context *ppp, uint8_t byte) in ppp_handle_client() argument
417 if (ppp->client_index >= (sizeof(CLIENT) - 1)) { in ppp_handle_client()
418 ppp->client_index = 0; in ppp_handle_client()
421 if (byte != client[ppp->client_index]) { in ppp_handle_client()
422 ppp->client_index = 0; in ppp_handle_client()
423 if (byte != client[ppp->client_index]) { in ppp_handle_client()
428 ++ppp->client_index; in ppp_handle_client()
429 if (ppp->client_index >= (sizeof(CLIENT) - 1)) { in ppp_handle_client()
432 sizeof(CLIENTSERVER) - 1, 0); in ppp_handle_client()
434 ppp->client_index = 0; in ppp_handle_client()
440 static int ppp_input_byte(struct ppp_driver_context *ppp, uint8_t byte) in ppp_input_byte() argument
442 int ret = -EAGAIN; in ppp_input_byte()
444 switch (ppp->state) { in ppp_input_byte()
447 if (byte == 0x7e) { in ppp_input_byte()
449 LOG_DBG("Sync byte (0x%02x) start", byte); in ppp_input_byte()
453 ppp_handle_client(ppp, byte); in ppp_input_byte()
460 if (byte != 0xff) { in ppp_input_byte()
462 if (byte == 0x7e) { in ppp_input_byte()
463 /* Just skip to the start of the pkt byte */ in ppp_input_byte()
464 return -EAGAIN; in ppp_input_byte()
467 LOG_DBG("Invalid (0x%02x) byte, expecting Address", in ppp_input_byte()
468 byte); in ppp_input_byte()
475 LOG_DBG("Address byte (0x%02x) start", byte); in ppp_input_byte()
483 ret = ppp_save_byte(ppp, byte); in ppp_input_byte()
488 ret = -EAGAIN; in ppp_input_byte()
497 if (byte == 0x7e) { in ppp_input_byte()
498 LOG_DBG("End of pkt (0x%02x)", byte); in ppp_input_byte()
502 if (byte == 0x7d) { in ppp_input_byte()
504 ppp->next_escaped = true; in ppp_input_byte()
508 if (ppp->next_escaped) { in ppp_input_byte()
510 byte ^= 0x20; in ppp_input_byte()
511 ppp->next_escaped = false; in ppp_input_byte()
514 ret = ppp_save_byte(ppp, byte); in ppp_input_byte()
519 ret = -EAGAIN; in ppp_input_byte()
525 LOG_ERR("[%p] Invalid state %d", ppp, ppp->state); in ppp_input_byte()
537 buf = ppp->pkt->buffer; in ppp_check_fcs()
542 crc = crc16_ccitt(0xffff, buf->data, buf->len); in ppp_check_fcs()
544 buf = buf->frags; in ppp_check_fcs()
547 crc = crc16_ccitt(crc, buf->data, buf->len); in ppp_check_fcs()
548 buf = buf->frags; in ppp_check_fcs()
554 ppp->stats.chkerr++; in ppp_check_fcs()
565 net_pkt_hexdump(ppp->pkt, "recv ppp"); in ppp_process_msg()
570 ppp->stats.drop++; in ppp_process_msg()
571 ppp->stats.pkts.rx++; in ppp_process_msg()
573 net_pkt_unref(ppp->pkt); in ppp_process_msg()
576 * FCS fields (16-bit) as the PPP L2 layer does not need in ppp_process_msg()
579 uint16_t addr_and_ctrl = net_buf_pull_be16(ppp->pkt->buffer); in ppp_process_msg()
586 ppp->stats.drop++; in ppp_process_msg()
587 ppp->stats.pkts.rx++; in ppp_process_msg()
589 net_pkt_unref(ppp->pkt); in ppp_process_msg()
591 /* Remove FCS bytes (2) */ in ppp_process_msg()
592 net_pkt_remove_tail(ppp->pkt, 2); in ppp_process_msg()
597 net_pkt_cursor_init(ppp->pkt); in ppp_process_msg()
598 net_pkt_set_overwrite(ppp->pkt, true); in ppp_process_msg()
600 if (net_recv_data(ppp->iface, ppp->pkt) < 0) { in ppp_process_msg()
601 net_pkt_unref(ppp->pkt); in ppp_process_msg()
606 ppp->pkt = NULL; in ppp_process_msg()
626 if (ppp->pkt && net_pkt_get_len(ppp->pkt) > 3) { in ppp_recv_cb()
636 *off = len - i - 1; in ppp_recv_cb()
652 memset(ppp->buf, 0, UART_BUF_LEN); in ppp_driver_feed_data()
662 memcpy(ppp->buf, data, data_to_copy); in ppp_driver_feed_data()
666 (void)ppp_recv_cb(ppp->buf, &recv_off); in ppp_driver_feed_data()
668 remaining = data_to_copy - recv_off; in ppp_driver_feed_data()
672 data_len -= remaining; in ppp_driver_feed_data()
684 buf = pkt->buffer; in calc_fcs()
700 crc = crc16_ccitt(crc, buf->data, buf->len); in calc_fcs()
701 buf = buf->frags; in calc_fcs()
710 static uint16_t ppp_escape_byte(uint8_t byte, int *offset) in ppp_escape_byte() argument
712 if (byte == 0x7e || byte == 0x7d || byte < 0x20) { in ppp_escape_byte()
714 return (0x7d << 8) | (byte ^ 0x20); in ppp_escape_byte()
718 return byte; in ppp_escape_byte()
723 struct ppp_driver_context *ppp = dev->data; in ppp_send()
724 struct net_buf *buf = pkt->buffer; in ppp_send()
729 uint8_t byte; in ppp_send() local
740 return -ENODATA; in ppp_send()
752 return -EPROTONOSUPPORT; in ppp_send()
757 return -ENOMEM; in ppp_send()
769 offset ? 1 : 2, in ppp_send()
774 offset ? 1 : 2, in ppp_send()
787 for (i = 0; i < buf->len; i++) { in ppp_send()
789 escaped = htons(ppp_escape_byte(buf->data[i], &offset)); in ppp_send()
792 offset ? 1 : 2, in ppp_send()
796 buf = buf->frags; in ppp_send()
801 offset ? 1 : 2, in ppp_send()
806 offset ? 1 : 2, in ppp_send()
809 byte = 0x7e; in ppp_send()
810 send_off = ppp_send_bytes(ppp, &byte, 1, send_off); in ppp_send()
824 len = ring_buf_get_claim(&ppp->rx_ringbuf, &data, in ppp_consume_ringbuf()
827 LOG_DBG("Ringbuf %p is empty!", &ppp->rx_ringbuf); in ppp_consume_ringbuf()
833 LOG_HEXDUMP_DBG(data, len, ppp->dev->name); in ppp_consume_ringbuf()
841 if (ppp->pkt && net_pkt_get_len(ppp->pkt) > 3) { in ppp_consume_ringbuf()
845 } while (--tmp); in ppp_consume_ringbuf()
847 ret = ring_buf_get_finish(&ppp->rx_ringbuf, len); in ppp_consume_ringbuf()
852 return -EAGAIN; in ppp_consume_ringbuf()
859 int ret = -EAGAIN; in ppp_isr_cb_work()
861 while (ret == -EAGAIN) { in ppp_isr_cb_work()
869 struct ppp_driver_context *ppp = dev->data; in ppp_driver_init()
874 ring_buf_init(&ppp->rx_ringbuf, sizeof(ppp->rx_buf), ppp->rx_buf); in ppp_driver_init()
875 k_work_init(&ppp->cb_work, ppp_isr_cb_work); in ppp_driver_init()
877 k_work_queue_start(&ppp->cb_workq, ppp_workq, in ppp_driver_init()
880 k_thread_name_set(&ppp->cb_workq.thread, "ppp_workq"); in ppp_driver_init()
882 k_work_init_delayable(&ppp->uart_recovery_work, uart_recovery); in ppp_driver_init()
885 ppp->pkt = NULL; in ppp_driver_init()
888 ppp->client_index = 0; in ppp_driver_init()
896 ppp->ll_addr.addr = ppp->mac_addr; in ppp_get_mac()
897 ppp->ll_addr.len = sizeof(ppp->mac_addr); in ppp_get_mac()
899 return &ppp->ll_addr; in ppp_get_mac()
904 struct ppp_driver_context *ppp = net_if_get_device(iface)->data; in ppp_iface_init()
911 if (ppp->init_done) { in ppp_iface_init()
915 ppp->init_done = true; in ppp_iface_init()
916 ppp->iface = iface; in ppp_iface_init()
924 if (net_bytes_from_str(ppp->mac_addr, sizeof(ppp->mac_addr), in ppp_iface_init()
930 /* 00-00-5E-00-53-xx Documentation RFC 7042 */ in ppp_iface_init()
931 ppp->mac_addr[0] = 0x00; in ppp_iface_init()
932 ppp->mac_addr[1] = 0x00; in ppp_iface_init()
933 ppp->mac_addr[2] = 0x5E; in ppp_iface_init()
934 ppp->mac_addr[3] = 0x00; in ppp_iface_init()
935 ppp->mac_addr[4] = 0x53; in ppp_iface_init()
936 ppp->mac_addr[5] = sys_rand32_get(); in ppp_iface_init()
939 net_if_set_link_addr(iface, ll_addr->addr, ll_addr->len, in ppp_iface_init()
942 memset(ppp->buf, 0, sizeof(ppp->buf)); in ppp_iface_init()
957 struct ppp_driver_context *context = dev->data; in ppp_get_stats()
959 return &context->stats; in ppp_get_stats()
980 rx = uart_fifo_read(uart, context->buf, sizeof(context->buf)); in ppp_uart_isr()
985 ret = ring_buf_put(&context->rx_ringbuf, context->buf, rx); in ppp_uart_isr()
993 k_work_submit_to_queue(&context->cb_workq, &context->cb_work); in ppp_uart_isr()
1000 struct ppp_driver_context *context = dev->data; in ppp_start()
1007 if (atomic_cas(&context->modem_init_done, false, true)) { in ppp_start()
1020 return -ENOENT; in ppp_start()
1023 context->dev = mux; in ppp_start()
1025 context->dev = DEVICE_DT_GET(DT_BUS(DT_INST(0, zephyr_gsm_ppp))); in ppp_start()
1027 /* dts chosen zephyr,ppp-uart case */ in ppp_start()
1028 context->dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_ppp_uart)); in ppp_start()
1030 LOG_INF("Initializing PPP to use %s", context->dev->name); in ppp_start()
1032 if (!device_is_ready(context->dev)) { in ppp_start()
1033 LOG_ERR("Device %s is not ready", context->dev->name); in ppp_start()
1034 return -ENODEV; in ppp_start()
1040 uart_irq_rx_disable(context->dev); in ppp_start()
1041 uart_irq_tx_disable(context->dev); in ppp_start()
1042 ppp_uart_flush(context->dev); in ppp_start()
1043 uart_irq_callback_user_data_set(context->dev, ppp_uart_isr, in ppp_start()
1045 uart_irq_rx_enable(context->dev); in ppp_start()
1050 net_if_carrier_on(context->iface); in ppp_start()
1056 struct ppp_driver_context *context = dev->data; in ppp_stop()
1058 net_if_carrier_off(context->iface); in ppp_stop()
1059 context->modem_init_done = false; in ppp_stop()