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