|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Suraj Sonawane <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/ztest.h> |
| 8 | +#include <zephyr/drivers/dai.h> |
| 9 | +#include <zephyr/device.h> |
| 10 | +#include <zephyr/logging/log.h> |
| 11 | + |
| 12 | +LOG_MODULE_REGISTER(test_virtual_dai, CONFIG_DAI_LOG_LEVEL); |
| 13 | + |
| 14 | +/* Get the virtual DAI device */ |
| 15 | +static const struct device *const virtual_dai_dev = DEVICE_DT_GET(DT_NODELABEL(virtual_dai)); |
| 16 | + |
| 17 | +ZTEST_SUITE(virtual_dai, NULL, NULL, NULL, NULL, NULL); |
| 18 | + |
| 19 | +/* Test 1: Verify device exists and is ready */ |
| 20 | +ZTEST(virtual_dai, test_device_exists) |
| 21 | +{ |
| 22 | + zassert_not_null(virtual_dai_dev, "Virtual DAI device should exist"); |
| 23 | + zassert_true(device_is_ready(virtual_dai_dev), "Device should be ready"); |
| 24 | +} |
| 25 | + |
| 26 | +/* Test 2: Test dai_config_set using values from dai_config_get */ |
| 27 | +ZTEST(virtual_dai, test_dai_config_set_using_retrieved_config) |
| 28 | +{ |
| 29 | + struct dai_config config; |
| 30 | + int ret; |
| 31 | + |
| 32 | + /* First get the configuration */ |
| 33 | + ret = dai_config_get(virtual_dai_dev, &config, 0); |
| 34 | + zassert_ok(ret, "dai_config_get should succeed"); |
| 35 | + |
| 36 | + /* Log the configuration */ |
| 37 | + LOG_INF("Config: type=%d, dai_index=%d, rate=%d, channels=%d", |
| 38 | + config.type, config.dai_index); |
| 39 | + |
| 40 | + /* Set the configuration */ |
| 41 | + ret = dai_config_set(virtual_dai_dev, &config, NULL); |
| 42 | + zassert_ok(ret, "dai_config_set should return success (0)"); |
| 43 | +} |
| 44 | + |
| 45 | +/* Test 3: Test dai_config_set with invalid type (should fail) */ |
| 46 | +ZTEST(virtual_dai, test_dai_config_set_invalid_type) |
| 47 | +{ |
| 48 | + struct dai_config config; |
| 49 | + struct dai_config invalid_config; |
| 50 | + int ret; |
| 51 | + |
| 52 | + /* Get the current configuration to see what type is valid */ |
| 53 | + ret = dai_config_get(virtual_dai_dev, &config, 0); |
| 54 | + zassert_ok(ret, "dai_config_get should succeed"); |
| 55 | + |
| 56 | + /* Create an invalid configuration (use a different type) */ |
| 57 | + invalid_config.type = config.type + 100; /* Invalid type */ |
| 58 | + invalid_config.dai_index = config.dai_index; |
| 59 | + |
| 60 | + /* This should fail with -EINVAL */ |
| 61 | + ret = dai_config_set(virtual_dai_dev, &invalid_config, NULL); |
| 62 | + zassert_equal(ret, -EINVAL, "dai_config_set should return -EINVAL for invalid type"); |
| 63 | +} |
| 64 | + |
| 65 | +/* Test 4: Test dai_trigger commands */ |
| 66 | +ZTEST(virtual_dai, test_dai_trigger_commands) |
| 67 | +{ |
| 68 | + int ret; |
| 69 | + |
| 70 | + /* Test START trigger*/ |
| 71 | + ret = dai_trigger(virtual_dai_dev, 0, DAI_TRIGGER_START); /* dir = 0 (TX) */ |
| 72 | + zassert_ok(ret, "START trigger should return success (0)"); |
| 73 | + |
| 74 | + /* Test STOP trigger*/ |
| 75 | + ret = dai_trigger(virtual_dai_dev, 0, DAI_TRIGGER_STOP); |
| 76 | + zassert_ok(ret, "STOP trigger should return success (0)"); |
| 77 | + |
| 78 | + /* Test PAUSE trigger*/ |
| 79 | + ret = dai_trigger(virtual_dai_dev, 0, DAI_TRIGGER_PAUSE); |
| 80 | + zassert_ok(ret, "PAUSE trigger should return success (0)"); |
| 81 | + |
| 82 | + /* Test COPY trigger*/ |
| 83 | + ret = dai_trigger(virtual_dai_dev, 0, DAI_TRIGGER_COPY); |
| 84 | + zassert_ok(ret, "COPY trigger should return success (0)"); |
| 85 | + |
| 86 | + /* Test invalid trigger command */ |
| 87 | + ret = dai_trigger(virtual_dai_dev, 0, 99); /* Invalid command */ |
| 88 | + zassert_equal(ret, -EINVAL, "Should return -EINVAL for invalid trigger"); |
| 89 | +} |
| 90 | + |
| 91 | +/* Test 5: Test dai_get_properties */ |
| 92 | +ZTEST(virtual_dai, test_dai_get_properties) |
| 93 | +{ |
| 94 | + const struct dai_properties *props; |
| 95 | + |
| 96 | + props = dai_get_properties(virtual_dai_dev, 0, 0); /* dir = 0 (TX), stream_id = 0 */ |
| 97 | + zassert_is_null(props, "dai_get_properties should return NULL"); |
| 98 | +} |
| 99 | + |
| 100 | +/* Test 6: Test dai_probe and dai_remove functions */ |
| 101 | +ZTEST(virtual_dai, test_probe_remove) |
| 102 | +{ |
| 103 | + int ret; |
| 104 | + |
| 105 | + ret = dai_probe(virtual_dai_dev); |
| 106 | + zassert_ok(ret, "Probe should succeed"); |
| 107 | + |
| 108 | + ret = dai_remove(virtual_dai_dev); |
| 109 | + zassert_ok(ret, "Remove should succeed"); |
| 110 | +} |
0 commit comments