xref: /aosp_15_r20/external/openthread/examples/platforms/simulation/misc.c (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
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 <setjmp.h>
32 #include <unistd.h>
33 
34 #include <openthread/logging.h>
35 #include <openthread/platform/misc.h>
36 
37 #include "openthread-system.h"
38 
39 extern jmp_buf gResetJump;
40 
41 static otPlatResetReason   sPlatResetReason = OT_PLAT_RESET_REASON_POWER_ON;
42 bool                       gPlatformPseudoResetWasRequested;
43 static otPlatMcuPowerState gPlatMcuPowerState = OT_PLAT_MCU_POWER_STATE_ON;
44 
otPlatReset(otInstance * aInstance)45 void otPlatReset(otInstance *aInstance)
46 {
47     OT_UNUSED_VARIABLE(aInstance);
48 
49 #if OPENTHREAD_PLATFORM_USE_PSEUDO_RESET
50     gPlatformPseudoResetWasRequested = true;
51     sPlatResetReason                 = OT_PLAT_RESET_REASON_SOFTWARE;
52 
53 #else // OPENTHREAD_PLATFORM_USE_PSEUDO_RESET
54     // Restart the process using execvp.
55     otSysDeinit();
56     platformUartRestore();
57 
58     longjmp(gResetJump, 1);
59     assert(false);
60 
61 #endif // OPENTHREAD_PLATFORM_USE_PSEUDO_RESET
62 }
63 
64 #if OPENTHREAD_CONFIG_PLATFORM_BOOTLOADER_MODE_ENABLE
otPlatResetToBootloader(otInstance * aInstance)65 otError otPlatResetToBootloader(otInstance *aInstance)
66 {
67     OT_UNUSED_VARIABLE(aInstance);
68 
69     return OT_ERROR_NOT_CAPABLE;
70 }
71 #endif
72 
otPlatGetResetReason(otInstance * aInstance)73 otPlatResetReason otPlatGetResetReason(otInstance *aInstance)
74 {
75     OT_UNUSED_VARIABLE(aInstance);
76 
77     return sPlatResetReason;
78 }
79 
otPlatWakeHost(void)80 void otPlatWakeHost(void)
81 {
82     // TODO: implement an operation to wake the host from sleep state.
83 }
84 
otPlatSetMcuPowerState(otInstance * aInstance,otPlatMcuPowerState aState)85 otError otPlatSetMcuPowerState(otInstance *aInstance, otPlatMcuPowerState aState)
86 {
87     OT_UNUSED_VARIABLE(aInstance);
88 
89     otError error = OT_ERROR_NONE;
90 
91     switch (aState)
92     {
93     case OT_PLAT_MCU_POWER_STATE_ON:
94     case OT_PLAT_MCU_POWER_STATE_LOW_POWER:
95         gPlatMcuPowerState = aState;
96         break;
97 
98     default:
99         error = OT_ERROR_FAILED;
100         break;
101     }
102 
103     return error;
104 }
105 
otPlatGetMcuPowerState(otInstance * aInstance)106 otPlatMcuPowerState otPlatGetMcuPowerState(otInstance *aInstance)
107 {
108     OT_UNUSED_VARIABLE(aInstance);
109 
110     return gPlatMcuPowerState;
111 }
112 
113 #if OPENTHREAD_CONFIG_PLATFORM_LOG_CRASH_DUMP_ENABLE
otPlatLogCrashDump(void)114 otError otPlatLogCrashDump(void) { return OT_ERROR_NONE; }
115 #endif
116