Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / chip-tool / commands / payload / SetupPayloadParseCommand.cpp
1 /*
2  *   Copyright (c) 2020 Project CHIP Authors
3  *   All rights reserved.
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19 #include "SetupPayloadParseCommand.h"
20 #include <setup_payload/ManualSetupPayloadParser.h>
21 #include <setup_payload/QRCodeSetupPayloadParser.h>
22 #include <setup_payload/SetupPayload.h>
23
24 using namespace ::chip;
25
26 CHIP_ERROR SetupPayloadParseCommand::Run(PersistentStorage & storage, NodeId localId, NodeId remoteId)
27 {
28     std::string codeString(mCode);
29     SetupPayload payload;
30
31     CHIP_ERROR err = CHIP_NO_ERROR;
32     err            = Parse(codeString, payload);
33     SuccessOrExit(err);
34
35     err = Print(payload);
36     SuccessOrExit(err);
37 exit:
38     return err;
39 }
40
41 CHIP_ERROR SetupPayloadParseCommand::Print(chip::SetupPayload payload)
42 {
43     std::string serialNumber;
44     std::vector<OptionalQRCodeInfo> optionalVendorData;
45     CHIP_ERROR err = CHIP_NO_ERROR;
46
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);
54
55     if (payload.getSerialNumber(serialNumber) == CHIP_NO_ERROR)
56     {
57         ChipLogProgress(SetupPayload, "SerialNumber: %s", serialNumber.c_str());
58     }
59
60     optionalVendorData = payload.getAllOptionalVendorData();
61     for (const OptionalQRCodeInfo & info : optionalVendorData)
62     {
63         if (info.type == optionalQRCodeInfoTypeString)
64         {
65             ChipLogProgress(SetupPayload, "OptionalQRCodeInfo: tag=%u,string value=%s", info.tag, info.data.c_str());
66         }
67         else if (info.type == optionalQRCodeInfoTypeInt32)
68         {
69             ChipLogProgress(SetupPayload, "OptionalQRCodeInfo: tag=%u,int value=%u", info.tag, info.int32);
70         }
71         else
72         {
73             err = CHIP_ERROR_INVALID_ARGUMENT;
74         }
75     }
76
77     SuccessOrExit(err);
78
79 exit:
80     return err;
81 }
82
83 CHIP_ERROR SetupPayloadParseCommand::Parse(std::string codeString, chip::SetupPayload & payload)
84 {
85
86     CHIP_ERROR err = CHIP_NO_ERROR;
87     if (IsQRCode(codeString))
88     {
89         ChipLogDetail(SetupPayload, "Parsing base41Representation: %s", codeString.c_str());
90         err = QRCodeSetupPayloadParser(codeString).populatePayload(payload);
91     }
92     else
93     {
94         ChipLogDetail(SetupPayload, "Parsing decimalRepresentation: %s", codeString.c_str());
95         err = ManualSetupPayloadParser(codeString).populatePayload(payload);
96     }
97
98     return err;
99 }
100
101 bool SetupPayloadParseCommand::IsQRCode(std::string codeString)
102 {
103     return codeString.rfind(QRCODE_PREFIX) == 0;
104 }