mirror of
https://github.com/esphome/esphome.git
synced 2024-12-24 22:44:54 +01:00
let make new platform implementation in external components (#7615)
Co-authored-by: Tomasz Duda <tomaszduda23@gmai.com>
This commit is contained in:
parent
abbd7faa64
commit
71e1e3b5f8
2 changed files with 39 additions and 22 deletions
|
@ -10,6 +10,7 @@
|
|||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <strings.h>
|
||||
|
||||
#ifdef USE_HOST
|
||||
#ifndef _WIN32
|
||||
|
@ -188,37 +189,39 @@ uint32_t fnv1_hash(const std::string &str) {
|
|||
return hash;
|
||||
}
|
||||
|
||||
uint32_t random_uint32() {
|
||||
#ifdef USE_ESP32
|
||||
return esp_random();
|
||||
uint32_t random_uint32() { return esp_random(); }
|
||||
#elif defined(USE_ESP8266)
|
||||
return os_random();
|
||||
uint32_t random_uint32() { return os_random(); }
|
||||
#elif defined(USE_RP2040)
|
||||
uint32_t random_uint32() {
|
||||
uint32_t result = 0;
|
||||
for (uint8_t i = 0; i < 32; i++) {
|
||||
result <<= 1;
|
||||
result |= rosc_hw->randombit;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#elif defined(USE_LIBRETINY)
|
||||
return rand();
|
||||
uint32_t random_uint32() { return rand(); }
|
||||
#elif defined(USE_HOST)
|
||||
uint32_t random_uint32() {
|
||||
std::random_device dev;
|
||||
std::mt19937 rng(dev());
|
||||
std::uniform_int_distribution<uint32_t> dist(0, std::numeric_limits<uint32_t>::max());
|
||||
return dist(rng);
|
||||
#else
|
||||
#error "No random source available for this configuration."
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
float random_float() { return static_cast<float>(random_uint32()) / static_cast<float>(UINT32_MAX); }
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
#ifdef USE_ESP32
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
esp_fill_random(data, len);
|
||||
return true;
|
||||
}
|
||||
#elif defined(USE_ESP8266)
|
||||
return os_get_random(data, len) == 0;
|
||||
bool random_bytes(uint8_t *data, size_t len) { return os_get_random(data, len) == 0; }
|
||||
#elif defined(USE_RP2040)
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
while (len-- != 0) {
|
||||
uint8_t result = 0;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
|
@ -228,10 +231,14 @@ bool random_bytes(uint8_t *data, size_t len) {
|
|||
*data++ = result;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#elif defined(USE_LIBRETINY)
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
lt_rand_bytes(data, len);
|
||||
return true;
|
||||
}
|
||||
#elif defined(USE_HOST)
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
FILE *fp = fopen("/dev/urandom", "r");
|
||||
if (fp == nullptr) {
|
||||
ESP_LOGW(TAG, "Could not open /dev/urandom, errno=%d", errno);
|
||||
|
@ -244,10 +251,8 @@ bool random_bytes(uint8_t *data, size_t len) {
|
|||
}
|
||||
fclose(fp);
|
||||
return true;
|
||||
#else
|
||||
#error "No random source available for this configuration."
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
// Strings
|
||||
|
||||
|
@ -619,11 +624,13 @@ void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green
|
|||
#if defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_HOST)
|
||||
// ESP8266 doesn't have mutexes, but that shouldn't be an issue as it's single-core and non-preemptive OS.
|
||||
Mutex::Mutex() {}
|
||||
Mutex::~Mutex() {}
|
||||
void Mutex::lock() {}
|
||||
bool Mutex::try_lock() { return true; }
|
||||
void Mutex::unlock() {}
|
||||
#elif defined(USE_ESP32) || defined(USE_LIBRETINY)
|
||||
Mutex::Mutex() { handle_ = xSemaphoreCreateMutex(); }
|
||||
Mutex::~Mutex() {}
|
||||
void Mutex::lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); }
|
||||
bool Mutex::try_lock() { return xSemaphoreTake(this->handle_, 0) == pdTRUE; }
|
||||
void Mutex::unlock() { xSemaphoreGive(this->handle_); }
|
||||
|
@ -657,11 +664,13 @@ void HighFrequencyLoopRequester::stop() {
|
|||
}
|
||||
bool HighFrequencyLoopRequester::is_high_frequency() { return num_requests > 0; }
|
||||
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
#if defined(USE_HOST)
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
static const uint8_t esphome_host_mac_address[6] = USE_ESPHOME_HOST_MAC_ADDRESS;
|
||||
memcpy(mac, esphome_host_mac_address, sizeof(esphome_host_mac_address));
|
||||
}
|
||||
#elif defined(USE_ESP32)
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
#if defined(CONFIG_SOC_IEEE802154_SUPPORTED)
|
||||
// When CONFIG_SOC_IEEE802154_SUPPORTED is defined, esp_efuse_mac_get_default
|
||||
// returns the 802.15.4 EUI-64 address, so we read directly from eFuse instead.
|
||||
|
@ -677,16 +686,20 @@ void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parame
|
|||
esp_efuse_mac_get_default(mac);
|
||||
}
|
||||
#endif
|
||||
#elif defined(USE_ESP8266)
|
||||
wifi_get_macaddr(STATION_IF, mac);
|
||||
#elif defined(USE_RP2040) && defined(USE_WIFI)
|
||||
WiFi.macAddress(mac);
|
||||
#elif defined(USE_LIBRETINY)
|
||||
WiFi.macAddress(mac);
|
||||
#else
|
||||
// this should be an error, but that messes with CI checks. #error No mac address method defined
|
||||
#endif
|
||||
}
|
||||
#elif defined(USE_ESP8266)
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
wifi_get_macaddr(STATION_IF, mac);
|
||||
}
|
||||
#elif defined(USE_RP2040) && defined(USE_WIFI)
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
WiFi.macAddress(mac);
|
||||
}
|
||||
#elif defined(USE_LIBRETINY)
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
WiFi.macAddress(mac);
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string get_mac_address() {
|
||||
uint8_t mac[6];
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
|
||||
#include "esphome/core/optional.h"
|
||||
|
||||
|
@ -545,6 +546,7 @@ class Mutex {
|
|||
public:
|
||||
Mutex();
|
||||
Mutex(const Mutex &) = delete;
|
||||
~Mutex();
|
||||
void lock();
|
||||
bool try_lock();
|
||||
void unlock();
|
||||
|
@ -554,6 +556,8 @@ class Mutex {
|
|||
private:
|
||||
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
|
||||
SemaphoreHandle_t handle_;
|
||||
#else
|
||||
void *handle_; // d-pointer to store private data on new platforms
|
||||
#endif
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue