65 otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
66 otInstance *instance =
nullptr;
68 instance =
lock.get_instance();
70 otBorderRouterConfig config;
71 if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &config) != OT_ERROR_NONE) {
75 const otIp6Prefix *omr_prefix = &config.mPrefix;
76 const otNetifAddress *unicast_addresses = otIp6GetUnicastAddresses(instance);
77 for (
const otNetifAddress *addr = unicast_addresses; addr; addr = addr->mNext) {
78 const otIp6Address *local_ip = &addr->mAddress;
79 if (otIp6PrefixMatch(&omr_prefix->mPrefix, local_ip)) {
92 const otSrpClientService *services,
93 const otSrpClientService *removed_services,
void *context) {
95 ESP_LOGW(TAG,
"SRP client reported an error: %s", otThreadErrorToString(err));
96 for (
const otSrpClientHostInfo *host = host_info; host; host =
nullptr) {
97 ESP_LOGW(TAG,
" Host: %s", host->mName);
99 for (
const otSrpClientService *service = services; service; service = service->mNext) {
100 ESP_LOGW(TAG,
" Service: %s", service->mName);
110 const otSrpClientService *services,
111 const otSrpClientService *removed_services,
void *context) {
113 if (err == OT_ERROR_NONE && removed_services != NULL && host_info != NULL &&
114 host_info->mState == OT_SRP_CLIENT_ITEM_STATE_REMOVED) {
115 ESP_LOGD(TAG,
"Successful Removal SRP Host and Services");
116 }
else if (err != OT_ERROR_NONE) {
118 ESP_LOGW(TAG,
"SRP client event/error: %s", otThreadErrorToString(err));
126 otInstance *instance =
lock.get_instance();
132 char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &
size);
134 uint16_t host_name_len = host_name.
size();
135 if (host_name_len >=
size) {
136 ESP_LOGW(TAG,
"Hostname is too long, choose a shorter project name");
139 memset(existing_host_name, 0,
size);
140 memcpy(existing_host_name, host_name.c_str(), host_name_len);
142 error = otSrpClientSetHostName(instance, existing_host_name);
144 ESP_LOGW(TAG,
"Could not set host name");
148 error = otSrpClientEnableAutoHostAddress(instance);
150 ESP_LOGW(TAG,
"Could not enable auto host address");
156 ESP_LOGD(TAG,
"Setting up SRP services. count = %d\n", mdns_services.size());
157 for (
const auto &service : mdns_services) {
158 otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
160 ESP_LOGW(TAG,
"Failed to allocate service entry");
165 char *
string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &
size);
166 std::string full_service = std::string(MDNS_STR_ARG(service.service_type)) +
"." + MDNS_STR_ARG(service.proto);
167 if (full_service.size() >=
size) {
168 ESP_LOGW(TAG,
"Service name too long: %s", full_service.c_str());
171 memcpy(
string, full_service.c_str(), full_service.size() + 1);
174 string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &
size);
175 if (host_name_len >=
size) {
176 ESP_LOGW(TAG,
"Instance name too long: %s", host_name.c_str());
179 memset(
string, 0,
size);
180 memcpy(
string, host_name.c_str(), host_name_len);
183 entry->mService.mPort = service.port.value();
185 otDnsTxtEntry *txt_entries =
186 reinterpret_cast<otDnsTxtEntry *
>(this->
pool_alloc_(
sizeof(otDnsTxtEntry) * service.txt_records.size()));
188 entry->mService.mNumTxtEntries = service.txt_records.size();
189 for (
size_t i = 0; i < service.txt_records.size(); i++) {
190 const auto &txt = service.txt_records[i];
193 const char *value_str = MDNS_STR_ARG(txt.value);
194 txt_entries[i].mKey = MDNS_STR_ARG(txt.key);
196 txt_entries[i].mValue =
reinterpret_cast<const uint8_t *
>(strdup(value_str));
197 txt_entries[i].mValueLength = strlen(value_str);
201 size_t value_len = strlen(value_str);
202 char *value_copy =
reinterpret_cast<char *
>(this->
pool_alloc_(value_len + 1));
203 memcpy(value_copy, value_str, value_len + 1);
204 txt_entries[i].mValue =
reinterpret_cast<const uint8_t *
>(value_copy);
205 txt_entries[i].mValueLength = value_len;
208 entry->mService.mTxtEntries = txt_entries;
209 entry->mService.mNumTxtEntries = service.txt_records.size();
212 error = otSrpClientAddService(instance, &entry->mService);
213 if (error != OT_ERROR_NONE) {
214 ESP_LOGW(TAG,
"Failed to add service: %s", otThreadErrorToString(error));
216 ESP_LOGD(TAG,
"Added service: %s", full_service.c_str());
220 ESP_LOGD(TAG,
"Finished SRP setup");
270 otLinkModeConfig link_mode_config{};
271#if CONFIG_OPENTHREAD_FTD
272 link_mode_config.mRxOnWhenIdle =
true;
273 link_mode_config.mDeviceType =
true;
274 link_mode_config.mNetworkData =
true;
275#elif CONFIG_OPENTHREAD_MTD
277 if (otLinkSetPollPeriod(instance, this->
poll_period_) != OT_ERROR_NONE) {
278 ESP_LOGE(TAG,
"Failed to set pollperiod");
280 ESP_LOGD(TAG,
"Link Polling Period: %" PRIu32, otLinkGetPollPeriod(instance));
282 link_mode_config.mRxOnWhenIdle = this->
poll_period_ == 0;
283 link_mode_config.mDeviceType =
false;
284 link_mode_config.mNetworkData =
false;
287 if (otThreadSetLinkMode(instance, link_mode_config) != OT_ERROR_NONE) {
288 ESP_LOGE(TAG,
"Failed to set linkmode");
290#ifdef ESPHOME_LOG_HAS_DEBUG
291 link_mode_config = otThreadGetLinkMode(instance);
292 ESP_LOGD(TAG,
"Link Mode Device Type: %s, Network Data: %s, RX On When Idle: %s",
293 TRUEFALSE(link_mode_config.mDeviceType), TRUEFALSE(link_mode_config.mNetworkData),
294 TRUEFALSE(link_mode_config.mRxOnWhenIdle));