1 /*
2 * Copyright (c) 2023 Google LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/drivers/uart.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/types.h>
10 #include <zephyr/ztest.h>
11 #include <zephyr/ztest_mock.h>
12
13 #include "uart_mock.h"
14
15 static struct uart_mock_data mock_data;
16 static struct device_state mock_state = {
17 .init_res = 0,
18 .initialized = 1,
19 };
20
uart_mock_callback_set(const struct device * dev,uart_callback_t callback,void * user_data)21 static int uart_mock_callback_set(const struct device *dev, uart_callback_t callback,
22 void *user_data)
23 {
24 struct uart_mock_data *data = dev->data;
25
26 data->user_data = user_data;
27 data->cb = callback;
28
29 return 0;
30 }
31
uart_mock_tx(const struct device * dev,const uint8_t * buf,size_t len,int32_t timeout)32 int uart_mock_tx(const struct device *dev, const uint8_t *buf, size_t len, int32_t timeout)
33 {
34 struct uart_mock_data *data = dev->data;
35
36 data->tx_buf = buf;
37
38 ztest_check_expected_data(buf, len);
39 ztest_check_expected_value(len);
40
41 k_sem_give(&data->resp_sent);
42
43 return 0;
44 }
45
uart_mock_rx_enable(const struct device * dev,uint8_t * buf,size_t len,int32_t timeout)46 static int uart_mock_rx_enable(const struct device *dev, uint8_t *buf, size_t len, int32_t timeout)
47 {
48 struct uart_mock_data *data = dev->data;
49
50 data->rx_buf = buf;
51 data->rx_buf_size = len;
52 data->rx_timeout = timeout;
53
54 return 0;
55 }
56
uart_mock_rx_disable(const struct device * dev)57 static int uart_mock_rx_disable(const struct device *dev)
58 {
59 return 0;
60 }
61
62 static DEVICE_API(uart, mock_api) = {
63 .callback_set = uart_mock_callback_set,
64 .tx = uart_mock_tx,
65 .rx_enable = uart_mock_rx_enable,
66 .rx_disable = uart_mock_rx_disable,
67 };
68
69 struct device uart_mock = {
70 .api = &mock_api,
71 .data = &mock_data,
72 .state = &mock_state,
73 };
74