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