blob: 5fa66d3fdcf10f0dbbea0ef7e342a52be4f97d40 [file] [log] [blame]
/**
*
* Copyright (c) 2020 Project CHIP Authors
*
* 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
* This file describes a Manual Entry Code Generator.
*
*/
#include "ManualSetupPayloadGenerator.h"
#include <inttypes.h>
#include <support/logging/CHIPLogging.h>
#include <support/verhoeff/Verhoeff.h>
using namespace chip;
static uint32_t shortPayloadRepresentation(const SetupPayload & payload)
{
int offset = 1;
uint32_t result = payload.requiresCustomFlow ? 1 : 0;
result |= (payload.discriminator & kManualSetupDiscriminatorFieldBitMask) << offset;
offset += kManualSetupDiscriminatorFieldLengthInBits;
result |= payload.setUpPINCode << offset;
return result;
}
// TODO: issue #3663 - Unbounded stack in src/setup_payload
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-usage="
static std::string decimalStringWithPadding(uint32_t number, int minLength)
{
char buf[minLength + 1];
snprintf(buf, sizeof(buf), "%0*" PRIu32, minLength, number);
return std::string(buf);
}
#pragma GCC diagnostic pop
CHIP_ERROR ManualSetupPayloadGenerator::payloadDecimalStringRepresentation(std::string & outDecimalString)
{
if (!mSetupPayload.isValidManualCode())
{
ChipLogError(SetupPayload, "Failed encoding invalid payload");
return CHIP_ERROR_INVALID_ARGUMENT;
}
uint32_t shortDecimal = shortPayloadRepresentation(mSetupPayload);
std::string decimalString = decimalStringWithPadding(shortDecimal, kManualSetupShortCodeCharLength);
if (mSetupPayload.requiresCustomFlow)
{
decimalString += decimalStringWithPadding(mSetupPayload.vendorID, kManualSetupVendorIdCharLength);
decimalString += decimalStringWithPadding(mSetupPayload.productID, kManualSetupProductIdCharLength);
}
decimalString += Verhoeff10::ComputeCheckChar(decimalString.c_str());
outDecimalString = decimalString;
return CHIP_NO_ERROR;
}