9a38a084019e4ace369b23d790dc628e999fdbbe
[platform/upstream/connectedhomeip.git] / examples / common / chip-app-server / QRCodeUtil.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include "QRCodeUtil.h"
20
21 #include <platform/CHIPDeviceLayer.h>
22
23 #include <setup_payload/QRCodeSetupPayloadGenerator.h>
24 #include <support/CodeUtils.h>
25 #include <support/ScopedBuffer.h>
26 #include <support/logging/CHIPLogging.h>
27
28 constexpr char qrCodeBaseUrl[]                   = "https://dhrishi.github.io/connectedhomeip/qrcode.html";
29 constexpr char specialCharsUnreservedInRfc3986[] = "-._~";
30
31 void PrintQRCode(chip::RendezvousInformationFlags rendezvousFlags)
32 {
33     uint32_t setupPinCode;
34     std::string QRCode;
35
36     if (GetQRCode(setupPinCode, QRCode, rendezvousFlags) == CHIP_NO_ERROR)
37     {
38         ChipLogProgress(AppServer, "SetupPINCode: [%" PRIu32 "]", setupPinCode);
39         ChipLogProgress(AppServer, "SetupQRCode:  [%s]", QRCode.c_str());
40
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)
45         {
46             ChipLogProgress(AppServer, "Copy/paste the below URL in a browser to see the QR Code:\n\t%s?data=%s", qrCodeBaseUrl,
47                             &qrCodeBuffer[0]);
48         }
49     }
50     else
51     {
52         ChipLogError(AppServer, "Getting QR code failed!");
53     }
54 }
55
56 CHIP_ERROR GetQRCode(uint32_t & setupPinCode, std::string & QRCode, chip::RendezvousInformationFlags rendezvousFlags)
57 {
58     using namespace ::chip::DeviceLayer;
59
60     CHIP_ERROR err = CHIP_NO_ERROR;
61     chip::SetupPayload payload;
62
63     payload.version               = 1;
64     payload.rendezvousInformation = rendezvousFlags;
65
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;
70
71     err = ConfigurationMgr().GetSetupDiscriminator(payload.discriminator);
72     VerifyOrExit(err == CHIP_NO_ERROR,
73                  ChipLogProgress(AppServer, "ConfigurationMgr().GetSetupDiscriminator() failed: %s", chip::ErrorStr(err)));
74
75     err = ConfigurationMgr().GetVendorId(payload.vendorID);
76     VerifyOrExit(err == CHIP_NO_ERROR,
77                  ChipLogProgress(AppServer, "ConfigurationMgr().GetVendorId() failed: %s", chip::ErrorStr(err)));
78
79     err = ConfigurationMgr().GetProductId(payload.productID);
80     VerifyOrExit(err == CHIP_NO_ERROR,
81                  ChipLogProgress(AppServer, "ConfigurationMgr().GetProductId() failed: %s", chip::ErrorStr(err)));
82
83     // TODO: Usage of STL will significantly increase the image size, this should be changed to more efficient method for
84     // generating payload
85     err = chip::QRCodeSetupPayloadGenerator(payload).payloadBase41Representation(QRCode);
86     VerifyOrExit(err == CHIP_NO_ERROR, ChipLogProgress(AppServer, "Generating QR Code failed: %s", chip::ErrorStr(err)));
87
88 exit:
89     return err;
90 }
91
92 static inline bool isCharUnreservedInRfc3986(const char c)
93 {
94     return isalpha(c) || isdigit(c) || (strchr(specialCharsUnreservedInRfc3986, c) != nullptr);
95 }
96
97 CHIP_ERROR EncodeQRCodeToUrl(const char * QRCode, size_t len, char * url, size_t maxSize)
98 {
99     const char upperHexDigits[] = "0123456789ABCDEF";
100     CHIP_ERROR err              = CHIP_NO_ERROR;
101     size_t i = 0, j = 0;
102
103     VerifyOrExit((QRCode != nullptr) && (url != nullptr), err = CHIP_ERROR_INVALID_ARGUMENT);
104
105     for (i = 0; i < len; ++i)
106     {
107         unsigned char c = QRCode[i];
108         if (isCharUnreservedInRfc3986(c))
109         {
110
111             VerifyOrExit((j + 1) < maxSize, err = CHIP_ERROR_BUFFER_TOO_SMALL);
112
113             url[j++] = c;
114         }
115         else
116         {
117
118             VerifyOrExit((j + 3) < maxSize, err = CHIP_ERROR_BUFFER_TOO_SMALL);
119
120             url[j++] = '%';
121             url[j++] = upperHexDigits[(c & 0xf0) >> 4];
122             url[j++] = upperHexDigits[(c & 0x0f)];
123         }
124     }
125
126     url[j] = '\0';
127
128 exit:
129     return err;
130 }