1 /*
2  *  Copyright (c) 2019, 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 defines the OpenThread Backbone Router API (for Thread 1.2 FTD with
32  *  `OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE`).
33  */
34 
35 #include "openthread-core-config.h"
36 
37 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
38 
39 #include <openthread/backbone_router_ftd.h>
40 
41 #include "common/as_core_type.hpp"
42 #include "common/locator_getters.hpp"
43 
44 using namespace ot;
45 
otBackboneRouterSetEnabled(otInstance * aInstance,bool aEnabled)46 void otBackboneRouterSetEnabled(otInstance *aInstance, bool aEnabled)
47 {
48     return AsCoreType(aInstance).Get<BackboneRouter::Local>().SetEnabled(aEnabled);
49 }
50 
otBackboneRouterGetState(otInstance * aInstance)51 otBackboneRouterState otBackboneRouterGetState(otInstance *aInstance)
52 {
53     return MapEnum(AsCoreType(aInstance).Get<BackboneRouter::Local>().GetState());
54 }
55 
otBackboneRouterGetConfig(otInstance * aInstance,otBackboneRouterConfig * aConfig)56 void otBackboneRouterGetConfig(otInstance *aInstance, otBackboneRouterConfig *aConfig)
57 {
58     AssertPointerIsNotNull(aConfig);
59 
60     AsCoreType(aInstance).Get<BackboneRouter::Local>().GetConfig(*aConfig);
61 }
62 
otBackboneRouterSetConfig(otInstance * aInstance,const otBackboneRouterConfig * aConfig)63 otError otBackboneRouterSetConfig(otInstance *aInstance, const otBackboneRouterConfig *aConfig)
64 {
65     AssertPointerIsNotNull(aConfig);
66 
67     return AsCoreType(aInstance).Get<BackboneRouter::Local>().SetConfig(*aConfig);
68 }
69 
otBackboneRouterRegister(otInstance * aInstance)70 otError otBackboneRouterRegister(otInstance *aInstance)
71 {
72     return AsCoreType(aInstance).Get<BackboneRouter::Local>().AddService(BackboneRouter::Local::kForceRegistration);
73 }
74 
otBackboneRouterGetRegistrationJitter(otInstance * aInstance)75 uint8_t otBackboneRouterGetRegistrationJitter(otInstance *aInstance)
76 {
77     return AsCoreType(aInstance).Get<BackboneRouter::Local>().GetRegistrationJitter();
78 }
79 
otBackboneRouterSetRegistrationJitter(otInstance * aInstance,uint8_t aJitter)80 void otBackboneRouterSetRegistrationJitter(otInstance *aInstance, uint8_t aJitter)
81 {
82     return AsCoreType(aInstance).Get<BackboneRouter::Local>().SetRegistrationJitter(aJitter);
83 }
84 
otBackboneRouterGetDomainPrefix(otInstance * aInstance,otBorderRouterConfig * aConfig)85 otError otBackboneRouterGetDomainPrefix(otInstance *aInstance, otBorderRouterConfig *aConfig)
86 {
87     return AsCoreType(aInstance).Get<BackboneRouter::Local>().GetDomainPrefix(AsCoreType(aConfig));
88 }
89 
otBackboneRouterSetDomainPrefixCallback(otInstance * aInstance,otBackboneRouterDomainPrefixCallback aCallback,void * aContext)90 void otBackboneRouterSetDomainPrefixCallback(otInstance                          *aInstance,
91                                              otBackboneRouterDomainPrefixCallback aCallback,
92                                              void                                *aContext)
93 {
94     return AsCoreType(aInstance).Get<BackboneRouter::Local>().SetDomainPrefixCallback(aCallback, aContext);
95 }
96 
97 #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_DUA_NDPROXYING_ENABLE
otBackboneRouterSetNdProxyCallback(otInstance * aInstance,otBackboneRouterNdProxyCallback aCallback,void * aContext)98 void otBackboneRouterSetNdProxyCallback(otInstance                     *aInstance,
99                                         otBackboneRouterNdProxyCallback aCallback,
100                                         void                           *aContext)
101 {
102     AsCoreType(aInstance).Get<BackboneRouter::NdProxyTable>().SetCallback(aCallback, aContext);
103 }
104 
otBackboneRouterGetNdProxyInfo(otInstance * aInstance,const otIp6Address * aDua,otBackboneRouterNdProxyInfo * aNdProxyInfo)105 otError otBackboneRouterGetNdProxyInfo(otInstance                  *aInstance,
106                                        const otIp6Address          *aDua,
107                                        otBackboneRouterNdProxyInfo *aNdProxyInfo)
108 {
109     AssertPointerIsNotNull(aNdProxyInfo);
110 
111     return AsCoreType(aInstance).Get<BackboneRouter::NdProxyTable>().GetInfo(
112         reinterpret_cast<const Ip6::Address &>(*aDua), *aNdProxyInfo);
113 }
114 #endif // OPENTHREAD_CONFIG_BACKBONE_ROUTER_DUA_NDPROXYING_ENABLE
115 
116 #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE
otBackboneRouterSetMulticastListenerCallback(otInstance * aInstance,otBackboneRouterMulticastListenerCallback aCallback,void * aContext)117 void otBackboneRouterSetMulticastListenerCallback(otInstance                               *aInstance,
118                                                   otBackboneRouterMulticastListenerCallback aCallback,
119                                                   void                                     *aContext)
120 {
121     AsCoreType(aInstance).Get<BackboneRouter::MulticastListenersTable>().SetCallback(aCallback, aContext);
122 }
123 
otBackboneRouterMulticastListenerGetNext(otInstance * aInstance,otChildIp6AddressIterator * aIterator,otBackboneRouterMulticastListenerInfo * aListenerInfo)124 otError otBackboneRouterMulticastListenerGetNext(otInstance                            *aInstance,
125                                                  otChildIp6AddressIterator             *aIterator,
126                                                  otBackboneRouterMulticastListenerInfo *aListenerInfo)
127 {
128     AssertPointerIsNotNull(aIterator);
129     AssertPointerIsNotNull(aListenerInfo);
130 
131     return AsCoreType(aInstance).Get<BackboneRouter::MulticastListenersTable>().GetNext(*aIterator, *aListenerInfo);
132 }
133 #endif
134 
135 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
136 #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_DUA_NDPROXYING_ENABLE
otBackboneRouterConfigNextDuaRegistrationResponse(otInstance * aInstance,const otIp6InterfaceIdentifier * aMlIid,uint8_t aStatus)137 void otBackboneRouterConfigNextDuaRegistrationResponse(otInstance                     *aInstance,
138                                                        const otIp6InterfaceIdentifier *aMlIid,
139                                                        uint8_t                         aStatus)
140 {
141     AsCoreType(aInstance).Get<BackboneRouter::Manager>().ConfigNextDuaRegistrationResponse(AsCoreTypePtr(aMlIid),
142                                                                                            aStatus);
143 }
144 #endif
145 
146 #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE
otBackboneRouterConfigNextMulticastListenerRegistrationResponse(otInstance * aInstance,uint8_t aStatus)147 void otBackboneRouterConfigNextMulticastListenerRegistrationResponse(otInstance *aInstance, uint8_t aStatus)
148 {
149     OT_ASSERT(aStatus <= ThreadStatusTlv::kMlrStatusMax);
150 
151     AsCoreType(aInstance).Get<BackboneRouter::Manager>().ConfigNextMulticastListenerRegistrationResponse(
152         static_cast<ThreadStatusTlv::MlrStatus>(aStatus));
153 }
154 
otBackboneRouterMulticastListenerClear(otInstance * aInstance)155 void otBackboneRouterMulticastListenerClear(otInstance *aInstance)
156 {
157     AsCoreType(aInstance).Get<BackboneRouter::MulticastListenersTable>().Clear();
158 }
159 
otBackboneRouterMulticastListenerAdd(otInstance * aInstance,const otIp6Address * aAddress,uint32_t aTimeout)160 otError otBackboneRouterMulticastListenerAdd(otInstance *aInstance, const otIp6Address *aAddress, uint32_t aTimeout)
161 {
162     if (aTimeout == 0)
163     {
164         BackboneRouter::Config config;
165         AsCoreType(aInstance).Get<BackboneRouter::Local>().GetConfig(config);
166         aTimeout = config.mMlrTimeout;
167     }
168 
169     aTimeout = Min(aTimeout, BackboneRouter::kMaxMlrTimeout);
170     aTimeout = Time::SecToMsec(aTimeout);
171 
172     return AsCoreType(aInstance).Get<BackboneRouter::MulticastListenersTable>().Add(AsCoreType(aAddress),
173                                                                                     TimerMilli::GetNow() + aTimeout);
174 }
175 #endif // OPENTHREAD_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE
176 
177 #endif // OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
178 
179 #endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
180