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 defines the top-level functions for the OpenThread NCP module.
33  */
34 
35 #ifndef OPENTHREAD_NCP_H_
36 #define OPENTHREAD_NCP_H_
37 
38 #include <stdarg.h>
39 
40 #include <openthread/platform/logging.h>
41 #include <openthread/platform/radio.h>
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /**
48  * @addtogroup api-ncp
49  *
50  * @brief
51  *   This module includes functions that control the Thread stack's execution.
52  *
53  * @{
54  *
55  */
56 
57 /**
58  * This function pointer is called to send HDLC encoded NCP data.
59  *
60  * @param[in]  aBuf        A pointer to a buffer with an output.
61  * @param[in]  aBufLength  A length of the output data stored in the buffer.
62  *
63  * @returns                Number of bytes processed by the callback.
64  *
65  */
66 typedef int (*otNcpHdlcSendCallback)(const uint8_t *aBuf, uint16_t aBufLength);
67 
68 /**
69  * This function is called after NCP send finished.
70  *
71  */
72 void otNcpHdlcSendDone(void);
73 
74 /**
75  * This function is called after HDLC encoded NCP data received.
76  *
77  * @param[in]  aBuf        A pointer to a buffer.
78  * @param[in]  aBufLength  The length of the data stored in the buffer.
79  *
80  */
81 void otNcpHdlcReceive(const uint8_t *aBuf, uint16_t aBufLength);
82 
83 /**
84  * Initialize the NCP based on HDLC framing.
85  *
86  * @param[in]  aInstance        The OpenThread instance structure.
87  * @param[in]  aSendCallback    The function pointer used to send NCP data.
88  *
89  */
90 void otNcpHdlcInit(otInstance *aInstance, otNcpHdlcSendCallback aSendCallback);
91 
92 /**
93  * Initialize the NCP based on SPI framing.
94  *
95  * @param[in]  aInstance  The OpenThread instance structure.
96  *
97  */
98 void otNcpSpiInit(otInstance *aInstance);
99 
100 /**
101  * @brief Send data to the host via a specific stream.
102  *
103  * This function attempts to send the given data to the host
104  * using the given aStreamId. This is useful for reporting
105  * error messages, implementing debug/diagnostic consoles,
106  * and potentially other types of datastreams.
107  *
108  * The write either is accepted in its entirety or rejected.
109  * Partial writes are not attempted.
110  *
111  * @param[in]  aStreamId  A numeric identifier for the stream to write to.
112  *                        If set to '0', will default to the debug stream.
113  * @param[in]  aDataPtr   A pointer to the data to send on the stream.
114  *                        If aDataLen is non-zero, this param MUST NOT be NULL.
115  * @param[in]  aDataLen   The number of bytes of data from aDataPtr to send.
116  *
117  * @retval OT_ERROR_NONE         The data was queued for delivery to the host.
118  * @retval OT_ERROR_BUSY         There are not enough resources to complete this
119  *                               request. This is usually a temporary condition.
120  * @retval OT_ERROR_INVALID_ARGS The given aStreamId was invalid.
121  */
122 otError otNcpStreamWrite(int aStreamId, const uint8_t *aDataPtr, int aDataLen);
123 
124 /**
125  * Writes OpenThread Log using `otNcpStreamWrite`.
126  *
127  * @param[in]  aLogLevel   The log level.
128  * @param[in]  aLogRegion  The log region.
129  * @param[in]  aFormat     A pointer to the format string.
130  * @param[in]  aArgs       va_list matching aFormat.
131  */
132 void otNcpPlatLogv(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aArgs);
133 
134 //-----------------------------------------------------------------------------------------
135 // Peek/Poke memory access control delegates
136 
137 /**
138  * Defines delegate (function pointer) type to control behavior of peek/poke operation.
139  *
140  * This delegate function is called to decide whether to allow peek or poke of a specific memory region. It is used
141  * if NCP support for peek/poke commands is enabled.
142  *
143  * @param[in] aAddress    Start address of memory region.
144  * @param[in] aCount      Number of bytes to peek or poke.
145  *
146  * @returns  TRUE to allow peek/poke of the given memory region, FALSE otherwise.
147  *
148  */
149 typedef bool (*otNcpDelegateAllowPeekPoke)(uint32_t aAddress, uint16_t aCount);
150 
151 /**
152  * This method registers peek/poke delegate functions with NCP module.
153  *
154  * The delegate functions are called by NCP module to decide whether to allow peek or poke of a specific memory region.
155  * If the delegate pointer is set to NULL, it allows peek/poke operation for any address.
156  *
157  * @param[in] aAllowPeekDelegate      Delegate function pointer for peek operation.
158  * @param[in] aAllowPokeDelegate      Delegate function pointer for poke operation.
159  *
160  */
161 void otNcpRegisterPeekPokeDelagates(otNcpDelegateAllowPeekPoke aAllowPeekDelegate,
162                                     otNcpDelegateAllowPeekPoke aAllowPokeDelegate);
163 
164 //-----------------------------------------------------------------------------------------
165 // Legacy network APIs
166 
167 #define OT_NCP_LEGACY_ULA_PREFIX_LENGTH 8 ///< Legacy ULA size (in bytes)
168 
169 /**
170  * Defines handler (function pointer) type for starting legacy network
171  *
172  * Invoked to start the legacy network.
173  *
174  */
175 typedef void (*otNcpHandlerStartLegacy)(void);
176 
177 /**
178  * Defines handler (function pointer) type for stopping legacy network
179  *
180  * Invoked to stop the legacy network.
181  *
182  */
183 typedef void (*otNcpHandlerStopLegacy)(void);
184 
185 /**
186  * Defines handler (function pointer) type for initiating joining process.
187  *
188  * @param[in] aExtAddress   A pointer to the extended address for the node to join
189  *                          or NULL if desired to join any neighboring node.
190  *
191  * Invoked to initiate a legacy join procedure to any or a specific node.
192  *
193  */
194 typedef void (*otNcpHandlerJoinLegacyNode)(const otExtAddress *aExtAddress);
195 
196 /**
197  * Defines handler (function pointer) type for setting the legacy ULA prefix.
198  *
199  * @param[in] aUlaPrefix   A pointer to buffer containing the legacy ULA prefix.
200  *
201  * Invoked to set the legacy ULA prefix.
202  *
203  */
204 typedef void (*otNcpHandlerSetLegacyUlaPrefix)(const uint8_t *aUlaPrefix);
205 
206 /**
207  * Defines a struct containing all the legacy handlers (function pointers).
208  *
209  */
210 typedef struct otNcpLegacyHandlers
211 {
212     otNcpHandlerStartLegacy        mStartLegacy;        ///< Start handler
213     otNcpHandlerStopLegacy         mStopLegacy;         ///< Stop handler
214     otNcpHandlerJoinLegacyNode     mJoinLegacyNode;     ///< Join handler
215     otNcpHandlerSetLegacyUlaPrefix mSetLegacyUlaPrefix; ///< Set ULA handler
216 } otNcpLegacyHandlers;
217 
218 /**
219  * This callback is invoked by the legacy stack to notify that a new
220  * legacy node did join the network.
221  *
222  * @param[in]   aExtAddr    A pointer to the extended address of the joined node.
223  *
224  */
225 void otNcpHandleLegacyNodeDidJoin(const otExtAddress *aExtAddr);
226 
227 /**
228  * This callback is invoked by the legacy stack to notify that the
229  * legacy ULA prefix has changed.
230  *
231  * @param[in]    aUlaPrefix  A pointer to the received ULA prefix.
232  *
233  */
234 void otNcpHandleDidReceiveNewLegacyUlaPrefix(const uint8_t *aUlaPrefix);
235 
236 /**
237  * This method registers a set of legacy handlers with NCP.
238  *
239  * The set of handlers provided by the struct @p aHandlers are used by
240  * NCP code to start/stop legacy network.
241  * The @p aHandlers can be NULL to disable legacy support on NCP.
242  * Individual handlers in the given handlers struct can also be NULL.
243  *
244  * @param[in] aHandlers    A pointer to a handler struct.
245  *
246  */
247 void otNcpRegisterLegacyHandlers(const otNcpLegacyHandlers *aHandlers);
248 
249 /**
250  * @}
251  *
252  */
253 
254 #ifdef __cplusplus
255 } // extern "C"
256 #endif
257 
258 #endif // OPENTHREAD_NCP_H_
259