|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Seagate Technology LLC |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr.h> |
| 8 | +#include <ztest.h> |
| 9 | +#include <drivers/led.h> |
| 10 | + |
| 11 | +#define BRIGHTNESS_MAX 100 |
| 12 | +#define TEST_MAX_COLORS 8 |
| 13 | +#define COLOR_FULL 0xff |
| 14 | + |
| 15 | +#if DT_NODE_HAS_STATUS(DT_ALIAS(led_controller_0), okay) |
| 16 | +#define LED_CTRL_NODE_ID DT_ALIAS(led_controller_0) |
| 17 | +#define LED_CTRL_DEV_NAME DT_LABEL(LED_CTRL_NODE_ID) |
| 18 | +#else |
| 19 | +#error "LED controller device not found" |
| 20 | +#endif |
| 21 | + |
| 22 | +#define _COLOR_MAPPING(led_node_id) \ |
| 23 | +const uint8_t test_color_mapping_##led_node_id[] = \ |
| 24 | + DT_PROP(led_node_id, color_mapping) |
| 25 | + |
| 26 | +#define COLOR_MAPPING(led_node_id) \ |
| 27 | + IF_ENABLED(DT_NODE_HAS_PROP(led_node_id, color_mapping), \ |
| 28 | + (_COLOR_MAPPING(led_node_id);)) |
| 29 | + |
| 30 | +#define LED_INFO_COLOR(led_node_id) \ |
| 31 | +{ \ |
| 32 | + .label = DT_LABEL(led_node_id), \ |
| 33 | + .index = DT_PROP_OR(led_node_id, index, 0), \ |
| 34 | + .num_colors = \ |
| 35 | + DT_PROP_LEN(led_node_id, color_mapping), \ |
| 36 | + .color_mapping = test_color_mapping_##led_node_id, \ |
| 37 | +}, |
| 38 | + |
| 39 | +#define LED_INFO_NO_COLOR(led_node_id) \ |
| 40 | +{ \ |
| 41 | + .label = DT_LABEL(led_node_id), \ |
| 42 | + .index = DT_PROP_OR(led_node_id, index, 0), \ |
| 43 | + .num_colors = 0, \ |
| 44 | + .color_mapping = NULL, \ |
| 45 | +}, |
| 46 | + |
| 47 | +#define LED_INFO(led_node_id) \ |
| 48 | + COND_CODE_1(DT_NODE_HAS_PROP(led_node_id, color_mapping), \ |
| 49 | + (LED_INFO_COLOR(led_node_id)), \ |
| 50 | + (LED_INFO_NO_COLOR(led_node_id))) |
| 51 | + |
| 52 | +#define LED_CONTROLLER_INFO(node_id) \ |
| 53 | + \ |
| 54 | +DT_FOREACH_CHILD(node_id, COLOR_MAPPING) \ |
| 55 | + \ |
| 56 | +const struct led_info test_led_info[] = { \ |
| 57 | + DT_FOREACH_CHILD(node_id, LED_INFO) \ |
| 58 | +}; \ |
| 59 | + \ |
| 60 | +static ZTEST_DMEM int num_leds = ARRAY_SIZE(test_led_info) |
| 61 | + |
| 62 | +LED_CONTROLLER_INFO(LED_CTRL_NODE_ID); |
| 63 | + |
| 64 | +static ZTEST_BMEM struct device *led_ctrl; |
| 65 | + |
| 66 | +struct device *get_led_controller(void) |
| 67 | +{ |
| 68 | + return device_get_binding(LED_CTRL_DEV_NAME); |
| 69 | +} |
| 70 | + |
| 71 | +void test_led_setup(void) |
| 72 | +{ |
| 73 | + led_ctrl = get_led_controller(); |
| 74 | + zassert_not_null(led_ctrl, |
| 75 | + "LED controller " LED_CTRL_DEV_NAME " not found"); |
| 76 | + |
| 77 | + zassert_not_equal(num_leds, 0, |
| 78 | + "No LEDs subnodes found in DT for controller " |
| 79 | + LED_CTRL_DEV_NAME); |
| 80 | +} |
| 81 | + |
| 82 | +void test_led_get_info(void) |
| 83 | +{ |
| 84 | + uint8_t led; |
| 85 | + int ret; |
| 86 | + |
| 87 | + if (!led_ctrl || !num_leds) |
| 88 | + ztest_test_skip(); |
| 89 | + |
| 90 | + for (led = 0; led < num_leds; led++) { |
| 91 | + const struct led_info *info; |
| 92 | + uint8_t col; |
| 93 | + |
| 94 | + ret = led_get_info(led_ctrl, led, &info); |
| 95 | + if (ret == -ENOTSUP) { |
| 96 | + TC_PRINT("led_get_info() syscall is not supported.\n"); |
| 97 | + ztest_test_skip(); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + zassert_equal(ret, 0, "LED %d - led_get_info() error (ret=%d)", |
| 102 | + led, ret); |
| 103 | + |
| 104 | + zassert_true(!strcmp(info->label, test_led_info[led].label), |
| 105 | + "LED %d - label: %s instead of %s", |
| 106 | + led, info->label, test_led_info[led].label); |
| 107 | + |
| 108 | + zassert_equal(info->index, test_led_info[led].index, |
| 109 | + "LED %d - index: %d instead of %d", led, |
| 110 | + info->index, test_led_info[led].index); |
| 111 | + |
| 112 | + zassert_equal(info->num_colors, test_led_info[led].num_colors, |
| 113 | + "LED %d - num_colors: %d instead of %d", led, |
| 114 | + info->num_colors, test_led_info[led].num_colors); |
| 115 | + |
| 116 | + TC_PRINT("LED %d - label: %s, index: %d, num_colors: %d", |
| 117 | + led, info->label, info->index, info->num_colors); |
| 118 | + |
| 119 | + if (!info->num_colors) |
| 120 | + continue; |
| 121 | + |
| 122 | + TC_PRINT(" color_mapping: "); |
| 123 | + |
| 124 | + for (col = 0; col < info->num_colors; col++) { |
| 125 | + zassert_equal(info->color_mapping[col], |
| 126 | + test_led_info[led].color_mapping[col], |
| 127 | + "LED %d - color_mapping[%d]=%d instead of %d", |
| 128 | + led, col, info->color_mapping[col], |
| 129 | + test_led_info[led].color_mapping[col]); |
| 130 | + TC_PRINT("%d", info->color_mapping[col]); |
| 131 | + } |
| 132 | + TC_PRINT("\n"); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +void test_led_on(void) |
| 137 | +{ |
| 138 | + uint8_t led; |
| 139 | + int ret; |
| 140 | + |
| 141 | + if (!led_ctrl || !num_leds) |
| 142 | + ztest_test_skip(); |
| 143 | + |
| 144 | + for (led = 0; led < num_leds; led++) { |
| 145 | + ret = led_on(led_ctrl, led); |
| 146 | + zassert_equal(ret, 0, "LED %d - failed to turn on", led); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +void test_led_off(void) |
| 151 | +{ |
| 152 | + uint8_t led; |
| 153 | + int ret; |
| 154 | + |
| 155 | + if (!led_ctrl || !num_leds) |
| 156 | + ztest_test_skip(); |
| 157 | + |
| 158 | + for (led = 0; led < num_leds; led++) { |
| 159 | + ret = led_off(led_ctrl, led); |
| 160 | + zassert_equal(ret, 0, "LED %d - failed to turn off", led); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +void test_led_set_color(void) |
| 165 | +{ |
| 166 | + uint8_t led; |
| 167 | + uint8_t colors[TEST_MAX_COLORS + 1]; |
| 168 | + int ret; |
| 169 | + |
| 170 | + if (!led_ctrl || !num_leds) |
| 171 | + ztest_test_skip(); |
| 172 | + |
| 173 | + for (led = 0; led < num_leds; led++) { |
| 174 | + uint8_t num_colors = test_led_info[led].num_colors; |
| 175 | + uint8_t col; |
| 176 | + |
| 177 | + if (num_colors > TEST_MAX_COLORS) { |
| 178 | + TC_PRINT("LED %d - skip set_color test, num_colors: %d" |
| 179 | + " (test limit is %d)", |
| 180 | + led, num_colors, TEST_MAX_COLORS); |
| 181 | + continue; |
| 182 | + } |
| 183 | + |
| 184 | + memset(colors, 0, sizeof(colors)); |
| 185 | + |
| 186 | + /* Try to set more colors than supported. */ |
| 187 | + ret = led_set_color(led_ctrl, led, num_colors + 1, colors); |
| 188 | + zassert_not_equal(ret, 0, "LED %d - setting %d" |
| 189 | + " colors should fail (%d supported)", |
| 190 | + led, num_colors + 1, num_colors); |
| 191 | + |
| 192 | + if (!num_colors) { |
| 193 | + continue; |
| 194 | + } |
| 195 | + |
| 196 | + /* Try to set less colors than supported. */ |
| 197 | + ret = led_set_color(led_ctrl, led, num_colors - 1, colors); |
| 198 | + zassert_not_equal(ret, 0, "LED %d - setting %d" |
| 199 | + " colors should fail (%d supported)", |
| 200 | + led, num_colors - 1, num_colors); |
| 201 | + |
| 202 | + /* Ensure the LED is on to get a visual feedback. */ |
| 203 | + led_set_brightness(led_ctrl, led, BRIGHTNESS_MAX / 2); |
| 204 | + |
| 205 | + /* Set each color gradually to its maximum level. */ |
| 206 | + for (col = 0; col < num_colors; col++) { |
| 207 | + uint16_t level; |
| 208 | + |
| 209 | + memset(colors, 0, sizeof(colors)); |
| 210 | + |
| 211 | + for (level = 0; level <= COLOR_FULL; level++) { |
| 212 | + colors[col] = level; |
| 213 | + |
| 214 | + ret = led_set_color(led_ctrl, led, |
| 215 | + num_colors, colors); |
| 216 | + zassert_equal(ret, 0, |
| 217 | + "LED %d - failed to set color[%d] to %d", |
| 218 | + led, level); |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +void test_led_set_brightness(void) |
| 225 | +{ |
| 226 | + uint8_t led; |
| 227 | + int ret; |
| 228 | + |
| 229 | + if (!led_ctrl || !num_leds) |
| 230 | + ztest_test_skip(); |
| 231 | + |
| 232 | + for (led = 0; led < num_leds; led++) { |
| 233 | + uint16_t level; |
| 234 | + |
| 235 | + for (level = 0; level <= BRIGHTNESS_MAX; level++) { |
| 236 | + ret = led_set_brightness(led_ctrl, led, level); |
| 237 | + zassert_equal(ret, 0, |
| 238 | + "LED %d - failed to set brightness to %d", |
| 239 | + led, level); |
| 240 | + } |
| 241 | + for (level = BRIGHTNESS_MAX + 1; level <= 255; level++) { |
| 242 | + ret = led_set_brightness(led_ctrl, led, level); |
| 243 | + zassert_not_equal(ret, 0, |
| 244 | + "LED %d - setting brightness to %d" |
| 245 | + " should fail (maximum: %d)", |
| 246 | + led, level, BRIGHTNESS_MAX); |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments