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