2 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "SetupPayloadParseCommand.h"
20 #include <setup_payload/ManualSetupPayloadParser.h>
21 #include <setup_payload/QRCodeSetupPayloadParser.h>
22 #include <setup_payload/SetupPayload.h>
24 using namespace ::chip;
26 CHIP_ERROR SetupPayloadParseCommand::Run(PersistentStorage & storage, NodeId localId, NodeId remoteId)
28 std::string codeString(mCode);
31 CHIP_ERROR err = CHIP_NO_ERROR;
32 err = Parse(codeString, payload);
41 CHIP_ERROR SetupPayloadParseCommand::Print(chip::SetupPayload payload)
43 std::string serialNumber;
44 std::vector<OptionalQRCodeInfo> optionalVendorData;
45 CHIP_ERROR err = CHIP_NO_ERROR;
47 ChipLogProgress(SetupPayload, "RequiresCustomFlow: %u", payload.requiresCustomFlow);
48 ChipLogProgress(SetupPayload, "VendorID: %u", payload.vendorID);
49 ChipLogProgress(SetupPayload, "Version: %u", payload.version);
50 ChipLogProgress(SetupPayload, "ProductID: %u", payload.productID);
51 ChipLogProgress(SetupPayload, "Discriminator: %u", payload.discriminator);
52 ChipLogProgress(SetupPayload, "SetUpPINCode: %u", payload.setUpPINCode);
53 ChipLogProgress(SetupPayload, "RendezvousInformation: %u", payload.rendezvousInformation);
55 if (payload.getSerialNumber(serialNumber) == CHIP_NO_ERROR)
57 ChipLogProgress(SetupPayload, "SerialNumber: %s", serialNumber.c_str());
60 optionalVendorData = payload.getAllOptionalVendorData();
61 for (const OptionalQRCodeInfo & info : optionalVendorData)
63 if (info.type == optionalQRCodeInfoTypeString)
65 ChipLogProgress(SetupPayload, "OptionalQRCodeInfo: tag=%u,string value=%s", info.tag, info.data.c_str());
67 else if (info.type == optionalQRCodeInfoTypeInt32)
69 ChipLogProgress(SetupPayload, "OptionalQRCodeInfo: tag=%u,int value=%u", info.tag, info.int32);
73 err = CHIP_ERROR_INVALID_ARGUMENT;
83 CHIP_ERROR SetupPayloadParseCommand::Parse(std::string codeString, chip::SetupPayload & payload)
86 CHIP_ERROR err = CHIP_NO_ERROR;
87 if (IsQRCode(codeString))
89 ChipLogDetail(SetupPayload, "Parsing base41Representation: %s", codeString.c_str());
90 err = QRCodeSetupPayloadParser(codeString).populatePayload(payload);
94 ChipLogDetail(SetupPayload, "Parsing decimalRepresentation: %s", codeString.c_str());
95 err = ManualSetupPayloadParser(codeString).populatePayload(payload);
101 bool SetupPayloadParseCommand::IsQRCode(std::string codeString)
103 return codeString.rfind(QRCODE_PREFIX) == 0;