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);