1 /*
2 * Copyright (c) 2022, 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 * This file provides an example on how to implement an OpenThread vendor interface to RCP.
32 */
33
34 #include "openthread-posix-config.h"
35
36 #if OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_VENDOR
37
38 #include "vendor_interface.hpp"
39 #include "common/new.hpp"
40
41 namespace ot {
42 namespace Posix {
43 using ot::Spinel::SpinelInterface;
44
45 /**
46 * This class defines the vendor implementation object.
47 *
48 */
49 class VendorInterfaceImpl
50 {
51 public:
VendorInterfaceImpl(SpinelInterface::ReceiveFrameCallback aCallback,void * aCallbackContext,SpinelInterface::RxFrameBuffer & aFrameBuffer)52 explicit VendorInterfaceImpl(SpinelInterface::ReceiveFrameCallback aCallback,
53 void *aCallbackContext,
54 SpinelInterface::RxFrameBuffer &aFrameBuffer)
55 : mReceiveFrameCallback(aCallback)
56 , mReceiveFrameContext(aCallbackContext)
57 , mRxFrameBuffer(aFrameBuffer)
58 {
59 OT_UNUSED_VARIABLE(mReceiveFrameCallback);
60 OT_UNUSED_VARIABLE(mReceiveFrameContext);
61 OT_UNUSED_VARIABLE(mRxFrameBuffer);
62 }
63
64 // TODO: Add vendor code (add methods and/or member variables).
65
66 private:
67 SpinelInterface::ReceiveFrameCallback mReceiveFrameCallback;
68 void *mReceiveFrameContext;
69 SpinelInterface::RxFrameBuffer &mRxFrameBuffer;
70 };
71
72 // ----------------------------------------------------------------------------
73 // `VendorInterface` API
74 // ----------------------------------------------------------------------------
75
76 static OT_DEFINE_ALIGNED_VAR(sVendorInterfaceImplRaw, sizeof(VendorInterfaceImpl), uint64_t);
77
VendorInterface(SpinelInterface::ReceiveFrameCallback aCallback,void * aCallbackContext,SpinelInterface::RxFrameBuffer & aFrameBuffer)78 VendorInterface::VendorInterface(SpinelInterface::ReceiveFrameCallback aCallback,
79 void *aCallbackContext,
80 SpinelInterface::RxFrameBuffer &aFrameBuffer)
81 {
82 new (&sVendorInterfaceImplRaw) VendorInterfaceImpl(aCallback, aCallbackContext, aFrameBuffer);
83 OT_UNUSED_VARIABLE(sVendorInterfaceImplRaw);
84 }
85
~VendorInterface(void)86 VendorInterface::~VendorInterface(void) { Deinit(); }
87
Init(const Url::Url & aRadioUrl)88 otError VendorInterface::Init(const Url::Url &aRadioUrl)
89 {
90 OT_UNUSED_VARIABLE(aRadioUrl);
91
92 // TODO: Implement vendor code here.
93
94 return OT_ERROR_NONE;
95 }
96
Deinit(void)97 void VendorInterface::Deinit(void)
98 {
99 // TODO: Implement vendor code here.
100 }
101
GetBusSpeed(void) const102 uint32_t VendorInterface::GetBusSpeed(void) const { return 1000000; }
103
HardwareReset(void)104 otError VendorInterface::HardwareReset(void)
105 {
106 // TODO: Implement vendor code here.
107
108 return OT_ERROR_NOT_IMPLEMENTED;
109 }
110
UpdateFdSet(fd_set & aReadFdSet,fd_set & aWriteFdSet,int & aMaxFd,struct timeval & aTimeout)111 void VendorInterface::UpdateFdSet(fd_set &aReadFdSet, fd_set &aWriteFdSet, int &aMaxFd, struct timeval &aTimeout)
112 {
113 OT_UNUSED_VARIABLE(aReadFdSet);
114 OT_UNUSED_VARIABLE(aWriteFdSet);
115 OT_UNUSED_VARIABLE(aMaxFd);
116 OT_UNUSED_VARIABLE(aTimeout);
117
118 // TODO: Implement vendor code here.
119 }
120
Process(const RadioProcessContext & aContext)121 void VendorInterface::Process(const RadioProcessContext &aContext)
122 {
123 OT_UNUSED_VARIABLE(aContext);
124
125 // TODO: Implement vendor code here.
126 }
127
WaitForFrame(uint64_t aTimeoutUs)128 otError VendorInterface::WaitForFrame(uint64_t aTimeoutUs)
129 {
130 OT_UNUSED_VARIABLE(aTimeoutUs);
131
132 // TODO: Implement vendor code here.
133
134 return OT_ERROR_NONE;
135 }
136
SendFrame(const uint8_t * aFrame,uint16_t aLength)137 otError VendorInterface::SendFrame(const uint8_t *aFrame, uint16_t aLength)
138 {
139 OT_UNUSED_VARIABLE(aFrame);
140 OT_UNUSED_VARIABLE(aLength);
141
142 // TODO: Implement vendor code here.
143
144 return OT_ERROR_NONE;
145 }
146
GetRcpInterfaceMetrics(void)147 const otRcpInterfaceMetrics *VendorInterface::GetRcpInterfaceMetrics(void)
148 {
149 // TODO: Implement vendor code here.
150
151 return nullptr;
152 }
153 } // namespace Posix
154 } // namespace ot
155
156 #endif // OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_VENDOR
157