Skip to content

Commit 16bea0b

Browse files
Add input validation
Co-authored-by: Aaron Siemsen <[email protected]>
1 parent 690abd6 commit 16bea0b

File tree

4 files changed

+120
-9
lines changed

4 files changed

+120
-9
lines changed

FprimeZephyrReference/Components/Drv/RtcManager/RtcManager.cpp

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,28 @@ void RtcManager ::timeGetPort_handler(FwIndexType portNum, Fw::Time& time) {
6363
void RtcManager ::TIME_SET_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, Drv::TimeData t) {
6464
// Check device readiness
6565
if (!device_is_ready(this->dev)) {
66+
// Emit device not ready event
6667
this->log_WARNING_HI_DeviceNotReady();
68+
69+
// Send command response
6770
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
6871
return;
6972
}
7073
this->log_WARNING_HI_DeviceNotReady_ThrottleClear();
7174

75+
// Validate time data
76+
if (!this->timeDataIsValid(t)) {
77+
// Emit time not set event
78+
this->log_WARNING_HI_TimeNotSet();
79+
80+
// Send command response
81+
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::VALIDATION_ERROR);
82+
return;
83+
}
84+
85+
// Store current time for logging
86+
Fw::Time time_before_set = this->getTime();
87+
7288
// Populate rtc_time structure from TimeData
7389
const struct rtc_time time_rtc = {
7490
.tm_sec = static_cast<int>(t.get_Second()),
@@ -82,9 +98,6 @@ void RtcManager ::TIME_SET_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, Drv::Time
8298
.tm_isdst = 0,
8399
};
84100

85-
// Store current time for logging
86-
Fw::Time time_before_set = this->getTime();
87-
88101
// Set time on RTC
89102
const int status = rtc_set_time(this->dev, &time_rtc);
90103

@@ -104,4 +117,40 @@ void RtcManager ::TIME_SET_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, Drv::Time
104117
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
105118
}
106119

120+
bool RtcManager ::timeDataIsValid(Drv::TimeData t) {
121+
bool valid = true;
122+
123+
if (t.get_Year() < 1900) {
124+
this->log_WARNING_HI_YearValidationFailed(t.get_Year());
125+
valid = false;
126+
}
127+
128+
if (t.get_Month() < 1 || t.get_Month() > 12) {
129+
this->log_WARNING_HI_MonthValidationFailed(t.get_Month());
130+
valid = false;
131+
}
132+
133+
if (t.get_Day() < 1 || t.get_Day() > 31) {
134+
this->log_WARNING_HI_DayValidationFailed(t.get_Day());
135+
valid = false;
136+
}
137+
138+
if (t.get_Hour() > 23) {
139+
this->log_WARNING_HI_HourValidationFailed(t.get_Hour());
140+
valid = false;
141+
}
142+
143+
if (t.get_Minute() > 59) {
144+
this->log_WARNING_HI_MinuteValidationFailed(t.get_Minute());
145+
valid = false;
146+
}
147+
148+
if (t.get_Second() > 59) {
149+
this->log_WARNING_HI_SecondValidationFailed(t.get_Second());
150+
valid = false;
151+
}
152+
153+
return valid;
154+
}
155+
107156
} // namespace Drv

