ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
helpers.cpp
Go to the documentation of this file.
2
3#ifdef USE_HOST
4
5#ifndef _WIN32
6#include <net/if.h>
7#include <netinet/in.h>
8#include <sys/ioctl.h>
9#endif
10#include <unistd.h>
11
13#include "esphome/core/log.h"
14
15namespace esphome {
16
17ESPHOME_LOG_TAG(TAG, "helpers.host");
18
19bool random_bytes(uint8_t *data, size_t len) {
20 FILE *fp = fopen("/dev/urandom", "r");
21 if (fp == nullptr) {
22 ESP_LOGW(TAG, "Could not open /dev/urandom, errno=%d", errno);
23 exit(1);
24 }
25 size_t read = fread(data, 1, len, fp);
26 if (read != len) {
27 ESP_LOGW(TAG, "Not enough data from /dev/urandom");
28 exit(1);
29 }
30 fclose(fp);
31 return true;
32}
33
34// The host has no interrupts to mask
37
38// Host platform uses std::mutex for proper thread synchronization
39Mutex::Mutex() { handle_ = new std::mutex(); }
40Mutex::~Mutex() { delete static_cast<std::mutex *>(handle_); }
41void Mutex::lock() { static_cast<std::mutex *>(handle_)->lock(); }
42bool Mutex::try_lock() { return static_cast<std::mutex *>(handle_)->try_lock(); }
43void Mutex::unlock() { static_cast<std::mutex *>(handle_)->unlock(); }
44
45void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
46 static constexpr uint8_t HOST_MAC_ADDRESS[MAC_ADDRESS_SIZE] = USE_ESPHOME_HOST_MAC_ADDRESS;
47 memcpy(mac, HOST_MAC_ADDRESS, sizeof(HOST_MAC_ADDRESS));
48}
49
50} // namespace esphome
51
52#endif // USE_HOST
~Mutex()=default
Definition helpers.cpp:40
void unlock()
Definition helpers.h:2107
Mutex()=default
Definition helpers.cpp:39
bool try_lock()
Definition helpers.h:2106
ESPHOME_LOG_TAG(TAG, CRASH_LOG_TAG)
bool random_bytes(uint8_t *data, size_t len)
Generate len random bytes using the platform's secure RNG (hardware RNG or OS CSPRNG).
Definition helpers.cpp:20
const void size_t len
Definition hal.h:68
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
Definition helpers.cpp:87