mirror of
https://github.com/esphome/esphome.git
synced 2025-02-24 20:12:30 +01:00
openthread: cleanup
This commit is contained in:
parent
2712350121
commit
958ab7fdd8
3 changed files with 13 additions and 31 deletions
|
@ -10,7 +10,7 @@ CODEOWNERS = ["@mrene"]
|
||||||
AUTO_LOAD = ["network"]
|
AUTO_LOAD = ["network"]
|
||||||
|
|
||||||
# Wi-fi / Bluetooth / Thread coexistence isn't implemented at this time
|
# Wi-fi / Bluetooth / Thread coexistence isn't implemented at this time
|
||||||
# TODO: Doesn't conflict with wifi if you're using another ESP as an RCP (radio coprocessor)
|
# TODO: Doesn't conflict with wifi if you're using another ESP as an RCP (radio coprocessor), but this isn't implemented yet
|
||||||
CONFLICTS_WITH = ["wifi"]
|
CONFLICTS_WITH = ["wifi"]
|
||||||
DEPENDENCIES = ["esp32"]
|
DEPENDENCIES = ["esp32"]
|
||||||
|
|
||||||
|
|
|
@ -75,26 +75,19 @@ std::optional<otIp6Address> OpenThreadComponent::get_omr_address_(std::optional<
|
||||||
vTaskDelay(100);
|
vTaskDelay(100);
|
||||||
lock = OpenThreadLockGuard::try_acquire(portMAX_DELAY);
|
lock = OpenThreadLockGuard::try_acquire(portMAX_DELAY);
|
||||||
if (!lock) {
|
if (!lock) {
|
||||||
ESP_LOGW("OT SRP", "Could not re-acquire lock");
|
ESP_LOGW(TAG, "Could not re-acquire lock");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const otIp6Prefix *omrPrefix = &aConfig.mPrefix;
|
const otIp6Prefix *omrPrefix = &aConfig.mPrefix;
|
||||||
|
|
||||||
char addressAsString[40];
|
|
||||||
otIp6PrefixToString(omrPrefix, addressAsString, 40);
|
|
||||||
ESP_LOGW("OT SRP", "USING omr prefix %s", addressAsString);
|
|
||||||
|
|
||||||
const otNetifAddress *unicastAddrs = otIp6GetUnicastAddresses(instance);
|
const otNetifAddress *unicastAddrs = otIp6GetUnicastAddresses(instance);
|
||||||
for (const otNetifAddress *addr = unicastAddrs; addr; addr = addr->mNext) {
|
for (const otNetifAddress *addr = unicastAddrs; addr; addr = addr->mNext) {
|
||||||
const otIp6Address *localIp = &addr->mAddress;
|
const otIp6Address *localIp = &addr->mAddress;
|
||||||
if (otIp6PrefixMatch(&omrPrefix->mPrefix, localIp)) {
|
if (otIp6PrefixMatch(&omrPrefix->mPrefix, localIp)) {
|
||||||
otIp6AddressToString(localIp, addressAsString, 40);
|
|
||||||
ESP_LOGW("OT SRP", "USING %s for SRP address", addressAsString);
|
|
||||||
return *localIp;
|
return *localIp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ESP_LOGW("OT SRP", "Could not find the OMR address");
|
ESP_LOGW(TAG, "Could not find the OMR address");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,30 +101,20 @@ void OpenThreadComponent::srp_setup_() {
|
||||||
char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &size);
|
char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &size);
|
||||||
uint16_t len = this->host_name_.size();
|
uint16_t len = this->host_name_.size();
|
||||||
if (len > size) {
|
if (len > size) {
|
||||||
ESP_LOGW("OT SRP", "Hostname is too long, choose a shorter project name");
|
ESP_LOGW(TAG, "Hostname is too long, choose a shorter project name");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
memcpy(existing_host_name, this->host_name_.c_str(), len + 1);
|
memcpy(existing_host_name, this->host_name_.c_str(), len + 1);
|
||||||
|
|
||||||
error = otSrpClientSetHostName(instance, existing_host_name);
|
error = otSrpClientSetHostName(instance, existing_host_name);
|
||||||
if (error != 0) {
|
if (error != 0) {
|
||||||
ESP_LOGW("OT SRP", "Could not set host name with srp server");
|
ESP_LOGW(TAG, "Could not set host name with srp server");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t arrayLength;
|
error = otSrpClientEnableAutoHostAddress(instance);
|
||||||
otIp6Address *hostAddressArray = otSrpClientBuffersGetHostAddressesArray(instance, &arrayLength);
|
|
||||||
|
|
||||||
const std::optional<otIp6Address> localIp = this->get_omr_address_(lock);
|
|
||||||
if (!localIp) {
|
|
||||||
ESP_LOGW("OT SRP", "Could not get local IP address");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
memcpy(hostAddressArray, &*localIp, sizeof(localIp));
|
|
||||||
|
|
||||||
error = otSrpClientSetHostAddresses(instance, hostAddressArray, 1);
|
|
||||||
if (error != 0) {
|
if (error != 0) {
|
||||||
ESP_LOGW("OT SRP", "Could not set ip address with srp server");
|
ESP_LOGW(TAG, "Could not enable auto host address");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,7 +124,7 @@ void OpenThreadComponent::srp_setup_() {
|
||||||
for (const auto &service : this->mdns_services_) {
|
for (const auto &service : this->mdns_services_) {
|
||||||
otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
|
otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
ESP_LOGW("OT SRP", "Failed to allocate service entry");
|
ESP_LOGW(TAG, "Failed to allocate service entry");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,7 +132,7 @@ void OpenThreadComponent::srp_setup_() {
|
||||||
char *string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &size);
|
char *string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &size);
|
||||||
std::string full_service = service.service_type + "." + service.proto;
|
std::string full_service = service.service_type + "." + service.proto;
|
||||||
if (full_service.size() > size) {
|
if (full_service.size() > size) {
|
||||||
ESP_LOGW("OT SRP", "Service name too long: %s", full_service.c_str());
|
ESP_LOGW(TAG, "Service name too long: %s", full_service.c_str());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
memcpy(string, full_service.c_str(), full_service.size() + 1);
|
memcpy(string, full_service.c_str(), full_service.size() + 1);
|
||||||
|
@ -157,7 +140,7 @@ void OpenThreadComponent::srp_setup_() {
|
||||||
// Set instance name (using host_name)
|
// Set instance name (using host_name)
|
||||||
string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &size);
|
string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &size);
|
||||||
if (this->host_name_.size() > size) {
|
if (this->host_name_.size() > size) {
|
||||||
ESP_LOGW("OT SRP", "Instance name too long: %s", this->host_name_.c_str());
|
ESP_LOGW(TAG, "Instance name too long: %s", this->host_name_.c_str());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
memcpy(string, this->host_name_.c_str(), this->host_name_.size() + 1);
|
memcpy(string, this->host_name_.c_str(), this->host_name_.size() + 1);
|
||||||
|
@ -181,7 +164,7 @@ void OpenThreadComponent::srp_setup_() {
|
||||||
// Add service
|
// Add service
|
||||||
error = otSrpClientAddService(instance, &entry->mService);
|
error = otSrpClientAddService(instance, &entry->mService);
|
||||||
if (error != OT_ERROR_NONE) {
|
if (error != OT_ERROR_NONE) {
|
||||||
ESP_LOGW("OT SRP", "Failed to add service: %s", otThreadErrorToString(error));
|
ESP_LOGW(TAG, "Failed to add service: %s", otThreadErrorToString(error));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,12 +28,11 @@ namespace esphome {
|
||||||
namespace openthread {
|
namespace openthread {
|
||||||
|
|
||||||
void OpenThreadComponent::setup() {
|
void OpenThreadComponent::setup() {
|
||||||
ESP_LOGI("openthread", "Setting up OpenThread...");
|
ESP_LOGI(TAG, "Setting up OpenThread...");
|
||||||
// Used eventfds:
|
// Used eventfds:
|
||||||
// * netif
|
// * netif
|
||||||
// * ot task queue
|
// * ot task queue
|
||||||
// * radio driver
|
// * radio driver
|
||||||
// TODO: Does anything else in esphome set this up?
|
|
||||||
esp_vfs_eventfd_config_t eventfd_config = {
|
esp_vfs_eventfd_config_t eventfd_config = {
|
||||||
.max_fds = 3,
|
.max_fds = 3,
|
||||||
};
|
};
|
||||||
|
@ -56,7 +55,7 @@ void OpenThreadComponent::setup() {
|
||||||
},
|
},
|
||||||
"ot_srp_setup", 10240, this, 5, nullptr);
|
"ot_srp_setup", 10240, this, 5, nullptr);
|
||||||
|
|
||||||
ESP_LOGI("openthread", "OpenThread started");
|
ESP_LOGI(TAG, "OpenThread started");
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t *config) {
|
static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t *config) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue