1 /*
2  *  Copyright (c) 2024, 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 #ifndef OT_GTEST_FAKE_COPROCESSOR_PLATFORM_HPP_
30 #define OT_GTEST_FAKE_COPROCESSOR_PLATFORM_HPP_
31 
32 #include <openthread/config.h>
33 
34 #include "fake_platform.hpp"
35 
36 #include "lib/spinel/radio_spinel.hpp"
37 
38 namespace ot {
39 
40 /**
41  * This spinel interface directly connects to the coprocessor with function calls.
42  */
43 class DirectSpinelInterface : public Spinel::SpinelInterface
44 {
45 public:
Init(ReceiveFrameCallback aCallback,void * aCallbackContext,RxFrameBuffer & aFrameBuffer)46     virtual otError Init(ReceiveFrameCallback aCallback, void *aCallbackContext, RxFrameBuffer &aFrameBuffer) override
47     {
48         mDecoderBuffer        = &aFrameBuffer;
49         mReceiveFrameCallback = aCallback;
50         mReceiveFrameContext  = aCallbackContext;
51         return kErrorNone;
52     }
53 
Deinit(void)54     virtual void Deinit(void) override {}
55 
56     virtual otError SendFrame(const uint8_t *aFrame, uint16_t aLength) override;
57 
58     virtual otError WaitForFrame(uint64_t aTimeoutUs) override;
59 
UpdateFdSet(void * aMainloopContext)60     virtual void UpdateFdSet(void *aMainloopContext) override {}
Process(const void * aMainloopContext)61     virtual void Process(const void *aMainloopContext) override {}
62 
GetBusSpeed(void) const63     virtual uint32_t GetBusSpeed(void) const override { return 0; }
64 
HardwareReset(void)65     virtual otError HardwareReset(void) override { return kErrorNone; }
66 
GetRcpInterfaceMetrics(void) const67     virtual const otRcpInterfaceMetrics *GetRcpInterfaceMetrics(void) const override { return nullptr; }
68 
OnReceived(void * aContext,otError aError)69     static void OnReceived(void *aContext, otError aError)
70     {
71         static_cast<DirectSpinelInterface *>(aContext)->OnReceived(aError);
72     }
73 
OnReceived(otError aError)74     void OnReceived(otError aError)
75     {
76         mReceived = true;
77         if (aError == kErrorNone)
78         {
79             mReceiveFrameCallback(mReceiveFrameContext);
80         }
81     }
82 
83     int Receive(const uint8_t *aBuffer, uint16_t aLength);
84 
85 private:
86     ReceiveFrameCallback            mReceiveFrameCallback = nullptr;
87     void                           *mReceiveFrameContext  = nullptr;
88     SpinelInterface::RxFrameBuffer *mDecoderBuffer        = nullptr;
89     bool                            mReceived             = false;
90 };
91 
92 class FakeCoprocessorPlatform : public FakePlatform
93 {
94 public:
95     FakeCoprocessorPlatform();
96     virtual ~FakeCoprocessorPlatform() = default;
97 
98     Spinel::RadioSpinel   mRadioSpinel;
99     Spinel::SpinelDriver  mSpinelDriver;
100     DirectSpinelInterface mSpinelInterface;
101 };
102 
103 } // namespace ot
104 #endif // OT_GTEST_FAKE_COPROCESSOR_PLATFORM_HPP_
105