<commit_before>/*
 *   Copyright (c) 2020 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.
 *
 */

#include "PairingCommand.h"
#include "platform/PlatformManager.h"
#include <controller/ExampleOperationalCredentialsIssuer.h>
#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPSafeCasts.h>
#include <support/logging/CHIPLogging.h>

#include <setup_payload/ManualSetupPayloadParser.h>
#include <setup_payload/QRCodeSetupPayloadParser.h>

using namespace ::chip;

constexpr uint64_t kBreadcrumb = 0;
constexpr uint32_t kTimeoutMs  = 6000;

CHIP_ERROR PairingCommand::Run()
{
    CHIP_ERROR err = CHIP_NO_ERROR;

    GetExecContext()->commissioner->RegisterDeviceAddressUpdateDelegate(this);
    GetExecContext()->commissioner->RegisterPairingDelegate(this);

#if CONFIG_PAIR_WITH_RANDOM_ID
    // Generate a random remote id so we don't end up reusing the same node id
    // for different nodes.
    //
    // TODO: Ideally we'd just ask for an operational cert for the commissionnee
    // and get the node from that, but the APIs are not set up that way yet.
    NodeId randomId;
    if (Controller::ExampleOperationalCredentialsIssuer::GetRandomOperationalNodeId(&randomId) == CHIP_NO_ERROR)
    {
        ChipLogProgress(Controller, "Generated random node id: 0x" ChipLogFormatX64, ChipLogValueX64(randomId));
        if (GetExecContext()->storage->SetRemoteNodeId(randomId) == CHIP_NO_ERROR)
        {
            GetExecContext()->remoteId = randomId;
        }
    }
#endif // CONFIG_PAIR_WITH_RANDOM_ID

    err = RunInternal(GetExecContext()->remoteId);
    VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Init Failure! PairDevice: %s", ErrorStr(err)));

exit:
    return err;
}

CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId)
{
    CHIP_ERROR err = CHIP_NO_ERROR;

    mRemoteId = remoteId;

    InitCallbacks();

    switch (mPairingMode)
    {
    case PairingMode::None:
        err = Unpair(remoteId);
        break;
    case PairingMode::Bypass:<commit_msg>Fix chip-tool to never use the stored node id when doing a new pairing. (#8714)<commit_after>/*
 *   Copyright (c) 2020 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.
 *
 */

#include "PairingCommand.h"
#include "platform/PlatformManager.h"
#include <controller/ExampleOperationalCredentialsIssuer.h>
#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPSafeCasts.h>
#include <protocols/secure_channel/PASESession.h>
#include <support/logging/CHIPLogging.h>

#include <setup_payload/ManualSetupPayloadParser.h>
#include <setup_payload/QRCodeSetupPayloadParser.h>

using namespace ::chip;

constexpr uint64_t kBreadcrumb = 0;
constexpr uint32_t kTimeoutMs  = 6000;

CHIP_ERROR PairingCommand::Run()
{
    CHIP_ERROR err = CHIP_NO_ERROR;

    GetExecContext()->commissioner->RegisterDeviceAddressUpdateDelegate(this);
    GetExecContext()->commissioner->RegisterPairingDelegate(this);

#if CONFIG_PAIR_WITH_RANDOM_ID
    // Generate a random remote id so we don't end up reusing the same node id
    // for different nodes.
    //
    // TODO: Ideally we'd just ask for an operational cert for the commissionnee
    // and get the node from that, but the APIs are not set up that way yet.
    NodeId randomId;
    ReturnErrorOnFailure(Controller::ExampleOperationalCredentialsIssuer::GetRandomOperationalNodeId(&randomId));

    ChipLogProgress(Controller, "Generated random node id: 0x" ChipLogFormatX64, ChipLogValueX64(randomId));

    ReturnErrorOnFailure(GetExecContext()->storage->SetRemoteNodeId(randomId));
    GetExecContext()->remoteId = randomId;
#else  // CONFIG_PAIR_WITH_RANDOM_ID
    // Use the default id, not whatever happens to be in our storage, since this
    // is a new pairing.
    GetExecContext()->remoteId = kTestDeviceNodeId;
#endif // CONFIG_PAIR_WITH_RANDOM_ID

    err = RunInternal(GetExecContext()->remoteId);
    VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Init Failure! PairDevice: %s", ErrorStr(err)));

exit:
    return err;
}

CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId)
{
    CHIP_ERROR err = CHIP_NO_ERROR;

    mRemoteId = remoteId;

    InitCallbacks();

    switch (mPairingMode)
    {
    case PairingMode::None:
        err = Unpair(remoteId);
        break;
    case PairingMode::Bypass:
{'fraction_non_alphanumeric': 0.09265766598853338, 'fraction_numerical': 0.008877381172554096, 'mean_word_length': 4.1067044381491975, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6267586', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1622, 'n_words': 475}