64 otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
65 otInstance *instance =
nullptr;
69 otBorderRouterConfig config;
70 if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &config) != OT_ERROR_NONE) {
74 const otIp6Prefix *omr_prefix = &config.mPrefix;
75 const otNetifAddress *unicast_addresses = otIp6GetUnicastAddresses(instance);
76 for (
const otNetifAddress *addr = unicast_addresses; addr; addr = addr->mNext) {
77 const otIp6Address *local_ip = &addr->mAddress;
78 if (otIp6PrefixMatch(&omr_prefix->mPrefix, local_ip)) {
91 const otSrpClientService *services,
92 const otSrpClientService *removed_services,
void *context) {
94 ESP_LOGW(TAG,
"SRP client reported an error: %s", otThreadErrorToString(err));
95 for (
const otSrpClientHostInfo *host = host_info; host; host =
nullptr) {
96 ESP_LOGW(TAG,
" Host: %s", host->mName);
98 for (
const otSrpClientService *service = services; service; service = service->mNext) {
99 ESP_LOGW(TAG,
" Service: %s", service->mName);
109 const otSrpClientService *services,
110 const otSrpClientService *removed_services,
void *context) {
112 if (err == OT_ERROR_NONE && removed_services != NULL && host_info != NULL &&
113 host_info->mState == OT_SRP_CLIENT_ITEM_STATE_REMOVED) {
114 ESP_LOGD(TAG,
"Successful Removal SRP Host and Services");
115 }
else if (err != OT_ERROR_NONE) {
117 ESP_LOGW(TAG,
"SRP client event/error: %s", otThreadErrorToString(err));
131 char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &
size);
133 uint16_t host_name_len = host_name.
size();
134 if (host_name_len >
size) {
135 ESP_LOGW(TAG,
"Hostname is too long, choose a shorter project name");
138 memset(existing_host_name, 0,
size);
139 memcpy(existing_host_name, host_name.c_str(), host_name_len);
141 error = otSrpClientSetHostName(instance, existing_host_name);
143 ESP_LOGW(TAG,
"Could not set host name");
147 error = otSrpClientEnableAutoHostAddress(instance);
149 ESP_LOGW(TAG,
"Could not enable auto host address");
155 ESP_LOGD(TAG,
"Setting up SRP services. count = %d\n", mdns_services.size());
156 for (
const auto &service : mdns_services) {
157 otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
159 ESP_LOGW(TAG,
"Failed to allocate service entry");
164 char *
string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &
size);
165 std::string full_service = std::string(MDNS_STR_ARG(service.service_type)) +
"." + MDNS_STR_ARG(service.proto);
166 if (full_service.size() >
size) {
167 ESP_LOGW(TAG,
"Service name too long: %s", full_service.c_str());
170 memcpy(
string, full_service.c_str(), full_service.size() + 1);
173 string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &
size);
174 if (host_name_len >
size) {
175 ESP_LOGW(TAG,
"Instance name too long: %s", host_name.c_str());
178 memset(
string, 0,
size);
179 memcpy(
string, host_name.c_str(), host_name_len);
182 entry->mService.mPort = service.port.value();
184 otDnsTxtEntry *txt_entries =
185 reinterpret_cast<otDnsTxtEntry *
>(this->
pool_alloc_(
sizeof(otDnsTxtEntry) * service.txt_records.size()));
187 entry->mService.mNumTxtEntries = service.txt_records.size();
188 for (
size_t i = 0; i < service.txt_records.size(); i++) {
189 const auto &txt = service.txt_records[i];
192 const char *value_str = MDNS_STR_ARG(txt.value);
193 txt_entries[i].mKey = MDNS_STR_ARG(txt.key);
194 txt_entries[i].mValue =
reinterpret_cast<const uint8_t *
>(strdup(value_str));
195 txt_entries[i].mValueLength = strlen(value_str);
197 entry->mService.mTxtEntries = txt_entries;
198 entry->mService.mNumTxtEntries = service.txt_records.size();
201 error = otSrpClientAddService(instance, &entry->mService);
202 if (error != OT_ERROR_NONE) {
203 ESP_LOGW(TAG,
"Failed to add service: %s", otThreadErrorToString(error));
205 ESP_LOGD(TAG,
"Added service: %s", full_service.c_str());
209 ESP_LOGD(TAG,
"Finished SRP setup");