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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use std::process::exit;
use std::thread::sleep;

use a653rs::bindings::*;
use a653rs::prelude::{Name, SystemTime};
use nix::libc::EAGAIN;

use a653rs_linux_core::error::SystemError;
use a653rs_linux_core::queuing::{QueuingDestination, QueuingSource};
use a653rs_linux_core::sampling::{SamplingDestination, SamplingSource};

use crate::partition::ApexLinuxPartition;
use crate::process::Process as LinuxProcess;
use crate::*;

impl ApexPartitionP4 for ApexLinuxPartition {
    fn get_partition_status() -> ApexPartitionStatus {
        let operating_mode = PARTITION_MODE.read().unwrap();

        ApexPartitionStatus {
            period: CONSTANTS.period.as_nanos() as i64,
            duration: CONSTANTS.duration.as_nanos() as i64,
            identifier: CONSTANTS.identifier,
            lock_level: 0,
            operating_mode,
            start_condition: CONSTANTS.start_condition,
            num_assigned_cores: 1,
        }
    }

    fn set_partition_mode(operating_mode: OperatingMode) -> Result<(), ErrorReturnCode> {
        let current_mode = PARTITION_MODE.read().unwrap();

        if let OperatingMode::Idle = current_mode {
            panic!()
        }

        match (operating_mode, current_mode) {
            (OperatingMode::Normal, OperatingMode::Normal) => Err(ErrorReturnCode::NoAction),
            (OperatingMode::WarmStart, OperatingMode::ColdStart) => {
                Err(ErrorReturnCode::InvalidMode)
            }
            (OperatingMode::Normal, _) => {
                SENDER
                    .try_send(&PartitionCall::Transition(operating_mode))
                    .unwrap();
                loop {
                    sleep(Duration::from_secs(500))
                }
            }
            (_, _) => {
                SENDER
                    .try_send(&PartitionCall::Transition(operating_mode))
                    .unwrap();
                exit(0)
            }
        }
    }
}

impl ApexProcessP4 for ApexLinuxPartition {
    fn create_process(attributes: &ApexProcessAttribute) -> Result<ProcessId, ErrorReturnCode> {
        // TODO do not unwrap both
        // Check current State (only allowed in warm and cold start)
        let attr = attributes.clone().into();
        Ok(LinuxProcess::create(attr).unwrap())
    }

    fn start(process_id: ProcessId) -> Result<(), ErrorReturnCode> {
        let proc = match process_id {
            1 => APERIODIC_PROCESS.get(),
            2 => PERIODIC_PROCESS.get(),
            _ => None,
        };

        let proc = match proc {
            Some(proc) => proc,
            None => return Err(ErrorReturnCode::InvalidParam),
        };

        // TODO use a bigger result which contains both panic and non-panic errors
        proc.start().unwrap();

        Ok(())
    }
}

impl ApexSamplingPortP4 for ApexLinuxPartition {
    fn create_sampling_port(
        sampling_port_name: SamplingPortName,
        // TODO Return ErrorCode for wrong max message size
        _max_message_size: MessageSize,
        port_direction: PortDirection,
        refresh_period: ApexSystemTime,
    ) -> Result<SamplingPortId, ErrorReturnCode> {
        if refresh_period <= 0 {
            trace!("yielding InvalidConfig, because refresh period <= 0");
            return Err(ErrorReturnCode::InvalidConfig);
        }

        let name = Name::new(sampling_port_name);
        let name = name.to_str().map_err(|e| {
            trace!("yielding InvalidConfig, because sampling port is not valid UTF-8:\n{e}");
            ErrorReturnCode::InvalidConfig
        })?;
        if let Some((i, s)) = CONSTANTS
            .sampling
            .iter()
            .enumerate()
            .find(|(_, s)| s.name.eq(name))
        {
            if s.dir != port_direction {
                trace!("yielding InvalidConfig, because mismatching port direction:\nexpected {:?}, got {port_direction:?}", s.dir);
                return Err(ErrorReturnCode::InvalidConfig);
            }

            let refresh = SystemTime::new(refresh_period).unwrap_duration();
            let ch = (i, refresh);

            let mut channels = SAMPLING_PORTS.read().unwrap();
            if channels.try_push(ch).is_some() {
                trace!(
                    "yielding InvalidConfig, maximum number of sampling ports already reached: {}",
                    channels.len()
                );
                return Err(ErrorReturnCode::InvalidConfig);
            }
            SAMPLING_PORTS.write(&channels).unwrap();

            return Ok(channels.len() as SamplingPortId);
        }

        trace!("yielding InvalidConfig, configuration does not declare sampling port {name}");
        Err(ErrorReturnCode::InvalidConfig)
    }

    fn write_sampling_message(
        sampling_port_id: SamplingPortId,
        message: &[ApexByte],
    ) -> Result<(), ErrorReturnCode> {
        if let Some((port, _)) = SAMPLING_PORTS
            .read()
            .unwrap()
            .get(sampling_port_id as usize - 1)
        {
            if let Some(port) = CONSTANTS.sampling.get(*port) {
                if message.len() > port.msg_size {
                    return Err(ErrorReturnCode::InvalidConfig);
                } else if message.is_empty() {
                    return Err(ErrorReturnCode::InvalidParam);
                } else if port.dir != PortDirection::Source {
                    return Err(ErrorReturnCode::InvalidMode);
                }
                SamplingSource::try_from(port.fd).unwrap().write(message);
                return Ok(());
            }
        }

        Err(ErrorReturnCode::InvalidParam)
    }

