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