xref: /aosp_15_r20/external/ot-br-posix/src/sdp_proxy/advertising_proxy.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1 /*
2  *    Copyright (c) 2020, The OpenThread Authors.
3  *    All rights reserved.
4  *
5  *    Redistribution and use in source and binary forms, with or without
6  *    modification, are permitted provided that the following conditions are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *    POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file includes definitions for Advertising Proxy.
32  */
33 
34 #ifndef OTBR_SRP_ADVERTISING_PROXY_HPP_
35 #define OTBR_SRP_ADVERTISING_PROXY_HPP_
36 
37 #include "openthread-br/config.h"
38 
39 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY
40 
41 #include <stdint.h>
42 
43 #include <openthread/instance.h>
44 #include <openthread/srp_server.h>
45 
46 #include "common/code_utils.hpp"
47 #include "mdns/mdns.hpp"
48 #include "ncp/rcp_host.hpp"
49 
50 namespace otbr {
51 
52 /**
53  * This class implements the Advertising Proxy.
54  */
55 class AdvertisingProxy : private NonCopyable
56 {
57 public:
58     /**
59      * This constructor initializes the Advertising Proxy object.
60      *
61      * @param[in] aHost       A reference to the NCP controller.
62      * @param[in] aPublisher  A reference to the mDNS publisher.
63      */
64     explicit AdvertisingProxy(Ncp::RcpHost &aHost, Mdns::Publisher &aPublisher);
65 
66     /**
67      * This method enables/disables the Advertising Proxy.
68      *
69      * @param[in] aIsEnabled  Whether to enable the Advertising Proxy.
70      */
71     void SetEnabled(bool aIsEnabled);
72 
73     /**
74      * This method publishes all registered hosts and services.
75      */
76     void PublishAllHostsAndServices(void);
77 
78     /**
79      * This method handles mDNS publisher's state changes.
80      *
81      * @param[in] aState  The state of mDNS publisher.
82      */
83     void HandleMdnsState(Mdns::Publisher::State aState);
84 
85 private:
86     struct OutstandingUpdate
87     {
88         otSrpServerServiceUpdateId mId;                // The ID of the SRP service update transaction.
89         std::string                mHostName;          // The host name.
90         uint32_t                   mCallbackCount = 0; // The number of callbacks which we are waiting for.
91     };
92 
93     static void AdvertisingHandler(otSrpServerServiceUpdateId aId,
94                                    const otSrpServerHost     *aHost,
95                                    uint32_t                   aTimeout,
96                                    void                      *aContext);
97     void        AdvertisingHandler(otSrpServerServiceUpdateId aId, const otSrpServerHost *aHost, uint32_t aTimeout);
98 
99     static Mdns::Publisher::TxtData     MakeTxtData(const otSrpServerService *aSrpService);
100     static Mdns::Publisher::SubTypeList MakeSubTypeList(const otSrpServerService *aSrpService);
101     void                                OnMdnsPublishResult(otSrpServerServiceUpdateId aUpdateId, otbrError aError);
102 
103     std::vector<Ip6Address> GetEligibleAddresses(const otIp6Address *aHostAddresses, uint8_t aHostAddressNum);
104 
105     void Start(void);
106     void Stop(void);
IsEnabled(void) const107     bool IsEnabled(void) const { return mIsEnabled; }
108 
109     /**
110      * This method publishes a specified host and its services.
111      *
112      * It also makes a OutstandingUpdate object when needed.
113      *
114      * @param[in]  aHost         A pointer to the host.
115      * @param[in]  aUpdate       A pointer to the output OutstandingUpdate object. When it's not null, the method will
116      *                           fill its fields, otherwise it's ignored.
117      *
118      * @retval  OTBR_ERROR_NONE  Successfully published the host and its services.
119      * @retval  ...              Failed to publish the host and/or its services.
120      */
121     otbrError PublishHostAndItsServices(const otSrpServerHost *aHost, OutstandingUpdate *aUpdate);
122 
GetInstance(void)123     otInstance *GetInstance(void) { return mHost.GetInstance(); }
124 
125     // A reference to the NCP controller, has no ownership.
126     Ncp::RcpHost &mHost;
127 
128     // A reference to the mDNS publisher, has no ownership.
129     Mdns::Publisher &mPublisher;
130 
131     bool mIsEnabled;
132 
133     // A vector that tracks outstanding updates.
134     std::vector<OutstandingUpdate> mOutstandingUpdates;
135 };
136 
137 } // namespace otbr
138 
139 #endif // OTBR_ENABLE_SRP_ADVERTISING_PROXY
140 
141 #endif // OTBR_SRP_ADVERTISING_PROXY_HPP_
142