Skip to content

Commit e4c21da

Browse files
aunsbjergkartben
authored andcommitted
drivers: fuel_gauge: make read-only pointer args const in api
For the lists of properties passed as a pointer to the get_props and set_props functions, there is no reason to not make the pointer const as the called functions will not and should not alter the pointed-to data. In practice, not having the pointers const can cause compilation errors if trying to pass in a const array of properties. Signed-off-by: Mikkel Jakobsen <mikkel.aunsbjerg@escolifesciences.com>
1 parent 7960747 commit e4c21da

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

drivers/fuel_gauge/fuel_gauge_syscall_handlers.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ static inline int z_vrfy_fuel_gauge_get_prop(const struct device *dev, fuel_gaug
2626

2727
#include <zephyr/syscalls/fuel_gauge_get_prop_mrsh.c>
2828

29-
static inline int z_vrfy_fuel_gauge_get_props(const struct device *dev, fuel_gauge_prop_t *props,
29+
static inline int z_vrfy_fuel_gauge_get_props(const struct device *dev,
30+
const fuel_gauge_prop_t *props,
3031
union fuel_gauge_prop_val *vals, size_t len)
3132
{
3233
union fuel_gauge_prop_val k_vals[len];
@@ -58,8 +59,9 @@ static inline int z_vrfy_fuel_gauge_set_prop(const struct device *dev, fuel_gaug
5859

5960
#include <zephyr/syscalls/fuel_gauge_set_prop_mrsh.c>
6061

61-
static inline int z_vrfy_fuel_gauge_set_props(const struct device *dev, fuel_gauge_prop_t *props,
62-
union fuel_gauge_prop_val *vals, size_t len)
62+
static inline int z_vrfy_fuel_gauge_set_props(const struct device *dev,
63+
const fuel_gauge_prop_t *props,
64+
const union fuel_gauge_prop_val *vals, size_t len)
6365
{
6466
union fuel_gauge_prop_val k_vals[len];
6567
fuel_gauge_prop_t k_props[len];
@@ -71,9 +73,6 @@ static inline int z_vrfy_fuel_gauge_set_props(const struct device *dev, fuel_gau
7173

7274
int ret = z_impl_fuel_gauge_set_props(dev, k_props, k_vals, len);
7375

74-
/* We only copy back vals because props will never be modified */
75-
K_OOPS(k_usermode_to_copy(vals, k_vals, len * sizeof(union fuel_gauge_prop_val)));
76-
7776
return ret;
7877
}
7978

include/zephyr/drivers/fuel_gauge.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,10 @@ static inline int z_impl_fuel_gauge_get_prop(const struct device *dev, fuel_gaug
288288
* @return 0 if successful, negative errno code of first failing property
289289
*/
290290

291-
__syscall int fuel_gauge_get_props(const struct device *dev, fuel_gauge_prop_t *props,
291+
__syscall int fuel_gauge_get_props(const struct device *dev, const fuel_gauge_prop_t *props,
292292
union fuel_gauge_prop_val *vals, size_t len);
293293
static inline int z_impl_fuel_gauge_get_props(const struct device *dev,
294-
fuel_gauge_prop_t *props,
294+
const fuel_gauge_prop_t *props,
295295
union fuel_gauge_prop_val *vals, size_t len)
296296
{
297297
const struct fuel_gauge_driver_api *api = (const struct fuel_gauge_driver_api *)dev->api;
@@ -342,12 +342,12 @@ static inline int z_impl_fuel_gauge_set_prop(const struct device *dev, fuel_gaug
342342
*
343343
* @return return=0 if successful. Otherwise, return array index of failing property.
344344
*/
345-
__syscall int fuel_gauge_set_props(const struct device *dev, fuel_gauge_prop_t *props,
346-
union fuel_gauge_prop_val *vals, size_t len);
345+
__syscall int fuel_gauge_set_props(const struct device *dev, const fuel_gauge_prop_t *props,
346+
const union fuel_gauge_prop_val *vals, size_t len);
347347

348348
static inline int z_impl_fuel_gauge_set_props(const struct device *dev,
349-
fuel_gauge_prop_t *props,
350-
union fuel_gauge_prop_val *vals, size_t len)
349+
const fuel_gauge_prop_t *props,
350+
const union fuel_gauge_prop_val *vals, size_t len)
351351
{
352352
for (size_t i = 0; i < len; i++) {
353353
int ret = fuel_gauge_set_prop(dev, props[i], vals[i]);

tests/drivers/fuel_gauge/sbs_gauge/src/test_sbs_gauge.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ZTEST_USER_F(sbs_gauge_new_api, test_get_some_props_failed_returns_bad_status)
4242
/* Valid property */
4343
FUEL_GAUGE_VOLTAGE,
4444
};
45-
union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)];
45+
union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)] = {0};
4646

4747
int ret = fuel_gauge_get_props(fixture->dev, prop_types, props, ARRAY_SIZE(props));
4848

@@ -55,7 +55,7 @@ ZTEST_USER_F(sbs_gauge_new_api, test_set_all_props_failed_returns_err)
5555
/* Invalid property */
5656
FUEL_GAUGE_PROP_MAX,
5757
};
58-
union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)];
58+
union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)] = {0};
5959

6060
int ret = fuel_gauge_set_props(fixture->dev, prop_types, props, ARRAY_SIZE(props));
6161

@@ -166,7 +166,7 @@ ZTEST_USER_F(sbs_gauge_new_api, test_get_props__returns_ok)
166166
FUEL_GAUGE_SBS_REMAINING_TIME_ALARM,
167167
};
168168

169-
union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)];
169+
union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)] = {0};
170170

171171
zassert_ok(fuel_gauge_get_props(fixture->dev, prop_types, props, ARRAY_SIZE(props)));
172172
}
@@ -181,7 +181,7 @@ ZTEST_USER_F(sbs_gauge_new_api, test_set_props__returns_ok)
181181
FUEL_GAUGE_SBS_REMAINING_CAPACITY_ALARM,
182182

183183
};
184-
union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)];
184+
union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)] = {0};
185185

186186
zassert_ok(fuel_gauge_set_props(fixture->dev, prop_types, props, ARRAY_SIZE(props)));
187187
}

0 commit comments

Comments
 (0)