1 /*
2 * Copyright (c) 2016-2018, 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 implements the callbacks from `SubMac` layer into `Mac` or `LinkRaw`.
32 */
33
34 #include "sub_mac.hpp"
35
36 #include "common/code_utils.hpp"
37 #include "common/instance.hpp"
38 #include "common/locator_getters.hpp"
39
40 #if OPENTHREAD_FTD || OPENTHREAD_MTD
41 #include "mac/mac.hpp"
42 #endif
43
44 namespace ot {
45 namespace Mac {
46
Callbacks(Instance & aInstance)47 SubMac::Callbacks::Callbacks(Instance &aInstance)
48 : InstanceLocator(aInstance)
49 {
50 }
51
52 #if OPENTHREAD_FTD || OPENTHREAD_MTD
53
ReceiveDone(RxFrame * aFrame,Error aError)54 void SubMac::Callbacks::ReceiveDone(RxFrame *aFrame, Error aError)
55 {
56 #if OPENTHREAD_CONFIG_LINK_RAW_ENABLE
57 if (Get<LinkRaw>().IsEnabled())
58 {
59 Get<LinkRaw>().InvokeReceiveDone(aFrame, aError);
60 }
61 else
62 #endif
63 {
64 Get<Mac>().HandleReceivedFrame(aFrame, aError);
65 }
66 }
67
RecordCcaStatus(bool aCcaSuccess,uint8_t aChannel)68 void SubMac::Callbacks::RecordCcaStatus(bool aCcaSuccess, uint8_t aChannel)
69 {
70 Get<Mac>().RecordCcaStatus(aCcaSuccess, aChannel);
71 }
72
RecordFrameTransmitStatus(const TxFrame & aFrame,const RxFrame * aAckFrame,Error aError,uint8_t aRetryCount,bool aWillRetx)73 void SubMac::Callbacks::RecordFrameTransmitStatus(const TxFrame &aFrame,
74 const RxFrame *aAckFrame,
75 Error aError,
76 uint8_t aRetryCount,
77 bool aWillRetx)
78 {
79 Get<Mac>().RecordFrameTransmitStatus(aFrame, aAckFrame, aError, aRetryCount, aWillRetx);
80 }
81
TransmitDone(TxFrame & aFrame,RxFrame * aAckFrame,Error aError)82 void SubMac::Callbacks::TransmitDone(TxFrame &aFrame, RxFrame *aAckFrame, Error aError)
83 {
84 #if OPENTHREAD_CONFIG_LINK_RAW_ENABLE
85 if (Get<LinkRaw>().IsEnabled())
86 {
87 Get<LinkRaw>().InvokeTransmitDone(aFrame, aAckFrame, aError);
88 }
89 else
90 #endif
91 {
92 Get<Mac>().HandleTransmitDone(aFrame, aAckFrame, aError);
93 }
94 }
95
EnergyScanDone(int8_t aMaxRssi)96 void SubMac::Callbacks::EnergyScanDone(int8_t aMaxRssi)
97 {
98 #if OPENTHREAD_CONFIG_LINK_RAW_ENABLE
99 if (Get<LinkRaw>().IsEnabled())
100 {
101 Get<LinkRaw>().InvokeEnergyScanDone(aMaxRssi);
102 }
103 else
104 #endif
105 {
106 Get<Mac>().EnergyScanDone(aMaxRssi);
107 }
108 }
109
FrameCounterUpdated(uint32_t aFrameCounter)110 void SubMac::Callbacks::FrameCounterUpdated(uint32_t aFrameCounter)
111 {
112 Get<KeyManager>().MacFrameCounterUpdated(aFrameCounter);
113 }
114
115 #elif OPENTHREAD_RADIO
116
ReceiveDone(RxFrame * aFrame,Error aError)117 void SubMac::Callbacks::ReceiveDone(RxFrame *aFrame, Error aError)
118 {
119 Get<LinkRaw>().InvokeReceiveDone(aFrame, aError);
120 }
121
RecordCcaStatus(bool,uint8_t)122 void SubMac::Callbacks::RecordCcaStatus(bool, uint8_t)
123 {
124 }
125
RecordFrameTransmitStatus(const TxFrame & aFrame,const RxFrame * aAckFrame,Error aError,uint8_t aRetryCount,bool aWillRetx)126 void SubMac::Callbacks::RecordFrameTransmitStatus(const TxFrame &aFrame,
127 const RxFrame *aAckFrame,
128 Error aError,
129 uint8_t aRetryCount,
130 bool aWillRetx)
131 {
132 Get<LinkRaw>().RecordFrameTransmitStatus(aFrame, aAckFrame, aError, aRetryCount, aWillRetx);
133 }
134
TransmitDone(TxFrame & aFrame,RxFrame * aAckFrame,Error aError)135 void SubMac::Callbacks::TransmitDone(TxFrame &aFrame, RxFrame *aAckFrame, Error aError)
136 {
137 Get<LinkRaw>().InvokeTransmitDone(aFrame, aAckFrame, aError);
138 }
139
EnergyScanDone(int8_t aMaxRssi)140 void SubMac::Callbacks::EnergyScanDone(int8_t aMaxRssi)
141 {
142 Get<LinkRaw>().InvokeEnergyScanDone(aMaxRssi);
143 }
144
FrameCounterUpdated(uint32_t aFrameCounter)145 void SubMac::Callbacks::FrameCounterUpdated(uint32_t aFrameCounter)
146 {
147 OT_UNUSED_VARIABLE(aFrameCounter);
148 }
149
150 #endif // OPENTHREAD_RADIO
151
152 } // namespace Mac
153 } // namespace ot
154