2d5b6c89b112c7113fe8318b08dfd6524e1a103d
[platform/core/webapi/libteec.git] / src / teec / TeecContext.h
1 /**
2  * Copyright (c) 2017-2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file
18  * @author Lukasz Kostyra (l.kostyra@samsung.com)
19  * @brief  TeecContext class declaration
20  */
21
22 #ifndef LIBTEEC_TEEC_CONTEXT_H_
23 #define LIBTEEC_TEEC_CONTEXT_H_
24
25 #include "TeecSession.h"
26 #include "TeecSharedMemory.h"
27
28 #include <unordered_map>
29 #include <string>
30
31 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
32 #include <tee_client_api.h>
33
34 namespace extension {
35 namespace libteec {
36
37 using TeecSessionMap = std::unordered_map<std::string, TeecSessionPtr>;
38 using TeecSharedMemoryMap = std::unordered_map<std::string, TeecSharedMemoryPtr>;
39
40 class TeecContext final
41 {
42 public:
43     TeecContext(const std::string& id);
44     ~TeecContext();
45
46     // returns session ID used to recognize sessions in GetSession and CloseSession
47     std::string OpenSession(const std::string& uuidstr, uint32_t connectionMethod,
48                             const void* connectionData, TEEC_Operation* operation,
49                             uint32_t* returnOrigin);
50     TeecSessionPtr GetSession(const std::string& id) const;
51     void CloseSession(const std::string& id);
52
53     // returns memory ID used to recognise the memory further down the road
54     std::string CreateSharedMemory(size_t size, uint32_t flags);
55     std::string CreateSharedMemory(void* buffer, size_t size, uint32_t flags);
56     TeecSharedMemoryPtr GetSharedMemory(const std::string& memId) const;
57     void ReleaseSharedMemory(const std::string& memId);
58
59 private:
60     TEEC_Context mContext;
61     TeecSessionMap mSessions;
62     TeecSharedMemoryMap mSharedMemories;
63     static const std::string teeFeatureName;
64     bool teeIsEnabled();
65 };
66
67 using TeecContextPtr = std::shared_ptr<TeecContext>;
68
69 } // namespace libteec
70 } // namespace extension
71
72 #endif // LIBTEEC_TEEC_CONTEXT_H_