From 03bcf8276a40bfb4bce451345aad3f9a6d9e074a Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Tue, 7 Sep 2021 14:00:17 -0600 Subject: [PATCH] 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 --- include/drivers/emul.h | 14 ++++++++++++++ subsys/emul/emul.c | 19 ++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/drivers/emul.h b/include/drivers/emul.h index 02edc75ff55..0a07d7e0d6f 100644 --- a/include/drivers/emul.h +++ b/include/drivers/emul.h @@ -98,6 +98,20 @@ extern const struct emul __emul_list_end[]; int emul_init_for_bus_from_list(const struct device *dev, const struct emul_list_for_bus *list); +/** + * @brief Retrieve the emul structure for an emulator by name + * + * @details Emulator objects are created via the EMUL_DEFINE() macro and placed in memory by the + * linker. If the emulator structure is needed for custom API calls, it can be retrieved by the name + * that the emulator exposes to the system (this is the devicetree node's label by default). + * + * @param name Emulator name to search for. A null pointer, or a pointer to an + * empty string, will cause NULL to be returned. + * + * @return pointer to emulator structure; NULL if not found or cannot be used. + */ +const struct emul *emul_get_binding(const char *name); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/subsys/emul/emul.c b/subsys/emul/emul.c index 75001bbfdeb..c0e578b7f5b 100644 --- a/subsys/emul/emul.c +++ b/subsys/emul/emul.c @@ -13,20 +13,13 @@ LOG_MODULE_REGISTER(emul); #include #include -/** - * Find a an emulator using its link information - * - * @param emul Emulator info to find - * @return pointer to emulator, or NULL if not found - */ -static const struct emul * -emul_find_by_link(const struct emul_link_for_bus *emul) +const struct emul *emul_get_binding(const char *name) { - const struct emul *erp; + const struct emul *emul_it; - for (erp = __emul_list_start; erp < __emul_list_end; erp++) { - if (strcmp(erp->dev_label, emul->label) == 0) { - return erp; + for (emul_it = __emul_list_start; emul_it < __emul_list_end; emul_it++) { + if (strcmp(emul_it->dev_label, name) == 0) { + return emul_it; } } @@ -49,7 +42,7 @@ int emul_init_for_bus_from_list(const struct device *dev, LOG_INF("Registering %d emulator(s) for %s", cfg->num_children, dev->name); for (elp = cfg->children; elp < end; elp++) { - const struct emul *emul = emul_find_by_link(elp); + const struct emul *emul = emul_get_binding(elp->label); __ASSERT(emul, "Cannot find emulator for '%s'", elp->label);