optee_teec/operation.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::{Param, ParamTypes, raw};
19use std::{marker::PhantomData, mem};
20
21/// This type defines the payload of either an open session operation or an
22/// invoke command operation. It is also used for cancellation of operations,
23/// which may be desirable even if no payload is passed.
24pub struct Operation<A, B, C, D> {
25 raw: raw::TEEC_Operation,
26 phantom0: PhantomData<A>,
27 phantom1: PhantomData<B>,
28 phantom2: PhantomData<C>,
29 phantom3: PhantomData<D>,
30}
31
32impl<A: Param, B: Param, C: Param, D: Param> Operation<A, B, C, D> {
33 pub fn new(started: u32, mut p0: A, mut p1: B, mut p2: C, mut p3: D) -> Operation<A, B, C, D> {
34 let mut raw_op: raw::TEEC_Operation = unsafe { mem::zeroed() };
35 raw_op.started = started;
36 raw_op.paramTypes = ParamTypes::new(
37 p0.param_type(),
38 p1.param_type(),
39 p2.param_type(),
40 p3.param_type(),
41 )
42 .into();
43 raw_op.params = [p0.to_raw(), p1.to_raw(), p2.to_raw(), p3.to_raw()];
44 Operation {
45 raw: raw_op,
46 phantom0: PhantomData,
47 phantom1: PhantomData,
48 phantom2: PhantomData,
49 phantom3: PhantomData,
50 }
51 }
52
53 pub(crate) fn as_mut_raw_ptr(&mut self) -> *mut raw::TEEC_Operation {
54 &mut self.raw
55 }
56
57 pub fn parameters(&self) -> (A, B, C, D) {
58 let (f0, f1, f2, f3) = ParamTypes::from(self.raw.paramTypes).into_flags();
59 (
60 A::from_raw(self.raw.params[0], f0),
61 B::from_raw(self.raw.params[1], f1),
62 C::from_raw(self.raw.params[2], f2),
63 D::from_raw(self.raw.params[3], f3),
64 )
65 }
66}