ESPHome 2026.5.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
17static const char *const 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// Host platform uses std::mutex for proper thread synchronization
35Mutex::Mutex() { handle_ = new std::mutex(); }
36Mutex::~Mutex() { delete static_cast<std::mutex *>(handle_); }
37void Mutex::lock() { static_cast<std::mutex *>(handle_)->lock(); }
38bool Mutex::try_lock() { return static_cast<std::mutex *>(handle_)->try_lock(); }
39void Mutex::unlock() { static_cast<std::mutex *>(handle_)->unlock(); }
40
41void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
42 static const uint8_t esphome_host_mac_address[6] = USE_ESPHOME_HOST_MAC_ADDRESS;
43 memcpy(mac, esphome_host_mac_address, sizeof(esphome_host_mac_address));
44}
45
46} // namespace esphome
47
48#endif // USE_HOST
~Mutex()=default
Definition helpers.cpp:36
void unlock()
Definition helpers.h:2047
Mutex()=default
Definition helpers.cpp:35
bool try_lock()
Definition helpers.h:2046
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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
std::string size_t len
Definition helpers.h:1045
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:74