1 /*
2  * Copyright (c) 2023 Trackunit Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "modem_backend_uart_isr.h"
8 #include "modem_backend_uart_async.h"
9 
10 #include <zephyr/modem/backend/uart.h>
11 
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(modem_backend_uart);
14 
15 #include <string.h>
16 
modem_backend_uart_receive_ready_handler(struct k_work * item)17 static void modem_backend_uart_receive_ready_handler(struct k_work *item)
18 {
19 	struct modem_backend_uart *backend =
20 		CONTAINER_OF(item, struct modem_backend_uart, receive_ready_work);
21 
22 	modem_pipe_notify_receive_ready(&backend->pipe);
23 }
24 
modem_backend_uart_init(struct modem_backend_uart * backend,const struct modem_backend_uart_config * config)25 struct modem_pipe *modem_backend_uart_init(struct modem_backend_uart *backend,
26 					   const struct modem_backend_uart_config *config)
27 {
28 	__ASSERT_NO_MSG(config->uart != NULL);
29 	__ASSERT_NO_MSG(config->receive_buf != NULL);
30 	__ASSERT_NO_MSG(config->receive_buf_size > 1);
31 	__ASSERT_NO_MSG((config->receive_buf_size % 2) == 0);
32 	__ASSERT_NO_MSG(config->transmit_buf != NULL);
33 	__ASSERT_NO_MSG(config->transmit_buf_size > 0);
34 
35 	memset(backend, 0x00, sizeof(*backend));
36 	backend->uart = config->uart;
37 	k_work_init(&backend->receive_ready_work, modem_backend_uart_receive_ready_handler);
38 
39 #ifdef CONFIG_MODEM_BACKEND_UART_ASYNC
40 	if (modem_backend_uart_async_is_supported(backend)) {
41 		modem_backend_uart_async_init(backend, config);
42 		return &backend->pipe;
43 	}
44 #endif /* CONFIG_MODEM_BACKEND_UART_ASYNC */
45 
46 #ifdef CONFIG_MODEM_BACKEND_UART_ISR
47 	modem_backend_uart_isr_init(backend, config);
48 
49 	return &backend->pipe;
50 #endif /* CONFIG_MODEM_BACKEND_UART_ISR */
51 
52 	__ASSERT(0, "No supported UART API");
53 
54 	return NULL;
55 }
56