65 otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
66 otInstance *instance =
nullptr;
68 instance =
lock.get_instance();
70 otBorderRouterConfig config;
71 if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &config) != OT_ERROR_NONE) {
75 const otIp6Prefix *omr_prefix = &config.mPrefix;
76 const otNetifAddress *unicast_addresses = otIp6GetUnicastAddresses(instance);
77 for (
const otNetifAddress *addr = unicast_addresses; addr; addr = addr->mNext) {
78 const otIp6Address *local_ip = &addr->mAddress;
79 if (otIp6PrefixMatch(&omr_prefix->mPrefix, local_ip)) {
92 const otSrpClientService *services,
93 const otSrpClientService *removed_services,
void *context) {
95 ESP_LOGW(TAG,
"SRP client reported an error: %s", otThreadErrorToString(err));
96 for (
const otSrpClientHostInfo *host = host_info; host; host =
nullptr) {
97 ESP_LOGW(TAG,
" Host: %s", host->mName);
99 for (
const otSrpClientService *service = services; service; service = service->mNext) {
100 ESP_LOGW(TAG,
" Service: %s", service->mName);
110 const otSrpClientService *services,
111 const otSrpClientService *removed_services,
void *context) {
113 if (err == OT_ERROR_NONE && removed_services != NULL && host_info != NULL &&
114 host_info->mState == OT_SRP_CLIENT_ITEM_STATE_REMOVED) {
115 ESP_LOGD(TAG,
"Successful Removal SRP Host and Services");
116 }
else if (err != OT_ERROR_NONE) {
118 ESP_LOGW(TAG,
"SRP client event/error: %s", otThreadErrorToString(err));
126 otInstance *instance =
lock.get_instance();
132 char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &
size);
134 uint16_t host_name_len = host_name.
size();
135 if (host_name_len >
size) {
136 ESP_LOGW(TAG,
"Hostname is too long, choose a shorter project name");
139 memset(existing_host_name, 0,
size);
140 memcpy(existing_host_name, host_name.c_str(), host_name_len);
142 error = otSrpClientSetHostName(instance, existing_host_name);
144 ESP_LOGW(TAG,
"Could not set host name");
148 error = otSrpClientEnableAutoHostAddress(instance);
150 ESP_LOGW(TAG,
"Could not enable auto host address");
156 ESP_LOGD(TAG,
"Setting up SRP services. count = %d\n", mdns_services.size());
157 for (
const auto &service : mdns_services) {
158 otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
160 ESP_LOGW(TAG,
"Failed to allocate service entry");
165 char *
string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &
size);
166 std::string full_service = std::string(MDNS_STR_ARG(service.service_type)) +
"." + MDNS_STR_ARG(service.proto);
167 if (full_service.size() >
size) {
168 ESP_LOGW(TAG,
"Service name too long: %s", full_service.c_str());
171 memcpy(
string, full_service.c_str(), full_service.size() + 1);
174 string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &
size);
175 if (host_name_len >
size) {
176 ESP_LOGW(TAG,
"Instance name too long: %s", host_name.c_str());
179 memset(
string, 0,
size);
180 memcpy(
string, host_name.c_str(), host_name_len);
183 entry->mService.mPort = service.port.value();
185 otDnsTxtEntry *txt_entries =
186 reinterpret_cast<otDnsTxtEntry *
>(this->
pool_alloc_(
sizeof(otDnsTxtEntry) * service.txt_records.size()));
188 entry->mService.mNumTxtEntries = service.txt_records.size();
189 for (
size_t i = 0; i < service.txt_records.size(); i++) {
190 const auto &txt = service.txt_records[i];
193 const char *value_str = MDNS_STR_ARG(txt.value);
194 txt_entries[i].mKey = MDNS_STR_ARG(txt.key);
195 txt_entries[i].mValue =
reinterpret_cast<const uint8_t *
>(strdup(value_str));
196 txt_entries[i].mValueLength = strlen(value_str);
198 entry->mService.mTxtEntries = txt_entries;
199 entry->mService.mNumTxtEntries = service.txt_records.size();
202 error = otSrpClientAddService(instance, &entry->mService);
203 if (error != OT_ERROR_NONE) {
204 ESP_LOGW(TAG,
"Failed to add service: %s", otThreadErrorToString(error));
206 ESP_LOGD(TAG,
"Added service: %s", full_service.c_str());
210 ESP_LOGD(TAG,
"Finished SRP setup");