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 TSD_BASIC_COMPONENT_CAPABILITY_CAPABILITY_MANAGER_H
12 : #define TSD_BASIC_COMPONENT_CAPABILITY_CAPABILITY_MANAGER_H
13 :
14 : #include "device_comm_agent.h"
15 : #include "hdc_message_builder.h"
16 : #include "inc/client_manager.h"
17 : #include "basic_define.h"
18 : #include "proto/tsd_message.pb.h"
19 : #include "tsd/tsd_client.h" // SubProcType
20 :
21 : #include <string>
22 :
23 : namespace tsd {
24 :
25 : // ---------------------------------------------------------------------------
26 : // tsdSupportLevel_ bitmap definitions — each bit identifies a device
27 : // capability negotiated via the TSD_CAPABILITY_LEVEL query.
28 : // ---------------------------------------------------------------------------
29 : constexpr uint32_t TSD_SUPPORT_HS_AISERVER_FEATURE_BIT = 0U;
30 : constexpr uint32_t TSD_SUPPORT_BUILTIN_UDF_BIT = 1U;
31 : constexpr uint32_t TSD_SUPPORT_CLOSE_LIST_BIT = 2U;
32 : constexpr uint32_t TSD_SUPPORT_ADPROF_BIT = 3U;
33 : constexpr uint32_t TSD_SUPPORT_EXTEND_PKG = 4U;
34 : constexpr uint32_t TSD_SUPPORT_MUL_HCCP = 5U;
35 : constexpr uint32_t TSD_SUPPORT_DRIVER_EXTEND_BIT = 6U;
36 : constexpr uint32_t TSD_SUPPORT_ASCENDCPP_PKG = 7U;
37 : constexpr uint32_t TSD_SUPPORT_COMMON_SINK_PKG_CONFIG = 8U;
38 : constexpr uint32_t TSD_SUPPORT_CANN_HCOMM_COMPAT_910B = 9U;
39 :
40 : const std::string MUTEX_FILE_PREFIX = "sink_file_mutex_";
41 :
42 : class CapabilityManager;
43 :
44 : // Function-pointer strategy table entry — one row per capability type.
45 : // Every per-type behaviour is expressed as data (a function pointer) rather
46 : // than a switch-case branch, so adding a new capability type only requires
47 : // appending one row to the table in capability_manager.cpp.
48 : // The functions receive a CapabilityManager reference so they can access
49 : // member getters/setters without friendship.
50 : struct CapabilitySpec {
51 : int32_t type;
52 : HDCMessage::MsgType msgType; // request: type → proto MsgType
53 : HDCMessage::MsgType rspMsgType; // response: proto MsgType for device rsp
54 : bool requiresStartCpAndComm; // IsOkToGetCapability gate
55 : uint32_t waitTimeout; // WaitCapabilityRsp timeout (ms)
56 : bool waitIgnoreFail; // WaitCapabilityRsp tolerate failure
57 : bool hasTimeoutFallback; // write "unsupported" value on timeout
58 :
59 : // Try to serve the result from already-cached state.
60 : // Returns true if the output pointer was written and no device round-trip
61 : // is needed.
62 : bool (*tryUseStored)(CapabilityManager& mgr, uint64_t ptr);
63 :
64 : // Write the current member state to the caller's output pointer.
65 : // Returns TSD_OK on success.
66 : TSD_StatusT (*saveResult)(const CapabilityManager& mgr, uint64_t ptr);
67 :
68 : // Write the "unsupported / zero" value to the caller's output pointer.
69 : // Used by WaitCapabilityRsp when hasTimeoutFallback is set and the
70 : // device response times out.
71 : void (*writeUnsupported)(uint64_t ptr);
72 :
73 : // Update internal member state from a device response message.
74 : void (*updateStateFromMsg)(CapabilityManager& mgr, const HDCMessage& msg);
75 : };
76 :
77 : // Look up the spec for a given capability type. Returns nullptr if unknown.
78 : const CapabilitySpec* FindCapabilitySpec(int32_t type);
79 :
80 : class CapabilityManager {
81 : public:
82 : CapabilityManager(uint32_t logicDeviceId, DeviceCommAgent& commAgent, bool isAdcEnv);
83 : ~CapabilityManager();
84 :
85 : TSD_StatusT CapabilityGet(const int32_t type, const uint64_t ptr);
86 :
87 : bool IsSupportCommonInterface(const uint32_t level);
88 : bool IsSupportCommonSink();
89 :
90 : // Check whether the given sub-process type is supported by the device.
91 : // Returns true if supported (or if no check is registered for the type).
92 : bool CheckSubProcSupported(SubProcType procType);
93 :
94 : bool IsOkToGetCapability(const int32_t type) const;
95 : void ConstructCapabilityMsg(HDCMessage& msg, const int32_t type);
96 : TSD_StatusT SendCapabilityMsg(const int32_t type);
97 : TSD_StatusT WaitCapabilityRsp(const int32_t type, const uint64_t ptr);
98 : TSD_StatusT SaveCapabilityResult(const int32_t type, const uint64_t ptr) const;
99 :
100 : void UpdateStateFromMsg(const HDCMessage& msg);
101 : void HandlePidQosRsp(const HDCMessage& msg);
102 307 : void SetStartCpStatus(bool startCp) { tsdStartCp_ = startCp; }
103 :
104 : // --- accessors used by CapabilitySpec strategy functions ---
105 4 : int64_t GetPidQos() const { return pidQos_; }
106 128 : uint32_t GetTsdSupportLevel() const { return tsdSupportLevel_; }
107 7 : bool IsSupportOmInnerDec() const { return supportOmInnerDec_; }
108 3 : bool IsAdprofSupport() const { return adprofSupport_; }
109 : bool IsStartCp() const { return tsdStartCp_; }
110 : DeviceCommAgent& GetCommAgent() { return commAgent_; }
111 : const DeviceCommAgent& GetCommAgent() const { return commAgent_; }
112 :
113 : // --- mutators used by CapabilitySpec strategy functions ---
114 4 : void SetTsdSupportLevel(uint32_t level) { tsdSupportLevel_ = level; }
115 2 : void SetAdprofSupport(bool support) { adprofSupport_ = support; }
116 4 : void SetSupportOmInnerDec(bool support) { supportOmInnerDec_ = support; }
117 0 : void SetPidQos(int64_t pidQos) { pidQos_ = pidQos; }
118 :
119 : private:
120 : TSD_StatusT WaitRspForCapability(const uint32_t timeout, const bool ignoreRecvErr);
121 : bool UseStoredCapabityInfo(const int32_t type, const uint64_t ptr);
122 :
123 : uint32_t logicDeviceId_;
124 : DeviceCommAgent& commAgent_;
125 : bool isAdcEnv_;
126 :
127 : uint32_t tsdSupportLevel_ = 0U;
128 : bool supportOmInnerDec_ = false;
129 : bool adprofSupport_ = false;
130 : int64_t pidQos_ = -1;
131 : bool tsdStartCp_ = false;
132 : std::string hostSoPath_;
133 : bool hasGetHostSoPath_ = false;
134 : };
135 :
136 : } // namespace tsd
137 : #endif // TSD_BASIC_COMPONENT_CAPABILITY_CAPABILITY_MANAGER_H
|