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