autosar_data_abstraction/software_component/interface/
clientserver.rs

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
use crate::{
    abstraction_element,
    datatype::{self, AbstractAutosarDataType},
    software_component::AbstractPortInterface,
    AbstractionElement, ArPackage, AutosarAbstractionError, Element, EnumItem,
};
use autosar_data::ElementName;
use datatype::AutosarDataType;

//##################################################################

/// A `ClientServerInterface` defines a set of operations that can be implemented by a server and called by a client
///
/// Use [`ArPackage::create_client_server_interface`] to create a new client server interface
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ClientServerInterface(pub(crate) Element);
abstraction_element!(ClientServerInterface, ClientServerInterface);

impl AbstractPortInterface for ClientServerInterface {}

impl ClientServerInterface {
    /// Create a new `ClientServerInterface`
    pub(crate) fn new(name: &str, package: &ArPackage) -> Result<Self, AutosarAbstractionError> {
        let elements = package.element().get_or_create_sub_element(ElementName::Elements)?;
        let client_server_interface = elements.create_named_sub_element(ElementName::ClientServerInterface, name)?;

        Ok(Self(client_server_interface))
    }

    /// Add a possible error to the client server interface
    pub fn create_possible_error(
        &self,
        name: &str,
        error_code: u64,
    ) -> Result<ApplicationError, AutosarAbstractionError> {
        let possible_errors = self.element().get_or_create_sub_element(ElementName::PossibleErrors)?;
        ApplicationError::new(name, error_code, &possible_errors)
    }

    /// add an operation to the client server interface
    pub fn create_operation(&self, name: &str) -> Result<ClientServerOperation, AutosarAbstractionError> {
        let operations = self.element().get_or_create_sub_element(ElementName::Operations)?;
        ClientServerOperation::new(name, &operations)
    }

    /// iterate over all operations
    pub fn operations(&self) -> impl Iterator<Item = ClientServerOperation> {
        self.element()
            .get_sub_element(ElementName::Operations)
            .into_iter()
            .flat_map(|operations| operations.sub_elements())
            .filter_map(|elem| ClientServerOperation::try_from(elem).ok())
    }

    /// iterate over all application errors
    pub fn possible_errors(&self) -> impl Iterator<Item = ApplicationError> {
        self.element()
            .get_sub_element(ElementName::PossibleErrors)
            .into_iter()
            .flat_map(|errors| errors.sub_elements())
            .filter_map(|elem| ApplicationError::try_from(elem).ok())
    }
}

//##################################################################

/// An `ApplicationError` represents an error that can be returned by a client server operation
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ApplicationError(Element);
abstraction_element!(ApplicationError, ApplicationError);

impl ApplicationError {
    /// Create a new `ApplicationError`
    fn new(name: &str, error_code: u64, parent_element: &Element) -> Result<Self, AutosarAbstractionError> {
        let application_error = parent_element.create_named_sub_element(ElementName::ApplicationError, name)?;
        application_error
            .create_sub_element(ElementName::ErrorCode)?
            .set_character_data(error_code)?;
        Ok(Self(application_error))
    }

    /// Get the error code of the application error
    pub fn error_code(&self) -> Option<u64> {
        self.element()
            .get_sub_element(ElementName::ErrorCode)?
            .character_data()?
            .parse_integer()
    }
}

//##################################################################

/// A `ClientServerOperation` defines an operation in a `ClientServerInterface`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ClientServerOperation(Element);
abstraction_element!(ClientServerOperation, ClientServerOperation);

impl ClientServerOperation {
    /// Create a new `ClientServerOperation`
    fn new(name: &str, parent_element: &Element) -> Result<Self, AutosarAbstractionError> {
        let operation = parent_element.create_named_sub_element(ElementName::ClientServerOperation, name)?;
        Ok(Self(operation))
    }

    /// Add an argument to the operation
    pub fn create_argument<T: AbstractAutosarDataType>(
        &self,
        name: &str,
        data_type: &T,
        direction: ArgumentDirection,
    ) -> Result<ArgumentDataPrototype, AutosarAbstractionError> {
        let arguments = self.element().get_or_create_sub_element(ElementName::Arguments)?;
        ArgumentDataPrototype::new(name, &arguments, data_type.element(), direction)
    }

    /// iterate over all arguments
    pub fn arguments(&self) -> impl Iterator<Item = ArgumentDataPrototype> {
        self.element()
            .get_sub_element(ElementName::Arguments)
            .into_iter()
            .flat_map(|arguments| arguments.sub_elements())
            .filter_map(|elem| ArgumentDataPrototype::try_from(elem).ok())
    }

    /// add a reference to possible error to the operation
    pub fn add_possible_error(&self, error: &ApplicationError) -> Result<(), AutosarAbstractionError> {
        if self.element().named_parent()? != error.element().named_parent()? {
            return Err(AutosarAbstractionError::InvalidParameter(
                "Error and operation must be in the same ClientServerInterface".to_string(),
            ));
        }

        let possible_errors = self
            .element()
            .get_or_create_sub_element(ElementName::PossibleErrorRefs)?;
        possible_errors
            .create_sub_element(ElementName::PossibleErrorRef)?
            .set_reference_target(error.element())?;
        Ok(())
    }

