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