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 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  * 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  * 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 otNcpRegisterPeekPokeDelegates(otNcpDelegateAllowPeekPoke aAllowPeekDelegate,
162                                     otNcpDelegateAllowPeekPoke aAllowPokeDelegate);
163 
164 /**
165  * @}
166  *
167  */
168 
169 #ifdef __cplusplus
170 } // extern "C"
171 #endif
172 
173 #endif // OPENTHREAD_NCP_H_
174