66 otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
67 otInstance *instance =
nullptr;
71 otBorderRouterConfig config;
72 if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &config) != OT_ERROR_NONE) {
76 const otIp6Prefix *omr_prefix = &config.mPrefix;
77 const otNetifAddress *unicast_addresses = otIp6GetUnicastAddresses(instance);
78 for (
const otNetifAddress *addr = unicast_addresses; addr; addr = addr->mNext) {
79 const otIp6Address *local_ip = &addr->mAddress;
80 if (otIp6PrefixMatch(&omr_prefix->mPrefix, local_ip)) {
93 const otSrpClientService *services,
94 const otSrpClientService *removed_services,
void *context) {
96 ESP_LOGW(TAG,
"SRP client reported an error: %s", otThreadErrorToString(err));
97 for (
const otSrpClientHostInfo *host = host_info; host; host =
nullptr) {
98 ESP_LOGW(TAG,
" Host: %s", host->mName);
100 for (
const otSrpClientService *service = services; service; service = service->mNext) {
101 ESP_LOGW(TAG,
" Service: %s", service->mName);
111 const otSrpClientService *services,
112 const otSrpClientService *removed_services,
void *context) {
114 if (err == OT_ERROR_NONE && removed_services != NULL && host_info != NULL &&
115 host_info->mState == OT_SRP_CLIENT_ITEM_STATE_REMOVED) {
116 ESP_LOGD(TAG,
"Successful Removal SRP Host and Services");
117 }
else if (err != OT_ERROR_NONE) {
119 ESP_LOGW(TAG,
"SRP client event/error: %s", otThreadErrorToString(err));
133 char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &
size);
135 uint16_t host_name_len = host_name.
size();
136 if (host_name_len >
size) {
137 ESP_LOGW(TAG,
"Hostname is too long, choose a shorter project name");
140 memset(existing_host_name, 0,
size);
141 memcpy(existing_host_name, host_name.c_str(), host_name_len);
143 error = otSrpClientSetHostName(instance, existing_host_name);
145 ESP_LOGW(TAG,
"Could not set host name");
149 error = otSrpClientEnableAutoHostAddress(instance);
151 ESP_LOGW(TAG,
"Could not enable auto host address");
157 ESP_LOGD(TAG,
"Setting up SRP services. count = %d\n", mdns_services.size());
158 for (
const auto &service : mdns_services) {
159 otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
161 ESP_LOGW(TAG,
"Failed to allocate service entry");
166 char *
string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &
size);
167 std::string full_service = std::string(MDNS_STR_ARG(service.service_type)) +
"." + MDNS_STR_ARG(service.proto);
168 if (full_service.size() >
size) {
169 ESP_LOGW(TAG,
"Service name too long: %s", full_service.c_str());
172 memcpy(
string, full_service.c_str(), full_service.size() + 1);
175 string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &
size);
176 if (host_name_len >
size) {
177 ESP_LOGW(TAG,
"Instance name too long: %s", host_name.c_str());
180 memset(
string, 0,
size);
181 memcpy(
string, host_name.c_str(), host_name_len);
186 otDnsTxtEntry *txt_entries =
187 reinterpret_cast<otDnsTxtEntry *
>(this->
pool_alloc_(
sizeof(otDnsTxtEntry) * service.txt_records.size()));
189 entry->mService.mNumTxtEntries = service.txt_records.size();
190 for (
size_t i = 0; i < service.txt_records.size(); i++) {
191 const auto &txt = service.txt_records[i];
194 const char *value_str = MDNS_STR_ARG(txt.value);
195 txt_entries[i].mKey = MDNS_STR_ARG(txt.key);
196 txt_entries[i].mValue =
reinterpret_cast<const uint8_t *
>(strdup(value_str));
197 txt_entries[i].mValueLength = strlen(value_str);
199 entry->mService.mTxtEntries = txt_entries;
200 entry->mService.mNumTxtEntries = service.txt_records.size();
203 error = otSrpClientAddService(instance, &entry->mService);
204 if (error != OT_ERROR_NONE) {
205 ESP_LOGW(TAG,
"Failed to add service: %s", otThreadErrorToString(error));
207 ESP_LOGD(TAG,
"Added service: %s", full_service.c_str());
211 ESP_LOGD(TAG,
"Finished SRP setup");