xref: /aosp_15_r20/external/openthread/src/lib/spinel/spinel_driver.hpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2024, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker  *  All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker  *
5*cfb92d14SAndroid Build Coastguard Worker  *  Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker  *  modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker  *  1. Redistributions of source code must retain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker  *  2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker  *     documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker  *  3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker  *     names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker  *     derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker  *
16*cfb92d14SAndroid Build Coastguard Worker  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker  *  POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker  */
28*cfb92d14SAndroid Build Coastguard Worker 
29*cfb92d14SAndroid Build Coastguard Worker #ifndef SPINEL_DRIVER_HPP_
30*cfb92d14SAndroid Build Coastguard Worker #define SPINEL_DRIVER_HPP_
31*cfb92d14SAndroid Build Coastguard Worker 
32*cfb92d14SAndroid Build Coastguard Worker #include <openthread/instance.h>
33*cfb92d14SAndroid Build Coastguard Worker 
34*cfb92d14SAndroid Build Coastguard Worker #include "lib/spinel/coprocessor_type.h"
35*cfb92d14SAndroid Build Coastguard Worker #include "lib/spinel/logger.hpp"
36*cfb92d14SAndroid Build Coastguard Worker #include "lib/spinel/spinel.h"
37*cfb92d14SAndroid Build Coastguard Worker #include "lib/spinel/spinel_interface.hpp"
38*cfb92d14SAndroid Build Coastguard Worker 
39*cfb92d14SAndroid Build Coastguard Worker /**
40*cfb92d14SAndroid Build Coastguard Worker  * Represents an opaque (and empty) type corresponding to a SpinelDriver object.
41*cfb92d14SAndroid Build Coastguard Worker  *
42*cfb92d14SAndroid Build Coastguard Worker  */
43*cfb92d14SAndroid Build Coastguard Worker struct otSpinelDriver
44*cfb92d14SAndroid Build Coastguard Worker {
45*cfb92d14SAndroid Build Coastguard Worker };
46*cfb92d14SAndroid Build Coastguard Worker 
47*cfb92d14SAndroid Build Coastguard Worker namespace ot {
48*cfb92d14SAndroid Build Coastguard Worker namespace Spinel {
49*cfb92d14SAndroid Build Coastguard Worker 
50*cfb92d14SAndroid Build Coastguard Worker /**
51*cfb92d14SAndroid Build Coastguard Worker  * Maximum number of Spinel Interface IDs.
52*cfb92d14SAndroid Build Coastguard Worker  *
53*cfb92d14SAndroid Build Coastguard Worker  */
54*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
55*cfb92d14SAndroid Build Coastguard Worker static constexpr uint8_t kSpinelHeaderMaxNumIid = 4;
56*cfb92d14SAndroid Build Coastguard Worker #else
57*cfb92d14SAndroid Build Coastguard Worker static constexpr uint8_t kSpinelHeaderMaxNumIid = 1;
58*cfb92d14SAndroid Build Coastguard Worker #endif
59*cfb92d14SAndroid Build Coastguard Worker 
60*cfb92d14SAndroid Build Coastguard Worker class SpinelDriver : public otSpinelDriver, public Logger
61*cfb92d14SAndroid Build Coastguard Worker {
62*cfb92d14SAndroid Build Coastguard Worker public:
63*cfb92d14SAndroid Build Coastguard Worker     typedef void (
64*cfb92d14SAndroid Build Coastguard Worker         *ReceivedFrameHandler)(const uint8_t *aFrame, uint16_t aLength, uint8_t aHeader, bool &aSave, void *aContext);
65*cfb92d14SAndroid Build Coastguard Worker     typedef void (*SavedFrameHandler)(const uint8_t *aFrame, uint16_t aLength, void *aContext);
66*cfb92d14SAndroid Build Coastguard Worker 
67*cfb92d14SAndroid Build Coastguard Worker     /**
68*cfb92d14SAndroid Build Coastguard Worker      * Constructor of the SpinelDriver.
69*cfb92d14SAndroid Build Coastguard Worker      *
70*cfb92d14SAndroid Build Coastguard Worker      */
71*cfb92d14SAndroid Build Coastguard Worker     SpinelDriver(void);
72*cfb92d14SAndroid Build Coastguard Worker 
73*cfb92d14SAndroid Build Coastguard Worker     /**
74*cfb92d14SAndroid Build Coastguard Worker      * Initialize this SpinelDriver Instance.
75*cfb92d14SAndroid Build Coastguard Worker      *
76*cfb92d14SAndroid Build Coastguard Worker      * @param[in]  aSpinelInterface            A reference to the Spinel interface.
77*cfb92d14SAndroid Build Coastguard Worker      * @param[in]  aSoftwareReset              TRUE to reset on init, FALSE to not reset on init.
78*cfb92d14SAndroid Build Coastguard Worker      * @param[in]  aIidList                    A Pointer to the list of IIDs to receive spinel frame from.
79*cfb92d14SAndroid Build Coastguard Worker      *                                         First entry must be the IID of the Host Application.
80*cfb92d14SAndroid Build Coastguard Worker      * @param[in]  aIidListLength              The Length of the @p aIidList.
81*cfb92d14SAndroid Build Coastguard Worker      *
82*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_COPROCESSOR_UNKNOWN  The initialization fails.
83*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_COPROCESSOR_RCP      The Co-processor is a RCP.
84*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_COPROCESSOR_NCP      The Co-processor is a NCP.
85*cfb92d14SAndroid Build Coastguard Worker      *
86*cfb92d14SAndroid Build Coastguard Worker      */
87*cfb92d14SAndroid Build Coastguard Worker     CoprocessorType Init(SpinelInterface    &aSpinelInterface,
88*cfb92d14SAndroid Build Coastguard Worker                          bool                aSoftwareReset,
89*cfb92d14SAndroid Build Coastguard Worker                          const spinel_iid_t *aIidList,
90*cfb92d14SAndroid Build Coastguard Worker                          uint8_t             aIidListLength);
91*cfb92d14SAndroid Build Coastguard Worker 
92*cfb92d14SAndroid Build Coastguard Worker     /**
93*cfb92d14SAndroid Build Coastguard Worker      * Deinitialize this SpinelDriver Instance.
94*cfb92d14SAndroid Build Coastguard Worker      *
95*cfb92d14SAndroid Build Coastguard Worker      */
96*cfb92d14SAndroid Build Coastguard Worker     void Deinit(void);
97*cfb92d14SAndroid Build Coastguard Worker 
98*cfb92d14SAndroid Build Coastguard Worker     /**
99*cfb92d14SAndroid Build Coastguard Worker      * Clear the rx frame buffer.
100*cfb92d14SAndroid Build Coastguard Worker      *
101*cfb92d14SAndroid Build Coastguard Worker      */
ClearRxBuffer(void)102*cfb92d14SAndroid Build Coastguard Worker     void ClearRxBuffer(void) { mRxFrameBuffer.Clear(); }
103*cfb92d14SAndroid Build Coastguard Worker 
104*cfb92d14SAndroid Build Coastguard Worker     /**
105*cfb92d14SAndroid Build Coastguard Worker      * Set the internal state of co-processor as ready.
106*cfb92d14SAndroid Build Coastguard Worker      *
107*cfb92d14SAndroid Build Coastguard Worker      * This method is used to skip a reset.
108*cfb92d14SAndroid Build Coastguard Worker      */
SetCoprocessorReady(void)109*cfb92d14SAndroid Build Coastguard Worker     void SetCoprocessorReady(void) { mIsCoprocessorReady = true; }
110*cfb92d14SAndroid Build Coastguard Worker 
111*cfb92d14SAndroid Build Coastguard Worker     /**
112*cfb92d14SAndroid Build Coastguard Worker      * Send a reset command to the co-processor.
113*cfb92d14SAndroid Build Coastguard Worker      *
114*cfb92d14SAndroid Build Coastguard Worker      * @prarm[in] aResetType    The reset type, SPINEL_RESET_PLATFORM, SPINEL_RESET_STACK, or SPINEL_RESET_BOOTLOADER.
115*cfb92d14SAndroid Build Coastguard Worker      *
116*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_ERROR_NONE               Successfully removed item from the property.
117*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_ERROR_BUSY               Failed due to another operation is on going.
118*cfb92d14SAndroid Build Coastguard Worker      *
119*cfb92d14SAndroid Build Coastguard Worker      */
120*cfb92d14SAndroid Build Coastguard Worker     otError SendReset(uint8_t aResetType);
121*cfb92d14SAndroid Build Coastguard Worker 
122*cfb92d14SAndroid Build Coastguard Worker     /**
123*cfb92d14SAndroid Build Coastguard Worker      * Reset the co-processor.
124*cfb92d14SAndroid Build Coastguard Worker      *
125*cfb92d14SAndroid Build Coastguard Worker      * This method will reset the co-processor and wait until the co-process is ready (receiving SPINEL_PROP_LAST_STATUS
126*cfb92d14SAndroid Build Coastguard Worker      * from the it). The reset will be either a software or hardware reset. If `aSoftwareReset` is `true`, then the
127*cfb92d14SAndroid Build Coastguard Worker      * method will first try a software reset. If the software reset succeeds, the method exits. Otherwise the method
128*cfb92d14SAndroid Build Coastguard Worker      * will then try a hardware reset. If `aSoftwareReset` is `false`, then method will directly try a hardware reset.
129*cfb92d14SAndroid Build Coastguard Worker      *
130*cfb92d14SAndroid Build Coastguard Worker      * @param[in]  aSoftwareReset                 TRUE to try SW reset first, FALSE to directly try HW reset.
131*cfb92d14SAndroid Build Coastguard Worker      *
132*cfb92d14SAndroid Build Coastguard Worker      */
133*cfb92d14SAndroid Build Coastguard Worker     void ResetCoprocessor(bool aSoftwareReset);
134*cfb92d14SAndroid Build Coastguard Worker 
135*cfb92d14SAndroid Build Coastguard Worker     /**
136*cfb92d14SAndroid Build Coastguard Worker      * Processes any pending the I/O data.
137*cfb92d14SAndroid Build Coastguard Worker      *
138*cfb92d14SAndroid Build Coastguard Worker      * The method should be called by the system loop to process received spinel frames.
139*cfb92d14SAndroid Build Coastguard Worker      *
140*cfb92d14SAndroid Build Coastguard Worker      * @param[in]  aContext   The process context.
141*cfb92d14SAndroid Build Coastguard Worker      *
142*cfb92d14SAndroid Build Coastguard Worker      */
143*cfb92d14SAndroid Build Coastguard Worker     void Process(const void *aContext);
144*cfb92d14SAndroid Build Coastguard Worker 
145*cfb92d14SAndroid Build Coastguard Worker     /**
146*cfb92d14SAndroid Build Coastguard Worker      * Checks whether there is pending frame in the buffer.
147*cfb92d14SAndroid Build Coastguard Worker      *
148*cfb92d14SAndroid Build Coastguard Worker      * The method is required by the system loop to update timer fd.
149*cfb92d14SAndroid Build Coastguard Worker      *
150*cfb92d14SAndroid Build Coastguard Worker      * @returns Whether there is pending frame in the buffer.
151*cfb92d14SAndroid Build Coastguard Worker      *
152*cfb92d14SAndroid Build Coastguard Worker      */
HasPendingFrame(void) const153*cfb92d14SAndroid Build Coastguard Worker     bool HasPendingFrame(void) const { return mRxFrameBuffer.HasSavedFrame(); }
154*cfb92d14SAndroid Build Coastguard Worker 
155*cfb92d14SAndroid Build Coastguard Worker     /**
156*cfb92d14SAndroid Build Coastguard Worker      * Returns the co-processor sw version string.
157*cfb92d14SAndroid Build Coastguard Worker      *
158*cfb92d14SAndroid Build Coastguard Worker      * @returns A pointer to the co-processor version string.
159*cfb92d14SAndroid Build Coastguard Worker      *
160*cfb92d14SAndroid Build Coastguard Worker      */
GetVersion(void) const161*cfb92d14SAndroid Build Coastguard Worker     const char *GetVersion(void) const { return mVersion; }
162*cfb92d14SAndroid Build Coastguard Worker 
163*cfb92d14SAndroid Build Coastguard Worker     /*
164*cfb92d14SAndroid Build Coastguard Worker      * Sends a spinel command to the co-processor.
165*cfb92d14SAndroid Build Coastguard Worker      *
166*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aCommand    The spinel command.
167*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aKey        The spinel property key.
168*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aTid        The spinel transaction id.
169*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aFormat     The format string of the arguments to send.
170*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aArgs       The argument list.
171*cfb92d14SAndroid Build Coastguard Worker      *
172*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_ERROR_NONE           Successfully sent the command through spinel interface.
173*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_ERROR_INVALID_STATE  The spinel interface is in an invalid state.
174*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_ERROR_NO_BUFS        The spinel interface doesn't have enough buffer.
175*cfb92d14SAndroid Build Coastguard Worker      *
176*cfb92d14SAndroid Build Coastguard Worker      */
177*cfb92d14SAndroid Build Coastguard Worker     otError SendCommand(uint32_t          aCommand,
178*cfb92d14SAndroid Build Coastguard Worker                         spinel_prop_key_t aKey,
179*cfb92d14SAndroid Build Coastguard Worker                         spinel_tid_t      aTid,
180*cfb92d14SAndroid Build Coastguard Worker                         const char       *aFormat,
181*cfb92d14SAndroid Build Coastguard Worker                         va_list           aArgs);
182*cfb92d14SAndroid Build Coastguard Worker 
183*cfb92d14SAndroid Build Coastguard Worker     /*
184*cfb92d14SAndroid Build Coastguard Worker      * Sends a spinel command without arguments to the co-processor.
185*cfb92d14SAndroid Build Coastguard Worker      *
186*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aCommand    The spinel command.
187*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aKey        The spinel property key.
188*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aTid        The spinel transaction id.
189*cfb92d14SAndroid Build Coastguard Worker      *
190*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_ERROR_NONE           Successfully sent the command through spinel interface.
191*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_ERROR_INVALID_STATE  The spinel interface is in an invalid state.
192*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_ERROR_NO_BUFS        The spinel interface doesn't have enough buffer.
193*cfb92d14SAndroid Build Coastguard Worker      *
194*cfb92d14SAndroid Build Coastguard Worker      */
195*cfb92d14SAndroid Build Coastguard Worker     otError SendCommand(uint32_t aCommand, spinel_prop_key_t aKey, spinel_tid_t aTid);
196*cfb92d14SAndroid Build Coastguard Worker 
197*cfb92d14SAndroid Build Coastguard Worker     /*
198*cfb92d14SAndroid Build Coastguard Worker      * Sets the handler to process the received spinel frame.
199*cfb92d14SAndroid Build Coastguard Worker      *
200*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aReceivedFrameHandler  The handler to process received spinel frames.
201*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aSavedFrameHandler     The handler to process saved spinel frames.
202*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aContext               The context to call the handler.
203*cfb92d14SAndroid Build Coastguard Worker      *
204*cfb92d14SAndroid Build Coastguard Worker      */
205*cfb92d14SAndroid Build Coastguard Worker     void SetFrameHandler(ReceivedFrameHandler aReceivedFrameHandler,
206*cfb92d14SAndroid Build Coastguard Worker                          SavedFrameHandler    aSavedFrameHandler,
207*cfb92d14SAndroid Build Coastguard Worker                          void                *aContext);
208*cfb92d14SAndroid Build Coastguard Worker 
209*cfb92d14SAndroid Build Coastguard Worker     /*
210*cfb92d14SAndroid Build Coastguard Worker      * Returns the spinel interface.
211*cfb92d14SAndroid Build Coastguard Worker      *
212*cfb92d14SAndroid Build Coastguard Worker      * @returns A pointer to the spinel interface object.
213*cfb92d14SAndroid Build Coastguard Worker      *
214*cfb92d14SAndroid Build Coastguard Worker      */
GetSpinelInterface(void) const215*cfb92d14SAndroid Build Coastguard Worker     SpinelInterface *GetSpinelInterface(void) const { return mSpinelInterface; }
216*cfb92d14SAndroid Build Coastguard Worker 
217*cfb92d14SAndroid Build Coastguard Worker     /**
218*cfb92d14SAndroid Build Coastguard Worker      * Returns if the co-processor has some capability
219*cfb92d14SAndroid Build Coastguard Worker      *
220*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aCapability  The capability queried.
221*cfb92d14SAndroid Build Coastguard Worker      *
222*cfb92d14SAndroid Build Coastguard Worker      * @returns `true` if the co-processor has the capability. `false` otherwise.
223*cfb92d14SAndroid Build Coastguard Worker      *
224*cfb92d14SAndroid Build Coastguard Worker      */
CoprocessorHasCap(unsigned int aCapability)225*cfb92d14SAndroid Build Coastguard Worker     bool CoprocessorHasCap(unsigned int aCapability) { return mCoprocessorCaps.Contains(aCapability); }
226*cfb92d14SAndroid Build Coastguard Worker 
227*cfb92d14SAndroid Build Coastguard Worker     /**
228*cfb92d14SAndroid Build Coastguard Worker      * Returns the spinel interface id.
229*cfb92d14SAndroid Build Coastguard Worker      *
230*cfb92d14SAndroid Build Coastguard Worker      * @returns the spinel interface id.
231*cfb92d14SAndroid Build Coastguard Worker      *
232*cfb92d14SAndroid Build Coastguard Worker      */
GetIid(void)233*cfb92d14SAndroid Build Coastguard Worker     spinel_iid_t GetIid(void) { return mIid; }
234*cfb92d14SAndroid Build Coastguard Worker 
235*cfb92d14SAndroid Build Coastguard Worker private:
236*cfb92d14SAndroid Build Coastguard Worker     static constexpr uint16_t kMaxSpinelFrame    = SPINEL_FRAME_MAX_SIZE;
237*cfb92d14SAndroid Build Coastguard Worker     static constexpr uint16_t kVersionStringSize = 128;
238*cfb92d14SAndroid Build Coastguard Worker     static constexpr uint32_t kUsPerMs           = 1000; ///< Microseconds per millisecond.
239*cfb92d14SAndroid Build Coastguard Worker     static constexpr uint32_t kMaxWaitTime       = 2000; ///< Max time to wait for response in milliseconds.
240*cfb92d14SAndroid Build Coastguard Worker     static constexpr uint16_t kCapsBufferSize    = 100;  ///< Max buffer size used to store `SPINEL_PROP_CAPS` value.
241*cfb92d14SAndroid Build Coastguard Worker 
242*cfb92d14SAndroid Build Coastguard Worker     /**
243*cfb92d14SAndroid Build Coastguard Worker      * Represents an array of elements with a fixed max size.
244*cfb92d14SAndroid Build Coastguard Worker      *
245*cfb92d14SAndroid Build Coastguard Worker      * @tparam Type        The array element type.
246*cfb92d14SAndroid Build Coastguard Worker      * @tparam kMaxSize    Specifies the max array size (maximum number of elements in the array).
247*cfb92d14SAndroid Build Coastguard Worker      *
248*cfb92d14SAndroid Build Coastguard Worker      */
249*cfb92d14SAndroid Build Coastguard Worker     template <typename Type, uint16_t kMaxSize> class Array
250*cfb92d14SAndroid Build Coastguard Worker     {
251*cfb92d14SAndroid Build Coastguard Worker         static_assert(kMaxSize != 0, "Array `kMaxSize` cannot be zero");
252*cfb92d14SAndroid Build Coastguard Worker 
253*cfb92d14SAndroid Build Coastguard Worker     public:
Array(void)254*cfb92d14SAndroid Build Coastguard Worker         Array(void)
255*cfb92d14SAndroid Build Coastguard Worker             : mLength(0)
256*cfb92d14SAndroid Build Coastguard Worker         {
257*cfb92d14SAndroid Build Coastguard Worker         }
258*cfb92d14SAndroid Build Coastguard Worker 
GetMaxSize(void) const259*cfb92d14SAndroid Build Coastguard Worker         uint16_t GetMaxSize(void) const { return kMaxSize; }
260*cfb92d14SAndroid Build Coastguard Worker 
IsFull(void) const261*cfb92d14SAndroid Build Coastguard Worker         bool IsFull(void) const { return (mLength == GetMaxSize()); }
262*cfb92d14SAndroid Build Coastguard Worker 
PushBack(const Type & aEntry)263*cfb92d14SAndroid Build Coastguard Worker         otError PushBack(const Type &aEntry)
264*cfb92d14SAndroid Build Coastguard Worker         {
265*cfb92d14SAndroid Build Coastguard Worker             return IsFull() ? OT_ERROR_NO_BUFS : (mElements[mLength++] = aEntry, OT_ERROR_NONE);
266*cfb92d14SAndroid Build Coastguard Worker         }
267*cfb92d14SAndroid Build Coastguard Worker 
Find(const Type & aEntry) const268*cfb92d14SAndroid Build Coastguard Worker         const Type *Find(const Type &aEntry) const
269*cfb92d14SAndroid Build Coastguard Worker         {
270*cfb92d14SAndroid Build Coastguard Worker             const Type *matched = nullptr;
271*cfb92d14SAndroid Build Coastguard Worker 
272*cfb92d14SAndroid Build Coastguard Worker             for (const Type &element : *this)
273*cfb92d14SAndroid Build Coastguard Worker             {
274*cfb92d14SAndroid Build Coastguard Worker                 if (element == aEntry)
275*cfb92d14SAndroid Build Coastguard Worker                 {
276*cfb92d14SAndroid Build Coastguard Worker                     matched = &element;
277*cfb92d14SAndroid Build Coastguard Worker                     break;
278*cfb92d14SAndroid Build Coastguard Worker                 }
279*cfb92d14SAndroid Build Coastguard Worker             }
280*cfb92d14SAndroid Build Coastguard Worker 
281*cfb92d14SAndroid Build Coastguard Worker             return matched;
282*cfb92d14SAndroid Build Coastguard Worker         }
283*cfb92d14SAndroid Build Coastguard Worker 
Contains(const Type & aEntry) const284*cfb92d14SAndroid Build Coastguard Worker         bool Contains(const Type &aEntry) const { return Find(aEntry) != nullptr; }
285*cfb92d14SAndroid Build Coastguard Worker 
begin(void)286*cfb92d14SAndroid Build Coastguard Worker         Type       *begin(void) { return &mElements[0]; }
end(void)287*cfb92d14SAndroid Build Coastguard Worker         Type       *end(void) { return &mElements[mLength]; }
begin(void) const288*cfb92d14SAndroid Build Coastguard Worker         const Type *begin(void) const { return &mElements[0]; }
end(void) const289*cfb92d14SAndroid Build Coastguard Worker         const Type *end(void) const { return &mElements[mLength]; }
290*cfb92d14SAndroid Build Coastguard Worker 
291*cfb92d14SAndroid Build Coastguard Worker     private:
292*cfb92d14SAndroid Build Coastguard Worker         Type     mElements[kMaxSize];
293*cfb92d14SAndroid Build Coastguard Worker         uint16_t mLength;
294*cfb92d14SAndroid Build Coastguard Worker     };
295*cfb92d14SAndroid Build Coastguard Worker 
296*cfb92d14SAndroid Build Coastguard Worker     otError WaitResponse(void);
297*cfb92d14SAndroid Build Coastguard Worker 
298*cfb92d14SAndroid Build Coastguard Worker     static void HandleReceivedFrame(void *aContext);
299*cfb92d14SAndroid Build Coastguard Worker     void        HandleReceivedFrame(void);
300*cfb92d14SAndroid Build Coastguard Worker 
301*cfb92d14SAndroid Build Coastguard Worker     static void HandleInitialFrame(const uint8_t *aFrame,
302*cfb92d14SAndroid Build Coastguard Worker                                    uint16_t       aLength,
303*cfb92d14SAndroid Build Coastguard Worker                                    uint8_t        aHeader,
304*cfb92d14SAndroid Build Coastguard Worker                                    bool          &aSave,
305*cfb92d14SAndroid Build Coastguard Worker                                    void          *aContext);
306*cfb92d14SAndroid Build Coastguard Worker     void        HandleInitialFrame(const uint8_t *aFrame, uint16_t aLength, uint8_t aHeader, bool &aSave);
307*cfb92d14SAndroid Build Coastguard Worker 
308*cfb92d14SAndroid Build Coastguard Worker     otError         CheckSpinelVersion(void);
309*cfb92d14SAndroid Build Coastguard Worker     otError         GetCoprocessorVersion(void);
310*cfb92d14SAndroid Build Coastguard Worker     otError         GetCoprocessorCaps(void);
311*cfb92d14SAndroid Build Coastguard Worker     CoprocessorType GetCoprocessorType(void);
312*cfb92d14SAndroid Build Coastguard Worker 
313*cfb92d14SAndroid Build Coastguard Worker     void ProcessFrameQueue(void);
314*cfb92d14SAndroid Build Coastguard Worker 
315*cfb92d14SAndroid Build Coastguard Worker     SpinelInterface::RxFrameBuffer mRxFrameBuffer;
316*cfb92d14SAndroid Build Coastguard Worker     SpinelInterface               *mSpinelInterface;
317*cfb92d14SAndroid Build Coastguard Worker 
318*cfb92d14SAndroid Build Coastguard Worker     spinel_prop_key_t mWaitingKey; ///< The property key of current transaction.
319*cfb92d14SAndroid Build Coastguard Worker     bool              mIsWaitingForResponse;
320*cfb92d14SAndroid Build Coastguard Worker 
321*cfb92d14SAndroid Build Coastguard Worker     spinel_iid_t                                mIid;
322*cfb92d14SAndroid Build Coastguard Worker     Array<spinel_iid_t, kSpinelHeaderMaxNumIid> mIidList;
323*cfb92d14SAndroid Build Coastguard Worker 
324*cfb92d14SAndroid Build Coastguard Worker     ReceivedFrameHandler mReceivedFrameHandler;
325*cfb92d14SAndroid Build Coastguard Worker     SavedFrameHandler    mSavedFrameHandler;
326*cfb92d14SAndroid Build Coastguard Worker     void                *mFrameHandlerContext;
327*cfb92d14SAndroid Build Coastguard Worker 
328*cfb92d14SAndroid Build Coastguard Worker     int mSpinelVersionMajor;
329*cfb92d14SAndroid Build Coastguard Worker     int mSpinelVersionMinor;
330*cfb92d14SAndroid Build Coastguard Worker 
331*cfb92d14SAndroid Build Coastguard Worker     bool mIsCoprocessorReady;
332*cfb92d14SAndroid Build Coastguard Worker     char mVersion[kVersionStringSize];
333*cfb92d14SAndroid Build Coastguard Worker 
334*cfb92d14SAndroid Build Coastguard Worker     Array<unsigned int, kCapsBufferSize> mCoprocessorCaps;
335*cfb92d14SAndroid Build Coastguard Worker };
336*cfb92d14SAndroid Build Coastguard Worker 
337*cfb92d14SAndroid Build Coastguard Worker } // namespace Spinel
338*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
339*cfb92d14SAndroid Build Coastguard Worker 
340*cfb92d14SAndroid Build Coastguard Worker #endif // SPINEL_DRIVER_HPP_
341