xref: /aosp_15_r20/external/openthread/examples/platforms/simulation/virtual_time/alarm-sim.c (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1 /*
2  *  Copyright (c) 2018, 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 #if OPENTHREAD_SIMULATION_VIRTUAL_TIME
32 
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <string.h>
36 
37 #include <openthread/platform/alarm-micro.h>
38 #include <openthread/platform/alarm-milli.h>
39 #include <openthread/platform/diag.h>
40 #include <openthread/platform/time.h>
41 
42 extern uint64_t sNow; // microseconds
43 
44 static bool     sIsMsRunning = false;
45 static uint32_t sMsAlarm     = 0;
46 
47 static bool     sIsUsRunning = false;
48 static uint32_t sUsAlarm     = 0;
49 
platformAlarmInit(uint32_t aSpeedUpFactor)50 void platformAlarmInit(uint32_t aSpeedUpFactor)
51 {
52     OT_UNUSED_VARIABLE(aSpeedUpFactor);
53 
54     sNow = 0;
55 }
56 
platformAlarmGetNow(void)57 uint64_t platformAlarmGetNow(void) { return sNow; }
58 
platformAlarmAdvanceNow(uint64_t aDelta)59 void platformAlarmAdvanceNow(uint64_t aDelta) { sNow += aDelta; }
60 
otPlatAlarmMilliGetNow(void)61 uint32_t otPlatAlarmMilliGetNow(void) { return (uint32_t)(sNow / OT_US_PER_MS); }
62 
otPlatAlarmMilliStartAt(otInstance * aInstance,uint32_t aT0,uint32_t aDt)63 void otPlatAlarmMilliStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt)
64 {
65     OT_UNUSED_VARIABLE(aInstance);
66 
67     sMsAlarm     = aT0 + aDt;
68     sIsMsRunning = true;
69 }
70 
otPlatAlarmMilliStop(otInstance * aInstance)71 void otPlatAlarmMilliStop(otInstance *aInstance)
72 {
73     OT_UNUSED_VARIABLE(aInstance);
74 
75     sIsMsRunning = false;
76 }
77 
otPlatAlarmMicroGetNow(void)78 uint32_t otPlatAlarmMicroGetNow(void) { return (uint32_t)sNow; }
79 
otPlatAlarmMicroStartAt(otInstance * aInstance,uint32_t aT0,uint32_t aDt)80 void otPlatAlarmMicroStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt)
81 {
82     OT_UNUSED_VARIABLE(aInstance);
83 
84     sUsAlarm     = aT0 + aDt;
85     sIsUsRunning = true;
86 }
87 
otPlatAlarmMicroStop(otInstance * aInstance)88 void otPlatAlarmMicroStop(otInstance *aInstance)
89 {
90     OT_UNUSED_VARIABLE(aInstance);
91 
92     sIsUsRunning = false;
93 }
94 
platformAlarmGetNext(void)95 uint64_t platformAlarmGetNext(void)
96 {
97     uint64_t remaining = INT64_MAX;
98 
99     if (sIsMsRunning)
100     {
101         int32_t milli = (int32_t)(sMsAlarm - otPlatAlarmMilliGetNow());
102 
103         if (milli < 0)
104         {
105             remaining = 0;
106         }
107         else
108         {
109             remaining = (uint64_t)milli;
110             remaining *= OT_US_PER_MS;
111         }
112     }
113 
114 #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
115     if (sIsUsRunning)
116     {
117         int32_t micro = (int32_t)(sUsAlarm - otPlatAlarmMicroGetNow());
118 
119         if (micro < 0)
120         {
121             remaining = 0;
122         }
123         else if (remaining > ((uint64_t)micro))
124         {
125             remaining = (uint64_t)micro;
126         }
127     }
128 #endif
129 
130     return remaining;
131 }
132 
platformAlarmProcess(otInstance * aInstance)133 void platformAlarmProcess(otInstance *aInstance)
134 {
135     int32_t remaining;
136 
137     if (sIsMsRunning)
138     {
139         remaining = (int32_t)(sMsAlarm - otPlatAlarmMilliGetNow());
140 
141         if (remaining <= 0)
142         {
143             sIsMsRunning = false;
144 
145 #if OPENTHREAD_CONFIG_DIAG_ENABLE
146 
147             if (otPlatDiagModeGet())
148             {
149                 otPlatDiagAlarmFired(aInstance);
150             }
151             else
152 #endif
153             {
154                 otPlatAlarmMilliFired(aInstance);
155             }
156         }
157     }
158 
159 #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
160 
161     if (sIsUsRunning)
162     {
163         remaining = (int32_t)(sUsAlarm - otPlatAlarmMicroGetNow());
164 
165         if (remaining <= 0)
166         {
167             sIsUsRunning = false;
168 
169             otPlatAlarmMicroFired(aInstance);
170         }
171     }
172 
173 #endif // OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
174 }
175 
otPlatTimeGet(void)176 uint64_t otPlatTimeGet(void) { return platformAlarmGetNow(); }
177 
178 #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
otPlatTimeGetXtalAccuracy(void)179 uint16_t otPlatTimeGetXtalAccuracy(void) { return 0; }
180 #endif
181 
182 #endif // OPENTHREAD_SIMULATION_VIRTUAL_TIME
183