1 /*
2 * Copyright 2020 Google LLC
3 * Copyright (c) 2020 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #define LOG_LEVEL CONFIG_EMUL_LOG_LEVEL
9 #include <logging/log.h>
10 LOG_MODULE_REGISTER(emul);
11
12 #include <device.h>
13 #include <drivers/emul.h>
14 #include <string.h>
15
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 *
emul_find_by_link(const struct emul_link_for_bus * emul)23 emul_find_by_link(const struct emul_link_for_bus *emul)
24 {
25 const struct emul *erp;
26
27 for (erp = __emul_list_start; erp < __emul_list_end; erp++) {
28 if (strcmp(erp->dev_label, emul->label) == 0) {
29 return erp;
30 }
31 }
32
33 return NULL;
34 }
35
emul_init_for_bus_from_list(const struct device * dev,const struct emul_list_for_bus * list)36 int emul_init_for_bus_from_list(const struct device *dev,
37 const struct emul_list_for_bus *list)
38 {
39 const struct emul_list_for_bus *cfg = dev->config;
40
41 /*
42 * Walk the list of children, find the corresponding emulator and
43 * initialise it.
44 */
45 const struct emul_link_for_bus *elp;
46 const struct emul_link_for_bus *const end =
47 cfg->children + cfg->num_children;
48
49 LOG_INF("Registering %d emulator(s) for %s", cfg->num_children,
50 dev->name);
51 for (elp = cfg->children; elp < end; elp++) {
52 const struct emul *emul = emul_find_by_link(elp);
53
54 __ASSERT(emul, "Cannot find emulator for '%s'", elp->label);
55
56 int rc = emul->init(emul, dev);
57
58 if (rc != 0) {
59 LOG_WRN("Init %s emulator failed: %d\n",
60 elp->label, rc);
61 }
62 }
63
64 return 0;
65 }
66