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  * 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  * Is called after NCP send finished.
70  *
71  */
72 void otNcpHdlcSendDone(void);
73 
74 /**
75  * 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 HDLC framing.
94  *
95  * @param[in]  aInstances       The OpenThread instance pointers array.
96  * @param[in]  aCount           Number of elements in the array.
97  * @param[in]  aSendCallback    The function pointer used to send NCP data.
98  *
99  */
100 void otNcpHdlcInitMulti(otInstance **aInstance, uint8_t aCount, otNcpHdlcSendCallback aSendCallback);
101 
102 /**
103  * Initialize the NCP based on SPI framing.
104  *
105  * @param[in]  aInstance  The OpenThread instance structure.
106  *
107  */
108 void otNcpSpiInit(otInstance *aInstance);
109 
110 /**
111  * @brief Send data to the host via a specific stream.
112  *
113  * Attempts to send the given data to the host
114  * using the given aStreamId. This is useful for reporting
115  * error messages, implementing debug/diagnostic consoles,
116  * and potentially other types of datastreams.
117  *
118  * The write either is accepted in its entirety or rejected.
119  * Partial writes are not attempted.
120  *
121  * @param[in]  aStreamId  A numeric identifier for the stream to write to.
122  *                        If set to '0', will default to the debug stream.
123  * @param[in]  aDataPtr   A pointer to the data to send on the stream.
124  *                        If aDataLen is non-zero, this param MUST NOT be NULL.
125  * @param[in]  aDataLen   The number of bytes of data from aDataPtr to send.
126  *
127  * @retval OT_ERROR_NONE         The data was queued for delivery to the host.
128  * @retval OT_ERROR_BUSY         There are not enough resources to complete this
129  *                               request. This is usually a temporary condition.
130  * @retval OT_ERROR_INVALID_ARGS The given aStreamId was invalid.
131  */
132 otError otNcpStreamWrite(int aStreamId, const uint8_t *aDataPtr, int aDataLen);
133 
134 /**
135  * Writes OpenThread Log using `otNcpStreamWrite`.
136  *
137  * @param[in]  aLogLevel   The log level.
138  * @param[in]  aLogRegion  The log region.
139  * @param[in]  aFormat     A pointer to the format string.
140  * @param[in]  aArgs       va_list matching aFormat.
141  */
142 void otNcpPlatLogv(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aArgs);
143 
144 //-----------------------------------------------------------------------------------------
145 // Peek/Poke memory access control delegates
146 
147 /**
148  * Defines delegate (function pointer) type to control behavior of peek/poke operation.
149  *
150  * This delegate function is called to decide whether to allow peek or poke of a specific memory region. It is used
151  * if NCP support for peek/poke commands is enabled.
152  *
153  * @param[in] aAddress    Start address of memory region.
154  * @param[in] aCount      Number of bytes to peek or poke.
155  *
156  * @returns  TRUE to allow peek/poke of the given memory region, FALSE otherwise.
157  *
158  */
159 typedef bool (*otNcpDelegateAllowPeekPoke)(uint32_t aAddress, uint16_t aCount);
160 
161 /**
162  * Registers peek/poke delegate functions with NCP module.
163  *
164  * The delegate functions are called by NCP module to decide whether to allow peek or poke of a specific memory region.
165  * If the delegate pointer is set to NULL, it allows peek/poke operation for any address.
166  *
167  * @param[in] aAllowPeekDelegate      Delegate function pointer for peek operation.
168  * @param[in] aAllowPokeDelegate      Delegate function pointer for poke operation.
169  *
170  */
171 void otNcpRegisterPeekPokeDelegates(otNcpDelegateAllowPeekPoke aAllowPeekDelegate,
172                                     otNcpDelegateAllowPeekPoke aAllowPokeDelegate);
173 
174 /**
175  * @}
176  *
177  */
178 
179 #ifdef __cplusplus
180 } // extern "C"
181 #endif
182 
183 #endif // OPENTHREAD_NCP_H_
184