Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2026 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 : #pragma once
19 :
20 : #include <lib/core/CHIPError.h>
21 : #include <lib/dnssd/Advertiser.h>
22 : #include <lib/support/CodeUtils.h>
23 : #include <lib/support/logging/CHIPLogging.h>
24 : #include <system/SystemPacketBuffer.h>
25 :
26 : #include <stddef.h>
27 :
28 : namespace chip {
29 : namespace app {
30 :
31 : /**
32 : * @brief Helper class to build TXT record entries for Thread MeshCoP advertisement.
33 : */
34 : class TxtStringsBuilder
35 : {
36 : public:
37 2 : TxtStringsBuilder() = default;
38 :
39 : // mNextStart and the mTxtStrings entries point into this object's own mTxtBuffer, so a defaulted
40 : // copy would leave them aliasing the source's buffer. The builder is used as a single local and is
41 : // never copied; forbid copying so an accidental copy is a compile error rather than a dangling alias.
42 : TxtStringsBuilder(const TxtStringsBuilder &) = delete;
43 : TxtStringsBuilder & operator=(const TxtStringsBuilder &) = delete;
44 :
45 : /**
46 : * @brief Fills the TXT record entries from the given advertising parameters.
47 : *
48 : * @param params The advertising parameters.
49 : * @return CHIP_ERROR CHIP_NO_ERROR on success, or CHIP_ERROR_BUFFER_TOO_SMALL if the buffer is too small.
50 : */
51 : CHIP_ERROR Fill(const Dnssd::CommissionAdvertisingParameters & params);
52 :
53 1 : size_t GetCount() const { return mCount; }
54 1 : const char ** GetEntries() const { return const_cast<const char **>(mTxtStrings); }
55 :
56 : private:
57 : CHIP_ERROR FormatAndAdd(const char * format, ...) ENFORCE_FORMAT(2, 3);
58 :
59 : static constexpr size_t kMaxTxtStringsBuffer = 256;
60 :
61 : char mTxtBuffer[kMaxTxtStringsBuffer];
62 : char * mNextStart = &mTxtBuffer[0];
63 : size_t mCount = 0;
64 : const char * mTxtStrings[Dnssd::CommissionAdvertisingParameters::kTxtMaxNumber];
65 : };
66 :
67 : /**
68 : * @brief Builds the Thread Rendezvous Announcement packet buffer.
69 : *
70 : * @param params The advertising parameters.
71 : * @param outBuffer The resulting packet buffer handle.
72 : * @return CHIP_ERROR CHIP_NO_ERROR on success, or other error on failure.
73 : */
74 : CHIP_ERROR BuildThreadRendezvousAnnouncement(const Dnssd::CommissionAdvertisingParameters & params,
75 : System::PacketBufferHandle & outBuffer);
76 :
77 : } // namespace app
78 : } // namespace chip
|