Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef TSD_PACKAGE_PROCESS_CONFIG_H
12 : #define TSD_PACKAGE_PROCESS_CONFIG_H
13 :
14 : #include <string>
15 : #include <memory>
16 : #include <map>
17 : #include "tsd/status.h"
18 : #include "tsd_log.h"
19 : #include "proto/tsd_message.pb.h"
20 : #include "plugin_pkg_version.h"
21 :
22 : namespace tsd {
23 : enum class DeviceInstallPath {
24 : RUNTIME_PATH = 0,
25 : OM_PATH = 1,
26 : AICPU_KERNELS_PATH = 2,
27 : COMPAT_PLUGIN_PATH = 3,
28 : MAX_PATH = 255
29 : };
30 : struct PackConfDetail {
31 : DeviceInstallPath decDstDir = DeviceInstallPath::MAX_PATH;
32 : std::string findPath = "";
33 : std::string hostTruePath = "";
34 : bool optionalFlag = false;
35 : bool validFlag = false;
36 : bool loadAsPerSocFlag = false;
37 75 : PackConfDetail()
38 75 : : decDstDir(DeviceInstallPath::MAX_PATH),
39 150 : findPath(""),
40 150 : hostTruePath(""),
41 75 : optionalFlag(false),
42 75 : validFlag(false),
43 75 : loadAsPerSocFlag(false)
44 75 : {}
45 13 : void PrintfInfo(const std::string& pkgName)
46 : {
47 13 : TSD_INFO(
48 : "package:%s, decDstDir:%u, findPath:%s, hostTruePath:%s, "
49 : "optionalFlag:%u, validFlag:%u, loadAsPerSocFlag:%u",
50 : pkgName.c_str(), static_cast<uint32_t>(decDstDir), findPath.c_str(), hostTruePath.c_str(),
51 : static_cast<uint32_t>(optionalFlag), static_cast<uint32_t>(validFlag),
52 : static_cast<uint32_t>(loadAsPerSocFlag));
53 13 : }
54 : };
55 :
56 : class PackageProcessConfig {
57 : public:
58 : static PackageProcessConfig* GetInstance();
59 : PackConfDetail GetConfigDetailInfo(const std::string& srcPath);
60 : TSD_StatusT ParseConfigDataFromProtoBuf(const HDCMessage& hdcMsg);
61 : TSD_StatusT ParseConfigDataFromFile(const std::string& pkgTitle);
62 : void ConstructPkgConfigMsg(HDCMessage& hdcMsg);
63 : bool IsNeedToUpdateConfig(const HDCMessage& hdcMsg) const;
64 : bool IsConfigPackageInfo(const std::string& oriPkgName);
65 8 : std::map<std::string, PackConfDetail> GetAllPackageConfigInfo() const { return configMap_; }
66 : TSD_StatusT GetPkgHostAndDeviceDstPath(
67 : const std::string& pkgName, std::string& orgFile, std::string& dstFile, const pid_t hostPid);
68 : std::string GetPackageHostTruePath(const std::string& pkgName);
69 : // 遍历 configMap_ ,对 compat 插件包解析同名 .ini 文件,刷新 host 侧插件包版本信息
70 : void RefreshHostPluginVersions();
71 : PluginPkgVersion GetHostPluginVersion(const std::string& pkgName);
72 : bool HasCompatPluginPackage() const;
73 :
74 : private:
75 : bool SetConfigDataOnServer(const SinkPackageConfig& hdcConfig);
76 : bool SetConfigDataOnHost(std::ifstream& inFile, const std::string& fileName, const std::string& pkgTitle);
77 : std::string GetHostFilePath(const std::string& fileDir, const std::string& fileName) const;
78 : bool SetPkgHostTruePath(PackConfDetail& tempNode, const std::string& pkgName, const std::string& pkgTitle) const;
79 : PackageProcessConfig();
80 5 : ~PackageProcessConfig() = default;
81 : bool ParseSinglePara(
82 : std::string& inputLine, PackConfDetail& tempNode, std::unordered_set<std::string>& finishedParseItemSet) const;
83 : bool ParseInstallPath(const std::string& para, PackConfDetail& tempNode) const;
84 : bool ParseOptionalFlag(const std::string& para, PackConfDetail& tempNode) const;
85 : bool ParsePackagePath(const std::string& para, PackConfDetail& tempNode) const;
86 : bool ParseLoadAsPerSocFlag(const std::string& para, PackConfDetail& tempNode) const;
87 :
88 : private:
89 : std::map<std::string, PackConfDetail> configMap_;
90 : std::map<std::string, PluginPkgVersion> hostPluginVersions_;
91 : std::mutex configMut_;
92 : bool finishParse_ = false;
93 : std::string hashCode_;
94 : using SingleParaParseFunc = bool (PackageProcessConfig::*)(const std::string&, PackConfDetail&) const;
95 : const std::map<std::string, SingleParaParseFunc> configParaParseFuncMap_;
96 : std::mutex hostPluginVersionMut_;
97 : };
98 : } // namespace tsd
99 : #endif // TSD_PACKAGE_PROCESS_CONFIG_H
|