1 /*
2  * Copyright (C) 2024 The Android Open Source Project
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 #pragma once
18 
19 #include <utils/Singleton.h>
20 
21 #include <fstream>
22 #include <optional>
23 #include <sstream>
24 #include <unordered_map>
25 
26 #include <log/log.h>
27 
28 namespace android::hardware::graphics::composer {
29 
30 class FileNode {
31 public:
32     FileNode(const std::string& nodePath);
33     ~FileNode();
34 
35     std::string dump();
36 
37     uint32_t getLastWrittenValue(const std::string& nodeName);
38 
39     std::optional<std::string> readString(const std::string& nodeName);
40 
41     bool WriteUint32(const std::string& nodeName, uint32_t value);
42 
43 private:
44     int getFileHandler(const std::string& nodeName);
45 
46     std::string mNodePath;
47     std::unordered_map<std::string, int> mFds;
48     std::unordered_map<int, uint32_t> mLastWrittenValue;
49 };
50 
51 class FileNodeManager : public Singleton<FileNodeManager> {
52 public:
53     FileNodeManager() = default;
54     ~FileNodeManager() = default;
55 
getFileNode(const std::string & nodePath)56     std::shared_ptr<FileNode> getFileNode(const std::string& nodePath) {
57         if (mFileNodes.find(nodePath) == mFileNodes.end()) {
58             mFileNodes[nodePath] = std::make_shared<FileNode>(nodePath);
59         }
60         return mFileNodes[nodePath];
61     }
62 
63 private:
64     std::unordered_map<std::string, std::shared_ptr<FileNode>> mFileNodes;
65 };
66 
67 } // namespace android::hardware::graphics::composer
68