1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! Activation Time Information Element
//!
//! The Activation Time IE indicates the time when a rule or function
//! should be activated in the User Plane Function.
//! Per 3GPP TS 29.244 Section 8.2.121.
use crate::error::PfcpError;
use crate::ie::{Ie, IeType};
/// Activation Time
///
/// Specifies a 3GPP NTP timestamp when a PDR, FAR, QER, URR, BAR, or MAR
/// should be activated in the UPF.
///
/// # 3GPP Reference
/// 3GPP TS 29.244 Section 8.2.121
///
/// # Structure
/// - 4 bytes: 3GPP NTP timestamp (u32, big-endian)
/// The timestamp follows 3GPP TS 23.012 format (seconds since Jan 1, 1900)
///
/// # Examples
///
/// ```
/// use rs_pfcp::ie::activation_time::ActivationTime;
///
/// // Create an activation time with timestamp 0x12345678
/// let activation = ActivationTime::new(0x12345678);
/// assert_eq!(activation.timestamp(), 0x12345678);
///
/// // Marshal and unmarshal
/// let bytes = activation.marshal();
/// let parsed = ActivationTime::unmarshal(&bytes).unwrap();
/// assert_eq!(activation, parsed);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ActivationTime {
/// 3GPP NTP timestamp
timestamp: u32,
}
impl ActivationTime {
/// Create a new Activation Time
///
/// # Arguments
/// * `timestamp` - 3GPP NTP timestamp value
///
/// # Example
/// ```
/// use rs_pfcp::ie::activation_time::ActivationTime;
///
/// let activation = ActivationTime::new(0xDEADBEEF);
/// assert_eq!(activation.timestamp(), 0xDEADBEEF);
/// ```
pub fn new(timestamp: u32) -> Self {
ActivationTime { timestamp }
}
/// Get the timestamp value
///
/// # Example
/// ```
/// use rs_pfcp::ie::activation_time::ActivationTime;
///
/// let activation = ActivationTime::new(100);
/// assert_eq!(activation.timestamp(), 100);
/// ```
pub fn timestamp(&self) -> u32 {
self.timestamp
}
/// Marshal Activation Time to bytes
///
/// # Returns
/// 4-byte vector containing 3GPP NTP timestamp (big-endian)
pub fn marshal(&self) -> Vec<u8> {
self.timestamp.to_be_bytes().to_vec()
}
/// Unmarshal Activation Time from bytes
///
/// # Arguments
/// * `data` - Byte slice containing timestamp data (must be at least 4 bytes)
///
/// # Errors
/// Returns error if data is too short
///
/// # Example
/// ```
/// use rs_pfcp::ie::activation_time::ActivationTime;
///
/// let activation = ActivationTime::new(0x11223344);
/// let bytes = activation.marshal();
/// let parsed = ActivationTime::unmarshal(&bytes).unwrap();
/// assert_eq!(activation, parsed);
/// ```
pub fn unmarshal(data: &[u8]) -> Result<Self, PfcpError> {
if data.len() < 4 {
return Err(PfcpError::invalid_length(
"Activation Time",
IeType::ActivationTime,
4,
data.len(),
));
}
let bytes: [u8; 4] = data[0..4].try_into().unwrap();
let timestamp = u32::from_be_bytes(bytes);
Ok(ActivationTime { timestamp })
}
/// Convert to generic IE
///
/// # Example
/// ```
/// use rs_pfcp::ie::activation_time::ActivationTime;
/// use rs_pfcp::ie::IeType;
///
/// let activation = ActivationTime::new(0xFFFFFFFF);
/// let ie = activation.to_ie();
/// assert_eq!(ie.ie_type, IeType::ActivationTime);
/// ```
pub fn to_ie(&self) -> Ie {
let data = self.marshal();
Ie::new(IeType::ActivationTime, data)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_activation_time_new() {
let timestamp = 0x12345678;
let at = ActivationTime::new(timestamp);
assert_eq!(at.timestamp(), timestamp);
}
#[test]
fn test_activation_time_marshal_unmarshal() {
let original = ActivationTime::new(0xDEADBEEF);
let bytes = original.marshal();
assert_eq!(bytes.len(), 4);
let parsed = ActivationTime::unmarshal(&bytes).unwrap();
assert_eq!(original, parsed);
assert_eq!(parsed.timestamp(), 0xDEADBEEF);
}
#[test]
fn test_activation_time_marshal_zero() {
let at = ActivationTime::new(0);
let bytes = at.marshal();
let parsed = ActivationTime::unmarshal(&bytes).unwrap();
assert_eq!(at, parsed);
assert_eq!(parsed.timestamp(), 0);
}
#[test]
fn test_activation_time_marshal_max_value() {
let at = ActivationTime::new(u32::MAX);
let bytes = at.marshal();
let parsed = ActivationTime::unmarshal(&bytes).unwrap();
assert_eq!(at, parsed);
assert_eq!(parsed.timestamp(), u32::MAX);
}
#[test]
fn test_activation_time_unmarshal_short() {
let data = vec![0x00, 0x00, 0x00]; // Only 3 bytes
let result = ActivationTime::unmarshal(&data);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(matches!(err, PfcpError::InvalidLength { .. }));
}
#[test]
fn test_activation_time_unmarshal_empty() {
let data = vec![];
let result = ActivationTime::unmarshal(&data);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(matches!(err, PfcpError::InvalidLength { .. }));
if let PfcpError::InvalidLength {
ie_name,
ie_type,
expected,
actual,
} = err
{
assert_eq!(ie_name, "Activation Time");
assert_eq!(ie_type, IeType::ActivationTime);
assert_eq!(expected, 4);
assert_eq!(actual, 0);
}
}
#[test]
fn test_activation_time_to_ie() {
let at = ActivationTime::new(0x44332211);
let ie = at.to_ie();
assert_eq!(ie.ie_type, IeType::ActivationTime);
assert_eq!(ie.payload.len(), 4);
// Verify IE can be unmarshaled
let parsed = ActivationTime::unmarshal(&ie.payload).unwrap();
assert_eq!(at, parsed);
}
#[test]
fn test_activation_time_round_trip_various() {
let values = vec![1, 100, 1000, 100000, 1000000, 0xFFFFFFFF];
for timestamp in values {
let original = ActivationTime::new(timestamp);
let bytes = original.marshal();
let parsed = ActivationTime::unmarshal(&bytes).unwrap();
assert_eq!(original, parsed, "Failed for timestamp {}", timestamp);
}
}
#[test]
fn test_activation_time_byte_order() {
// Verify big-endian encoding
let at = ActivationTime::new(0x12345678);
let bytes = at.marshal();
assert_eq!(bytes, vec![0x12, 0x34, 0x56, 0x78]);
}
#[test]
fn test_activation_time_clone() {
let at1 = ActivationTime::new(0xABCDEF00);
let at2 = at1;
assert_eq!(at1, at2);
}
#[test]
fn test_activation_time_5g_rule_activation() {
// Scenario: Schedule PDR activation
let activation = ActivationTime::new(0x5A5A5A5A);
let bytes = activation.marshal();
let parsed = ActivationTime::unmarshal(&bytes).unwrap();
assert_eq!(parsed.timestamp(), 0x5A5A5A5A);
assert_eq!(activation, parsed);
}
#[test]
fn test_activation_time_scheduled_provisioning() {
// Scenario: Time-based rule provisioning
let activation = ActivationTime::new(0x12341234);
let bytes = activation.marshal();
let parsed = ActivationTime::unmarshal(&bytes).unwrap();
assert_eq!(parsed, activation);
}
}