Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CHIPDeviceController when storage delegate is nullptr #4089

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/controller/CHIPDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,10 @@ CHIP_ERROR Device::LoadSecureSessionParameters(ResetTransport resetNeeded)

bool Device::GetIpAddress(Inet::IPAddress & addr) const
{
if (mState == ConnectionState::SecureConnected)
addr = mDeviceAddr;
return mState == ConnectionState::SecureConnected;
if (mState == ConnectionState::NotConnected)
return false;
addr = mDeviceAddr;
return true;
}

void Device::AddResponseHandler(EndpointId endpoint, ClusterId cluster, Callback::Callback<> * onResponse)
Expand Down
3 changes: 2 additions & 1 deletion src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,8 @@ void DeviceCommissioner::RendezvousCleanup(CHIP_ERROR status)
mPairingDelegate->OnPairingComplete(status);
}

if (mDeviceBeingPaired != kNumMaxActiveDevices)
// TODO: make mStorageDelegate mandatory once all controller applications implement the interface.
if (mDeviceBeingPaired != kNumMaxActiveDevices && mStorageDelegate != nullptr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pan-apple Please take a look here too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a TODO here to remove this condition when all the controller apps have implemented the storage delegate? To support multiple device pairings, storage delegate cannot be optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll add TODO

{
// Let's release the device that's being paired.
// If pairing was successful, its information is
Expand Down
27 changes: 14 additions & 13 deletions src/controller/CHIPDeviceController_deprecated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,15 @@ bool ChipDeviceController::IsConnected() const
return mState == kState_Initialized;
}

bool ChipDeviceController::GetIpAddress(Inet::IPAddress & addr) const
bool ChipDeviceController::GetIpAddress(Inet::IPAddress & addr)
{
if (IsConnected() && mDevice != nullptr)
return mDevice->GetIpAddress(addr);
if (!IsConnected())
return false;

return false;
if (mDevice == nullptr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like an unusual sideffect - get ip addres may do a non-const init. Do we really need it?

or should we ignore since _deprecated should go away anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit it's not the most elegant solution, but I thought it didn't have to be since _deprecated will go away anyway ;). However, until it happens we should have code that works at least. Currently, it's impossible to test that Rendezvous completed successfully. Should I rename getIpAddress to something not suggesting that it's a simple getter? retrieveIpAddress mabe?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just wondering about losing const.
Would if work if we return false if mDevice is nullptr?

That way we can stay const.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, mDevice is only initialized when sending a message/command and getIpAddress was introduced to automatically obtain IP address of a connected device before sending it a message to avoid typing long IPv6 addresses manually. The feature will probably be replaced with service discovery, but for now it's quite helpful I think.

InitDevice();

return mDevice != nullptr && mDevice->GetIpAddress(addr);
}

CHIP_ERROR ChipDeviceController::DisconnectDevice()
Expand Down Expand Up @@ -205,15 +208,7 @@ CHIP_ERROR ChipDeviceController::SendMessage(void * appReqState, PacketBufferHan

if (mDevice == nullptr)
{
if (mPairingWithoutSecurity)
{
err = mCommissioner.GetDevice(mRemoteDeviceId, mSerializedTestDevice, &mDevice);
}
else
{
err = mCommissioner.GetDevice(mRemoteDeviceId, &mDevice);
}
SuccessOrExit(err);
SuccessOrExit(InitDevice());
}

VerifyOrExit(mDevice != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);
Expand All @@ -240,5 +235,11 @@ void ChipDeviceController::OnMessage(System::PacketBufferHandle msgBuf)
}
}

CHIP_ERROR ChipDeviceController::InitDevice()
{
return mPairingWithoutSecurity ? mCommissioner.GetDevice(mRemoteDeviceId, mSerializedTestDevice, &mDevice)
: mCommissioner.GetDevice(mRemoteDeviceId, &mDevice);
}

} // namespace DeviceController
} // namespace chip
4 changes: 3 additions & 1 deletion src/controller/CHIPDeviceController_deprecated.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class DLL_EXPORT ChipDeviceController : public Controller::DeviceStatusDelegate
*
* @return bool If IP Address was returned
*/
bool GetIpAddress(Inet::IPAddress & addr) const;
bool GetIpAddress(Inet::IPAddress & addr);

// ----- Messaging -----
/**
Expand Down Expand Up @@ -185,6 +185,8 @@ class DLL_EXPORT ChipDeviceController : public Controller::DeviceStatusDelegate
void OnMessage(System::PacketBufferHandle msg) override;

private:
CHIP_ERROR InitDevice();

enum
{
kState_NotInitialized = 0,
Expand Down