Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 INNER_INC_PACKAGE_MANAGER_H
12 : #define INNER_INC_PACKAGE_MANAGER_H
13 :
14 : #include "capability_manager.h"
15 : #include "device_comm_agent.h"
16 : #include "package_env_info.h"
17 : #include "package_hash_store.h"
18 : #include "package_process_config.h"
19 : #include "plugin_pkg_version.h"
20 : #include "plugin_version_manager.h"
21 : #include "package_sender.h"
22 : #include "package_check_code_service.h"
23 : #include "package_loader.h"
24 : #include "hdc_message_builder.h"
25 : #include "driver/ascend_hal.h"
26 : #include "proto/tsd_message.pb.h"
27 : #include "basic_define.h"
28 :
29 : #include <map>
30 : #include <string>
31 : #include <functional>
32 :
33 : namespace tsd {
34 :
35 : class PackageManager {
36 : public:
37 : PackageManager(
38 : uint32_t logicDeviceId, DeviceCommAgent& commAgent, CapabilityManager& capabilityMgr, uint32_t platInfoMode,
39 : bool isAdcEnv, uint32_t chipType);
40 : ~PackageManager();
41 :
42 : // === Open 流程入口 ===
43 25 : TSD_StatusT LoadPackageConfigInfoToDevice(const bool hasPluginVersion)
44 : {
45 25 : return loader_.LoadPackageConfigInfoToDevice(hasPluginVersion);
46 : }
47 31 : TSD_StatusT LoadSysOpKernel() { return loader_.LoadSysOpKernel(); }
48 29 : TSD_StatusT LoadPackageToDeviceByConfig() { return loader_.LoadPackageToDeviceByConfig(); }
49 :
50 1 : TSD_StatusT LoadFileToDevice(
51 : const char_t* const filePath, const uint64_t pathLen, const char_t* const fileName, const uint64_t fileNameLen,
52 : const MessageContext& baseCtx)
53 : {
54 1 : return loader_.LoadFileToDevice(filePath, pathLen, fileName, fileNameLen, baseCtx);
55 : }
56 :
57 : // === 设备响应处理(由 ProcessModeManager 静态回调转发)===
58 9 : void SaveDeviceCheckCode(const HDCMessage& msg) { checkCodeSvc_.SaveDeviceCheckCode(msg); }
59 96 : void StoreAllPkgHashValue(const HDCMessage& msg) { hashStore_.StoreAllPkgHashValue(msg); }
60 :
61 : // === 状态管理 ===
62 : void ResetOnClose();
63 : bool IsAicpuPackageExistInDevice() const { return loader_.aicpuPackageExistInDevice_; }
64 : void SetAicpuPackageExistInDevice(bool val) { loader_.aicpuPackageExistInDevice_ = val; }
65 258 : uint32_t GetHostCheckCode(TsdLoadPackageType type) const { return checkCodeSvc_.GetHostCheckCode(type); }
66 16 : void GetAscendLatestIntallPath(std::string& pkgBasePath) const { envInfo_.GetAscendLatestIntallPath(pkgBasePath); }
67 :
68 : // === 包扫描 ===
69 23 : bool CheckPackageExists(const bool loadAicpuKernelFlag = true)
70 : {
71 23 : return envInfo_.CheckPackageExists(loadAicpuKernelFlag);
72 : }
73 :
74 : bool GetPackageTitle(std::string& packageTitle) const { return envInfo_.GetPackageTitle(packageTitle); }
75 :
76 : const std::string& GetPackageName(uint32_t type) const { return envInfo_.GetPackageName(type); }
77 :
78 : const std::string& GetPackagePath(uint32_t type) const { return envInfo_.GetPackagePath(type); }
79 :
80 : uint32_t GetPlatInfoMode() const { return envInfo_.GetPlatInfoMode(); }
81 :
82 10 : void SetPlatInfoMode(uint32_t mode) { envInfo_.SetPlatInfoMode(mode); }
83 : bool IsAdcEnv() const { return envInfo_.IsAdcEnv(); }
84 : uint32_t GetPlatInfoChipType() const { return envInfo_.GetPlatInfoChipType(); }
85 5 : void SetPlatInfoChipType(uint32_t chipType) { envInfo_.SetPlatInfoChipType(chipType); }
86 :
87 12 : TSD_StatusT InitTsdClient() { return checkCodeSvc_.InitTsdClient(); }
88 1 : TSD_StatusT WaitPkgRsp(const uint32_t timeout, const bool ignoreRecvErr = false)
89 : {
90 1 : return checkCodeSvc_.WaitPkgRsp(timeout, ignoreRecvErr);
91 : }
92 4 : TSD_StatusT SendAICPUPackage(const int32_t peerNode, const std::string& path)
93 : {
94 4 : return sender_.SendAICPUPackage(peerNode, path);
95 : }
96 7 : TSD_StatusT SendAICPUPackageSimple(
97 : const int32_t peerNode, const std::string& orgFile, const std::string& dstFile, bool useCannPath)
98 : {
99 7 : return sender_.SendAICPUPackageSimple(peerNode, orgFile, dstFile, useCannPath);
100 : }
101 14 : TSD_StatusT SendHostPackageComplex(
102 : const int32_t peerNode, const std::string& orgFile, const std::string& dstFile, HDCMessage& msg,
103 : const std::function<bool(void)>& compareCallBack, bool useCannPath)
104 :
105 : {
106 14 : return sender_.SendHostPackageComplex(peerNode, orgFile, dstFile, msg, compareCallBack, useCannPath);
107 : }
108 10 : TSD_StatusT SendMsgAndHostPackage(
109 : const int32_t peerNode, const std::string& orgFile, const std::string& dstFile, HDCMessage& msg,
110 : const std::function<bool(void)>& compareCallBack, bool useCannPath)
111 :
112 : {
113 10 : return sender_.SendMsgAndHostPackage(peerNode, orgFile, dstFile, msg, compareCallBack, useCannPath);
114 : }
115 11 : TSD_StatusT SendCommonPackage(const int32_t peerNode, const std::string& path, const uint32_t packageType)
116 :
117 : {
118 11 : return sender_.SendCommonPackage(peerNode, path, packageType);
119 : }
120 4 : TSD_StatusT SendFileToDevice(
121 : const char_t* const filePath, const uint64_t pathLen, const char_t* const fileName, const uint64_t fileNameLen,
122 : const bool addPreFix = false)
123 :
124 : {
125 4 : return sender_.SendFileToDevice(filePath, pathLen, fileName, fileNameLen, addPreFix);
126 : }
127 2 : TSD_StatusT CompareAndSendCommonSinkPkg(
128 : const std::string& pkgPureName, const std::string& hostPkgHash, const int32_t peerNode,
129 : const std::string& orgFile, const std::string& dstFile)
130 : {
131 2 : return sender_.CompareAndSendCommonSinkPkg(pkgPureName, hostPkgHash, peerNode, orgFile, dstFile);
132 : }
133 :
134 9 : TSD_StatusT GetDeviceCheckCode() { return checkCodeSvc_.GetDeviceCheckCode(); }
135 4 : TSD_StatusT GetDeviceCheckCodeOnce(const HDCMessage& msg) { return checkCodeSvc_.GetDeviceCheckCodeOnce(msg); }
136 8 : TSD_StatusT GetDeviceCheckCodeRetry(const HDCMessage& msg) { return checkCodeSvc_.GetDeviceCheckCodeRetry(msg); }
137 5 : void GetDeviceCheckCodeRetrySupport() { checkCodeSvc_.GetDeviceCheckCodeRetrySupport(); }
138 15 : TSD_StatusT PrepareForCheckCode() { return checkCodeSvc_.PrepareForCheckCode(); }
139 6 : TSD_StatusT GetDeviceHsPkgCheckCode(
140 : const uint32_t checkCode, const HDCMessage::MsgType msgType, const bool beforeSendFlag,
141 : const MessageContext& baseCtx)
142 : {
143 6 : return checkCodeSvc_.GetDeviceHsPkgCheckCode(checkCode, msgType, beforeSendFlag, baseCtx);
144 : }
145 6 : TSD_StatusT GetCannHsPkgCheckCode(
146 : const std::string& pkgPureName, const std::string& hostPkgHash, const MessageContext& baseCtx)
147 : {
148 6 : return checkCodeSvc_.GetCannHsPkgCheckCode(pkgPureName, hostPkgHash, baseCtx);
149 : }
150 :
151 27 : TSD_StatusT LoadSinglePackageToDevice(
152 : const std::string& pkgPureName, const PackConfDetail& detail, int32_t peerNode, const std::string& dstDirPreFix)
153 : {
154 27 : return loader_.LoadSinglePackageToDevice(pkgPureName, detail, peerNode, dstDirPreFix);
155 : }
156 :
157 8 : TSD_StatusT LoadCannHsPkgToDevice(const std::string& pkgPureName, const MessageContext& baseCtx)
158 :
159 : {
160 8 : return loader_.LoadCannHsPkgToDevice(pkgPureName, baseCtx);
161 : }
162 :
163 7 : TSD_StatusT LoadFileAndWaitRsp(
164 : const std::string& pkgPureName, const std::string& hostPkgHash, const int32_t peerNode,
165 : const std::string& orgFile, const std::string& dstFile, const MessageContext& baseCtx)
166 : {
167 7 : return loader_.LoadFileAndWaitRsp(pkgPureName, hostPkgHash, peerNode, orgFile, dstFile, baseCtx);
168 : }
169 :
170 9 : TSD_StatusT LoadRuntimePkgToDevice(const MessageContext& baseCtx)
171 : {
172 9 : return loader_.LoadRuntimePkgToDevice(baseCtx);
173 : }
174 :
175 2 : TSD_StatusT LoadDShapePkgToDevice(const MessageContext& baseCtx) { return loader_.LoadDShapePkgToDevice(baseCtx); }
176 :
177 0 : TSD_StatusT LoadOmFileToDevice(
178 : const char_t* const filePath, const uint64_t pathLen, const char_t* const fileName, const uint64_t fileNameLen,
179 : const MessageContext& baseCtx)
180 : {
181 0 : return loader_.LoadOmFileToDevice(filePath, pathLen, fileName, fileNameLen, baseCtx);
182 : }
183 :
184 : TSD_StatusT GetTrustedBasePathFromDevice(int32_t& peerNode, std::string& dstDirPreFix)
185 : {
186 : return envInfo_.GetTrustedBasePathFromDevice(peerNode, dstDirPreFix);
187 : }
188 : void SetDeviceCommonSinkPackHashValue(const std::string& pkgName, const std::string& hashValue)
189 : {
190 : hashStore_.SetDeviceCommonSinkPackHashValue(pkgName, hashValue);
191 : }
192 : std::string GetDeviceCommonSinkPackHashValue(const std::string& pkgName) const
193 : {
194 : return hashStore_.GetDeviceCommonSinkPackHashValue(pkgName);
195 : }
196 : void SetHostCommonSinkPackHashValue(const std::string& pkgName, const std::string& hashValue)
197 : {
198 : hashStore_.SetHostCommonSinkPackHashValue(pkgName, hashValue);
199 : }
200 : std::string GetHostCommonSinkPackHashValue(const std::string& pkgName) const
201 : {
202 : return hashStore_.GetHostCommonSinkPackHashValue(pkgName);
203 : }
204 9 : bool IsCommonSinkHostAndDevicePkgSame(const std::string& pkgName) const
205 :
206 : {
207 9 : return hashStore_.IsCommonSinkHostAndDevicePkgSame(pkgName);
208 : }
209 17 : bool IsCompatPluginPackage(const PackConfDetail& detail) const
210 : {
211 17 : return pluginVersion_.IsCompatPluginPackage(detail);
212 : }
213 6 : PluginUpdateStrategy GetPluginUpdateStrategy() { return pluginVersion_.GetPluginUpdateStrategy(); }
214 10 : bool ShouldLoadCompatPluginPkg(const std::string& pkgPureName)
215 :
216 : {
217 10 : return pluginVersion_.ShouldLoadCompatPluginPkg(pkgPureName);
218 : }
219 : bool CompareHostDeviceCompatPluginVersion(const std::string& pkgPureName)
220 :
221 : {
222 : return pluginVersion_.CompareHostDeviceCompatPluginVersion(pkgPureName);
223 : }
224 0 : void HandleDevicePluginVersionRsp(const HDCMessage& msg) { pluginVersion_.HandleDevicePluginVersionRsp(msg); }
225 : void HandleNormalPackageCheckCodeRsp(const HDCMessage& msg) { checkCodeSvc_.HandleNormalPackageCheckCodeRsp(msg); }
226 : void HandleCannHsCheckCodeRsp(const HDCMessage& msg) { checkCodeSvc_.HandleCannHsCheckCodeRsp(msg); }
227 8 : bool SupportLoadPkg(const std::string& pkgName) const { return loader_.SupportLoadPkg(pkgName); }
228 2 : bool IsOkToLoadFileToDevice(const char_t* const fileName, const uint64_t fileNameLen)
229 : {
230 2 : return loader_.IsOkToLoadFileToDevice(fileName, fileNameLen);
231 : }
232 : void ReportSinkPkgRspError(const std::string& pkgPureName) { loader_.ReportSinkPkgRspError(pkgPureName); }
233 7 : std::string GetCurHostMutexFile(bool useCannPath) const { return envInfo_.GetCurHostMutexFile(useCannPath); }
234 3 : bool GetShortSocVersion(std::string& shortSocVersion) const { return envInfo_.GetShortSocVersion(shortSocVersion); }
235 : ResponseCode GetPkgRspCode() const { return pkgRspCode_; }
236 : void SetPkgRspCode(ResponseCode code) { pkgRspCode_ = code; }
237 : bool getCheckCodeRetrySupport_;
238 : bool deviceIdle_;
239 : std::string loadPackageErrorMsg_;
240 : ResponseCode pkgRspCode_ = ResponseCode::FAIL;
241 :
242 : private:
243 : PackageEnvInfo envInfo_;
244 : PackageHashStore hashStore_;
245 : PluginVersionManager pluginVersion_;
246 : std::string (&packageName_)[static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_MAX)];
247 : std::map<std::string, std::string>& pkgHostHashValue_;
248 : std::map<std::string, std::string>& pkgDeviceHashValue_;
249 : std::string GetTrustedBasePath(bool useV2) const { return envInfo_.GetTrustedBasePath(useV2); }
250 :
251 : DeviceCommAgent& commAgent_;
252 : CapabilityManager& capabilityMgr_;
253 :
254 : std::map<std::string, PluginPkgVersion>& devicePluginVersions_;
255 : PluginUpdateStrategy& pluginUpdateStrategy_;
256 : bool& hasComputedPluginStrategy_;
257 : PackageSender sender_;
258 : PackageCheckCodeService checkCodeSvc_;
259 : PackageLoader loader_;
260 :
261 : public:
262 : bool& aicpuPackageExistInDevice_;
263 : uint32_t (&packagePeerCheckCode_)[static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_MAX)];
264 : uint32_t (&packageHostCheckCode_)[static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_MAX)];
265 : };
266 :
267 : } // namespace tsd
268 :
269 : #endif // INNER_INC_PACKAGE_MANAGER_H
|