3 * Copyright (c) 2020 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "QRCodeUtil.h"
21 #include <platform/CHIPDeviceLayer.h>
23 #include <setup_payload/QRCodeSetupPayloadGenerator.h>
24 #include <support/CodeUtils.h>
25 #include <support/ScopedBuffer.h>
26 #include <support/logging/CHIPLogging.h>
28 constexpr char qrCodeBaseUrl[] = "https://dhrishi.github.io/connectedhomeip/qrcode.html";
29 constexpr char specialCharsUnreservedInRfc3986[] = "-._~";
31 void PrintQRCode(chip::RendezvousInformationFlags rendezvousFlags)
33 uint32_t setupPinCode;
36 if (GetQRCode(setupPinCode, QRCode, rendezvousFlags) == CHIP_NO_ERROR)
38 ChipLogProgress(AppServer, "SetupPINCode: [%" PRIu32 "]", setupPinCode);
39 ChipLogProgress(AppServer, "SetupQRCode: [%s]", QRCode.c_str());
41 chip::Platform::ScopedMemoryBuffer<char> qrCodeBuffer;
42 const size_t qrCodeBufferMaxSize = 3 * QRCode.size() + 1;
43 qrCodeBuffer.Alloc(qrCodeBufferMaxSize);
44 if (EncodeQRCodeToUrl(QRCode.c_str(), QRCode.size(), &qrCodeBuffer[0], qrCodeBufferMaxSize) == CHIP_NO_ERROR)
46 ChipLogProgress(AppServer, "Copy/paste the below URL in a browser to see the QR Code:\n\t%s?data=%s", qrCodeBaseUrl,
52 ChipLogError(AppServer, "Getting QR code failed!");
56 CHIP_ERROR GetQRCode(uint32_t & setupPinCode, std::string & QRCode, chip::RendezvousInformationFlags rendezvousFlags)
58 using namespace ::chip::DeviceLayer;
60 CHIP_ERROR err = CHIP_NO_ERROR;
61 chip::SetupPayload payload;
64 payload.rendezvousInformation = rendezvousFlags;
66 err = ConfigurationMgr().GetSetupPinCode(payload.setUpPINCode);
67 VerifyOrExit(err == CHIP_NO_ERROR,
68 ChipLogProgress(AppServer, "ConfigurationMgr().GetSetupPinCode() failed: %s", chip::ErrorStr(err)));
69 setupPinCode = payload.setUpPINCode;
71 err = ConfigurationMgr().GetSetupDiscriminator(payload.discriminator);
72 VerifyOrExit(err == CHIP_NO_ERROR,
73 ChipLogProgress(AppServer, "ConfigurationMgr().GetSetupDiscriminator() failed: %s", chip::ErrorStr(err)));
75 err = ConfigurationMgr().GetVendorId(payload.vendorID);
76 VerifyOrExit(err == CHIP_NO_ERROR,
77 ChipLogProgress(AppServer, "ConfigurationMgr().GetVendorId() failed: %s", chip::ErrorStr(err)));
79 err = ConfigurationMgr().GetProductId(payload.productID);
80 VerifyOrExit(err == CHIP_NO_ERROR,
81 ChipLogProgress(AppServer, "ConfigurationMgr().GetProductId() failed: %s", chip::ErrorStr(err)));
83 // TODO: Usage of STL will significantly increase the image size, this should be changed to more efficient method for
85 err = chip::QRCodeSetupPayloadGenerator(payload).payloadBase41Representation(QRCode);
86 VerifyOrExit(err == CHIP_NO_ERROR, ChipLogProgress(AppServer, "Generating QR Code failed: %s", chip::ErrorStr(err)));
92 static inline bool isCharUnreservedInRfc3986(const char c)
94 return isalpha(c) || isdigit(c) || (strchr(specialCharsUnreservedInRfc3986, c) != nullptr);
97 CHIP_ERROR EncodeQRCodeToUrl(const char * QRCode, size_t len, char * url, size_t maxSize)
99 const char upperHexDigits[] = "0123456789ABCDEF";
100 CHIP_ERROR err = CHIP_NO_ERROR;
103 VerifyOrExit((QRCode != nullptr) && (url != nullptr), err = CHIP_ERROR_INVALID_ARGUMENT);
105 for (i = 0; i < len; ++i)
107 unsigned char c = QRCode[i];
108 if (isCharUnreservedInRfc3986(c))
111 VerifyOrExit((j + 1) < maxSize, err = CHIP_ERROR_BUFFER_TOO_SMALL);
118 VerifyOrExit((j + 3) < maxSize, err = CHIP_ERROR_BUFFER_TOO_SMALL);
121 url[j++] = upperHexDigits[(c & 0xf0) >> 4];
122 url[j++] = upperHexDigits[(c & 0x0f)];