57 otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
58 otInstance *instance =
nullptr;
62 otBorderRouterConfig config;
63 if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &config) != OT_ERROR_NONE) {
67 const otIp6Prefix *omr_prefix = &config.mPrefix;
68 const otNetifAddress *unicast_addresses = otIp6GetUnicastAddresses(instance);
69 for (
const otNetifAddress *addr = unicast_addresses; addr; addr = addr->mNext) {
70 const otIp6Address *local_ip = &addr->mAddress;
71 if (otIp6PrefixMatch(&omr_prefix->mPrefix, local_ip)) {
84 const otSrpClientService *services,
85 const otSrpClientService *removed_services,
void *context) {
87 ESP_LOGW(TAG,
"SRP client reported an error: %s", otThreadErrorToString(err));
88 for (
const otSrpClientHostInfo *host = host_info; host; host =
nullptr) {
89 ESP_LOGW(TAG,
" Host: %s", host->mName);
91 for (
const otSrpClientService *service = services; service; service = service->mNext) {
92 ESP_LOGW(TAG,
" Service: %s", service->mName);
102 const otSrpClientService *services,
103 const otSrpClientService *removed_services,
void *context) {
105 if (err == OT_ERROR_NONE && removed_services != NULL && host_info != NULL &&
106 host_info->mState == OT_SRP_CLIENT_ITEM_STATE_REMOVED) {
107 ESP_LOGD(TAG,
"Successful Removal SRP Host and Services");
108 }
else if (err != OT_ERROR_NONE) {
110 ESP_LOGW(TAG,
"SRP client event/error: %s", otThreadErrorToString(err));
124 char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &size);
126 uint16_t host_name_len = host_name.size();
127 if (host_name_len > size) {
128 ESP_LOGW(TAG,
"Hostname is too long, choose a shorter project name");
131 memset(existing_host_name, 0, size);
132 memcpy(existing_host_name, host_name.c_str(), host_name_len);
134 error = otSrpClientSetHostName(instance, existing_host_name);
136 ESP_LOGW(TAG,
"Could not set host name");
140 error = otSrpClientEnableAutoHostAddress(instance);
142 ESP_LOGW(TAG,
"Could not enable auto host address");
149 ESP_LOGD(TAG,
"Setting up SRP services. count = %d\n", this->
mdns_services_.size());
151 otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
153 ESP_LOGW(TAG,
"Failed to allocate service entry");
158 char *
string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &size);
159 std::string full_service = service.service_type +
"." + service.proto;
160 if (full_service.size() > size) {
161 ESP_LOGW(TAG,
"Service name too long: %s", full_service.c_str());
164 memcpy(
string, full_service.c_str(), full_service.size() + 1);
167 string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &size);
168 if (host_name_len > size) {
169 ESP_LOGW(TAG,
"Instance name too long: %s", host_name.c_str());
172 memset(
string, 0, size);
173 memcpy(
string, host_name.c_str(), host_name_len);
178 otDnsTxtEntry *txt_entries =
179 reinterpret_cast<otDnsTxtEntry *
>(this->
pool_alloc_(
sizeof(otDnsTxtEntry) * service.txt_records.size()));
181 entry->mService.mNumTxtEntries = service.txt_records.size();
182 for (
size_t i = 0; i < service.txt_records.size(); i++) {
183 const auto &txt = service.txt_records[i];
185 txt_entries[i].mKey = strdup(txt.key.c_str());
186 txt_entries[i].mValue =
reinterpret_cast<const uint8_t *
>(strdup(value.c_str()));
187 txt_entries[i].mValueLength = value.size();
189 entry->mService.mTxtEntries = txt_entries;
190 entry->mService.mNumTxtEntries = service.txt_records.size();
193 error = otSrpClientAddService(instance, &entry->mService);
194 if (error != OT_ERROR_NONE) {
195 ESP_LOGW(TAG,
"Failed to add service: %s", otThreadErrorToString(error));
197 ESP_LOGD(TAG,
"Added service: %s", full_service.c_str());
201 ESP_LOGD(TAG,
"Finished SRP setup");