    unsafe fn read_sampling_message(
        sampling_port_id: SamplingPortId,
        message: &mut [ApexByte],
    ) -> Result<(Validity, MessageSize), ErrorReturnCode> {
        let read = if let Ok(read) = SAMPLING_PORTS.read() {
            read
        } else {
            return Err(ErrorReturnCode::NotAvailable);
        };
        if let Some((port, val)) = read.get(sampling_port_id as usize - 1) {
            if let Some(port) = CONSTANTS.sampling.get(*port) {
                if message.is_empty() {
                    return Err(ErrorReturnCode::InvalidParam);
                } else if port.dir != PortDirection::Destination {
                    return Err(ErrorReturnCode::InvalidMode);
                }
                let (msg_len, copied) = SamplingDestination::try_from(port.fd)
                    .unwrap()
                    .read(message);

                if msg_len == 0 {
                    return Err(ErrorReturnCode::NoAction);
                }

                let valid = if copied.elapsed() <= *val {
                    Validity::Valid
                } else {
                    Validity::Invalid
                };

                return Ok((valid, msg_len as u32));
            }
        }

        Err(ErrorReturnCode::InvalidParam)
    }
}

impl ApexQueuingPortP4 for ApexLinuxPartition {
    fn create_queuing_port(
        queuing_port_name: QueuingPortName,
        max_message_size: MessageSize,
        max_nb_message: MessageRange,
        port_direction: PortDirection,
        _queuing_discipline: QueuingDiscipline,
    ) -> Result<QueuingPortId, ErrorReturnCode> {
        let name = Name::new(queuing_port_name);
        let name = name.to_str().map_err(|e| {
            trace!("yielding InvalidConfig, because queuing port is not valid UTF-8:\n{e}");
            ErrorReturnCode::InvalidConfig
        })?;

        if let Some((i, q)) = CONSTANTS
            .queuing
            .iter()
            .enumerate()
            .find(|(_, q)| q.name.eq(name))
        {
            // check max message size
            if max_message_size != q.msg_size as MessageSize {
                trace!("yielding InvalidConfig, because the queuing port max message size ({}) mismatches the configuration table value ({})", max_message_size, q.msg_size);
                return Err(ErrorReturnCode::InvalidConfig);
            }

            // check max number of messages
            if max_nb_message != q.max_num_msg as MessageRange {
                trace!("yielding InvalidConfig, because the queuing port max number of messages ({}) mismatches the configuration table value ({})", max_nb_message, q.max_num_msg);
                return Err(ErrorReturnCode::InvalidConfig);
            }

            // check correct port direction
            if q.dir != port_direction {
                trace!("yielding InvalidConfig, because queuing port has mismatching port direction:\nexpected {:?}, got {port_direction:?}", q.dir);
                return Err(ErrorReturnCode::InvalidConfig);
            }

            // check partition mode
            if let OperatingMode::Normal = PARTITION_MODE.read().unwrap() {
                trace!("yielding InvalidMode, because queuing port creation is not allowed in normal mode");
                return Err(ErrorReturnCode::InvalidMode);
            }

            let ch = i;

            let mut channels = QUEUING_PORTS.read().unwrap();

            // check if channel already exists
            if channels.contains(&ch) {
                trace!("yielding NoAction, because queuing port has already been created");
                return Err(ErrorReturnCode::NoAction);
            }

            // check if max number of channels is reached
            if channels.try_push(ch).is_some() {
                trace!(
                    "yielding InvalidConfig, maximum number of queuing ports (={}) already reached",
                    channels.len()
                );
                return Err(ErrorReturnCode::InvalidConfig);
            }
            QUEUING_PORTS.write(&channels).unwrap();

            return Ok(channels.len() as QueuingPortId);
        }

        trace!("yielding InvalidConfig, configuration does not declare queuing port {name}");
        Err(ErrorReturnCode::InvalidConfig)
    }

    fn send_queuing_message(
        queuing_port_id: QueuingPortId,
        message: &[ApexByte],
        _time_out: ApexSystemTime,
    ) -> Result<(), ErrorReturnCode> {
        let port = QUEUING_PORTS
            .read()
            .ok()
            .and_then(|ports| ports.into_iter().nth(queuing_port_id as usize - 1))
            .and_then(|port| CONSTANTS.queuing.get(port))
            .ok_or(ErrorReturnCode::InvalidParam)?;

        if message.len() > port.msg_size {
            return Err(ErrorReturnCode::InvalidConfig);
        } else if message.is_empty() {
            return Err(ErrorReturnCode::InvalidParam);
        } else if port.dir != PortDirection::Source {
            return Err(ErrorReturnCode::InvalidMode);
        }

        let written_bytes = QueuingSource::try_from(port.fd)
            .unwrap()
            .write(message, *SYSTEM_TIME)
            .ok_or(ErrorReturnCode::NotAvailable)?; // Queue is overflowed

        if written_bytes < message.len() {
            warn!(
                "Tried to write {} bytes to queuing port, but only {} bytes could be written",
                message.len(),
                written_bytes
            );
        }

        Ok(())
    }

