1 /*
2 * Copyright (c) 2022, Commonwealth Scientific and Industrial Research
3 * Organisation (CSIRO) ABN 41 687 119 230.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #define DT_DRV_COMPAT zephyr_bt_hci_entropy
9
10 #include <zephyr/drivers/entropy.h>
11 #include <zephyr/bluetooth/hci.h>
12 #include <string.h>
13
entropy_bt_init(const struct device * dev)14 static int entropy_bt_init(const struct device *dev)
15 {
16 /* Nothing to do */
17 return 0;
18 }
19
entropy_bt_get_entropy(const struct device * dev,uint8_t * buffer,uint16_t length)20 static int entropy_bt_get_entropy(const struct device *dev,
21 uint8_t *buffer, uint16_t length)
22 {
23 if (!bt_is_ready()) {
24 return -EAGAIN;
25 }
26
27 return bt_hci_le_rand(buffer, length);
28 }
29
30 /* HCI commands cannot be run from an interrupt context */
31 static const struct entropy_driver_api entropy_bt_api = {
32 .get_entropy = entropy_bt_get_entropy,
33 .get_entropy_isr = NULL
34 };
35
36 #define ENTROPY_BT_HCI_INIT(inst) \
37 DEVICE_DT_INST_DEFINE(inst, entropy_bt_init, \
38 NULL, NULL, NULL, \
39 PRE_KERNEL_1, \
40 CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
41 &entropy_bt_api);
42
43 DT_INST_FOREACH_STATUS_OKAY(ENTROPY_BT_HCI_INIT)
44