|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Alvaro Garcia Gomez <maxpowel@gmail.com> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/device.h> |
| 8 | +#include <zephyr/drivers/fuel_gauge.h> |
| 9 | +#include <zephyr/drivers/i2c.h> |
| 10 | +#include <zephyr/logging/log.h> |
| 11 | +#include <zephyr/sys/byteorder.h> |
| 12 | +#include <zephyr/sys/util.h> |
| 13 | +#include <zephyr/ztest.h> |
| 14 | +#include <zephyr/ztest_assert.h> |
| 15 | + |
| 16 | +struct sy24561_fixture { |
| 17 | + const struct device *dev; |
| 18 | + const struct fuel_gauge_driver_api *api; |
| 19 | +}; |
| 20 | + |
| 21 | +void emul_sy24561_set_crate_status(int value); |
| 22 | + |
| 23 | +static void *sy24561_setup(void) |
| 24 | +{ |
| 25 | + static ZTEST_DMEM struct sy24561_fixture fixture; |
| 26 | + |
| 27 | + fixture.dev = DEVICE_DT_GET_ANY(silergy_sy24561); |
| 28 | + k_object_access_all_grant(fixture.dev); |
| 29 | + |
| 30 | + zassert_true(device_is_ready(fixture.dev), "Fuel Gauge not found"); |
| 31 | + |
| 32 | + return &fixture; |
| 33 | +} |
| 34 | + |
| 35 | +ZTEST_USER_F(sy24561, test_get_some_props_failed_returns_bad_status) |
| 36 | +{ |
| 37 | + fuel_gauge_prop_t prop_types[] = { |
| 38 | + /* First invalid property */ |
| 39 | + FUEL_GAUGE_PROP_MAX, |
| 40 | + /* Second invalid property */ |
| 41 | + FUEL_GAUGE_PROP_MAX, |
| 42 | + /* Valid property */ |
| 43 | + FUEL_GAUGE_VOLTAGE, |
| 44 | + }; |
| 45 | + union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)]; |
| 46 | + |
| 47 | + int ret = fuel_gauge_get_props(fixture->dev, prop_types, props, ARRAY_SIZE(props)); |
| 48 | + |
| 49 | + zassert_equal(ret, -ENOTSUP, "Getting bad property has a good status."); |
| 50 | +} |
| 51 | + |
| 52 | +ZTEST_USER_F(sy24561, test_get_props__returns_ok) |
| 53 | +{ |
| 54 | + /* Validate what props are supported by the driver */ |
| 55 | + |
| 56 | + fuel_gauge_prop_t prop_types[] = { |
| 57 | + FUEL_GAUGE_VOLTAGE, |
| 58 | + FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE, |
| 59 | + FUEL_GAUGE_STATUS, |
| 60 | + FUEL_GAUGE_CURRENT_DIRECTION, |
| 61 | + }; |
| 62 | + |
| 63 | + union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)]; |
| 64 | + |
| 65 | + zassert_ok(fuel_gauge_get_props(fixture->dev, prop_types, props, ARRAY_SIZE(props))); |
| 66 | +} |
| 67 | + |
| 68 | +ZTEST_USER_F(sy24561, test_set_invalid_temperature_returns_bad_status) |
| 69 | +{ |
| 70 | + union fuel_gauge_prop_val val; |
| 71 | + int ret = 0; |
| 72 | + |
| 73 | + val.temperature = 0; |
| 74 | + ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_TEMPERATURE, val); |
| 75 | + zassert_equal(ret, -EINVAL, "Setting too low temperature has good status"); |
| 76 | + |
| 77 | + val.temperature = 0xffff; |
| 78 | + ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_TEMPERATURE, val); |
| 79 | + zassert_equal(ret, -EINVAL, "Setting too high temperature has good status"); |
| 80 | +} |
| 81 | + |
| 82 | +ZTEST_USER_F(sy24561, test_set_invalid_alarm_threshold_returns_bad_status) |
| 83 | +{ |
| 84 | + union fuel_gauge_prop_val val; |
| 85 | + int ret = 0; |
| 86 | + |
| 87 | + val.sbs_remaining_capacity_alarm = 0; |
| 88 | + ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_SBS_REMAINING_CAPACITY_ALARM, val); |
| 89 | + zassert_equal(ret, -EINVAL, "Setting too low alarm threshold has good status"); |
| 90 | + |
| 91 | + val.sbs_remaining_capacity_alarm = 0xffff; |
| 92 | + ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_SBS_REMAINING_CAPACITY_ALARM, val); |
| 93 | + zassert_equal(ret, -EINVAL, "Setting too high alarm threshold has good status"); |
| 94 | +} |
| 95 | + |
| 96 | +ZTEST_SUITE(sy24561, NULL, sy24561_setup, NULL, NULL, NULL); |
0 commit comments