59 otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
60 otInstance *instance =
nullptr;
64 otBorderRouterConfig config;
65 if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &config) != OT_ERROR_NONE) {
69 const otIp6Prefix *omr_prefix = &config.mPrefix;
70 const otNetifAddress *unicast_addresses = otIp6GetUnicastAddresses(instance);
71 for (
const otNetifAddress *addr = unicast_addresses; addr; addr = addr->mNext) {
72 const otIp6Address *local_ip = &addr->mAddress;
73 if (otIp6PrefixMatch(&omr_prefix->mPrefix, local_ip)) {
80void srp_callback(otError err,
const otSrpClientHostInfo *host_info,
const otSrpClientService *services,
81 const otSrpClientService *removed_services,
void *context) {
83 ESP_LOGW(TAG,
"SRP client reported an error: %s", otThreadErrorToString(err));
84 for (
const otSrpClientHostInfo *host = host_info; host; host =
nullptr) {
85 ESP_LOGW(TAG,
" Host: %s", host->mName);
87 for (
const otSrpClientService *service = services; service; service = service->mNext) {
88 ESP_LOGW(TAG,
" Service: %s", service->mName);
102 otSrpClientSetCallback(instance,
srp_callback,
nullptr);
106 char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &size);
108 uint16_t host_name_len = host_name.size();
109 if (host_name_len > size) {
110 ESP_LOGW(TAG,
"Hostname is too long, choose a shorter project name");
113 memset(existing_host_name, 0, size);
114 memcpy(existing_host_name, host_name.c_str(), host_name_len);
116 error = otSrpClientSetHostName(instance, existing_host_name);
118 ESP_LOGW(TAG,
"Could not set host name");
122 error = otSrpClientEnableAutoHostAddress(instance);
124 ESP_LOGW(TAG,
"Could not enable auto host address");
131 ESP_LOGD(TAG,
"Setting up SRP services. count = %d\n", this->
mdns_services_.size());
133 otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
135 ESP_LOGW(TAG,
"Failed to allocate service entry");
140 char *
string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &size);
141 std::string full_service = service.service_type +
"." + service.proto;
142 if (full_service.size() > size) {
143 ESP_LOGW(TAG,
"Service name too long: %s", full_service.c_str());
146 memcpy(
string, full_service.c_str(), full_service.size() + 1);
149 string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &size);
150 if (host_name_len > size) {
151 ESP_LOGW(TAG,
"Instance name too long: %s", host_name.c_str());
154 memset(
string, 0, size);
155 memcpy(
string, host_name.c_str(), host_name_len);
160 otDnsTxtEntry *txt_entries =
161 reinterpret_cast<otDnsTxtEntry *
>(this->
pool_alloc_(
sizeof(otDnsTxtEntry) * service.txt_records.size()));
163 entry->mService.mNumTxtEntries = service.txt_records.size();
164 for (
size_t i = 0; i < service.txt_records.size(); i++) {
165 const auto &txt = service.txt_records[i];
167 txt_entries[i].mKey = strdup(txt.key.c_str());
168 txt_entries[i].mValue =
reinterpret_cast<const uint8_t *
>(strdup(value.c_str()));
169 txt_entries[i].mValueLength = value.size();
171 entry->mService.mTxtEntries = txt_entries;
172 entry->mService.mNumTxtEntries = service.txt_records.size();
175 error = otSrpClientAddService(instance, &entry->mService);
176 if (error != OT_ERROR_NONE) {
177 ESP_LOGW(TAG,
"Failed to add service: %s", otThreadErrorToString(error));
179 ESP_LOGD(TAG,
"Added service: %s", full_service.c_str());