    /// Get the possible errors of the operation
    pub fn possible_errors(&self) -> impl Iterator<Item = ApplicationError> {
        self.element()
            .get_sub_element(ElementName::PossibleErrorRefs)
            .into_iter()
            .flat_map(|errors| errors.sub_elements())
            .filter_map(|refelem| {
                refelem
                    .get_reference_target()
                    .ok()
                    .and_then(|elem| ApplicationError::try_from(elem).ok())
            })
    }
}

//##################################################################

/// The `ArgumentDirection` defines the direction of an argument in a `ClientServerOperation`
///
/// Input arguments are used to pass data from the client to the server and are usualy passed by value.
/// Output arguments are used to pass data from the server to the client and are usually passed by reference.
/// In/Out arguments are used to pass data in both directions and are usually passed by reference.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArgumentDirection {
    /// The argument is an input argument
    In,
    /// The argument is an output argument
    Out,
    /// The argument is an in/out argument
    InOut,
}

impl TryFrom<EnumItem> for ArgumentDirection {
    type Error = AutosarAbstractionError;

    fn try_from(item: EnumItem) -> Result<Self, Self::Error> {
        match item {
            EnumItem::In => Ok(ArgumentDirection::In),
            EnumItem::Out => Ok(ArgumentDirection::Out),
            EnumItem::Inout => Ok(ArgumentDirection::InOut),
            _ => Err(AutosarAbstractionError::ValueConversionError {
                value: item.to_string(),
                dest: "ArgumentDirection".to_string(),
            }),
        }
    }
}

impl From<ArgumentDirection> for EnumItem {
    fn from(direction: ArgumentDirection) -> Self {
        match direction {
            ArgumentDirection::In => EnumItem::In,
            ArgumentDirection::Out => EnumItem::Out,
            ArgumentDirection::InOut => EnumItem::Inout,
        }
    }
}

//##################################################################

/// An `ArgumentDataPrototype` represents an argument in a `ClientServerOperation`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ArgumentDataPrototype(Element);
abstraction_element!(ArgumentDataPrototype, ArgumentDataPrototype);

impl ArgumentDataPrototype {
    /// Create a new `ArgumentDataPrototype`
    fn new(
        name: &str,
        parent_element: &Element,
        data_type: &Element,
        direction: ArgumentDirection,
    ) -> Result<Self, AutosarAbstractionError> {
        let argument = parent_element.create_named_sub_element(ElementName::ArgumentDataPrototype, name)?;
        argument
            .create_sub_element(ElementName::TypeTref)?
            .set_reference_target(data_type)?;
        argument
            .create_sub_element(ElementName::Direction)?
            .set_character_data::<EnumItem>(direction.into())?;
        Ok(Self(argument))
    }

    /// Get the data type of the argument
    pub fn data_type(&self) -> Option<AutosarDataType> {
        let data_type_elem = self
            .element()
            .get_sub_element(ElementName::TypeTref)?
            .get_reference_target()
            .ok()?;
        AutosarDataType::try_from(data_type_elem).ok()
    }

    /// Get the direction of the argument
    pub fn direction(&self) -> Option<ArgumentDirection> {
        let value = self
            .element()
            .get_sub_element(ElementName::Direction)?
            .character_data()?
            .enum_value()?;

        ArgumentDirection::try_from(value).ok()
    }
}

//##################################################################

#[cfg(test)]
mod test {
    use super::*;
    use autosar_data::{AutosarModel, AutosarVersion};
    use datatype::{BaseTypeEncoding, ImplementationDataTypeSettings};

    #[test]
    fn test_client_server_interface() {
        let model = AutosarModel::new();
        let _file = model.create_file("filename", AutosarVersion::LATEST).unwrap();
        let package = ArPackage::get_or_create(&model, "/package").unwrap();
        let client_server_interface = ClientServerInterface::new("TestInterface", &package).unwrap();

        assert_eq!(client_server_interface.name().unwrap(), "TestInterface");
        assert_eq!(client_server_interface.operations().count(), 0);
        assert_eq!(client_server_interface.possible_errors().count(), 0);

        let error = client_server_interface.create_possible_error("TestError", 42).unwrap();
        assert_eq!(client_server_interface.possible_errors().count(), 1);
        assert_eq!(error.name().unwrap(), "TestError");
        assert_eq!(error.error_code().unwrap(), 42);

        let operation = client_server_interface.create_operation("TestOperation").unwrap();
        assert_eq!(client_server_interface.operations().count(), 1);
        assert_eq!(operation.name().unwrap(), "TestOperation");
        assert_eq!(operation.arguments().count(), 0);

        operation.add_possible_error(&error).unwrap();
        assert_eq!(operation.possible_errors().count(), 1);

        let base_type = package
            .create_sw_base_type("base", 32, BaseTypeEncoding::None, None, None, None)
            .unwrap();
        let impl_settings = ImplementationDataTypeSettings::Value {
            name: "ImplementationValue".to_string(),
            base_type,
            compu_method: None,
            data_constraint: None,
        };
        let datatype = package.create_implementation_data_type(impl_settings).unwrap();
        let argument = operation
            .create_argument("TestArgument", &datatype, ArgumentDirection::In)
            .unwrap();
        assert_eq!(argument.name().unwrap(), "TestArgument");
        assert_eq!(argument.data_type().unwrap().name().unwrap(), "ImplementationValue");
        assert_eq!(argument.direction().unwrap(), ArgumentDirection::In);
        assert_eq!(operation.arguments().count(), 1);
    }
}