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