FprimeZephyrReference/Components/Drv/RtcManager/RtcManager.fpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,43 @@ module Drv {
3535

3636
@ TimeSet event indicates that the time was set successfully
3737
event TimeSet(
38-
pt: U32 @< POSIX time in seconds
39-
u: U32 @< Microseconds
38+
seconds: U32 @< Seconds since epoch
39+
useconds: U32 @< Microseconds
4040
) severity activity high id 3 format "Time set on RTC, previous time: {}.{}"
4141

4242
@ TimeNotSet event indicates that the time was not set successfully
4343
event TimeNotSet() severity warning high id 4 format "Time not set on RTC"
4444

45+
@ YearValidationFailed event indicates that the provided year is invalid
46+
event YearValidationFailed(
47+
year: U32 @< The invalid year
48+
) severity warning high id 5 format "Provided year is invalid should be >= 1900: {}"
49+
50+
@ MonthValidationFailed event indicates that the provided month is invalid
51+
event MonthValidationFailed(
52+
month: U32 @< The invalid month
53+
) severity warning high id 6 format "Provided month is invalid should be in [1, 12]: {}"
54+
55+
@ DayValidationFailed event indicates that the provided day is invalid
56+
event DayValidationFailed(
57+
day: U32 @< The invalid day
58+
) severity warning high id 7 format "Provided day is invalid should be in [1, 31]: {}"
59+
60+
@ HourValidationFailed event indicates that the provided hour is invalid
61+
event HourValidationFailed(
62+
hour: U32 @< The invalid hour
63+
) severity warning high id 8 format "Provided hour is invalid should be in [0, 23]: {}"
64+
65+
@ MinuteValidationFailed event indicates that the provided minute is invalid
66+
event MinuteValidationFailed(
67+
minute: U32 @< The invalid minute
68+
) severity warning high id 9 format "Provided minute is invalid should be in [0, 59]: {}"
69+
70+
@ SecondValidationFailed event indicates that the provided second is invalid
71+
event SecondValidationFailed(
72+
second: U32 @< The invalid second
73+
) severity warning high id 10 format "Provided second is invalid should be in [0, 59]: {}"
74+
4575
###############################################################################
4676
# Standard AC Ports: Required for Channels, Events, Commands, and Parameters #
4777
###############################################################################

FprimeZephyrReference/Components/Drv/RtcManager/RtcManager.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "FprimeZephyrReference/Components/Drv/RtcManager/RtcManagerComponentAc.hpp"
1010

1111
#include <cerrno>
12+
#include <string>
13+
#include <vector>
1214

1315
#include <Fw/Logger/Logger.hpp>
1416

@@ -58,6 +60,14 @@ class RtcManager final : public RtcManagerComponentBase {
5860
Drv::TimeData t //!< Set the time
5961
) override;
6062

63+
private:
64+
// ----------------------------------------------------------------------
65+
// Private helper methods
66+
// ----------------------------------------------------------------------
67+
68+
//! Validate time data
69+
bool timeDataIsValid(Drv::TimeData t);
70+
6171
//! device stores the initialized Zephyr RTC device
6272
const struct device* dev;
6373
};

FprimeZephyrReference/test/int/rtc_test.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ def test_01_time_set(fprime_test_api: IntegrationTestAPI):
6868

6969
# Ensure microseconds are included in event
7070
microseconds_arg: U32Type = result.args[1]
71-
assert 0 <= microseconds_arg.val < 100_000_000, "Microseconds arg should be >= 0 and < 1 million"
71+
assert 0 <= microseconds_arg.val < 100_000_000, (
72+
"Microseconds arg should be >= 0 and < 1 million"
73+
)
7274

7375
# Fetch FPrime time from event
7476
fp_time: TimeType = result.get_time()
@@ -145,11 +147,31 @@ def test_03_time_not_set_event(fprime_test_api: IntegrationTestAPI):
145147

146148
# Assert time not set event is emitted
147149
fprime_test_api.assert_event(
148-
"ReferenceDeployment.rtcManager.TimeNotSet", timeout=2
150+
"ReferenceDeployment.rtcManager.YearValidationFailed", timeout=2
151+
)
152+
153+
fprime_test_api.assert_event(
154+
"ReferenceDeployment.rtcManager.MonthValidationFailed", timeout=2
149155
)
156+
150157
fprime_test_api.assert_event(
151-
"CdhCore.cmdDisp.OpCodeDispatched", timeout=2
158+
"ReferenceDeployment.rtcManager.DayValidationFailed", timeout=2
152159
)
160+
153161
fprime_test_api.assert_event(
154-
"CdhCore.cmdDisp.OpCodeError", timeout=2
162+
"ReferenceDeployment.rtcManager.HourValidationFailed", timeout=2
155163
)
164+
165+
fprime_test_api.assert_event(
166+
"ReferenceDeployment.rtcManager.MinuteValidationFailed", timeout=2
167+
)
168+
169+
fprime_test_api.assert_event(
170+
"ReferenceDeployment.rtcManager.SecondValidationFailed", timeout=2
171+
)
172+
173+
fprime_test_api.assert_event("ReferenceDeployment.rtcManager.TimeNotSet", timeout=2)
174+
175+
fprime_test_api.assert_event("CdhCore.cmdDisp.OpCodeDispatched", timeout=2)
176+
177+
fprime_test_api.assert_event("CdhCore.cmdDisp.OpCodeError", timeout=2)

0 commit comments

Comments
 (0)