Line data Source code
1 : /** 2 : * 3 : * Copyright (c) 2020 Project CHIP Authors 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 : * @file 20 : * This file describes a Manual Entry Code Generator based on the 21 : * CHIP specification. 22 : * 23 : * The encoding of the binary data to a decimal string is split 24 : * into 5 chunks <1-digit/3-bits><5 digits/16-bits><4-digits/13-bits><5-digits/16-bits><5-digits/16-bits>: 25 : * - <1 digit> Represents: 26 : * - <bits 1..0> Discriminator <bits 11.10> 27 : * - <bit 2> VID/PID present flag 28 : * - <5 digits> Represents: 29 : * - <bits 13..0> PIN Code <bits 13..0> 30 : * - <bits 15..14> Discriminator <bits 9..8> 31 : * - <4 digits> Represents: 32 : * - <bits 12..0> PIN Code <bits 26..14> 33 : * - <5 digits> Vendor ID 34 : * - <5 digits> Product ID 35 : * 36 : */ 37 : 38 : #pragma once 39 : 40 : #include "SetupPayload.h" 41 : 42 : #include <lib/core/CHIPError.h> 43 : #include <lib/support/Span.h> 44 : 45 : #include <string> 46 : 47 : namespace chip { 48 : 49 : class ManualSetupPayloadGenerator 50 : { 51 : private: 52 : PayloadContents mPayloadContents; 53 : 54 : public: 55 0 : ManualSetupPayloadGenerator(const PayloadContents & payload) : mPayloadContents(payload) {} 56 : 57 : /** 58 : * This function is called to encode the binary data of a payload to a 59 : * decimal null-terminated string. 60 : * 61 : * The resulting size of the outBuffer will be the size of data written and not including the null terminator. 62 : * 63 : * @param[out] outBuffer 64 : * Output buffer to write the decimal string. 65 : * 66 : * @retval #CHIP_NO_ERROR if the method succeeded. 67 : * @retval #CHIP_ERROR_INVALID_ARGUMENT if the payload is invalid. 68 : * @retval #CHIP_ERROR_BUFFER_TOO_SMALL if outBuffer has insufficient size. 69 : */ 70 : CHIP_ERROR payloadDecimalStringRepresentation(MutableCharSpan & outBuffer); 71 : 72 : // Populates decimal string representation of the payload into outDecimalString. 73 : // Wrapper for using std::string. 74 : CHIP_ERROR payloadDecimalStringRepresentation(std::string & outDecimalString); 75 : 76 : /** 77 : * This function disables internal checks about the validity of the generated payload. 78 : * It allows using the generator to generate invalid payloads. 79 : * Default is false. 80 : */ 81 : void SetAllowInvalidPayload(bool allow) { mAllowInvalidPayload = allow; } 82 : 83 : /** 84 : * This function allow forcing the generation of a short code when the commissioning 85 : * flow is not standard by ignoring the vendor id and product id informations but with 86 : * the VID/PID present flag set. 87 : * Default is false. 88 : */ 89 : void SetForceShortCode(bool useShort) { mForceShortCode = useShort; } 90 : 91 : private: 92 : bool mAllowInvalidPayload = false; 93 : bool mForceShortCode = false; 94 : }; 95 : 96 : } // namespace chip