• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Copyright (c) 2017, 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 definition for Thread border agent.
32  */
33 
34 #ifndef OTBR_AGENT_BORDER_AGENT_HPP_
35 #define OTBR_AGENT_BORDER_AGENT_HPP_
36 
37 #include "openthread-br/config.h"
38 
39 #if !(OTBR_ENABLE_MDNS_AVAHI || OTBR_ENABLE_MDNS_MDNSSD || OTBR_ENABLE_MDNS_MOJO)
40 #error "Border Agent feature requires at least one `OTBR_MDNS` implementation"
41 #endif
42 
43 #include <vector>
44 
45 #include <stdint.h>
46 
47 #include "backbone_router/backbone_agent.hpp"
48 #include "common/code_utils.hpp"
49 #include "common/mainloop.hpp"
50 #include "mdns/mdns.hpp"
51 #include "ncp/ncp_openthread.hpp"
52 #include "sdp_proxy/advertising_proxy.hpp"
53 #include "sdp_proxy/discovery_proxy.hpp"
54 #include "trel_dnssd/trel_dnssd.hpp"
55 
56 #ifndef OTBR_VENDOR_NAME
57 #define OTBR_VENDOR_NAME "OpenThread"
58 #endif
59 
60 #ifndef OTBR_PRODUCT_NAME
61 #define OTBR_PRODUCT_NAME "BorderRouter"
62 #endif
63 
64 #ifndef OTBR_MESHCOP_SERVICE_INSTANCE_NAME
65 #define OTBR_MESHCOP_SERVICE_INSTANCE_NAME (OTBR_VENDOR_NAME " " OTBR_PRODUCT_NAME)
66 #endif
67 
68 namespace otbr {
69 
70 /**
71  * @addtogroup border-router-border-agent
72  *
73  * @brief
74  *   This module includes definition for Thread border agent
75  *
76  * @{
77  */
78 
79 /**
80  * This class implements Thread border agent functionality.
81  *
82  */
83 class BorderAgent : private NonCopyable
84 {
85 public:
86     /**
87      * The constructor to initialize the Thread border agent.
88      *
89      * @param[in] aNcp  A reference to the NCP controller.
90      * @param[in] aPublisher  A reference to the mDNS Publisher.
91      *
92      */
93     BorderAgent(otbr::Ncp::ControllerOpenThread &aNcp, Mdns::Publisher &aPublisher);
94 
95     ~BorderAgent(void) = default;
96 
97     /**
98      * Overrides MeshCoP service (i.e. _meshcop._udp) instance name, product name, vendor name and vendor OUI.
99      *
100      * This method must be called before this BorderAgent is enabled by SetEnabled.
101      *
102      * @param[in] aServiceInstanceName    The service instance name; suffix may be appended to this value to avoid
103      *                                    name conflicts.
104      * @param[in] aProductName            The product name; must not exceed length of kMaxProductNameLength
105      *                                    and an empty string will be ignored.
106      * @param[in] aVendorName             The vendor name; must not exceed length of kMaxVendorNameLength
107      *                                    and an empty string will be ignored.
108      * @param[in] aVendorOui              The vendor OUI; must have length of 3 bytes or be empty and ignored.
109      * @param[in] aNonStandardTxtEntries  Non-standard (vendor-specific) TXT entries whose key MUST start with "v"
110      *
111      * @returns OTBR_ERROR_INVALID_ARGS  If aVendorName, aProductName or aVendorOui exceeds the
112      *                                   allowed ranges or invalid keys are found in aNonStandardTxtEntries
113      * @returns OTBR_ERROR_NONE          If successfully set the meshcop service values.
114      */
115     otbrError SetMeshCopServiceValues(const std::string              &aServiceInstanceName,
116                                       const std::string              &aProductName,
117                                       const std::string              &aVendorName,
118                                       const std::vector<uint8_t>     &aVendorOui             = {},
119                                       const Mdns::Publisher::TxtList &aNonStandardTxtEntries = {});
120 
121     /**
122      * This method enables/disables the Border Agent.
123      *
124      * @param[in] aIsEnabled  Whether to enable the Border Agent.
125      *
126      */
127     void SetEnabled(bool aIsEnabled);
128 
129     /**
130      * This method handles mDNS publisher's state changes.
131      *
132      * @param[in] aState  The state of mDNS publisher.
133      *
134      */
135     void HandleMdnsState(Mdns::Publisher::State aState);
136 
137 private:
138     void Start(void);
139     void Stop(void);
IsEnabled(void) const140     bool IsEnabled(void) const { return mIsEnabled; }
141     void PublishMeshCopService(void);
142     void UpdateMeshCopService(void);
143     void UnpublishMeshCopService(void);
144 #if OTBR_ENABLE_DBUS_SERVER
145     void HandleUpdateVendorMeshCoPTxtEntries(std::map<std::string, std::vector<uint8_t>> aUpdate);
146 #endif
147 
148     void HandleThreadStateChanged(otChangedFlags aFlags);
149 
150     bool        IsThreadStarted(void) const;
151     std::string GetServiceInstanceNameWithExtAddr(const std::string &aServiceInstanceName) const;
152     std::string GetAlternativeServiceInstanceName() const;
153 
154     otbr::Ncp::ControllerOpenThread &mNcp;
155     Mdns::Publisher                 &mPublisher;
156     bool                             mIsEnabled;
157 
158     std::map<std::string, std::vector<uint8_t>> mMeshCopTxtUpdate;
159 
160     std::vector<uint8_t> mVendorOui;
161 
162     std::string mVendorName;
163     std::string mProductName;
164 
165     // The base service instance name typically consists of the vendor and product name. But it can
166     // also be overridden by `OTBR_MESHCOP_SERVICE_INSTANCE_NAME` or method `SetMeshCopServiceValues()`.
167     // For example, this value can be "OpenThread Border Router".
168     std::string mBaseServiceInstanceName;
169 
170     // The actual instance name advertised in the mDNS service. This is usually the value of
171     // `mBaseServiceInstanceName` plus the Extended Address and optional random number for avoiding
172     // conflicts. For example, this value can be "OpenThread Border Router #7AC3" or
173     // "OpenThread Border Router #7AC3 (14379)".
174     std::string mServiceInstanceName;
175 };
176 
177 /**
178  * @}
179  */
180 
181 } // namespace otbr
182 
183 #endif // OTBR_AGENT_BORDER_AGENT_HPP_
184