1 /*
2  *  Copyright (c) 2016-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 implements the OpenThread Border Router API.
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
37 
38 #include <openthread/border_router.h>
39 
40 #include "border_router/routing_manager.hpp"
41 #include "common/debug.hpp"
42 #include "instance/instance.hpp"
43 
44 using namespace ot;
45 
otBorderRouterGetNetData(otInstance * aInstance,bool aStable,uint8_t * aData,uint8_t * aDataLength)46 otError otBorderRouterGetNetData(otInstance *aInstance, bool aStable, uint8_t *aData, uint8_t *aDataLength)
47 {
48     return AsCoreType(aInstance).Get<NetworkData::Local>().CopyNetworkData(
49         aStable ? NetworkData::kStableSubset : NetworkData::kFullSet, aData, *aDataLength);
50 }
51 
otBorderRouterAddOnMeshPrefix(otInstance * aInstance,const otBorderRouterConfig * aConfig)52 otError otBorderRouterAddOnMeshPrefix(otInstance *aInstance, const otBorderRouterConfig *aConfig)
53 {
54     Error error = kErrorNone;
55 
56 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
57     if (aConfig->mDp)
58     {
59         error = AsCoreType(aInstance).Get<BackboneRouter::Local>().SetDomainPrefix(AsCoreType(aConfig));
60     }
61     else
62 #endif
63     {
64         error = AsCoreType(aInstance).Get<NetworkData::Local>().AddOnMeshPrefix(AsCoreType(aConfig));
65     }
66 
67     return error;
68 }
69 
otBorderRouterRemoveOnMeshPrefix(otInstance * aInstance,const otIp6Prefix * aPrefix)70 otError otBorderRouterRemoveOnMeshPrefix(otInstance *aInstance, const otIp6Prefix *aPrefix)
71 {
72     Error error = kErrorNone;
73 
74 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
75     error = AsCoreType(aInstance).Get<BackboneRouter::Local>().RemoveDomainPrefix(AsCoreType(aPrefix));
76 
77     if (error == kErrorNotFound)
78 #endif
79     {
80         error = AsCoreType(aInstance).Get<NetworkData::Local>().RemoveOnMeshPrefix(AsCoreType(aPrefix));
81     }
82 
83     return error;
84 }
85 
otBorderRouterGetNextOnMeshPrefix(otInstance * aInstance,otNetworkDataIterator * aIterator,otBorderRouterConfig * aConfig)86 otError otBorderRouterGetNextOnMeshPrefix(otInstance            *aInstance,
87                                           otNetworkDataIterator *aIterator,
88                                           otBorderRouterConfig  *aConfig)
89 {
90     AssertPointerIsNotNull(aIterator);
91 
92     return AsCoreType(aInstance).Get<NetworkData::Local>().GetNextOnMeshPrefix(*aIterator, AsCoreType(aConfig));
93 }
94 
otBorderRouterAddRoute(otInstance * aInstance,const otExternalRouteConfig * aConfig)95 otError otBorderRouterAddRoute(otInstance *aInstance, const otExternalRouteConfig *aConfig)
96 {
97     return AsCoreType(aInstance).Get<NetworkData::Local>().AddHasRoutePrefix(AsCoreType(aConfig));
98 }
99 
otBorderRouterRemoveRoute(otInstance * aInstance,const otIp6Prefix * aPrefix)100 otError otBorderRouterRemoveRoute(otInstance *aInstance, const otIp6Prefix *aPrefix)
101 {
102     return AsCoreType(aInstance).Get<NetworkData::Local>().RemoveHasRoutePrefix(AsCoreType(aPrefix));
103 }
104 
otBorderRouterGetNextRoute(otInstance * aInstance,otNetworkDataIterator * aIterator,otExternalRouteConfig * aConfig)105 otError otBorderRouterGetNextRoute(otInstance            *aInstance,
106                                    otNetworkDataIterator *aIterator,
107                                    otExternalRouteConfig *aConfig)
108 {
109     AssertPointerIsNotNull(aIterator);
110 
111     return AsCoreType(aInstance).Get<NetworkData::Local>().GetNextExternalRoute(*aIterator, AsCoreType(aConfig));
112 }
113 
otBorderRouterRegister(otInstance * aInstance)114 otError otBorderRouterRegister(otInstance *aInstance)
115 {
116     AsCoreType(aInstance).Get<NetworkData::Notifier>().HandleServerDataUpdated();
117 
118     return kErrorNone;
119 }
120 
121 #if OPENTHREAD_CONFIG_BORDER_ROUTER_SIGNAL_NETWORK_DATA_FULL
otBorderRouterSetNetDataFullCallback(otInstance * aInstance,otBorderRouterNetDataFullCallback aCallback,void * aContext)122 void otBorderRouterSetNetDataFullCallback(otInstance                       *aInstance,
123                                           otBorderRouterNetDataFullCallback aCallback,
124                                           void                             *aContext)
125 {
126     AsCoreType(aInstance).Get<NetworkData::Notifier>().SetNetDataFullCallback(aCallback, aContext);
127 }
128 #endif
129 
130 #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
131