76 otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
77 otInstance *instance =
nullptr;
81 otBorderRouterConfig config;
82 if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &config) != OT_ERROR_NONE) {
86 const otIp6Prefix *omr_prefix = &config.mPrefix;
87 const otNetifAddress *unicast_addresses = otIp6GetUnicastAddresses(instance);
88 for (
const otNetifAddress *addr = unicast_addresses; addr; addr = addr->mNext) {
89 const otIp6Address *local_ip = &addr->mAddress;
90 if (otIp6PrefixMatch(&omr_prefix->mPrefix, local_ip)) {
103 const otSrpClientService *services,
104 const otSrpClientService *removed_services,
void *context) {
106 ESP_LOGW(TAG,
"SRP client reported an error: %s", otThreadErrorToString(err));
107 for (
const otSrpClientHostInfo *host = host_info; host; host =
nullptr) {
108 ESP_LOGW(TAG,
" Host: %s", host->mName);
110 for (
const otSrpClientService *service = services; service; service = service->mNext) {
111 ESP_LOGW(TAG,
" Service: %s", service->mName);
121 const otSrpClientService *services,
122 const otSrpClientService *removed_services,
void *context) {
124 if (err == OT_ERROR_NONE && removed_services != NULL && host_info != NULL &&
125 host_info->mState == OT_SRP_CLIENT_ITEM_STATE_REMOVED) {
126 ESP_LOGD(TAG,
"Successful Removal SRP Host and Services");
127 }
else if (err != OT_ERROR_NONE) {
129 ESP_LOGW(TAG,
"SRP client event/error: %s", otThreadErrorToString(err));
143 char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &
size);
145 uint16_t host_name_len = host_name.size();
146 if (host_name_len >
size) {
147 ESP_LOGW(TAG,
"Hostname is too long, choose a shorter project name");
150 memset(existing_host_name, 0,
size);
151 memcpy(existing_host_name, host_name.c_str(), host_name_len);
153 error = otSrpClientSetHostName(instance, existing_host_name);
155 ESP_LOGW(TAG,
"Could not set host name");
159 error = otSrpClientEnableAutoHostAddress(instance);
161 ESP_LOGW(TAG,
"Could not enable auto host address");
167 ESP_LOGD(TAG,
"Setting up SRP services. count = %d\n", mdns_services.size());
168 for (
const auto &service : mdns_services) {
169 otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
171 ESP_LOGW(TAG,
"Failed to allocate service entry");
176 char *
string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &
size);
177 std::string full_service = std::string(MDNS_STR_ARG(service.service_type)) +
"." + MDNS_STR_ARG(service.proto);
178 if (full_service.size() >
size) {
179 ESP_LOGW(TAG,
"Service name too long: %s", full_service.c_str());
182 memcpy(
string, full_service.c_str(), full_service.size() + 1);
185 string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &
size);
186 if (host_name_len >
size) {
187 ESP_LOGW(TAG,
"Instance name too long: %s", host_name.c_str());
190 memset(
string, 0,
size);
191 memcpy(
string, host_name.c_str(), host_name_len);
196 otDnsTxtEntry *txt_entries =
197 reinterpret_cast<otDnsTxtEntry *
>(this->
pool_alloc_(
sizeof(otDnsTxtEntry) * service.txt_records.size()));
199 entry->mService.mNumTxtEntries = service.txt_records.size();
200 for (
size_t i = 0; i < service.txt_records.size(); i++) {
201 const auto &txt = service.txt_records[i];
204 const char *value_str = MDNS_STR_ARG(txt.value);
205 txt_entries[i].mKey = MDNS_STR_ARG(txt.key);
206 txt_entries[i].mValue =
reinterpret_cast<const uint8_t *
>(strdup(value_str));
207 txt_entries[i].mValueLength = strlen(value_str);
209 entry->mService.mTxtEntries = txt_entries;
210 entry->mService.mNumTxtEntries = service.txt_records.size();
213 error = otSrpClientAddService(instance, &entry->mService);
214 if (error != OT_ERROR_NONE) {
215 ESP_LOGW(TAG,
"Failed to add service: %s", otThreadErrorToString(error));
217 ESP_LOGD(TAG,
"Added service: %s", full_service.c_str());
221 ESP_LOGD(TAG,
"Finished SRP setup");