mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 17:54:13 +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 <cstdarg>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <strings.h>
|
||||||
|
|
||||||
#ifdef USE_HOST
|
#ifdef USE_HOST
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
@ -188,37 +189,39 @@ uint32_t fnv1_hash(const std::string &str) {
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t random_uint32() {
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
return esp_random();
|
uint32_t random_uint32() { return esp_random(); }
|
||||||
#elif defined(USE_ESP8266)
|
#elif defined(USE_ESP8266)
|
||||||
return os_random();
|
uint32_t random_uint32() { return os_random(); }
|
||||||
#elif defined(USE_RP2040)
|
#elif defined(USE_RP2040)
|
||||||
|
uint32_t random_uint32() {
|
||||||
uint32_t result = 0;
|
uint32_t result = 0;
|
||||||
for (uint8_t i = 0; i < 32; i++) {
|
for (uint8_t i = 0; i < 32; i++) {
|
||||||
result <<= 1;
|
result <<= 1;
|
||||||
result |= rosc_hw->randombit;
|
result |= rosc_hw->randombit;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
}
|
||||||
#elif defined(USE_LIBRETINY)
|
#elif defined(USE_LIBRETINY)
|
||||||
return rand();
|
uint32_t random_uint32() { return rand(); }
|
||||||
#elif defined(USE_HOST)
|
#elif defined(USE_HOST)
|
||||||
|
uint32_t random_uint32() {
|
||||||
std::random_device dev;
|
std::random_device dev;
|
||||||
std::mt19937 rng(dev());
|
std::mt19937 rng(dev());
|
||||||
std::uniform_int_distribution<uint32_t> dist(0, std::numeric_limits<uint32_t>::max());
|
std::uniform_int_distribution<uint32_t> dist(0, std::numeric_limits<uint32_t>::max());
|
||||||
return dist(rng);
|
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); }
|
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
|
#ifdef USE_ESP32
|
||||||
|
bool random_bytes(uint8_t *data, size_t len) {
|
||||||
esp_fill_random(data, len);
|
esp_fill_random(data, len);
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
#elif defined(USE_ESP8266)
|
#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)
|
#elif defined(USE_RP2040)
|
||||||
|
bool random_bytes(uint8_t *data, size_t len) {
|
||||||
while (len-- != 0) {
|
while (len-- != 0) {
|
||||||
uint8_t result = 0;
|
uint8_t result = 0;
|
||||||
for (uint8_t i = 0; i < 8; i++) {
|
for (uint8_t i = 0; i < 8; i++) {
|
||||||
|
@ -228,10 +231,14 @@ bool random_bytes(uint8_t *data, size_t len) {
|
||||||
*data++ = result;
|
*data++ = result;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
#elif defined(USE_LIBRETINY)
|
#elif defined(USE_LIBRETINY)
|
||||||
|
bool random_bytes(uint8_t *data, size_t len) {
|
||||||
lt_rand_bytes(data, len);
|
lt_rand_bytes(data, len);
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
#elif defined(USE_HOST)
|
#elif defined(USE_HOST)
|
||||||
|
bool random_bytes(uint8_t *data, size_t len) {
|
||||||
FILE *fp = fopen("/dev/urandom", "r");
|
FILE *fp = fopen("/dev/urandom", "r");
|
||||||
if (fp == nullptr) {
|
if (fp == nullptr) {
|
||||||
ESP_LOGW(TAG, "Could not open /dev/urandom, errno=%d", errno);
|
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);
|
fclose(fp);
|
||||||
return true;
|
return true;
|
||||||
#else
|
|
||||||
#error "No random source available for this configuration."
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// Strings
|
// 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)
|
#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.
|
// 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() {}
|
||||||
|
Mutex::~Mutex() {}
|
||||||
void Mutex::lock() {}
|
void Mutex::lock() {}
|
||||||
bool Mutex::try_lock() { return true; }
|
bool Mutex::try_lock() { return true; }
|
||||||
void Mutex::unlock() {}
|
void Mutex::unlock() {}
|
||||||
#elif defined(USE_ESP32) || defined(USE_LIBRETINY)
|
#elif defined(USE_ESP32) || defined(USE_LIBRETINY)
|
||||||
Mutex::Mutex() { handle_ = xSemaphoreCreateMutex(); }
|
Mutex::Mutex() { handle_ = xSemaphoreCreateMutex(); }
|
||||||
|
Mutex::~Mutex() {}
|
||||||
void Mutex::lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); }
|
void Mutex::lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); }
|
||||||
bool Mutex::try_lock() { return xSemaphoreTake(this->handle_, 0) == pdTRUE; }
|
bool Mutex::try_lock() { return xSemaphoreTake(this->handle_, 0) == pdTRUE; }
|
||||||
void Mutex::unlock() { xSemaphoreGive(this->handle_); }
|
void Mutex::unlock() { xSemaphoreGive(this->handle_); }
|
||||||
|
@ -657,11 +664,13 @@ void HighFrequencyLoopRequester::stop() {
|
||||||
}
|
}
|
||||||
bool HighFrequencyLoopRequester::is_high_frequency() { return num_requests > 0; }
|
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)
|
#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;
|
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));
|
memcpy(mac, esphome_host_mac_address, sizeof(esphome_host_mac_address));
|
||||||
|
}
|
||||||
#elif defined(USE_ESP32)
|
#elif defined(USE_ESP32)
|
||||||
|
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||||
#if defined(CONFIG_SOC_IEEE802154_SUPPORTED)
|
#if defined(CONFIG_SOC_IEEE802154_SUPPORTED)
|
||||||
// When CONFIG_SOC_IEEE802154_SUPPORTED is defined, esp_efuse_mac_get_default
|
// 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.
|
// 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);
|
esp_efuse_mac_get_default(mac);
|
||||||
}
|
}
|
||||||
#endif
|
#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() {
|
std::string get_mac_address() {
|
||||||
uint8_t mac[6];
|
uint8_t mac[6];
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
#include "esphome/core/optional.h"
|
#include "esphome/core/optional.h"
|
||||||
|
|
||||||
|
@ -545,6 +546,7 @@ class Mutex {
|
||||||
public:
|
public:
|
||||||
Mutex();
|
Mutex();
|
||||||
Mutex(const Mutex &) = delete;
|
Mutex(const Mutex &) = delete;
|
||||||
|
~Mutex();
|
||||||
void lock();
|
void lock();
|
||||||
bool try_lock();
|
bool try_lock();
|
||||||
void unlock();
|
void unlock();
|
||||||
|
@ -554,6 +556,8 @@ class Mutex {
|
||||||
private:
|
private:
|
||||||
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
|
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
|
||||||
SemaphoreHandle_t handle_;
|
SemaphoreHandle_t handle_;
|
||||||
|
#else
|
||||||
|
void *handle_; // d-pointer to store private data on new platforms
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue