trace_recorder_parser/snapshot/
object_properties.rs

1use crate::types::{
2    IsrPriority, ObjectClass, ObjectHandle, Priority, TaskPriority, UNNAMED_OBJECT,
3};
4use derive_more::{Display, Into};
5use std::collections::BTreeMap;
6use std::marker::PhantomData;
7
8#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
9pub struct ObjectPropertyTable {
10    pub queue_object_properties: BTreeMap<ObjectHandle, ObjectProperties<QueueObjectClass>>,
11    pub semaphore_object_properties: BTreeMap<ObjectHandle, ObjectProperties<SemaphoreObjectClass>>,
12    pub mutex_object_properties: BTreeMap<ObjectHandle, ObjectProperties<MutexObjectClass>>,
13    pub task_object_properties: BTreeMap<ObjectHandle, ObjectProperties<TaskObjectClass>>,
14    pub isr_object_properties: BTreeMap<ObjectHandle, ObjectProperties<IsrObjectClass>>,
15    pub timer_object_properties: BTreeMap<ObjectHandle, ObjectProperties<TimerObjectClass>>,
16    pub event_group_object_properties:
17        BTreeMap<ObjectHandle, ObjectProperties<EventGroupObjectClass>>,
18    pub stream_buffer_object_properties:
19        BTreeMap<ObjectHandle, ObjectProperties<StreamBufferObjectClass>>,
20    pub message_buffer_object_properties:
21        BTreeMap<ObjectHandle, ObjectProperties<MessageBufferObjectClass>>,
22}
23
24pub trait ObjectClassExt {
25    fn class() -> ObjectClass;
26}
27
28#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
29#[display(fmt = "{}('{}')", "self.class()", "self.display_name()")]
30pub struct ObjectProperties<C: ObjectClassExt> {
31    name: Option<String>,
32    properties: [u8; 4],
33    _class: PhantomData<C>,
34}
35
36impl<C: ObjectClassExt> ObjectProperties<C> {
37    pub const UNNAMED_OBJECT: &'static str = UNNAMED_OBJECT;
38
39    pub(crate) fn new(name: Option<String>, properties: [u8; 4]) -> Self {
40        ObjectProperties {
41            name,
42            properties,
43            _class: PhantomData,
44        }
45    }
46
47    pub fn name(&self) -> Option<&str> {
48        self.name.as_deref()
49    }
50
51    pub fn display_name(&self) -> &str {
52        self.name().unwrap_or(Self::UNNAMED_OBJECT)
53    }
54
55    pub fn class(&self) -> ObjectClass {
56        C::class()
57    }
58}
59
60#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
61pub struct QueueObjectClass;
62impl ObjectClassExt for QueueObjectClass {
63    fn class() -> ObjectClass {
64        ObjectClass::Queue
65    }
66}
67
68impl ObjectProperties<QueueObjectClass> {
69    /// Current number of message in queue
70    pub fn queue_length(&self) -> u8 {
71        self.properties[0]
72    }
73}
74
75#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
76pub struct SemaphoreObjectClass;
77impl ObjectClassExt for SemaphoreObjectClass {
78    fn class() -> ObjectClass {
79        ObjectClass::Semaphore
80    }
81}
82
83#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
84pub enum SemaphoreState {
85    #[display(fmt = "Cleared")]
86    Cleared,
87    #[display(fmt = "Signaled")]
88    Signaled,
89}
90
91impl ObjectProperties<SemaphoreObjectClass> {
92    pub fn state(&self) -> SemaphoreState {
93        if self.properties[0] == 0 {
94            SemaphoreState::Cleared
95        } else {
96            SemaphoreState::Signaled
97        }
98    }
99}
100
101#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
102pub struct MutexObjectClass;
103impl ObjectClassExt for MutexObjectClass {
104    fn class() -> ObjectClass {
105        ObjectClass::Mutex
106    }
107}
108#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Into, Display)]
109#[display(fmt = "{_0}")]
110pub struct TaskHandle(u8);
111
112#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
113pub enum MutexOwner {
114    #[display(fmt = "TaskHandle({_0})")]
115    TaskHandle(TaskHandle),
116    #[display(fmt = "Free")]
117    Free,
118}
119
120impl ObjectProperties<MutexObjectClass> {
121    pub fn owner(&self) -> MutexOwner {
122        let owner = self.properties[0];
123        if owner == 0 {
124            MutexOwner::Free
125        } else {
126            MutexOwner::TaskHandle(TaskHandle(owner))
127        }
128    }
129}
130
131#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
132pub struct TaskObjectClass;
133impl ObjectClassExt for TaskObjectClass {
134    fn class() -> ObjectClass {
135        ObjectClass::Task
136    }
137}
138
139impl TaskObjectClass {
140    pub const STARTUP_TASK_NAME: &'static str = crate::types::STARTUP_TASK_NAME;
141}
142
143#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
144pub enum TaskState {
145    #[display(fmt = "Inactive")]
146    Inactive,
147    #[display(fmt = "Active")]
148    Active,
149}
150
151impl ObjectProperties<TaskObjectClass> {
152    pub fn current_priority(&self) -> TaskPriority {
153        Priority(self.properties[0].into())
154    }
155
156    pub fn state(&self) -> TaskState {
157        if self.properties[1] == 0 {
158            TaskState::Inactive
159        } else {
160            TaskState::Active
161        }
162    }
163}
164
165#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
166pub struct IsrObjectClass;
167impl ObjectClassExt for IsrObjectClass {
168    fn class() -> ObjectClass {
169        ObjectClass::Isr
170    }
171}
172
173impl ObjectProperties<IsrObjectClass> {
174    pub fn priority(&self) -> IsrPriority {
175        Priority(self.properties[1].into())
176    }
177}
178
179#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
180pub struct TimerObjectClass;
181impl ObjectClassExt for TimerObjectClass {
182    fn class() -> ObjectClass {
183        ObjectClass::Timer
184    }
185}
186
187#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
188pub struct EventGroupObjectClass;
189impl ObjectClassExt for EventGroupObjectClass {
190    fn class() -> ObjectClass {
191        ObjectClass::EventGroup
192    }
193}
194
195#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
196pub struct StreamBufferObjectClass;
197impl ObjectClassExt for StreamBufferObjectClass {
198    fn class() -> ObjectClass {
199        ObjectClass::StreamBuffer
200    }
201}
202
203#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
204pub struct MessageBufferObjectClass;
205impl ObjectClassExt for MessageBufferObjectClass {
206    fn class() -> ObjectClass {
207        ObjectClass::MessageBuffer
208    }
209}