Enable networking and some other components on host platform (#6114)

This commit is contained in:
Clyde Stubbs 2024-01-19 12:10:23 +11:00 committed by GitHub
parent 6a6a70f1e5
commit 8267b3274c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 29 additions and 3 deletions

View file

@ -6,6 +6,7 @@ from esphome.const import (
PLATFORM_HOST, PLATFORM_HOST,
) )
from esphome.core import CORE from esphome.core import CORE
from esphome.helpers import IS_MACOS
import esphome.config_validation as cv import esphome.config_validation as cv
import esphome.codegen as cg import esphome.codegen as cg
@ -14,7 +15,6 @@ from .const import KEY_HOST
# force import gpio to register pin schema # force import gpio to register pin schema
from .gpio import host_pin_to_code # noqa from .gpio import host_pin_to_code # noqa
CODEOWNERS = ["@esphome/core"] CODEOWNERS = ["@esphome/core"]
AUTO_LOAD = ["network"] AUTO_LOAD = ["network"]
@ -35,5 +35,9 @@ CONFIG_SCHEMA = cv.All(
async def to_code(config): async def to_code(config):
cg.add_build_flag("-DUSE_HOST") cg.add_build_flag("-DUSE_HOST")
cg.add_build_flag("-std=c++17")
cg.add_build_flag("-lsodium")
if IS_MACOS:
cg.add_build_flag("-L/opt/homebrew/lib")
cg.add_define("ESPHOME_BOARD", "host") cg.add_define("ESPHOME_BOARD", "host")
cg.add_platformio_option("platform", "platformio/native") cg.add_platformio_option("platform", "platformio/native")

View file

@ -14,6 +14,13 @@
#include <IPAddress.h> #include <IPAddress.h>
#endif /* USE_ADRDUINO */ #endif /* USE_ADRDUINO */
#ifdef USE_HOST
#include <arpa/inet.h>
using ip_addr_t = in_addr;
using ip4_addr_t = in_addr;
#define ipaddr_aton(x, y) inet_aton((x), (y))
#endif
#if USE_ESP32_FRAMEWORK_ARDUINO #if USE_ESP32_FRAMEWORK_ARDUINO
#define arduino_ns Arduino_h #define arduino_ns Arduino_h
#elif USE_LIBRETINY #elif USE_LIBRETINY
@ -32,6 +39,14 @@ namespace network {
struct IPAddress { struct IPAddress {
public: public:
#ifdef USE_HOST
IPAddress() { ip_addr_.s_addr = 0; }
IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
this->ip_addr_.s_addr = htonl((first << 24) | (second << 16) | (third << 8) | fourth);
}
IPAddress(const std::string &in_address) { inet_aton(in_address.c_str(), &ip_addr_); }
IPAddress(const ip_addr_t *other_ip) { ip_addr_ = *other_ip; }
#else
IPAddress() { ip_addr_set_zero(&ip_addr_); } IPAddress() { ip_addr_set_zero(&ip_addr_); }
IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) { IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
IP_ADDR4(&ip_addr_, first, second, third, fourth); IP_ADDR4(&ip_addr_, first, second, third, fourth);
@ -107,6 +122,7 @@ struct IPAddress {
} }
return *this; return *this;
} }
#endif
protected: protected:
ip_addr_t ip_addr_; ip_addr_t ip_addr_;

View file

@ -87,7 +87,7 @@ class BSDSocketImpl : public Socket {
int listen(int backlog) override { return ::listen(fd_, backlog); } int listen(int backlog) override { return ::listen(fd_, backlog); }
ssize_t read(void *buf, size_t len) override { return ::read(fd_, buf, len); } ssize_t read(void *buf, size_t len) override { return ::read(fd_, buf, len); }
ssize_t recvfrom(void *buf, size_t len, sockaddr *addr, socklen_t *addr_len) override { ssize_t recvfrom(void *buf, size_t len, sockaddr *addr, socklen_t *addr_len) override {
#if defined(USE_ESP32) #if defined(USE_ESP32) || defined(USE_HOST)
return ::recvfrom(this->fd_, buf, len, 0, addr, addr_len); return ::recvfrom(this->fd_, buf, len, 0, addr, addr_len);
#else #else
return ::lwip_recvfrom(this->fd_, buf, len, 0, addr, addr_len); return ::lwip_recvfrom(this->fd_, buf, len, 0, addr, addr_len);

View file

@ -169,7 +169,7 @@ float Component::get_actual_setup_priority() const {
void Component::set_setup_priority(float priority) { this->setup_priority_override_ = priority; } void Component::set_setup_priority(float priority) { this->setup_priority_override_ = priority; }
bool Component::has_overridden_loop() const { bool Component::has_overridden_loop() const {
#ifdef CLANG_TIDY #if defined(USE_HOST) || defined(CLANG_TIDY)
bool loop_overridden = true; bool loop_overridden = true;
bool call_loop_overridden = true; bool call_loop_overridden = true;
#else #else

View file

@ -3,6 +3,7 @@ from contextlib import suppress
import logging import logging
import os import os
import platform
from pathlib import Path from pathlib import Path
from typing import Union from typing import Union
import tempfile import tempfile
@ -11,6 +12,10 @@ import re
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
IS_MACOS = platform.system() == "Darwin"
IS_WINDOWS = platform.system() == "Windows"
IS_LINUX = platform.system() == "Linux"
def ensure_unique_string(preferred_string, current_strings): def ensure_unique_string(preferred_string, current_strings):
test_string = preferred_string test_string = preferred_string

View file

@ -388,3 +388,4 @@ lib_deps =
build_flags = build_flags =
${common.build_flags} ${common.build_flags}
-DUSE_HOST -DUSE_HOST
-std=c++17