Skip to content

Commit 03bcf82

Browse files
Yuval Peresscfriedt
authored andcommitted
emul: Support getting an emulator by name
Issue #38271 Implement a getter for emulators similar to device_get_binding. This function can be used to get the emulator instance during tests to call emulator specific functions. Example: The current BMI160 emulator pre-defines a finite set of data samples that will be returned. If a test was to be written for logic that uses that data, then the emulator would become completely useless without the ability for the test to define what data should be returned. This will also help in exercising error conditions in tests. Signed-off-by: Yuval Peress <[email protected]>
1 parent d47bd60 commit 03bcf82

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

include/drivers/emul.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ extern const struct emul __emul_list_end[];
9898
int emul_init_for_bus_from_list(const struct device *dev,
9999
const struct emul_list_for_bus *list);
100100

101+
/**
102+
* @brief Retrieve the emul structure for an emulator by name
103+
*
104+
* @details Emulator objects are created via the EMUL_DEFINE() macro and placed in memory by the
105+
* linker. If the emulator structure is needed for custom API calls, it can be retrieved by the name
106+
* that the emulator exposes to the system (this is the devicetree node's label by default).
107+
*
108+
* @param name Emulator name to search for. A null pointer, or a pointer to an
109+
* empty string, will cause NULL to be returned.
110+
*
111+
* @return pointer to emulator structure; NULL if not found or cannot be used.
112+
*/
113+
const struct emul *emul_get_binding(const char *name);
114+
101115
#ifdef __cplusplus
102116
}
103117
#endif /* __cplusplus */

subsys/emul/emul.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,13 @@ LOG_MODULE_REGISTER(emul);
1313
#include <drivers/emul.h>
1414
#include <string.h>
1515

16-
/**
17-
* Find a an emulator using its link information
18-
*
19-
* @param emul Emulator info to find
20-
* @return pointer to emulator, or NULL if not found
21-
*/
22-
static const struct emul *
23-
emul_find_by_link(const struct emul_link_for_bus *emul)
16+
const struct emul *emul_get_binding(const char *name)
2417
{
25-
const struct emul *erp;
18+
const struct emul *emul_it;
2619

27-
for (erp = __emul_list_start; erp < __emul_list_end; erp++) {
28-
if (strcmp(erp->dev_label, emul->label) == 0) {
29-
return erp;
20+
for (emul_it = __emul_list_start; emul_it < __emul_list_end; emul_it++) {
21+
if (strcmp(emul_it->dev_label, name) == 0) {
22+
return emul_it;
3023
}
3124
}
3225

@@ -49,7 +42,7 @@ int emul_init_for_bus_from_list(const struct device *dev,
4942
LOG_INF("Registering %d emulator(s) for %s", cfg->num_children,
5043
dev->name);
5144
for (elp = cfg->children; elp < end; elp++) {
52-
const struct emul *emul = emul_find_by_link(elp);
45+
const struct emul *emul = emul_get_binding(elp->label);
5346

5447
__ASSERT(emul, "Cannot find emulator for '%s'", elp->label);
5548

0 commit comments

Comments
 (0)