1 /*
2  *  Copyright (c) 2016, 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 jam detection feature.
33  */
34 
35 #ifndef OPENTHREAD_JAM_DETECTION_H_
36 #define OPENTHREAD_JAM_DETECTION_H_
37 
38 #include <openthread/instance.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * @addtogroup api-jam-detection
46  *
47  * @brief
48  *   This module includes functions for signal jamming detection feature.
49  *
50  *   The functions in this module are available when jam-detection feature (`OPENTHREAD_CONFIG_JAM_DETECTION_ENABLE`)
51  *   is enabled.
52  *
53  * @{
54  *
55  */
56 
57 /**
58  * Pointer is called if signal jam detection is enabled and a jam is detected.
59  *
60  * @param[in]  aJamState Current jam state (`true` if jam is detected, `false` otherwise).
61  * @param[in]  aContext  A pointer to application-specific context.
62  *
63  */
64 typedef void (*otJamDetectionCallback)(bool aJamState, void *aContext);
65 
66 /**
67  * Set the Jam Detection RSSI Threshold (in dBm).
68  *
69  * @param[in]  aInstance       A pointer to an OpenThread instance.
70  * @param[in]  aRssiThreshold  The RSSI threshold.
71  *
72  * @retval OT_ERROR_NONE    Successfully set the threshold.
73  *
74  */
75 otError otJamDetectionSetRssiThreshold(otInstance *aInstance, int8_t aRssiThreshold);
76 
77 /**
78  * Get the Jam Detection RSSI Threshold (in dBm).
79  *
80  * @param[in]  aInstance A pointer to an OpenThread instance.
81  *
82  * @returns The Jam Detection RSSI Threshold.
83  */
84 int8_t otJamDetectionGetRssiThreshold(otInstance *aInstance);
85 
86 /**
87  * Set the Jam Detection Detection Window (in seconds).
88  *
89  * @param[in]  aInstance            A pointer to an OpenThread instance.
90  * @param[in]  aWindow              The Jam Detection window (valid range is 1 to 63)
91  *
92  * @retval OT_ERROR_NONE          Successfully set the window.
93  * @retval OT_ERROR_INVALID_ARGS  The given input parameter not within valid range (1-63)
94  *
95  */
96 otError otJamDetectionSetWindow(otInstance *aInstance, uint8_t aWindow);
97 
98 /**
99  * Get the Jam Detection Detection Window (in seconds).
100  *
101  * @param[in]  aInstance            A pointer to an OpenThread instance.
102  *
103  * @returns The Jam Detection Window.
104  *
105  */
106 uint8_t otJamDetectionGetWindow(otInstance *aInstance);
107 
108 /**
109  * Set the Jam Detection Busy Period (in seconds).
110  *
111  * The number of aggregate seconds within the detection window where the RSSI must be above
112  * threshold to trigger detection.
113  *
114  * @param[in]  aInstance            A pointer to an OpenThread instance.
115  * @param[in]  aBusyPeriod          The Jam Detection busy period (should be non-zero and
116                                     less than or equal to Jam Detection Window)
117  *
118  * @retval OT_ERROR_NONE          Successfully set the window.
119  * @retval OT_ERROR_INVALID_ARGS  The given input is not within the valid range.
120  *
121  */
122 otError otJamDetectionSetBusyPeriod(otInstance *aInstance, uint8_t aBusyPeriod);
123 
124 /**
125  * Get the Jam Detection Busy Period (in seconds)
126  *
127  * @param[in]  aInstance            A pointer to an OpenThread instance.
128  *
129  * @returns The Jam Detection Busy Period.
130  *
131  */
132 uint8_t otJamDetectionGetBusyPeriod(otInstance *aInstance);
133 
134 /**
135  * Start the jamming detection.
136  *
137  * @param[in]  aInstance            A pointer to an OpenThread instance.
138  * @param[in]  aCallback            A pointer to a function called to notify of jamming state change.
139  * @param[in]  aContext             A pointer to application-specific context.
140  *
141  * @retval OT_ERROR_NONE         Successfully started the jamming detection.
142  * @retval OT_ERROR_ALREADY      Jam detection has been started before.
143  *
144  */
145 otError otJamDetectionStart(otInstance *aInstance, otJamDetectionCallback aCallback, void *aContext);
146 
147 /**
148  * Stop the jamming detection.
149  *
150  * @param[in]  aInstance            A pointer to an OpenThread instance.
151  *
152  * @retval OT_ERROR_NONE         Successfully stopped the jamming detection.
153  * @retval OT_ERROR_ALREADY      Jam detection is already stopped.
154  *
155  */
156 otError otJamDetectionStop(otInstance *aInstance);
157 
158 /**
159  * Get the Jam Detection Status (enabled/disabled)
160  *
161  * @param[in]  aInstance            A pointer to an OpenThread instance.
162  *
163  * @returns The Jam Detection status (true if enabled, false otherwise).
164  *
165  */
166 bool otJamDetectionIsEnabled(otInstance *aInstance);
167 
168 /**
169  * Get the Jam Detection State
170  *
171  * @param[in]  aInstance            A pointer to an OpenThread instance.
172  *
173  * @returns The Jam Detection state (`true` jam is detected, `false' otherwise).
174  *
175  */
176 bool otJamDetectionGetState(otInstance *aInstance);
177 
178 /**
179  * Get the current history bitmap.
180  *
181  * This value provides information about current state of jamming detection
182  * module for monitoring/debugging purpose. It returns a 64-bit value where
183  * each bit corresponds to one second interval starting with bit 0 for the
184  * most recent interval and bit 63 for the oldest intervals (63 sec earlier).
185  * The bit is set to 1 if the jamming detection module observed/detected
186  * high signal level during the corresponding one second interval.
187  *
188  * @param[in]  aInstance            A pointer to an OpenThread instance.
189  *
190  * @returns The current history bitmap.
191  *
192  */
193 uint64_t otJamDetectionGetHistoryBitmap(otInstance *aInstance);
194 
195 /**
196  * @}
197  *
198  */
199 
200 #ifdef __cplusplus
201 } // extern "C"
202 #endif
203 
204 #endif // OPENTHREAD_JAM_DETECTION_H_
205