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 : #include "device_comm_agent.h"
11 : #include "error_manager.h"
12 : #include "tsd_log.h"
13 : #include "tsd/status.h"
14 : #include "tsd_util_func.h"
15 :
16 : namespace tsd {
17 263 : DeviceCommAgent::DeviceCommAgent(const uint32_t logicDeviceId) : logicDeviceId_(logicDeviceId) {}
18 :
19 263 : DeviceCommAgent::~DeviceCommAgent() {}
20 :
21 72 : TSD_StatusT DeviceCommAgent::InitTsdClient(bool isAdcEnv)
22 : {
23 72 : isAdcEnv_ = isAdcEnv;
24 72 : if (devCommClient_ != nullptr) {
25 36 : TSD_INFO("[TsdClient] tsd client has already been initialized");
26 36 : return TSD_OK;
27 : }
28 36 : const drvError_t ret = drvGetProcessSign(&procSign_);
29 36 : if (ret != DRV_ERROR_NONE) {
30 0 : TSD_ERROR("driver get process sign failed. ret[%d].", ret);
31 0 : return TSD_CLT_OPEN_FAILED;
32 : }
33 36 : TSD_INFO(
34 : "[TsdClient][deviceId=%u] get process sign success, procPid[%u]", logicDeviceId_,
35 : static_cast<uint32_t>(procSign_.tgid));
36 :
37 36 : if (logicDeviceId_ >= MAX_DEVNUM_PER_HOST) {
38 1 : TSD_ERROR("[TsdClient] deviceId[%u] is not supported, not in [0-127] exit open function", logicDeviceId_);
39 1 : return TSD_DEVICEID_ERROR;
40 : }
41 35 : TSD_RUN_INFO("[TsdClient] deviceId[%u] begin to init hdc client", logicDeviceId_);
42 35 : devCommClient_ = DeviceComm::GetInstance(logicDeviceId_, DeviceCommType::HDC);
43 35 : if (devCommClient_ == nullptr) {
44 0 : TSD_ERROR("[TsdClient][deviceId=%u] devCommClient_ is null in Open function", logicDeviceId_);
45 0 : return TSD_INSTANCE_NOT_FOUND;
46 : }
47 35 : TSD_StatusT hdcRet = devCommClient_->CommInit(static_cast<uint32_t>(procSign_.tgid), isAdcEnv_);
48 35 : if (hdcRet != TSD_OK) {
49 0 : TSD_ERROR("[TsdClient][deviceId=%u] Init tsdclient failed in Open function", logicDeviceId_);
50 0 : ReleaseDeviceConnection();
51 0 : return hdcRet;
52 : }
53 35 : uint32_t sessionId = 0U;
54 35 : hdcRet = devCommClient_->CommCreateSession(sessionId);
55 35 : if (hdcRet != TSD_OK) {
56 17 : TSD_ERROR("[TsdClient][deviceId=%u]CreateSession for TSD failed in Open function", logicDeviceId_);
57 : if (hdcRet == DRV_ERROR_REPEATED_INIT) {
58 : REPORT_INPUT_ERROR("E39009", std::vector<std::string>(), std::vector<std::string>());
59 : } else if (hdcRet == DRV_ERROR_REMOTE_NOT_LISTEN) {
60 : REPORT_INPUT_ERROR("E39005", std::vector<std::string>(), std::vector<std::string>());
61 : }
62 17 : ReleaseDeviceConnection();
63 17 : return TSD_HDC_CREATE_SESSION_FAILED;
64 : }
65 18 : tsdSessionId_ = sessionId;
66 18 : return hdcRet;
67 : }
68 :
69 129 : TSD_StatusT DeviceCommAgent::SendMsg(const HDCMessage& msg)
70 : {
71 129 : if (devCommClient_ == nullptr) {
72 0 : TSD_ERROR("[DeviceCommAgent] devCommClient_ is null, send msg failed.");
73 0 : return TSD_INSTANCE_NOT_INITIALED;
74 : }
75 129 : return devCommClient_->CommSendMsg(tsdSessionId_, msg);
76 : }
77 :
78 126 : TSD_StatusT DeviceCommAgent::RecvData(bool ignoreRecvErr, uint32_t timeout)
79 : {
80 126 : if (devCommClient_ == nullptr) {
81 1 : return TSD_INSTANCE_NOT_INITIALED;
82 : }
83 125 : return devCommClient_->CommRecvData(tsdSessionId_, ignoreRecvErr, timeout);
84 : }
85 :
86 4 : TSD_StatusT DeviceCommAgent::GetHdcConctStatus(int32_t& sessStat, bool isAdcEnv)
87 : {
88 4 : if (devCommClient_ != nullptr) {
89 1 : return devCommClient_->CommGetConctStatus(sessStat);
90 : }
91 3 : TSD_WARN("[DeviceCommAgent] devCommClient_ is null in get status function");
92 3 : if (isAdcEnv) {
93 1 : sessStat = HDC_SESSION_STATUS_CONNECT;
94 : } else {
95 2 : sessStat = HDC_SESSION_STATUS_CLOSE;
96 : }
97 3 : return TSD_OK;
98 : }
99 :
100 12 : TSD_StatusT DeviceCommAgent::GetVersionVerify(std::shared_ptr<VersionVerify>& inspector)
101 : {
102 12 : if (devCommClient_ == nullptr) {
103 0 : return TSD_INSTANCE_NOT_INITIALED;
104 : }
105 12 : return devCommClient_->CommGetVersionVerify(tsdSessionId_, inspector);
106 : }
107 :
108 53 : void DeviceCommAgent::ReleaseDeviceConnection()
109 : {
110 53 : if (devCommClient_ != nullptr) {
111 53 : devCommClient_->CommDestroy();
112 53 : devCommClient_ = nullptr;
113 : }
114 53 : }
115 : } // namespace tsd
|