    unsafe fn receive_queuing_message(
        queuing_port_id: QueuingPortId,
        _time_out: ApexSystemTime,
        message: &mut [ApexByte],
    ) -> Result<(MessageSize, QueueOverflow), ErrorReturnCode> {
        let port = QUEUING_PORTS
            .read()
            .ok()
            .and_then(|ports| ports.into_iter().nth(queuing_port_id as usize - 1))
            .and_then(|port| CONSTANTS.queuing.get(port))
            .ok_or(ErrorReturnCode::InvalidParam)?;

        if message.is_empty() {
            return Err(ErrorReturnCode::InvalidParam);
        } else if port.dir != PortDirection::Destination {
            return Err(ErrorReturnCode::InvalidMode);
        }
        let (msg_len, has_overflowed) = QueuingDestination::try_from(port.fd)
            .unwrap()
            .read(message)
            .ok_or(ErrorReturnCode::NotAvailable)?; // standard states that a length of 0 should also be set here, which the API
                                                    // does not allow

        Ok((msg_len as MessageSize, has_overflowed as QueueOverflow))
    }

    fn get_queuing_port_status(
        queuing_port_id: QueuingPortId,
    ) -> Result<QueuingPortStatus, ErrorReturnCode> {
        let port = QUEUING_PORTS
            .read()
            .ok()
            .and_then(|ports| ports.into_iter().nth(queuing_port_id as usize - 1))
            .and_then(|port| CONSTANTS.queuing.get(port))
            .ok_or(ErrorReturnCode::InvalidParam)?;

        let num_msgs = match port.dir {
            PortDirection::Source => QueuingSource::try_from(port.fd)
                .unwrap()
                .get_current_num_messages(),
            PortDirection::Destination => QueuingDestination::try_from(port.fd)
                .unwrap()
                .get_current_num_messages(),
        };

        let status = QueuingPortStatus {
            nb_message: num_msgs as MessageRange,
            max_nb_message: port.max_num_msg as MessageRange,
            max_message_size: port.msg_size as MessageSize,
            port_direction: port.dir,
            waiting_processes: 0,
        };

        Ok(status)
    }

    fn clear_queuing_port(queuing_port_id: QueuingPortId) -> Result<(), ErrorReturnCode> {
        let port = QUEUING_PORTS
            .read()
            .ok()
            .and_then(|ports| ports.into_iter().nth(queuing_port_id as usize - 1))
            .and_then(|port| CONSTANTS.queuing.get(port))
            .ok_or(ErrorReturnCode::InvalidParam)?;

        if port.dir != PortDirection::Destination {
            return Err(ErrorReturnCode::InvalidMode);
        }

        QueuingDestination::try_from(port.fd)
            .unwrap()
            .clear(*SYSTEM_TIME);

        Ok(())
    }
}

impl ApexTimeP4 for ApexLinuxPartition {
    fn periodic_wait() -> Result<(), ErrorReturnCode> {
        // TODO do not unwrap() (Maybe raise an error?);
        let proc = LinuxProcess::get_self().unwrap();
        if !proc.periodic() {
            return Err(ErrorReturnCode::InvalidMode);
        }

        proc.cg().unwrap().freeze().unwrap();
        Ok(())
    }

    fn get_time() -> ApexSystemTime {
        SYSTEM_TIME
            .elapsed()
            .as_nanos()
            .clamp(0, ApexSystemTime::MAX as u128) as ApexSystemTime
    }
}

impl ApexErrorP4 for ApexLinuxPartition {
    fn report_application_message(message: &[ApexByte]) -> Result<(), ErrorReturnCode> {
        if message.len() > MAX_ERROR_MESSAGE_SIZE {
            return Err(ErrorReturnCode::InvalidParam);
        }
        if let Ok(msg) = std::str::from_utf8(message) {
            // Logging may fail temporarily, because the resource can not be written to
            // (e.g. queue is full), but the API does not allow us any other
            // return code than INVALID_PARAM.
            if let Err(e) = SENDER.try_send(&PartitionCall::Message(msg.to_string())) {
                if let Some(e) = e.source().downcast_ref::<std::io::Error>() {
                    if e.raw_os_error() == Some(EAGAIN) {
                        return Ok(());
                    }
                }
                panic!("Failed to report application message: {}", e);
            }
        }
        Ok(())
    }

    fn raise_application_error(
        error_code: ErrorCode,
        message: &[ApexByte],
    ) -> Result<(), ErrorReturnCode> {
        if let ErrorCode::ApplicationError = error_code {
            Self::report_application_message(message).unwrap();
            Self::raise_system_error(SystemError::ApplicationError);
            Ok(())
        } else {
            Err(ErrorReturnCode::InvalidParam)
        }
    }
}