74 otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
75 otInstance *instance =
nullptr;
79 otBorderRouterConfig config;
80 if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &config) != OT_ERROR_NONE) {
84 const otIp6Prefix *omr_prefix = &config.mPrefix;
85 const otNetifAddress *unicast_addresses = otIp6GetUnicastAddresses(instance);
86 for (
const otNetifAddress *addr = unicast_addresses; addr; addr = addr->mNext) {
87 const otIp6Address *local_ip = &addr->mAddress;
88 if (otIp6PrefixMatch(&omr_prefix->mPrefix, local_ip)) {
101 const otSrpClientService *services,
102 const otSrpClientService *removed_services,
void *context) {
104 ESP_LOGW(TAG,
"SRP client reported an error: %s", otThreadErrorToString(err));
105 for (
const otSrpClientHostInfo *host = host_info; host; host =
nullptr) {
106 ESP_LOGW(TAG,
" Host: %s", host->mName);
108 for (
const otSrpClientService *service = services; service; service = service->mNext) {
109 ESP_LOGW(TAG,
" Service: %s", service->mName);
119 const otSrpClientService *services,
120 const otSrpClientService *removed_services,
void *context) {
122 if (err == OT_ERROR_NONE && removed_services != NULL && host_info != NULL &&
123 host_info->mState == OT_SRP_CLIENT_ITEM_STATE_REMOVED) {
124 ESP_LOGD(TAG,
"Successful Removal SRP Host and Services");
125 }
else if (err != OT_ERROR_NONE) {
127 ESP_LOGW(TAG,
"SRP client event/error: %s", otThreadErrorToString(err));
141 char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &size);
143 uint16_t host_name_len = host_name.size();
144 if (host_name_len > size) {
145 ESP_LOGW(TAG,
"Hostname is too long, choose a shorter project name");
148 memset(existing_host_name, 0, size);
149 memcpy(existing_host_name, host_name.c_str(), host_name_len);
151 error = otSrpClientSetHostName(instance, existing_host_name);
153 ESP_LOGW(TAG,
"Could not set host name");
157 error = otSrpClientEnableAutoHostAddress(instance);
159 ESP_LOGW(TAG,
"Could not enable auto host address");
165 ESP_LOGD(TAG,
"Setting up SRP services. count = %d\n", mdns_services.size());
166 for (
const auto &service : mdns_services) {
167 otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
169 ESP_LOGW(TAG,
"Failed to allocate service entry");
174 char *
string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &size);
175 std::string full_service = std::string(MDNS_STR_ARG(service.service_type)) +
"." + MDNS_STR_ARG(service.proto);
176 if (full_service.size() > size) {
177 ESP_LOGW(TAG,
"Service name too long: %s", full_service.c_str());
180 memcpy(
string, full_service.c_str(), full_service.size() + 1);
183 string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &size);
184 if (host_name_len > size) {
185 ESP_LOGW(TAG,
"Instance name too long: %s", host_name.c_str());
188 memset(
string, 0, size);
189 memcpy(
string, host_name.c_str(), host_name_len);
194 otDnsTxtEntry *txt_entries =
195 reinterpret_cast<otDnsTxtEntry *
>(this->
pool_alloc_(
sizeof(otDnsTxtEntry) * service.txt_records.size()));
197 entry->mService.mNumTxtEntries = service.txt_records.size();
198 for (
size_t i = 0; i < service.txt_records.size(); i++) {
199 const auto &txt = service.txt_records[i];
202 const char *value_str = MDNS_STR_ARG(txt.value);
203 txt_entries[i].mKey = MDNS_STR_ARG(txt.key);
204 txt_entries[i].mValue =
reinterpret_cast<const uint8_t *
>(strdup(value_str));
205 txt_entries[i].mValueLength = strlen(value_str);
207 entry->mService.mTxtEntries = txt_entries;
208 entry->mService.mNumTxtEntries = service.txt_records.size();
211 error = otSrpClientAddService(instance, &entry->mService);
212 if (error != OT_ERROR_NONE) {
213 ESP_LOGW(TAG,
"Failed to add service: %s", otThreadErrorToString(error));
215 ESP_LOGD(TAG,
"Added service: %s", full_service.c_str());
219 ESP_LOGD(TAG,
"Finished SRP setup");