73 otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
74 otInstance *instance =
nullptr;
78 otBorderRouterConfig config;
79 if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &config) != OT_ERROR_NONE) {
83 const otIp6Prefix *omr_prefix = &config.mPrefix;
84 const otNetifAddress *unicast_addresses = otIp6GetUnicastAddresses(instance);
85 for (
const otNetifAddress *addr = unicast_addresses; addr; addr = addr->mNext) {
86 const otIp6Address *local_ip = &addr->mAddress;
87 if (otIp6PrefixMatch(&omr_prefix->mPrefix, local_ip)) {
100 const otSrpClientService *services,
101 const otSrpClientService *removed_services,
void *context) {
103 ESP_LOGW(TAG,
"SRP client reported an error: %s", otThreadErrorToString(err));
104 for (
const otSrpClientHostInfo *host = host_info; host; host =
nullptr) {
105 ESP_LOGW(TAG,
" Host: %s", host->mName);
107 for (
const otSrpClientService *service = services; service; service = service->mNext) {
108 ESP_LOGW(TAG,
" Service: %s", service->mName);
118 const otSrpClientService *services,
119 const otSrpClientService *removed_services,
void *context) {
121 if (err == OT_ERROR_NONE && removed_services != NULL && host_info != NULL &&
122 host_info->mState == OT_SRP_CLIENT_ITEM_STATE_REMOVED) {
123 ESP_LOGD(TAG,
"Successful Removal SRP Host and Services");
124 }
else if (err != OT_ERROR_NONE) {
126 ESP_LOGW(TAG,
"SRP client event/error: %s", otThreadErrorToString(err));
140 char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &size);
142 uint16_t host_name_len = host_name.size();
143 if (host_name_len > size) {
144 ESP_LOGW(TAG,
"Hostname is too long, choose a shorter project name");
147 memset(existing_host_name, 0, size);
148 memcpy(existing_host_name, host_name.c_str(), host_name_len);
150 error = otSrpClientSetHostName(instance, existing_host_name);
152 ESP_LOGW(TAG,
"Could not set host name");
156 error = otSrpClientEnableAutoHostAddress(instance);
158 ESP_LOGW(TAG,
"Could not enable auto host address");
164 ESP_LOGD(TAG,
"Setting up SRP services. count = %d\n", mdns_services.size());
165 for (
const auto &service : mdns_services) {
166 otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
168 ESP_LOGW(TAG,
"Failed to allocate service entry");
173 char *
string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &size);
174 std::string full_service = std::string(MDNS_STR_ARG(service.service_type)) +
"." + MDNS_STR_ARG(service.proto);
175 if (full_service.size() > size) {
176 ESP_LOGW(TAG,
"Service name too long: %s", full_service.c_str());
179 memcpy(
string, full_service.c_str(), full_service.size() + 1);
182 string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &size);
183 if (host_name_len > size) {
184 ESP_LOGW(TAG,
"Instance name too long: %s", host_name.c_str());
187 memset(
string, 0, size);
188 memcpy(
string, host_name.c_str(), host_name_len);
193 otDnsTxtEntry *txt_entries =
194 reinterpret_cast<otDnsTxtEntry *
>(this->
pool_alloc_(
sizeof(otDnsTxtEntry) * service.txt_records.size()));
196 entry->mService.mNumTxtEntries = service.txt_records.size();
197 for (
size_t i = 0; i < service.txt_records.size(); i++) {
198 const auto &txt = service.txt_records[i];
201 const char *value_str = MDNS_STR_ARG(txt.value);
202 txt_entries[i].mKey = MDNS_STR_ARG(txt.key);
203 txt_entries[i].mValue =
reinterpret_cast<const uint8_t *
>(strdup(value_str));
204 txt_entries[i].mValueLength = strlen(value_str);
206 entry->mService.mTxtEntries = txt_entries;
207 entry->mService.mNumTxtEntries = service.txt_records.size();
210 error = otSrpClientAddService(instance, &entry->mService);
211 if (error != OT_ERROR_NONE) {
212 ESP_LOGW(TAG,
"Failed to add service: %s", otThreadErrorToString(error));
214 ESP_LOGD(TAG,
"Added service: %s", full_service.c_str());
218 ESP_LOGD(TAG,
"Finished SRP setup");