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 AdditionalData Payload parser based on the 21 : * CHIP specification. 22 : */ 23 : 24 : #include "AdditionalDataPayloadParser.h" 25 : 26 : #include <cstdlib> 27 : #include <string.h> 28 : #include <vector> 29 : 30 : #include <lib/core/CHIPError.h> 31 : #include <lib/core/TLVData.h> 32 : #include <lib/core/TLVUtilities.h> 33 : #include <lib/support/BytesToHex.h> 34 : #include <lib/support/CodeUtils.h> 35 : #include <protocols/Protocols.h> 36 : #include <setup_payload/AdditionalDataPayloadGenerator.h> 37 : 38 : namespace chip { 39 : 40 5 : CHIP_ERROR AdditionalDataPayloadParser::populatePayload(SetupPayloadData::AdditionalDataPayload & outPayload) 41 : { 42 5 : TLV::ContiguousBufferTLVReader reader; 43 5 : TLV::ContiguousBufferTLVReader innerReader; 44 : 45 5 : reader.Init(mPayloadBufferData, mPayloadBufferLength); 46 5 : ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Structure, TLV::AnonymousTag())); 47 : 48 : // Open the container 49 5 : ReturnErrorOnFailure(reader.OpenContainer(innerReader)); 50 5 : if (innerReader.Next(TLV::kTLVType_ByteString, TLV::ContextTag(SetupPayloadData::kRotatingDeviceIdTag)) == CHIP_NO_ERROR) 51 : { 52 : // Get the value of the rotating device id 53 3 : ByteSpan rotatingDeviceId; 54 3 : ReturnErrorOnFailure(innerReader.GetByteView(rotatingDeviceId)); 55 : 56 3 : VerifyOrReturnError(rotatingDeviceId.size() <= RotatingDeviceId::kMaxLength, CHIP_ERROR_INVALID_STRING_LENGTH); 57 : char rotatingDeviceIdBufferTemp[RotatingDeviceId::kHexMaxLength]; 58 : 59 2 : ReturnErrorOnFailure(Encoding::BytesToUppercaseHexString(rotatingDeviceId.data(), rotatingDeviceId.size(), 60 : rotatingDeviceIdBufferTemp, RotatingDeviceId::kHexMaxLength)); 61 2 : outPayload.rotatingDeviceId = std::string(rotatingDeviceIdBufferTemp, rotatingDeviceId.size() * 2); 62 : } 63 : else 64 : { 65 2 : outPayload.rotatingDeviceId = ""; 66 : } 67 : 68 : // Verify the end of the container 69 4 : ReturnErrorOnFailure(reader.VerifyEndOfContainer()); 70 : 71 3 : return CHIP_NO_ERROR; 72 : } 73 : 74 : } // namespace chip