LCOV - code coverage report
Current view: top level - setup_payload - QRCodeSetupPayloadGenerator.h (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 0 2 0.0 %
Date: 2024-02-15 08:20:41 Functions: 0 2 0.0 %

          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 QRCode Setup Payload generator based on the
      21             :  *      CHIP specification.
      22             :  *
      23             :  *      The encoding of the binary data to a base38 string is as follows:
      24             :  *      - Every 2 bytes (16 bits) of binary source data are encoded to 3
      25             :  *        characters of the Base-38 alphabet.
      26             :  *      - If an odd number of bytes are to be encoded, the remaining
      27             :  *        single byte is encoded to 2 characters of the Base-38 alphabet.
      28             :  */
      29             : 
      30             : #include "SetupPayload.h"
      31             : 
      32             : #include <lib/support/Span.h>
      33             : 
      34             : #pragma once
      35             : 
      36             : namespace chip {
      37             : 
      38             : class QRCodeSetupPayloadGenerator
      39             : {
      40             : private:
      41             :     SetupPayload mPayload;
      42             : 
      43             : public:
      44           0 :     QRCodeSetupPayloadGenerator(const SetupPayload & setupPayload) : mPayload(setupPayload) {}
      45             : 
      46             :     /**
      47             :      * This function is called to encode the binary data of a payload to a
      48             :      * base38 null-terminated string.
      49             :      *
      50             :      * If the payload has any optional data that needs to be TLV encoded, this
      51             :      * function will fail.
      52             :      *
      53             :      * @param[out] base38Representation
      54             :      *                  The string to copy the base38 to.
      55             :      *
      56             :      * @retval #CHIP_NO_ERROR if the method succeeded.
      57             :      * @retval #CHIP_ERROR_INVALID_ARGUMENT if the payload is invalid.
      58             :      * @retval other Other CHIP or platform-specific error codes indicating
      59             :      *               that an error occurred preventing the function from
      60             :      *               producing the requested string.
      61             :      */
      62             :     CHIP_ERROR payloadBase38Representation(std::string & base38Representation);
      63             : 
      64             :     /**
      65             :      * This function is called to encode the binary data of a payload to a
      66             :      * base38 null-terminated string.
      67             :      *
      68             :      * If the payload has any optional data that needs to be TLV encoded, this
      69             :      * function will allocate a scratch heap buffer to hold the TLV data while
      70             :      * encoding.
      71             :      *
      72             :      * @param[out] base38Representation
      73             :      *                  The string to copy the base38 to.
      74             :      *
      75             :      * @retval #CHIP_NO_ERROR if the method succeeded.
      76             :      * @retval #CHIP_ERROR_INVALID_ARGUMENT if the payload is invalid.
      77             :      * @retval other Other CHIP or platform-specific error codes indicating
      78             :      *               that an error occurred preventing the function from
      79             :      *               producing the requested string.
      80             :      */
      81             :     CHIP_ERROR payloadBase38RepresentationWithAutoTLVBuffer(std::string & base38Representation);
      82             : 
      83             :     /**
      84             :      * This function is called to encode the binary data of a payload to a
      85             :      * base38 null-terminated string, using the caller-provided buffer as
      86             :      * temporary scratch space for optional data that needs to be TLV-encoded.
      87             :      * If that buffer is not big enough to hold the TLV-encoded part of the
      88             :      * payload, this function will fail.
      89             :      *
      90             :      * @param[out] base38Representation
      91             :      *                  The string to copy the base38 to.
      92             :      * @param[in]  tlvDataStart
      93             :      *                  A pointer to an uint8_t buffer into which the TLV
      94             :      *                  should be written.
      95             :      * @param[in]  tlvDataStartSize
      96             :      *                  The maximum number of bytes that should be written to
      97             :      *                  the output buffer.
      98             :      *
      99             :      * @retval #CHIP_NO_ERROR if the method succeeded.
     100             :      * @retval #CHIP_ERROR_INVALID_ARGUMENT if the payload is invalid.
     101             :      * @retval other Other CHIP or platform-specific error codes indicating
     102             :      *               that an error occurred preventing the function from
     103             :      *               producing the requested string.
     104             :      */
     105             :     CHIP_ERROR payloadBase38Representation(std::string & base38Representation, uint8_t * tlvDataStart, uint32_t tlvDataStartSize);
     106             : 
     107             :     /**
     108             :      * This function disables internal checks about the validity of the generated payload.
     109             :      * It allows using the generator to generate invalid payloads.
     110             :      * Default is false.
     111             :      */
     112             :     void SetAllowInvalidPayload(bool allow) { mAllowInvalidPayload = allow; }
     113             : 
     114             : private:
     115             :     CHIP_ERROR generateTLVFromOptionalData(SetupPayload & outPayload, uint8_t * tlvDataStart, uint32_t maxLen,
     116             :                                            size_t & tlvDataLengthInBytes);
     117             : 
     118             :     bool mAllowInvalidPayload = false;
     119             : };
     120             : 
     121             : /**
     122             :  * A minimal QR code setup payload generator that omits any optional data,
     123             :  * for compatibility with devices that don't support std::string or STL.
     124             :  */
     125             : class QRCodeBasicSetupPayloadGenerator
     126             : {
     127             : private:
     128             :     PayloadContents mPayload;
     129             : 
     130             : public:
     131           0 :     QRCodeBasicSetupPayloadGenerator(const PayloadContents & payload) : mPayload(payload) {}
     132             : 
     133             :     /**
     134             :      * This function is called to encode the binary data of a payload to a
     135             :      * base38 null-terminated string.
     136             :      *
     137             :      * The resulting size of the out_buf span will be the size of data written
     138             :      * and not including the null terminator.
     139             :      *
     140             :      * This function will fail if the payload has any optional data requiring
     141             :      * TLV encoding.
     142             :      *
     143             :      * @param[out] outBuffer
     144             :      *                  The buffer to copy the base38 to.
     145             :      *
     146             :      * @retval #CHIP_NO_ERROR if the method succeeded.
     147             :      * @retval #CHIP_ERROR_INVALID_ARGUMENT if the payload is invalid.
     148             :      * @retval #CHIP_ERROR_BUFFER_TOO_SMALL if outBuffer has insufficient size.
     149             :      * @retval other Other CHIP or platform-specific error codes indicating
     150             :      *               that an error occurred preventing the function from
     151             :      *               producing the requested string.
     152             :      */
     153             :     CHIP_ERROR payloadBase38Representation(MutableCharSpan & outBuffer);
     154             : 
     155             :     // TODO - Find the optimal value for maximum length of QR Code Base38 string
     156             :     static constexpr uint16_t kMaxQRCodeBase38RepresentationLength = 128;
     157             : };
     158             : 
     159             : } // namespace chip

Generated by: LCOV version 1.14