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
// 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 optee_utee_sys as raw;

/// Represent the characteristics of an object.
/// This info can be returned by [GenericObject](crate::GenericObject) function
/// [info](crate::GenericObject::info)
pub struct ObjectInfo {
    pub(crate) raw: raw::TEE_ObjectInfo,
}

// Since raw struct is not implemented Copy attribute yet, every item in raw
// struct needs a function to extract.
impl ObjectInfo {
    /// Return an [ObjectInfo](crate::ObjectInfo) struct based on the raw
    /// structure `TEE_ObjectInfo`.
    /// The raw structure contains following fields:
    ///
    /// 1) `objectType`: The parameter represents one of the
    ///    [TransientObjectType](crate::TransientObjectType).
    /// 2) `objectSize`: The current size in bits of the object as determined
    ///    by its attributes.
    ///    This will always be less than or equal to maxObjectSize. Set to 0 for
    ///    uninitialized and data only objects.
    /// 3) `maxObjectSize`: The maximum objectSize which this object can
    ///    represent.
    ///     * For a [PersistentObject](crate::PersistentObject), set to
    ///       `objectSize`.
    ///     * For a [TransientObject](crate::TransientObject), set to the
    ///       parameter `maxObjectSize` passed to
    ///       [allocate](crate::TransientObject::allocate).
    /// 4) `objectUsage`: A bit vector of UsageFlag.
    /// 5) `dataSize`:
    ///     * For a [PersistentObject](crate::PersistentObject), set to the
    ///       current size of the data associated with the object.
    ///     * For a [TransientObject](crate::TransientObject), always set to 0.
    /// 6) `dataPosition`:
    ///     * For a [PersistentObject](crate::PersistentObject), set to the
    ///       current position in the data for this handle.
    ///       Data positions for different handles on the same object may
    ///       differ.
    ///     * For a [TransientObject](crate::TransientObject), set to 0.
    /// 7) `handleFlags`: A bit vector containing one or more
    ///    [HandleFlag](crate::HandleFlag) or [DataFlag](crate::DataFlag).
    pub fn from_raw(raw: raw::TEE_ObjectInfo) -> Self {
        Self { raw }
    }

    /// Return the `dataSize` field of the raw structure `TEE_ObjectInfo`.
    pub fn data_size(&self) -> usize {
        self.raw.dataSize as usize
    }

    /// Return the `objectSize` field of the raw structure `TEE_ObjectInfo`.
    pub fn object_size(&self) -> usize {
        self.raw.objectSize as usize
    }

    /// Return the `objectType` field of the raw structure `TEE_ObjectInfo`.
    pub fn object_type(&self) -> u32 {
        self.raw.objectType
    }
}