• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "fake_coprocessor_platform.hpp"
30 
31 #include <openthread/instance.h>
32 #include <openthread/link.h>
33 #include <openthread/ncp.h>
34 #include <openthread/platform/toolchain.h>
35 
36 #include "common/code_utils.hpp"
37 #include "lib/hdlc/hdlc.hpp"
38 #include "lib/spinel/spinel.h"
39 #include "lib/spinel/spinel_interface.hpp"
40 
41 // Currently radio spinel depends on this OpenThread user API
42 // TODO remove this dependency in future.
otLinkGetFrameCounter(otInstance *)43 OT_TOOL_WEAK uint32_t otLinkGetFrameCounter(otInstance *) { return 0; }
44 
45 namespace ot {
46 
SendFrame(const uint8_t * aFrame,uint16_t aLength)47 otError DirectSpinelInterface::SendFrame(const uint8_t *aFrame, uint16_t aLength)
48 {
49     otError                            error = OT_ERROR_NONE;
50     Spinel::FrameBuffer<kMaxFrameSize> encoderBuffer;
51     Hdlc::Encoder                      hdlcEncoder(encoderBuffer);
52 
53     SuccessOrExit(error = hdlcEncoder.BeginFrame());
54     SuccessOrExit(error = hdlcEncoder.Encode(aFrame, aLength));
55     SuccessOrExit(error = hdlcEncoder.EndFrame());
56 
57     otNcpHdlcReceive(encoderBuffer.GetFrame(), encoderBuffer.GetLength());
58 
59 exit:
60     return error;
61 }
62 
WaitForFrame(uint64_t aTimeoutUs)63 otError DirectSpinelInterface::WaitForFrame(uint64_t aTimeoutUs)
64 {
65     while (!mReceived && (aTimeoutUs = FakePlatform::CurrentPlatform().Run(aTimeoutUs)))
66     {
67         // Empty
68     }
69 
70     Error error = mReceived ? kErrorNone : kErrorResponseTimeout;
71     mReceived   = false;
72     return error;
73 }
74 
Receive(const uint8_t * aBuffer,uint16_t aLength)75 int DirectSpinelInterface::Receive(const uint8_t *aBuffer, uint16_t aLength)
76 {
77     Hdlc::Decoder hdlcDecoder;
78 
79     hdlcDecoder.Init(*mDecoderBuffer, &DirectSpinelInterface::OnReceived, this);
80     hdlcDecoder.Decode(aBuffer, aLength);
81 
82     return aLength;
83 }
84 
FakeCoprocessorPlatform()85 FakeCoprocessorPlatform::FakeCoprocessorPlatform()
86 {
87     spinel_iid_t iids[]{
88         0,
89     };
90 
91     otNcpHdlcInit(mInstance, [](const uint8_t *aBuf, uint16_t aLength) -> int {
92         int rval = static_cast<FakeCoprocessorPlatform &>(FakePlatform::CurrentPlatform())
93                        .mSpinelInterface.Receive(aBuf, aLength);
94         otNcpHdlcSendDone();
95         return rval;
96     });
97 
98     mSpinelDriver.Init(mSpinelInterface, false, iids, OT_ARRAY_LENGTH(iids));
99 
100     mRadioSpinel.Init(true, false, &mSpinelDriver, 0, false);
101 }
102 
103 } // namespace ot
104