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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::{Param, ParamTypes};
use optee_teec_sys as raw;
use std::marker::PhantomData;
use std::mem;

/// This type defines the payload of either an open session operation or an
/// invoke command operation. It is also used for cancellation of operations,
/// which may be desirable even if no payload is passed.
pub struct Operation<A, B, C, D> {
    pub raw: raw::TEEC_Operation,
    phantom0: PhantomData<A>,
    phantom1: PhantomData<B>,
    phantom2: PhantomData<C>,
    phantom3: PhantomData<D>,
}

impl<A: Param, B: Param, C: Param, D: Param> Operation<A, B, C, D> {
    pub fn new(started: u32, mut p0: A, mut p1: B, mut p2: C, mut p3: D) -> Operation<A, B, C, D> {
        let mut raw_op: raw::TEEC_Operation = unsafe { mem::zeroed() };
        raw_op.started = started;
        raw_op.paramTypes = ParamTypes::new(
            p0.param_type(),
            p1.param_type(),
            p2.param_type(),
            p3.param_type(),
        )
        .into();
        raw_op.params = [p0.into_raw(), p1.into_raw(), p2.into_raw(), p3.into_raw()];
        Operation {
            raw: raw_op,
            phantom0: PhantomData,
            phantom1: PhantomData,
            phantom2: PhantomData,
            phantom3: PhantomData,
        }
    }

    pub fn as_mut_raw_ptr(&mut self) -> *mut raw::TEEC_Operation {
        &mut self.raw
    }

    pub fn parameters(&self) -> (A, B, C, D) {
        let (f0, f1, f2, f3) = ParamTypes::from(self.raw.paramTypes).into_flags();
        (
            A::from_raw(self.raw.params[0], f0),
            B::from_raw(self.raw.params[1], f1),
            C::from_raw(self.raw.params[2], f2),
            D::from_raw(self.raw.params[3], f3),
        )
    }
}