xref: /aosp_15_r20/external/openthread/examples/platforms/simulation/diag.c (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2016, 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 #include "platform-simulation.h"
30*cfb92d14SAndroid Build Coastguard Worker 
31*cfb92d14SAndroid Build Coastguard Worker #include <stdbool.h>
32*cfb92d14SAndroid Build Coastguard Worker #include <stdio.h>
33*cfb92d14SAndroid Build Coastguard Worker #include <string.h>
34*cfb92d14SAndroid Build Coastguard Worker #include <sys/time.h>
35*cfb92d14SAndroid Build Coastguard Worker 
36*cfb92d14SAndroid Build Coastguard Worker #include <openthread/config.h>
37*cfb92d14SAndroid Build Coastguard Worker #include <openthread/platform/alarm-milli.h>
38*cfb92d14SAndroid Build Coastguard Worker #include <openthread/platform/diag.h>
39*cfb92d14SAndroid Build Coastguard Worker #include <openthread/platform/radio.h>
40*cfb92d14SAndroid Build Coastguard Worker 
41*cfb92d14SAndroid Build Coastguard Worker #include "utils/code_utils.h"
42*cfb92d14SAndroid Build Coastguard Worker 
43*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_DIAG_ENABLE
44*cfb92d14SAndroid Build Coastguard Worker 
45*cfb92d14SAndroid Build Coastguard Worker /**
46*cfb92d14SAndroid Build Coastguard Worker  * Diagnostics mode variables.
47*cfb92d14SAndroid Build Coastguard Worker  *
48*cfb92d14SAndroid Build Coastguard Worker  */
49*cfb92d14SAndroid Build Coastguard Worker static bool sDiagMode = false;
50*cfb92d14SAndroid Build Coastguard Worker 
51*cfb92d14SAndroid Build Coastguard Worker enum
52*cfb92d14SAndroid Build Coastguard Worker {
53*cfb92d14SAndroid Build Coastguard Worker     SIM_GPIO = 0,
54*cfb92d14SAndroid Build Coastguard Worker };
55*cfb92d14SAndroid Build Coastguard Worker 
56*cfb92d14SAndroid Build Coastguard Worker static otGpioMode sGpioMode  = OT_GPIO_MODE_INPUT;
57*cfb92d14SAndroid Build Coastguard Worker static bool       sGpioValue = false;
58*cfb92d14SAndroid Build Coastguard Worker static uint8_t    sRawPowerSetting[OPENTHREAD_CONFIG_POWER_CALIBRATION_RAW_POWER_SETTING_SIZE];
59*cfb92d14SAndroid Build Coastguard Worker static uint16_t   sRawPowerSettingLength = 0;
60*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagSetOutputCallback(otInstance * aInstance,otPlatDiagOutputCallback aCallback,void * aContext)61*cfb92d14SAndroid Build Coastguard Worker void otPlatDiagSetOutputCallback(otInstance *aInstance, otPlatDiagOutputCallback aCallback, void *aContext)
62*cfb92d14SAndroid Build Coastguard Worker {
63*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
64*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aCallback);
65*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aContext);
66*cfb92d14SAndroid Build Coastguard Worker }
67*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagModeSet(bool aMode)68*cfb92d14SAndroid Build Coastguard Worker void otPlatDiagModeSet(bool aMode) { sDiagMode = aMode; }
69*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagModeGet(void)70*cfb92d14SAndroid Build Coastguard Worker bool otPlatDiagModeGet(void) { return sDiagMode; }
71*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagChannelSet(uint8_t aChannel)72*cfb92d14SAndroid Build Coastguard Worker void otPlatDiagChannelSet(uint8_t aChannel) { OT_UNUSED_VARIABLE(aChannel); }
73*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagTxPowerSet(int8_t aTxPower)74*cfb92d14SAndroid Build Coastguard Worker void otPlatDiagTxPowerSet(int8_t aTxPower) { OT_UNUSED_VARIABLE(aTxPower); }
75*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagRadioReceived(otInstance * aInstance,otRadioFrame * aFrame,otError aError)76*cfb92d14SAndroid Build Coastguard Worker void otPlatDiagRadioReceived(otInstance *aInstance, otRadioFrame *aFrame, otError aError)
77*cfb92d14SAndroid Build Coastguard Worker {
78*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
79*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aFrame);
80*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aError);
81*cfb92d14SAndroid Build Coastguard Worker }
82*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagAlarmCallback(otInstance * aInstance)83*cfb92d14SAndroid Build Coastguard Worker void otPlatDiagAlarmCallback(otInstance *aInstance) { OT_UNUSED_VARIABLE(aInstance); }
84*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagGpioSet(uint32_t aGpio,bool aValue)85*cfb92d14SAndroid Build Coastguard Worker otError otPlatDiagGpioSet(uint32_t aGpio, bool aValue)
86*cfb92d14SAndroid Build Coastguard Worker {
87*cfb92d14SAndroid Build Coastguard Worker     otError error = OT_ERROR_NONE;
88*cfb92d14SAndroid Build Coastguard Worker 
89*cfb92d14SAndroid Build Coastguard Worker     otEXPECT_ACTION(aGpio == SIM_GPIO, error = OT_ERROR_INVALID_ARGS);
90*cfb92d14SAndroid Build Coastguard Worker     sGpioValue = aValue;
91*cfb92d14SAndroid Build Coastguard Worker 
92*cfb92d14SAndroid Build Coastguard Worker exit:
93*cfb92d14SAndroid Build Coastguard Worker     return error;
94*cfb92d14SAndroid Build Coastguard Worker }
95*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagGpioGet(uint32_t aGpio,bool * aValue)96*cfb92d14SAndroid Build Coastguard Worker otError otPlatDiagGpioGet(uint32_t aGpio, bool *aValue)
97*cfb92d14SAndroid Build Coastguard Worker {
98*cfb92d14SAndroid Build Coastguard Worker     otError error = OT_ERROR_NONE;
99*cfb92d14SAndroid Build Coastguard Worker 
100*cfb92d14SAndroid Build Coastguard Worker     otEXPECT_ACTION((aGpio == SIM_GPIO) && (aValue != NULL), error = OT_ERROR_INVALID_ARGS);
101*cfb92d14SAndroid Build Coastguard Worker     *aValue = sGpioValue;
102*cfb92d14SAndroid Build Coastguard Worker 
103*cfb92d14SAndroid Build Coastguard Worker exit:
104*cfb92d14SAndroid Build Coastguard Worker     return error;
105*cfb92d14SAndroid Build Coastguard Worker }
106*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagGpioSetMode(uint32_t aGpio,otGpioMode aMode)107*cfb92d14SAndroid Build Coastguard Worker otError otPlatDiagGpioSetMode(uint32_t aGpio, otGpioMode aMode)
108*cfb92d14SAndroid Build Coastguard Worker {
109*cfb92d14SAndroid Build Coastguard Worker     otError error = OT_ERROR_NONE;
110*cfb92d14SAndroid Build Coastguard Worker 
111*cfb92d14SAndroid Build Coastguard Worker     otEXPECT_ACTION(aGpio == SIM_GPIO, error = OT_ERROR_INVALID_ARGS);
112*cfb92d14SAndroid Build Coastguard Worker     sGpioMode = aMode;
113*cfb92d14SAndroid Build Coastguard Worker 
114*cfb92d14SAndroid Build Coastguard Worker exit:
115*cfb92d14SAndroid Build Coastguard Worker     return error;
116*cfb92d14SAndroid Build Coastguard Worker }
117*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagGpioGetMode(uint32_t aGpio,otGpioMode * aMode)118*cfb92d14SAndroid Build Coastguard Worker otError otPlatDiagGpioGetMode(uint32_t aGpio, otGpioMode *aMode)
119*cfb92d14SAndroid Build Coastguard Worker {
120*cfb92d14SAndroid Build Coastguard Worker     otError error = OT_ERROR_NONE;
121*cfb92d14SAndroid Build Coastguard Worker 
122*cfb92d14SAndroid Build Coastguard Worker     otEXPECT_ACTION((aGpio == SIM_GPIO) && (aMode != NULL), error = OT_ERROR_INVALID_ARGS);
123*cfb92d14SAndroid Build Coastguard Worker     *aMode = sGpioMode;
124*cfb92d14SAndroid Build Coastguard Worker 
125*cfb92d14SAndroid Build Coastguard Worker exit:
126*cfb92d14SAndroid Build Coastguard Worker     return error;
127*cfb92d14SAndroid Build Coastguard Worker }
128*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagRadioSetRawPowerSetting(otInstance * aInstance,const uint8_t * aRawPowerSetting,uint16_t aRawPowerSettingLength)129*cfb92d14SAndroid Build Coastguard Worker otError otPlatDiagRadioSetRawPowerSetting(otInstance    *aInstance,
130*cfb92d14SAndroid Build Coastguard Worker                                           const uint8_t *aRawPowerSetting,
131*cfb92d14SAndroid Build Coastguard Worker                                           uint16_t       aRawPowerSettingLength)
132*cfb92d14SAndroid Build Coastguard Worker {
133*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
134*cfb92d14SAndroid Build Coastguard Worker     otError error = OT_ERROR_NONE;
135*cfb92d14SAndroid Build Coastguard Worker 
136*cfb92d14SAndroid Build Coastguard Worker     otEXPECT_ACTION((aRawPowerSetting != NULL) && (aRawPowerSettingLength <= sizeof(sRawPowerSetting)),
137*cfb92d14SAndroid Build Coastguard Worker                     error = OT_ERROR_INVALID_ARGS);
138*cfb92d14SAndroid Build Coastguard Worker     memcpy(sRawPowerSetting, aRawPowerSetting, aRawPowerSettingLength);
139*cfb92d14SAndroid Build Coastguard Worker     sRawPowerSettingLength = aRawPowerSettingLength;
140*cfb92d14SAndroid Build Coastguard Worker 
141*cfb92d14SAndroid Build Coastguard Worker exit:
142*cfb92d14SAndroid Build Coastguard Worker     return error;
143*cfb92d14SAndroid Build Coastguard Worker }
144*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagRadioGetRawPowerSetting(otInstance * aInstance,uint8_t * aRawPowerSetting,uint16_t * aRawPowerSettingLength)145*cfb92d14SAndroid Build Coastguard Worker otError otPlatDiagRadioGetRawPowerSetting(otInstance *aInstance,
146*cfb92d14SAndroid Build Coastguard Worker                                           uint8_t    *aRawPowerSetting,
147*cfb92d14SAndroid Build Coastguard Worker                                           uint16_t   *aRawPowerSettingLength)
148*cfb92d14SAndroid Build Coastguard Worker {
149*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
150*cfb92d14SAndroid Build Coastguard Worker     otError error = OT_ERROR_NONE;
151*cfb92d14SAndroid Build Coastguard Worker 
152*cfb92d14SAndroid Build Coastguard Worker     otEXPECT_ACTION((aRawPowerSetting != NULL) && (aRawPowerSettingLength != NULL), error = OT_ERROR_INVALID_ARGS);
153*cfb92d14SAndroid Build Coastguard Worker     otEXPECT_ACTION((sRawPowerSettingLength != 0), error = OT_ERROR_NOT_FOUND);
154*cfb92d14SAndroid Build Coastguard Worker     otEXPECT_ACTION((sRawPowerSettingLength <= *aRawPowerSettingLength), error = OT_ERROR_INVALID_ARGS);
155*cfb92d14SAndroid Build Coastguard Worker 
156*cfb92d14SAndroid Build Coastguard Worker     memcpy(aRawPowerSetting, sRawPowerSetting, sRawPowerSettingLength);
157*cfb92d14SAndroid Build Coastguard Worker     *aRawPowerSettingLength = sRawPowerSettingLength;
158*cfb92d14SAndroid Build Coastguard Worker 
159*cfb92d14SAndroid Build Coastguard Worker exit:
160*cfb92d14SAndroid Build Coastguard Worker     return error;
161*cfb92d14SAndroid Build Coastguard Worker }
162*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagRadioRawPowerSettingEnable(otInstance * aInstance,bool aEnable)163*cfb92d14SAndroid Build Coastguard Worker otError otPlatDiagRadioRawPowerSettingEnable(otInstance *aInstance, bool aEnable)
164*cfb92d14SAndroid Build Coastguard Worker {
165*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
166*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aEnable);
167*cfb92d14SAndroid Build Coastguard Worker 
168*cfb92d14SAndroid Build Coastguard Worker     return OT_ERROR_NONE;
169*cfb92d14SAndroid Build Coastguard Worker }
170*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagRadioTransmitCarrier(otInstance * aInstance,bool aEnable)171*cfb92d14SAndroid Build Coastguard Worker otError otPlatDiagRadioTransmitCarrier(otInstance *aInstance, bool aEnable)
172*cfb92d14SAndroid Build Coastguard Worker {
173*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
174*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aEnable);
175*cfb92d14SAndroid Build Coastguard Worker 
176*cfb92d14SAndroid Build Coastguard Worker     return OT_ERROR_NONE;
177*cfb92d14SAndroid Build Coastguard Worker }
178*cfb92d14SAndroid Build Coastguard Worker 
otPlatDiagRadioTransmitStream(otInstance * aInstance,bool aEnable)179*cfb92d14SAndroid Build Coastguard Worker otError otPlatDiagRadioTransmitStream(otInstance *aInstance, bool aEnable)
180*cfb92d14SAndroid Build Coastguard Worker {
181*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
182*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aEnable);
183*cfb92d14SAndroid Build Coastguard Worker 
184*cfb92d14SAndroid Build Coastguard Worker     return OT_ERROR_NONE;
185*cfb92d14SAndroid Build Coastguard Worker }
186*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_CONFIG_DIAG_ENABLE
187