1 /*
2  *  Copyright (c) 2018, 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 channel manager APIs.
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #if OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE && \
37     (OPENTHREAD_FTD ||                          \
38      (OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE && OPENTHREAD_CONFIG_CHANNEL_MANAGER_CSL_CHANNEL_SELECT_ENABLE))
39 
40 #include <openthread/channel_manager.h>
41 
42 #include "common/as_core_type.hpp"
43 #include "common/locator_getters.hpp"
44 #include "utils/channel_manager.hpp"
45 
46 using namespace ot;
47 
48 #if OPENTHREAD_FTD
otChannelManagerRequestChannelChange(otInstance * aInstance,uint8_t aChannel)49 void otChannelManagerRequestChannelChange(otInstance *aInstance, uint8_t aChannel)
50 {
51     AsCoreType(aInstance).Get<Utils::ChannelManager>().RequestNetworkChannelChange(aChannel);
52 }
53 #endif
54 
otChannelManagerGetRequestedChannel(otInstance * aInstance)55 uint8_t otChannelManagerGetRequestedChannel(otInstance *aInstance)
56 {
57     return AsCoreType(aInstance).Get<Utils::ChannelManager>().GetRequestedChannel();
58 }
59 
60 #if OPENTHREAD_FTD
otChannelManagerGetDelay(otInstance * aInstance)61 uint16_t otChannelManagerGetDelay(otInstance *aInstance)
62 {
63     return AsCoreType(aInstance).Get<Utils::ChannelManager>().GetDelay();
64 }
65 
otChannelManagerSetDelay(otInstance * aInstance,uint16_t aDelay)66 otError otChannelManagerSetDelay(otInstance *aInstance, uint16_t aDelay)
67 {
68     return AsCoreType(aInstance).Get<Utils::ChannelManager>().SetDelay(aDelay);
69 }
70 
71 #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
otChannelManagerRequestChannelSelect(otInstance * aInstance,bool aSkipQualityCheck)72 otError otChannelManagerRequestChannelSelect(otInstance *aInstance, bool aSkipQualityCheck)
73 {
74     return AsCoreType(aInstance).Get<Utils::ChannelManager>().RequestNetworkChannelSelect(aSkipQualityCheck);
75 }
76 #endif
77 
otChannelManagerSetAutoChannelSelectionEnabled(otInstance * aInstance,bool aEnabled)78 void otChannelManagerSetAutoChannelSelectionEnabled(otInstance *aInstance, bool aEnabled)
79 {
80     AsCoreType(aInstance).Get<Utils::ChannelManager>().SetAutoNetworkChannelSelectionEnabled(aEnabled);
81 }
82 
otChannelManagerGetAutoChannelSelectionEnabled(otInstance * aInstance)83 bool otChannelManagerGetAutoChannelSelectionEnabled(otInstance *aInstance)
84 {
85     return AsCoreType(aInstance).Get<Utils::ChannelManager>().GetAutoNetworkChannelSelectionEnabled();
86 }
87 #endif // OPENTHREAD_FTD
88 
89 #if (OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE && OPENTHREAD_CONFIG_CHANNEL_MANAGER_CSL_CHANNEL_SELECT_ENABLE)
90 #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
otChannelManagerRequestCslChannelSelect(otInstance * aInstance,bool aSkipQualityCheck)91 otError otChannelManagerRequestCslChannelSelect(otInstance *aInstance, bool aSkipQualityCheck)
92 {
93     return AsCoreType(aInstance).Get<Utils::ChannelManager>().RequestCslChannelSelect(aSkipQualityCheck);
94 }
95 #endif
96 
otChannelManagerSetAutoCslChannelSelectionEnabled(otInstance * aInstance,bool aEnabled)97 void otChannelManagerSetAutoCslChannelSelectionEnabled(otInstance *aInstance, bool aEnabled)
98 {
99     AsCoreType(aInstance).Get<Utils::ChannelManager>().SetAutoCslChannelSelectionEnabled(aEnabled);
100 }
101 
otChannelManagerGetAutoCslChannelSelectionEnabled(otInstance * aInstance)102 bool otChannelManagerGetAutoCslChannelSelectionEnabled(otInstance *aInstance)
103 {
104     return AsCoreType(aInstance).Get<Utils::ChannelManager>().GetAutoCslChannelSelectionEnabled();
105 }
106 #endif
107 
otChannelManagerSetAutoChannelSelectionInterval(otInstance * aInstance,uint32_t aInterval)108 otError otChannelManagerSetAutoChannelSelectionInterval(otInstance *aInstance, uint32_t aInterval)
109 {
110     return AsCoreType(aInstance).Get<Utils::ChannelManager>().SetAutoChannelSelectionInterval(aInterval);
111 }
112 
otChannelManagerGetAutoChannelSelectionInterval(otInstance * aInstance)113 uint32_t otChannelManagerGetAutoChannelSelectionInterval(otInstance *aInstance)
114 {
115     return AsCoreType(aInstance).Get<Utils::ChannelManager>().GetAutoChannelSelectionInterval();
116 }
117 
otChannelManagerGetSupportedChannels(otInstance * aInstance)118 uint32_t otChannelManagerGetSupportedChannels(otInstance *aInstance)
119 {
120     return AsCoreType(aInstance).Get<Utils::ChannelManager>().GetSupportedChannels();
121 }
122 
otChannelManagerSetSupportedChannels(otInstance * aInstance,uint32_t aChannelMask)123 void otChannelManagerSetSupportedChannels(otInstance *aInstance, uint32_t aChannelMask)
124 {
125     return AsCoreType(aInstance).Get<Utils::ChannelManager>().SetSupportedChannels(aChannelMask);
126 }
127 
otChannelManagerGetFavoredChannels(otInstance * aInstance)128 uint32_t otChannelManagerGetFavoredChannels(otInstance *aInstance)
129 {
130     return AsCoreType(aInstance).Get<Utils::ChannelManager>().GetFavoredChannels();
131 }
132 
otChannelManagerSetFavoredChannels(otInstance * aInstance,uint32_t aChannelMask)133 void otChannelManagerSetFavoredChannels(otInstance *aInstance, uint32_t aChannelMask)
134 {
135     return AsCoreType(aInstance).Get<Utils::ChannelManager>().SetFavoredChannels(aChannelMask);
136 }
137 
otChannelManagerGetCcaFailureRateThreshold(otInstance * aInstance)138 uint16_t otChannelManagerGetCcaFailureRateThreshold(otInstance *aInstance)
139 {
140     return AsCoreType(aInstance).Get<Utils::ChannelManager>().GetCcaFailureRateThreshold();
141 }
142 
otChannelManagerSetCcaFailureRateThreshold(otInstance * aInstance,uint16_t aThreshold)143 void otChannelManagerSetCcaFailureRateThreshold(otInstance *aInstance, uint16_t aThreshold)
144 {
145     return AsCoreType(aInstance).Get<Utils::ChannelManager>().SetCcaFailureRateThreshold(aThreshold);
146 }
147 
148 #endif // OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE && OPENTHREAD_FTD
149