-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SetUpCodePairer::PairDevice to handle scanning from a setup code
- Loading branch information
1 parent
2d5a99b
commit 676adf0
Showing
10 changed files
with
315 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* Implementation of SetUp Code Pairer, a class that parses a given | ||
* setup code and uses the extracted informations to discover and | ||
* filter commissionables nodes, before initiating the pairing process. | ||
* | ||
*/ | ||
|
||
#include <controller/SetUpCodePairer.h> | ||
|
||
#include <controller/CHIPDeviceController.h> | ||
#include <lib/mdns/Resolver.h> | ||
#include <lib/support/CodeUtils.h> | ||
|
||
namespace chip { | ||
namespace Controller { | ||
|
||
CHIP_ERROR SetUpCodePairer::PairDevice(NodeId remoteId, const char * setUpCode) | ||
{ | ||
SetupPayload payload; | ||
|
||
bool isQRCode = strncmp(setUpCode, kQRCodePrefix, strlen(kQRCodePrefix)) == 0; | ||
ReturnErrorOnFailure(isQRCode ? QRCodeSetupPayloadParser(setUpCode).populatePayload(payload) | ||
: ManualSetupPayloadParser(setUpCode).populatePayload(payload)); | ||
|
||
mRemoteId = remoteId; | ||
mSetUpPINCode = payload.setUpPINCode; | ||
|
||
return Connect(payload.rendezvousInformation, payload.discriminator, !isQRCode); | ||
} | ||
|
||
CHIP_ERROR SetUpCodePairer::Connect(RendezvousInformationFlag rendezvousInformation, uint16_t discriminator, bool isShort) | ||
{ | ||
CHIP_ERROR err = CHIP_NO_ERROR; | ||
bool isRunning = false; | ||
|
||
bool searchOverAll = rendezvousInformation == RendezvousInformationFlag::kNone; | ||
if (searchOverAll || rendezvousInformation == RendezvousInformationFlag::kBLE) | ||
{ | ||
if (CHIP_NO_ERROR == (err = StartDiscoverOverBle(discriminator, isShort))) | ||
{ | ||
isRunning = true; | ||
} | ||
VerifyOrReturnError(searchOverAll || CHIP_NO_ERROR == err, err); | ||
} | ||
|
||
if (searchOverAll || rendezvousInformation == RendezvousInformationFlag::kOnNetwork) | ||
{ | ||
if (CHIP_NO_ERROR == (err = StartDiscoverOverIP(discriminator, isShort))) | ||
{ | ||
isRunning = true; | ||
} | ||
VerifyOrReturnError(searchOverAll || CHIP_NO_ERROR == err, err); | ||
} | ||
|
||
if (searchOverAll || rendezvousInformation == RendezvousInformationFlag::kSoftAP) | ||
{ | ||
if (CHIP_NO_ERROR == (err = StartDiscoverOverSoftAP(discriminator, isShort))) | ||
{ | ||
isRunning = true; | ||
} | ||
VerifyOrReturnError(searchOverAll || CHIP_NO_ERROR == err, err); | ||
} | ||
|
||
return isRunning ? CHIP_NO_ERROR : CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; | ||
} | ||
|
||
CHIP_ERROR SetUpCodePairer::StartDiscoverOverBle(uint16_t discriminator, bool isShort) | ||
{ | ||
#if CONFIG_NETWORK_LAYER_BLE | ||
VerifyOrReturnError(mBleLayer != nullptr, CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE); | ||
return mBleLayer->NewBleConnectionByDiscriminator(discriminator, this, OnDiscoveredDeviceOverBleSuccess, | ||
OnDiscoveredDeviceOverBleError); | ||
#else | ||
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; | ||
#endif // CONFIG_NETWORK_LAYER_BLE | ||
} | ||
|
||
CHIP_ERROR SetUpCodePairer::StopConnectOverBle() | ||
{ | ||
#if CONFIG_NETWORK_LAYER_BLE | ||
VerifyOrReturnError(mBleLayer != nullptr, CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE); | ||
return mBleLayer->CancelBleIncompleteConnection(); | ||
#else | ||
return CHIP_NO_ERROR; | ||
#endif // CONFIG_NETWORK_LAYER_BLE | ||
} | ||
|
||
CHIP_ERROR SetUpCodePairer::StartDiscoverOverIP(uint16_t discriminator, bool isShort) | ||
{ | ||
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS | ||
mCommissioner->RegisterDeviceDiscoveryDelegate(this); | ||
Mdns::DiscoveryFilter filter(isShort ? Mdns::DiscoveryFilterType::kShort : Mdns::DiscoveryFilterType::kLong, discriminator); | ||
return mCommissioner->DiscoverCommissionableNodes(filter); | ||
#else | ||
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; | ||
#endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS | ||
} | ||
|
||
CHIP_ERROR SetUpCodePairer::StopConnectOverIP() | ||
{ | ||
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS | ||
mCommissioner->RegisterDeviceDiscoveryDelegate(nullptr); | ||
#endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS | ||
return CHIP_NO_ERROR; | ||
} | ||
|
||
CHIP_ERROR SetUpCodePairer::StartDiscoverOverSoftAP(uint16_t discriminator, bool isShort) | ||
{ | ||
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; | ||
} | ||
|
||
CHIP_ERROR SetUpCodePairer::StopConnectOverSoftAP() | ||
{ | ||
return CHIP_NO_ERROR; | ||
} | ||
|
||
void SetUpCodePairer::OnDeviceDiscovered(RendezvousParameters & params) | ||
{ | ||
LogErrorOnFailure(mCommissioner->PairDevice(mRemoteId, params.SetSetupPINCode(mSetUpPINCode))); | ||
} | ||
|
||
#if CONFIG_NETWORK_LAYER_BLE | ||
void SetUpCodePairer::OnDiscoveredDeviceOverBle(BLE_CONNECTION_OBJECT connObj) | ||
{ | ||
LogErrorOnFailure(StopConnectOverIP()); | ||
LogErrorOnFailure(StopConnectOverSoftAP()); | ||
|
||
Transport::PeerAddress peerAddress = Transport::PeerAddress::BLE(); | ||
RendezvousParameters params = RendezvousParameters().SetPeerAddress(peerAddress).SetConnectionObject(connObj); | ||
OnDeviceDiscovered(params); | ||
} | ||
|
||
void SetUpCodePairer::OnDiscoveredDeviceOverBleSuccess(void * appState, BLE_CONNECTION_OBJECT connObj) | ||
{ | ||
(static_cast<SetUpCodePairer *>(appState))->OnDiscoveredDeviceOverBle(connObj); | ||
} | ||
|
||
void SetUpCodePairer::OnDiscoveredDeviceOverBleError(void * appState, CHIP_ERROR err) | ||
{ | ||
LogErrorOnFailure(err); | ||
} | ||
#endif // CONFIG_NETWORK_LAYER_BLE | ||
|
||
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS | ||
void SetUpCodePairer::OnDiscoveredDevice(const Mdns::DiscoveredNodeData & nodeData) | ||
{ | ||
LogErrorOnFailure(StopConnectOverBle()); | ||
LogErrorOnFailure(StopConnectOverIP()); | ||
LogErrorOnFailure(StopConnectOverSoftAP()); | ||
|
||
Transport::PeerAddress peerAddress = Transport::PeerAddress::UDP(nodeData.ipAddress[0], nodeData.port, nodeData.interfaceId[0]); | ||
RendezvousParameters params = RendezvousParameters().SetPeerAddress(peerAddress); | ||
OnDeviceDiscovered(params); | ||
} | ||
#endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS | ||
|
||
} // namespace Controller | ||
} // namespace chip |
Oops, something went wrong.