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  * @brief
32  *   This file includes the OpenThread API for channel monitoring feature
33  */
34 
35 #ifndef OPENTHREAD_CHANNEL_MONITOR_H_
36 #define OPENTHREAD_CHANNEL_MONITOR_H_
37 
38 #include <openthread/instance.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * @addtogroup api-channel-monitor
46  *
47  * @brief
48  *   This module includes functions for channel monitoring feature.
49  *
50  *   The functions in this module are available when channel monitor feature
51  *   (`OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE`) is enabled.
52  *
53  *   Channel monitoring will periodically monitor all channels to help determine the cleaner channels (channels
54  *   with less interference).
55  *
56  *   When channel monitoring is active, a zero-duration Energy Scan is performed, collecting a single RSSI sample on
57  *   every channel per sample interval. The RSSI samples are compared with a pre-specified RSSI threshold. As an
58  *   indicator of channel quality, the channel monitoring module maintains and provides the average rate/percentage of
59  *   RSSI samples that are above the threshold within (approximately) a specified sample window (referred to as channel
60  *   occupancy).
61  *
62  * @{
63  *
64  */
65 
66 /**
67  * This function enables/disables the Channel Monitoring operation.
68  *
69  * Once operation starts, any previously collected data is cleared. However, after operation is disabled, the previous
70  * collected data is still valid and can be read.
71  *
72  * @note OpenThread core internally enables/disables the Channel Monitoring operation when the IPv6 interface is
73  * brought up/down (i.e., call to `otIp6SetEnabled()`).
74  *
75  * @param[in]  aInstance       A pointer to an OpenThread instance.
76  * @param[in]  aEnabled        TRUE to enable/start Channel Monitoring operation, FALSE to disable/stop it.
77  *
78  * @retval OT_ERROR_NONE      Channel Monitoring state changed successfully
79  * @retval OT_ERROR_ALREADY   Channel Monitoring is already in the same state.
80  *
81  */
82 otError otChannelMonitorSetEnabled(otInstance *aInstance, bool aEnabled);
83 
84 /**
85  * This function indicates whether the Channel Monitoring operation is enabled and running.
86  *
87  * @param[in]  aInstance       A pointer to an OpenThread instance.
88  *
89  * @returns TRUE if the Channel Monitoring operation is enabled, FALSE otherwise.
90  *
91  */
92 bool otChannelMonitorIsEnabled(otInstance *aInstance);
93 
94 /**
95  * Get channel monitoring sample interval in milliseconds.
96  *
97  * @param[in]  aInstance       A pointer to an OpenThread instance.
98  *
99  * @returns  The channel monitor sample interval in milliseconds.
100  *
101  */
102 uint32_t otChannelMonitorGetSampleInterval(otInstance *aInstance);
103 
104 /**
105  * Get channel monitoring RSSI threshold in dBm.
106  *
107  * @param[in]  aInstance       A pointer to an OpenThread instance.
108  *
109  * @returns  The RSSI threshold in dBm.
110  *
111  */
112 int8_t otChannelMonitorGetRssiThreshold(otInstance *aInstance);
113 
114 /**
115  * Get channel monitoring averaging sample window length (number of samples).
116  *
117  * @param[in]  aInstance       A pointer to an OpenThread instance.
118  *
119  * @returns  The averaging sample window.
120  *
121  */
122 uint32_t otChannelMonitorGetSampleWindow(otInstance *aInstance);
123 
124 /**
125  * Get channel monitoring total number of RSSI samples (per channel).
126  *
127  * The count indicates total number samples per channel by channel monitoring module since its start (since Thread
128  * network interface was enabled).
129  *
130  * @param[in]  aInstance       A pointer to an OpenThread instance.
131  *
132  * @returns  Total number of RSSI samples (per channel) taken so far.
133  *
134  */
135 uint32_t otChannelMonitorGetSampleCount(otInstance *aInstance);
136 
137 /**
138  * Gets the current channel occupancy for a given channel.
139  *
140  * The channel occupancy value represents the average rate/percentage of RSSI samples that were above RSSI threshold
141  * ("bad" RSSI samples).
142  *
143  * For the first "sample window" samples, the average is maintained as the actual percentage (i.e., ratio of number
144  * of "bad" samples by total number of samples). After "window" samples, the averager uses an exponentially
145  * weighted moving average. Practically, this means the average is representative of up to `3 * window` last samples
146  * with highest weight given to the latest `kSampleWindow` samples.
147  *
148  * Max value of `0xffff` indicates all RSSI samples were above RSSI threshold (i.e. 100% of samples were "bad").
149  *
150  * @param[in]  aInstance       A pointer to an OpenThread instance.
151  * @param[in]  aChannel        The channel for which to get the link occupancy.
152  *
153  * @returns The current channel occupancy for the given channel.
154  *
155  */
156 uint16_t otChannelMonitorGetChannelOccupancy(otInstance *aInstance, uint8_t aChannel);
157 
158 /**
159  * @}
160  *
161  */
162 
163 #ifdef __cplusplus
164 } // extern "C"
165 #endif
166 
167 #endif // OPENTHREAD_CHANNEL_MONITOR_H_
168