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