Skip to content

Commit

Permalink
[dns-sd] Fix operational advertising
Browse files Browse the repository at this point in the history
After project-chip#5337 has been merged, operational advertising no
longer works since at the time the advertising is started
nodeID stored in the admin table is not yet initialized.

Start operational advertising when the device is assigned
an IP address. In the future, e2e commissioning flow will
probably take care of that in a different way.
  • Loading branch information
Damian-Nordic committed Apr 22, 2021
1 parent 5d60301 commit 9b02c16
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class SetupListModel : public ListScreen::Model
}
else if (i == 2)
{
app::Mdns::AdvertiseCommisioning();
app::Mdns::AdvertiseCommisionable();
OpenDefaultPairingWindow(ResetAdmins::kNo, PairingWindowAdvertisement::kMdns);
}
}
Expand Down
3 changes: 0 additions & 3 deletions examples/all-clusters-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ int main(int argc, char * argv[])
// Init ZCL Data Model and CHIP App Server
InitServer();

// Init Mdns Server
app::Mdns::StartServer();

chip::DeviceLayer::PlatformMgr().RunEventLoop();

exit:
Expand Down
5 changes: 2 additions & 3 deletions src/app/server/Mdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ CHIP_ERROR AdvertiseOperational()
}

/// Set MDNS commisioning advertisement
CHIP_ERROR AdvertiseCommisioning()
CHIP_ERROR AdvertiseCommisionable()
{

auto advertiseParameters = chip::Mdns::CommissionAdvertisingParameters().SetPort(CHIP_PORT).EnableIpV4(true);

uint8_t mac[8];
Expand Down Expand Up @@ -171,7 +170,7 @@ void StartServer()
// so configuraton should be added to enable commissioning advertising based on supported
// Rendezvous methods.
#if !CHIP_DEVICE_CONFIG_ENABLE_THREAD
err = app::Mdns::AdvertiseCommisioning();
err = app::Mdns::AdvertiseCommisionable();
#endif
}

Expand Down
6 changes: 3 additions & 3 deletions src/app/server/Mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ namespace chip {
namespace app {
namespace Mdns {

/// Set MDNS operational advertisement
/// Start operational advertising
CHIP_ERROR AdvertiseOperational();

/// Set MDNS commisioning advertisement
CHIP_ERROR AdvertiseCommisioning();
/// Start commisionable node advertising
CHIP_ERROR AdvertiseCommisionable();

/// (Re-)starts the minmdns server
void StartServer();
Expand Down
45 changes: 30 additions & 15 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,6 @@ class ServerRendezvousAdvertisementDelegate : public RendezvousAdvertisementDele
return CHIP_NO_ERROR;
}

#if CHIP_DEVICE_CONFIG_ENABLE_MDNS
void RendezvousComplete() const override
{
// Once rendezvous completed, assume we are operational
if (app::Mdns::AdvertiseOperational() != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to start advertising operational state at rendezvous completion time.");
}
}
#endif

void SetDelegate(AppDelegate * delegate) { mDelegate = delegate; }
void SetBLE(bool ble) { isBLE = ble; }
void SetAdminId(AdminId id) { mAdmin = id; }
Expand Down Expand Up @@ -465,6 +454,33 @@ CHIP_ERROR OpenDefaultPairingWindow(ResetAdmins resetAdmins, chip::PairingWindow
return gRendezvousServer.WaitForPairing(std::move(params), &gExchangeMgr, &gTransports, &gSessions, adminInfo);
}

#if CHIP_DEVICE_CONFIG_ENABLE_MDNS && !CHIP_DEVICE_LAYER_TARGET_ESP32
static void ChipEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t)
{
const auto advertise = [] {
CHIP_ERROR err = app::Mdns::AdvertiseOperational();
if (err != CHIP_NO_ERROR)
ChipLogError(AppServer, "Failed to start operational advertising: %s", chip::ErrorStr(err));
};

switch (event->Type)
{
case DeviceLayer::DeviceEventType::kInternetConnectivityChange:
VerifyOrReturn(event->InternetConnectivityChange.IPv4 == DeviceLayer::kConnectivity_Established);
advertise();
break;
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
case DeviceLayer::DeviceEventType::kThreadStateChange:
VerifyOrReturn(event->ThreadStateChange.AddressChanged);
advertise();
break;
#endif
default:
break;
}
}
#endif

// The function will initialize datamodel handler and then start the server
// The server assumes the platform's networking has been setup already
void InitServer(AppDelegate * delegate)
Expand Down Expand Up @@ -537,11 +553,10 @@ void InitServer(AppDelegate * delegate)
#endif
}

// Starting mDNS server only for Thread devices due to problem reported in issue #5076.
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS
// ESP32 examples have a custom logic for enabling DNS-SD
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS && !CHIP_DEVICE_LAYER_TARGET_ESP32
app::Mdns::StartServer();
#endif
PlatformMgr().AddEventHandler(ChipEventHandler, {});
#endif

gCallbacks.SetSessionMgr(&gSessions);
Expand Down

0 comments on commit 9b02c16

Please sign in to comment.