Skip to content

Commit f29203d

Browse files
pdgendtkartben
authored andcommitted
tests: kernel: device: Add API sections tests
Add tests for the subsystem_api to validate runtime API section checks. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent 971dfa6 commit f29203d

File tree

4 files changed

+51
-32
lines changed

4 files changed

+51
-32
lines changed

tests/kernel/device/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ CONFIG_ZTEST=y
22
CONFIG_TEST_USERSPACE=y
33
CONFIG_PM_DEVICE=y
44
CONFIG_PM_DEVICE_RUNTIME=y
5+
CONFIG_APPLICATION_DEFINED_SYSCALL=y

tests/kernel/device/src/abstract_driver.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@
1212
#define MY_DRIVER_B "my_driver_B"
1313

1414
/* define individual driver A */
15-
static int my_driver_A_do_this(const struct device *device, int foo, int bar)
15+
static int my_driver_A_do_this(const struct device *dev, int foo, int bar)
1616
{
1717
return foo + bar;
1818
}
1919

20-
static void my_driver_A_do_that(const struct device *device,
21-
unsigned int *baz)
20+
static void my_driver_A_do_that(const struct device *dev, unsigned int *baz)
2221
{
2322
*baz = 1;
2423
}
2524

26-
static struct subsystem_api my_driver_A_api_funcs = {
25+
static DEVICE_API(abstract, my_driver_A_api_funcs) = {
2726
.do_this = my_driver_A_do_this,
28-
.do_that = my_driver_A_do_that
27+
.do_that = my_driver_A_do_that,
2928
};
3029

3130
int common_driver_init(const struct device *dev)
@@ -34,20 +33,19 @@ int common_driver_init(const struct device *dev)
3433
}
3534

3635
/* define individual driver B */
37-
static int my_driver_B_do_this(const struct device *device, int foo, int bar)
36+
static int my_driver_B_do_this(const struct device *dev, int foo, int bar)
3837
{
3938
return foo - bar;
4039
}
4140

42-
static void my_driver_B_do_that(const struct device *device,
43-
unsigned int *baz)
41+
static void my_driver_B_do_that(const struct device *dev, unsigned int *baz)
4442
{
4543
*baz = 2;
4644
}
4745

48-
static struct subsystem_api my_driver_B_api_funcs = {
46+
static DEVICE_API(abstract, my_driver_B_api_funcs) = {
4947
.do_this = my_driver_B_do_this,
50-
.do_that = my_driver_B_do_that
48+
.do_that = my_driver_B_do_that,
5149
};
5250

5351
/**

tests/kernel/device/src/abstract_driver.h

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,40 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#ifndef _ABSTRACT_DRIVER_H_
8+
#define _ABSTRACT_DRIVER_H_
9+
710
#include <zephyr/ztest.h>
811
#include <zephyr/device.h>
12+
#include <zephyr/sys/check.h>
913

1014
/* define subsystem common API for drivers */
11-
typedef int (*subsystem_do_this_t)(const struct device *device, int foo,
12-
int bar);
13-
typedef void (*subsystem_do_that_t)(const struct device *device,
14-
unsigned int *baz);
15-
16-
struct subsystem_api {
17-
subsystem_do_this_t do_this;
18-
subsystem_do_that_t do_that;
15+
typedef int (*abstract_do_this_t)(const struct device *dev, int foo, int bar);
16+
typedef void (*abstract_do_that_t)(const struct device *dev, unsigned int *baz);
17+
18+
__subsystem struct abstract_driver_api {
19+
abstract_do_this_t do_this;
20+
abstract_do_that_t do_that;
1921
};
2022

21-
static inline int subsystem_do_this(const struct device *device, int foo,
22-
int bar)
23+
__syscall int abstract_do_this(const struct device *dev, int foo, int bar);
24+
25+
static inline int z_impl_abstract_do_this(const struct device *dev, int foo, int bar)
2326
{
24-
struct subsystem_api *api;
27+
__ASSERT_NO_MSG(DEVICE_API_IS(abstract, dev));
2528

26-
api = (struct subsystem_api *)device->api;
27-
return api->do_this(device, foo, bar);
29+
return DEVICE_API_GET(abstract, dev)->do_this(dev, foo, bar);
2830
}
2931

30-
static inline void subsystem_do_that(const struct device *device,
31-
unsigned int *baz)
32+
__syscall void abstract_do_that(const struct device *dev, unsigned int *baz);
33+
34+
static inline void z_impl_abstract_do_that(const struct device *dev, unsigned int *baz)
3235
{
33-
struct subsystem_api *api;
36+
__ASSERT_NO_MSG(DEVICE_API_IS(abstract, dev));
3437

35-
api = (struct subsystem_api *)device->api;
36-
api->do_that(device, baz);
38+
DEVICE_API_GET(abstract, dev)->do_that(dev, baz);
3739
}
40+
41+
#include <syscalls/abstract_driver.h>
42+
43+
#endif /* _ABSTRACT_DRIVER_H_ */

tests/kernel/device/src/main.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,20 +384,20 @@ ZTEST(device, test_abstraction_driver_common)
384384
dev = device_get_binding(MY_DRIVER_A);
385385
zassert_false((dev == NULL));
386386

387-
ret = subsystem_do_this(dev, foo, bar);
387+
ret = abstract_do_this(dev, foo, bar);
388388
zassert_true(ret == (foo + bar), "common API do_this fail");
389389

390-
subsystem_do_that(dev, &baz);
390+
abstract_do_that(dev, &baz);
391391
zassert_true(baz == 1, "common API do_that fail");
392392

393393
/* verify driver B API has called */
394394
dev = device_get_binding(MY_DRIVER_B);
395395
zassert_false((dev == NULL));
396396

397-
ret = subsystem_do_this(dev, foo, bar);
397+
ret = abstract_do_this(dev, foo, bar);
398398
zassert_true(ret == (foo - bar), "common API do_this fail");
399399

400-
subsystem_do_that(dev, &baz);
400+
abstract_do_that(dev, &baz);
401401
zassert_true(baz == 2, "common API do_that fail");
402402
}
403403

@@ -413,6 +413,20 @@ ZTEST(device, test_deferred_init)
413413
zassert_true(device_is_ready(FAKEDEFERDRIVER0));
414414
}
415415

416+
ZTEST(device, test_device_api)
417+
{
418+
const struct device *dev;
419+
420+
dev = device_get_binding(MY_DRIVER_A);
421+
zexpect_true(DEVICE_API_IS(abstract, dev));
422+
423+
dev = device_get_binding(MY_DRIVER_B);
424+
zexpect_true(DEVICE_API_IS(abstract, dev));
425+
426+
dev = device_get_binding(DUMMY_NOINIT);
427+
zexpect_false(DEVICE_API_IS(abstract, dev));
428+
}
429+
416430
ZTEST_USER(device, test_deferred_init_user)
417431
{
418432
int ret;

0 commit comments

Comments
 (0)