68 otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
69 otInstance *instance =
nullptr;
73 otBorderRouterConfig config;
74 if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &config) != OT_ERROR_NONE) {
78 const otIp6Prefix *omr_prefix = &config.mPrefix;
79 const otNetifAddress *unicast_addresses = otIp6GetUnicastAddresses(instance);
80 for (
const otNetifAddress *addr = unicast_addresses; addr; addr = addr->mNext) {
81 const otIp6Address *local_ip = &addr->mAddress;
82 if (otIp6PrefixMatch(&omr_prefix->mPrefix, local_ip)) {
89void srp_callback(otError err,
const otSrpClientHostInfo *host_info,
const otSrpClientService *services,
90 const otSrpClientService *removed_services,
void *context) {
92 ESP_LOGW(TAG,
"SRP client reported an error: %s", otThreadErrorToString(err));
93 for (
const otSrpClientHostInfo *host = host_info; host; host =
nullptr) {
94 ESP_LOGW(TAG,
" Host: %s", host->mName);
96 for (
const otSrpClientService *service = services; service; service = service->mNext) {
97 ESP_LOGW(TAG,
" Service: %s", service->mName);
111 otSrpClientSetCallback(instance,
srp_callback,
nullptr);
115 char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &size);
117 uint16_t host_name_len = host_name.size();
118 if (host_name_len > size) {
119 ESP_LOGW(TAG,
"Hostname is too long, choose a shorter project name");
122 memset(existing_host_name, 0, size);
123 memcpy(existing_host_name, host_name.c_str(), host_name_len);
125 error = otSrpClientSetHostName(instance, existing_host_name);
127 ESP_LOGW(TAG,
"Could not set host name");
131 error = otSrpClientEnableAutoHostAddress(instance);
133 ESP_LOGW(TAG,
"Could not enable auto host address");
140 ESP_LOGD(TAG,
"Setting up SRP services. count = %d\n", this->
mdns_services_.size());
142 otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
144 ESP_LOGW(TAG,
"Failed to allocate service entry");
149 char *
string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &size);
150 std::string full_service = service.service_type +
"." + service.proto;
151 if (full_service.size() > size) {
152 ESP_LOGW(TAG,
"Service name too long: %s", full_service.c_str());
155 memcpy(
string, full_service.c_str(), full_service.size() + 1);
158 string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &size);
159 if (host_name_len > size) {
160 ESP_LOGW(TAG,
"Instance name too long: %s", host_name.c_str());
163 memset(
string, 0, size);
164 memcpy(
string, host_name.c_str(), host_name_len);
169 otDnsTxtEntry *txt_entries =
170 reinterpret_cast<otDnsTxtEntry *
>(this->
pool_alloc_(
sizeof(otDnsTxtEntry) * service.txt_records.size()));
172 entry->mService.mNumTxtEntries = service.txt_records.size();
173 for (
size_t i = 0; i < service.txt_records.size(); i++) {
174 const auto &txt = service.txt_records[i];
176 txt_entries[i].mKey = strdup(txt.key.c_str());
177 txt_entries[i].mValue =
reinterpret_cast<const uint8_t *
>(strdup(value.c_str()));
178 txt_entries[i].mValueLength = value.size();
180 entry->mService.mTxtEntries = txt_entries;
181 entry->mService.mNumTxtEntries = service.txt_records.size();
184 error = otSrpClientAddService(instance, &entry->mService);
185 if (error != OT_ERROR_NONE) {
186 ESP_LOGW(TAG,
"Failed to add service: %s", otThreadErrorToString(error));
188 ESP_LOGD(TAG,
"Added service: %s", full_service.c_str());
192 ESP_LOGD(TAG,
"Finished SRP setup");