LCOV - code coverage report
Current view: top level - dgwclient - dgw_client.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 90.5 % 949 859
Test Date: 2026-08-12 11:05:07 Functions: 97.9 % 48 47

            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              : #include "dgw_client.h"
      12              : #include <algorithm>
      13              : #include <map>
      14              : #include <mutex>
      15              : #include <string>
      16              : #include <sys/types.h>
      17              : #include <unistd.h>
      18              : #include <securec.h>
      19              : #include "driver/ascend_hal.h"
      20              : #include "driver/ascend_hal_define.h"
      21              : #include "common/bqs_status.h"
      22              : #include "bqs_log.h"
      23              : #include "bqs_util.h"
      24              : #include "common/type_def.h"
      25              : #include "queue_schedule_feature_ctrl.h"
      26              : #define AICPU_PLAT_GET_CHIP(type) (((type) >> 8U) & 0xffU)
      27              : 
      28              : namespace {
      29              : std::mutex g_dgwClientMut;
      30              : std::map<std::tuple<uint32_t, pid_t, bool>, std::shared_ptr<bqs::DgwClient>> g_dgwClientInstanceMap;
      31              : // allowed max routes number
      32              : constexpr uint32_t MAX_ROUTES_NUM = 8000U;
      33              : // allowed max endpoints number in one group
      34              : constexpr uint32_t MAX_ENDPOINTS_NUM_IN_SINGLE_GROUP = 1000U;
      35              : // default qsPid when create client
      36              : constexpr pid_t DEFAULT_QS_PID = -1;
      37              : constexpr uint16_t MAJOR_VERSION = 3U;
      38              : constexpr uint32_t QUERY_LINK_STATUS_INTERVAL = 100000U; // 100ms
      39              : constexpr uint32_t QUERY_LINK_STATUS_UNIT = 1000000U;    // 1s
      40              : constexpr uint32_t RESOURCE_ID_HOST_DEVICE_BIT_NUM = 14;
      41              : constexpr uint32_t ROUCE_ID_DEVICE_ID_DATA_MASK = 0x3FFFU;
      42              : constexpr uint32_t ROUCE_ID_FRONT_PART_DATA_MASK = 0xC000U;
      43              : 
      44              : std::vector<uint32_t> g_userDeviceInfo;
      45              : bool g_hadGetVisibleDevices = false;
      46              : int64_t g_chipType = 18;
      47              : bool g_hadGetChipType = false;
      48              : } // namespace
      49              : 
      50              : namespace bqs {
      51              : 
      52            1 : DgwClient::DgwClient(const uint32_t deviceId)
      53            1 :     : deviceId_(deviceId),
      54            1 :       qsPid_(DEFAULT_QS_PID),
      55            1 :       procSign_(),
      56            1 :       curPid_(-1),
      57            1 :       curGroupId_(0U),
      58            1 :       piplineQueueId_(0U),
      59            1 :       initFlag_(false),
      60            1 :       isProxy_(false),
      61            1 :       isServerOldVersion_(false)
      62            1 : {}
      63              : 
      64            0 : DgwClient::DgwClient(const uint32_t deviceId, const pid_t qsPid)
      65            0 :     : deviceId_(deviceId),
      66            0 :       qsPid_(qsPid),
      67            0 :       procSign_(),
      68            0 :       curPid_(-1),
      69            0 :       curGroupId_(0U),
      70            0 :       piplineQueueId_(0U),
      71            0 :       initFlag_(false),
      72            0 :       isProxy_(false),
      73            0 :       isServerOldVersion_(false)
      74            0 : {}
      75              : 
      76           16 : DgwClient::DgwClient(const uint32_t deviceId, const pid_t qsPid, const bool proxy)
      77           16 :     : deviceId_(deviceId),
      78           16 :       qsPid_(qsPid),
      79           16 :       procSign_(),
      80           16 :       curPid_(-1),
      81           16 :       curGroupId_(0U),
      82           16 :       piplineQueueId_(0U),
      83           16 :       initFlag_(false),
      84           16 :       isProxy_(proxy),
      85           16 :       isServerOldVersion_(false)
      86           16 : {}
      87              : 
      88           70 : std::shared_ptr<DgwClient> DgwClient::GetInstance(const uint32_t deviceId)
      89              : {
      90           70 :     return GetInstance(deviceId, DEFAULT_QS_PID, false);
      91              : }
      92              : 
      93            2 : std::shared_ptr<DgwClient> DgwClient::GetInstance(const uint32_t deviceId, const pid_t qsPid)
      94              : {
      95            2 :     return GetInstance(deviceId, qsPid, false);
      96              : }
      97              : 
      98           77 : std::shared_ptr<DgwClient> DgwClient::GetInstance(const uint32_t deviceId, const pid_t qsPid, const bool proxy)
      99              : {
     100           77 :     BQS_LOG_INFO("[DgwClient] begin to get instance, deviceId=%u, proxy=%d", deviceId, proxy);
     101           77 :     uint32_t logicDeviceId = deviceId;
     102           77 :     if (proxy) {
     103            5 :         if (!g_hadGetChipType && (GetPlatformInfo(deviceId) != static_cast<int32_t>(BQS_STATUS_OK))) {
     104            0 :             return nullptr;
     105              :         }
     106           10 :         if (QSFeatureCtrl::IsSupportSetVisibleDevices(g_chipType) &&
     107            5 :             (ChangeUserDeviceIdToLogicDeviceId(deviceId, logicDeviceId) != static_cast<int32_t>(BQS_STATUS_OK))) {
     108            1 :             return nullptr;
     109              :         }
     110            4 :         BQS_LOG_INFO("[DgwClient] after change deviceId, logicDeviceId=%u", logicDeviceId);
     111              :     }
     112              : 
     113           76 :     std::lock_guard<std::mutex> lk(g_dgwClientMut);
     114           76 :     const auto iter = g_dgwClientInstanceMap.find({logicDeviceId, qsPid, proxy});
     115           76 :     if (iter != g_dgwClientInstanceMap.end()) {
     116           60 :         return iter->second;
     117              :     } else {
     118           16 :         std::shared_ptr<DgwClient> clientImplPtr = nullptr;
     119              :         try {
     120           16 :             clientImplPtr = std::make_shared<DgwClient>(logicDeviceId, qsPid, proxy);
     121            0 :         } catch (std::bad_alloc& error) {
     122            0 :             BQS_LOG_ERROR(
     123              :                 "[DgwClient] fail to create client(%u-%d-%d) for %s", logicDeviceId, qsPid, proxy, error.what());
     124            0 :         }
     125           16 :         if (clientImplPtr != nullptr) {
     126           16 :             (void)g_dgwClientInstanceMap.insert({{logicDeviceId, qsPid, proxy}, clientImplPtr});
     127              :         }
     128           16 :         return clientImplPtr;
     129           16 :     }
     130           76 : }
     131              : 
     132            7 : int32_t DgwClient::Initialize(
     133              :     const uint32_t dgwPid, const std::string procSign, const bool isProxy, const int32_t timeout)
     134              : {
     135            7 :     BQS_LOG_INFO(
     136              :         "[DgwClient] Initialize Begin, change qsPid from %d to %d, isproxy: %d", qsPid_, static_cast<pid_t>(dgwPid),
     137              :         isProxy);
     138            7 :     qsPid_ = static_cast<pid_t>(dgwPid);
     139            7 :     procSign_ = procSign;
     140            7 :     curPid_ = getpid();
     141            7 :     isProxy_ = isProxy;
     142            7 :     isServerOldVersion_ = false;
     143              : 
     144            7 :     auto drvRet = halEschedAttachDevice(deviceId_);
     145            7 :     if ((drvRet != DRV_ERROR_NONE) && (drvRet != DRV_ERROR_PROCESS_REPEAT_ADD)) {
     146            1 :         BQS_LOG_ERROR("Failed to attach device[%u], result[%d].", deviceId_, static_cast<int32_t>(drvRet));
     147            1 :         return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     148              :     }
     149              : 
     150              :     // send init event to qs
     151            6 :     QsBindInit qsBindInit = {};
     152            6 :     qsBindInit.pid = curPid_;
     153            6 :     qsBindInit.grpId = curGroupId_;
     154            6 :     qsBindInit.majorVersion = MAJOR_VERSION;
     155              : 
     156            6 :     QsProcMsgRsp qsProcMsgRsp = {};
     157            6 :     int32_t ret = SendEventToQsSync(&qsBindInit, sizeof(QsBindInit), ACL_BIND_QUEUE_INIT, qsProcMsgRsp, timeout);
     158            6 :     if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     159            0 :         return ret;
     160              :     }
     161            6 :     if (qsProcMsgRsp.retCode != static_cast<int32_t>(BQS_STATUS_OK)) {
     162            0 :         BQS_LOG_WARN("[DgwClient] Initialize event_reply retCode is[%u]", qsProcMsgRsp.retCode);
     163            0 :         return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     164              :     }
     165              : 
     166            6 :     if (qsProcMsgRsp.majorVersion != qsBindInit.majorVersion && qsProcMsgRsp.majorVersion < MAJOR_VERSION) {
     167            6 :         BQS_LOG_INFO(
     168              :             "[DgwClient] server majorVersion is[%u] client majorVersion is[%u], set isServerOldVersion",
     169              :             qsProcMsgRsp.majorVersion, qsBindInit.majorVersion);
     170            6 :         isServerOldVersion_ = true;
     171              :     }
     172              : 
     173            6 :     piplineQueueId_ = qsProcMsgRsp.retValue;
     174            6 :     BQS_LOG_DEBUG("[DgwClient] Success to get queue id[%u].", piplineQueueId_);
     175              : 
     176              :     // host FlowGW LOCAL_Q need set this before use these queues
     177            6 :     if (!isProxy_) {
     178              :         QueueSetInputPara inPutParam;
     179            6 :         (void)halQueueSet(deviceId_, QUEUE_ENABLE_LOCAL_QUEUE, &inPutParam);
     180              :     }
     181              : 
     182              :     // Before attach queue, process should call halQueueInit.
     183            6 :     drvRet = halQueueInit(deviceId_);
     184            6 :     if ((drvRet != DRV_ERROR_NONE) && (drvRet != DRV_ERROR_REPEATED_INIT)) {
     185            1 :         BQS_LOG_ERROR("[DgwClient] halQueueInit error, ret=%d", static_cast<int32_t>(drvRet));
     186            1 :         return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     187              :     }
     188              : 
     189            5 :     drvRet = halQueueAttach(deviceId_, piplineQueueId_, -1);
     190            5 :     if (drvRet != DRV_ERROR_NONE) {
     191            1 :         BQS_LOG_ERROR(
     192              :             "[DgwClient] Attach queue failed, queue id[%u], drvRet [%d].", piplineQueueId_,
     193              :             static_cast<int32_t>(drvRet));
     194            1 :         return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     195              :     }
     196              : 
     197            4 :     BuffCfg buffCfg = {};
     198            4 :     ret = halBuffInit(&buffCfg);
     199            4 :     if ((ret != static_cast<int32_t>(DRV_ERROR_NONE)) && (ret != static_cast<int32_t>(DRV_ERROR_REPEATED_INIT))) {
     200            1 :         BQS_LOG_ERROR("[DgwClient] Failed to halBuffInit, ret=[%d].", ret);
     201            1 :         return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     202              :     }
     203              : 
     204            3 :     initFlag_ = true;
     205            3 :     BQS_LOG_INFO("[DgwClient] Success to Initialize deviceId=[%u], pid=[%u].", deviceId_, curPid_);
     206            3 :     return static_cast<int32_t>(BQS_STATUS_OK);
     207              : }
     208              : 
     209            1 : int32_t DgwClient::Finalize() { return static_cast<int32_t>(BQS_STATUS_OK); }
     210              : 
     211           12 : int32_t DgwClient::CreateHcomHandle(
     212              :     const std::string& rankTable, const int32_t rankId, const void* const reserve, uint64_t& handle,
     213              :     const int32_t timeout)
     214              : {
     215              :     (void)reserve;
     216           12 :     BQS_LOG_INFO("[DgwClient] Begin to create hcom handle.");
     217           12 :     if (!initFlag_) {
     218            1 :         BQS_LOG_ERROR("[DgwClient] Please check whether datagw client has been initialized.");
     219            1 :         return static_cast<int32_t>(BQS_STATUS_NOT_INIT);
     220              :     }
     221           11 :     if (rankTable.empty()) {
     222            1 :         BQS_LOG_ERROR("[DgwClient] Rank table is empty!");
     223            1 :         return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
     224              :     }
     225              : 
     226              :     // calculate mbuf len: 1. HcomHandleInfo 2. rank table 3. CfgRetInfo
     227           10 :     const size_t cfgLen = sizeof(HcomHandleInfo) + rankTable.length();
     228              :     // check two uint32 integer adding whether overflow and execute adding when not overflow.
     229           10 :     uint32_t mbufLen = 0U;
     230           10 :     bool isOverflow = false;
     231           10 :     BqsCheckAssign32UAdd(cfgLen, sizeof(CfgRetInfo), mbufLen, isOverflow);
     232           10 :     if (isOverflow) {
     233            1 :         BQS_LOG_ERROR("mbufLen[%u] is invalid.", mbufLen);
     234            1 :         return BQS_STATUS_PARAM_INVALID;
     235              :     }
     236              :     // create hcom handle info
     237              :     HcomHandleInfo info;
     238            9 :     info.rankId = rankId;
     239            9 :     info.rankTableLen = rankTable.length();
     240            9 :     info.rankTableOffset = sizeof(HcomHandleInfo);
     241              : 
     242            9 :     std::list<std::pair<uintptr_t, size_t>> dataList;
     243            9 :     (void)dataList.emplace_back(std::make_pair(PtrToValue(&info), sizeof(HcomHandleInfo)));
     244            9 :     (void)dataList.emplace_back(std::make_pair(PtrToValue(rankTable.c_str()), rankTable.length()));
     245              : 
     246              :     // operate config to server
     247            9 :     std::vector<int32_t> emptyVec;
     248            9 :     const ConfigParams cfgParams = {
     249              :         .info = &info,
     250              :         .query = nullptr,
     251              :         .cfgInfo = nullptr,
     252              :         .cfgLen = cfgLen,
     253            9 :         .totalLen = static_cast<size_t>(mbufLen),
     254            9 :     };
     255              :     const auto ret =
     256            9 :         OperateConfigToServer(QueueSubEventType::DGW_CREATE_HCOM_HANDLE, cfgParams, dataList, emptyVec, timeout);
     257            9 :     if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     258            1 :         BQS_LOG_ERROR("[DgwClient] Failed to create hcom handle.");
     259            1 :         return ret;
     260              :     }
     261              :     // get hcom handle
     262            8 :     handle = info.hcomHandle;
     263            8 :     BQS_LOG_INFO("[DgwClient] Success to create hcom handle[%lu].", handle);
     264            8 :     return static_cast<int32_t>(BQS_STATUS_OK);
     265            9 : }
     266              : 
     267            9 : int32_t DgwClient::DestroyHcomHandle(const uint64_t handle, const int32_t timeout)
     268              : {
     269            9 :     BQS_LOG_INFO("[DgwClient] Begin to Destroy hcom handle[%lu].", handle);
     270            9 :     if (!initFlag_) {
     271            1 :         BQS_LOG_ERROR("[DgwClient] Please check whether datagw client has been initialized.");
     272            1 :         return static_cast<int32_t>(BQS_STATUS_NOT_INIT);
     273              :     }
     274              : 
     275              :     // calculate mbuf len: 1. HcomHandleInfo; 2. CfgRetInfo
     276            8 :     const size_t cfgLen = sizeof(HcomHandleInfo);
     277            8 :     const size_t mbufLen = cfgLen + sizeof(CfgRetInfo);
     278              :     // hcom handle info
     279              :     HcomHandleInfo info;
     280            8 :     info.rankTableLen = 0UL;
     281            8 :     info.rankTableOffset = 0UL;
     282            8 :     info.hcomHandle = handle;
     283              : 
     284            8 :     std::list<std::pair<uintptr_t, size_t>> dataList;
     285            8 :     (void)dataList.emplace_back(std::make_pair(PtrToValue(&info), sizeof(HcomHandleInfo)));
     286              : 
     287              :     // operate config to server
     288            8 :     std::vector<int32_t> emptyVec;
     289            8 :     const ConfigParams cfgParams = {
     290              :         .info = &info,
     291              :         .query = nullptr,
     292              :         .cfgInfo = nullptr,
     293              :         .cfgLen = cfgLen,
     294              :         .totalLen = mbufLen,
     295            8 :     };
     296              :     const auto ret =
     297            8 :         OperateConfigToServer(QueueSubEventType::DGW_DESTORY_HCOM_HANDLE, cfgParams, dataList, emptyVec, timeout);
     298            8 :     if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     299            1 :         BQS_LOG_ERROR("[DgwClient] Failed to destroy hcom handle[%lu].", handle);
     300            1 :         return ret;
     301              :     }
     302            7 :     BQS_LOG_INFO("[DgwClient] Success to destroy hcom handle[%lu].", handle);
     303            7 :     return static_cast<int32_t>(BQS_STATUS_OK);
     304            8 : }
     305              : 
     306           51 : static bool IsQueueOperationCmd(ConfigInfo& cfgInfo)
     307              : {
     308              :     return (
     309           87 :         (cfgInfo.cmd == ConfigCmd::DGW_CFG_CMD_BIND_ROUTE) || (cfgInfo.cmd == ConfigCmd::DGW_CFG_CMD_UNBIND_ROUTE) ||
     310           87 :         (cfgInfo.cmd == ConfigCmd::DGW_CFG_CMD_QRY_ROUTE));
     311              : }
     312              : 
     313           31 : static bool IsGroupOperationCmd(ConfigInfo& cfgInfo)
     314              : {
     315           31 :     return ((cfgInfo.cmd == ConfigCmd::DGW_CFG_CMD_ADD_GROUP) || (cfgInfo.cmd == ConfigCmd::DGW_CFG_CMD_QRY_GROUP));
     316              : }
     317              : 
     318           44 : int32_t DgwClient::UpdateConfig(ConfigInfo& cfgInfo, std::vector<int32_t>& cfgRets, const int32_t timeout)
     319              : {
     320           44 :     BQS_LOG_INFO("[DgwClient] Begin to update config.");
     321              :     // check dgw client initialized
     322           44 :     if (!initFlag_) {
     323            1 :         BQS_LOG_ERROR("[DgwClient] please check whether datagw client has initialized successfully.");
     324            1 :         return static_cast<int32_t>(BQS_STATUS_NOT_INIT);
     325              :     }
     326              : 
     327           43 :     if (isServerOldVersion_ && IsGroupOperationCmd(cfgInfo)) {
     328            1 :         Endpoint* endpoints = cfgInfo.cfg.groupCfg.endpoints;
     329            1 :         if (endpoints != nullptr) {
     330            1 :             const bqs::EndpointType type = endpoints->type;
     331            1 :             if (type == bqs::EndpointType::MEM_QUEUE && endpoints->attr.memQueueAttr.queueType == bqs::CLIENT_Q) {
     332            1 :                 BQS_LOG_ERROR("[DgwClient] isServerOldVersion CLIENT_Q function interception");
     333            1 :                 return static_cast<int32_t>(BQS_STATUS_ENDPOINT_MEM_TYPE_NOT_SUPPORT);
     334              :             }
     335              :         }
     336              :     }
     337              : 
     338              :     // mbuf memory layout: 1.config info / 2.Routes or Endpoints / 3.config results
     339              :     // calculate config length: 1.config info + 2.Routes or Endpoints
     340           42 :     size_t cfgLen = 0UL;
     341           42 :     std::list<std::pair<uintptr_t, size_t>> dataList;
     342              :     // here is to consider compatibility between client and server different versions
     343           42 :     std::unique_ptr<Route[]> spareRoutes = nullptr;
     344           42 :     std::unique_ptr<Endpoint[]> spareEndpoints = nullptr;
     345           42 :     if (IsQueueOperationCmd(cfgInfo)) {
     346           15 :         const uint32_t routeNum = cfgInfo.cfg.routesCfg.routeNum;
     347           15 :         spareRoutes.reset(new (std::nothrow) Route[routeNum]);
     348           15 :         if (spareRoutes == nullptr) {
     349            0 :             BQS_LOG_ERROR("[DgwClient] malloc failed on spareRoutes.");
     350            0 :             return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     351              :         }
     352           27 :     } else if (IsGroupOperationCmd(cfgInfo)) {
     353           10 :         const uint32_t endpointNum = cfgInfo.cfg.groupCfg.endpointNum;
     354           10 :         spareEndpoints.reset(new (std::nothrow) Endpoint[endpointNum]);
     355           10 :         if (spareEndpoints == nullptr) {
     356            0 :             BQS_LOG_ERROR("[DgwClient] malloc failed on spareEndpoints.");
     357            0 :             return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     358              :         }
     359              :     } else {
     360           17 :         BQS_LOG_INFO("[DgwClient] config cmd is %d.", static_cast<int32_t>(cfgInfo.cmd));
     361              :     }
     362           42 :     auto ret = CalcConfigInfoLen(cfgInfo, cfgLen, dataList, spareRoutes, spareEndpoints);
     363           42 :     if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     364            1 :         BQS_LOG_ERROR("[DgwClient] check and calculate length of config info failed.");
     365            1 :         return ret;
     366              :     }
     367              :     // calculate result length: 3.config results
     368           41 :     size_t retLen = 0UL;
     369           41 :     ret = CalcResultLen(cfgInfo, retLen);
     370           41 :     if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     371            1 :         BQS_LOG_ERROR("[DgwClient] calculate length of result failed.");
     372            1 :         return ret;
     373              :     }
     374              : 
     375              :     ConfigQuery unusedParam;
     376           40 :     const ConfigParams cfgParams = {
     377              :         .info = nullptr,
     378              :         .query = &unusedParam,
     379              :         .cfgInfo = &cfgInfo,
     380              :         .cfgLen = cfgLen,
     381           40 :         .totalLen = cfgLen + retLen,
     382           40 :     };
     383           40 :     ret = OperateConfigToServer(QueueSubEventType::UPDATE_CONFIG, cfgParams, dataList, cfgRets, timeout);
     384           40 :     BQS_LOG_INFO(
     385              :         "[DgwClient] Finish to update config, cmd is %d, ret is [%d].", static_cast<int32_t>(cfgInfo.cmd), ret);
     386           40 :     return ret;
     387           42 : }
     388              : 
     389           13 : int32_t DgwClient::QueryConfig(const ConfigQuery& query, ConfigInfo& cfgInfo, const int32_t timeout)
     390              : {
     391           13 :     BQS_LOG_INFO("[DgwClient] Begin to query config.");
     392              :     // check dgw client initialized
     393           13 :     if (!initFlag_) {
     394            1 :         BQS_LOG_ERROR("[DgwClient] please check whether datagw client has initialized successfully.");
     395            1 :         return static_cast<int32_t>(BQS_STATUS_NOT_INIT);
     396              :     }
     397              :     // check route num/group num
     398           12 :     auto ret = CheckConfigNum(query, cfgInfo);
     399           12 :     if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     400            3 :         BQS_LOG_ERROR("[DgwClient] calculate and check config num failed. Please check config num!");
     401            3 :         return ret;
     402              :     }
     403              : 
     404              :     // mbuf memory layout: 1. config query / 2.config info / 3.Routes or Endpoints / 4.config result
     405            9 :     std::list<std::pair<uintptr_t, size_t>> dataList;
     406              :     // calculate query length: 1. config query
     407            9 :     const size_t qryLen = sizeof(ConfigQuery);
     408            9 :     (void)dataList.emplace_back(std::make_pair(PtrToValue(&query), sizeof(ConfigQuery)));
     409              :     // here is to consider compatibility between client and server different versions
     410            9 :     std::unique_ptr<Route[]> spareRoutes = nullptr;
     411            9 :     std::unique_ptr<Endpoint[]> spareEndpoints = nullptr;
     412            9 :     if (IsQueueOperationCmd(cfgInfo)) {
     413            6 :         const uint32_t routeNum = cfgInfo.cfg.routesCfg.routeNum;
     414            6 :         spareRoutes.reset(new (std::nothrow) Route[routeNum]);
     415            6 :         if (spareRoutes == nullptr) {
     416            0 :             BQS_LOG_ERROR("[DgwClient] malloc failed on spareRoutes.");
     417            0 :             return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     418              :         }
     419            3 :     } else if (IsGroupOperationCmd(cfgInfo)) {
     420            3 :         const uint32_t endpointNum = cfgInfo.cfg.groupCfg.endpointNum;
     421            3 :         spareEndpoints.reset(new (std::nothrow) Endpoint[endpointNum]);
     422            3 :         if (spareEndpoints == nullptr) {
     423            0 :             BQS_LOG_ERROR("[DgwClient] malloc failed on spareEndpoints.");
     424            0 :             return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     425              :         }
     426              :     } else {
     427            0 :         BQS_LOG_INFO("[DgwClient] config cmd is %d.", static_cast<int32_t>(cfgInfo.cmd));
     428              :     }
     429              : 
     430              :     // calculate config length: 2.config info + 3.Routes or Endpoints
     431            9 :     size_t cfgLen = 0UL;
     432            9 :     ret = CalcConfigInfoLen(cfgInfo, cfgLen, dataList, spareRoutes, spareEndpoints);
     433            9 :     if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     434            0 :         BQS_LOG_ERROR("[DgwClient] check and calculate length of config info failed.");
     435            0 :         return ret;
     436              :     }
     437              :     // calculate result length: 3.config results
     438            9 :     size_t retLen = 0UL;
     439            9 :     ret = CalcResultLen(cfgInfo, retLen);
     440            9 :     if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     441            0 :         BQS_LOG_ERROR("[DgwClient] calculate length of result failed.");
     442            0 :         return ret;
     443              :     }
     444              : 
     445              :     // opereate config to server
     446            9 :     std::vector<int32_t> emptyVec;
     447            9 :     const ConfigParams cfgParams = {
     448              :         .info = nullptr,
     449              :         .query = const_cast<ConfigQuery*>(&query),
     450              :         .cfgInfo = &cfgInfo,
     451              :         .cfgLen = cfgLen,
     452            9 :         .totalLen = qryLen + cfgLen + retLen,
     453            9 :     };
     454            9 :     ret = OperateConfigToServer(QueueSubEventType::QUERY_CONFIG, cfgParams, dataList, emptyVec, timeout);
     455            9 :     BQS_LOG_INFO("[DgwClient] Finish to update config, ret is [%d].", ret);
     456            9 :     return ret;
     457            9 : }
     458              : 
     459           24 : int32_t DgwClient::QueryConfigNum(ConfigQuery& query, const int32_t timeout)
     460              : {
     461           24 :     BQS_LOG_INFO("[DgwClient] Begin to query config number.");
     462              :     // check dgw client initialized
     463           24 :     if (!initFlag_) {
     464            1 :         BQS_LOG_ERROR("[DgwClient] please check whether datagw client has initialized successfully.");
     465            1 :         return static_cast<int32_t>(BQS_STATUS_NOT_INIT);
     466              :     }
     467              : 
     468              :     // mbuf memory layout: 1. config query / 2.config result
     469           23 :     std::list<std::pair<uintptr_t, size_t>> dataList;
     470              :     // calculate query length: 1. config query
     471           23 :     const size_t qryLen = sizeof(ConfigQuery);
     472           23 :     (void)dataList.emplace_back(std::make_pair(PtrToValue(&query), sizeof(ConfigQuery)));
     473              :     // calculate result length: 2.config results
     474           23 :     constexpr size_t retLen = sizeof(CfgRetInfo);
     475              : 
     476              :     // operate config to server
     477           23 :     std::vector<int32_t> emptyVec;
     478              :     ConfigInfo unusedParam;
     479           23 :     const ConfigParams cfgParams = {
     480              :         .info = nullptr,
     481              :         .query = &query,
     482              :         .cfgInfo = &unusedParam,
     483              :         .cfgLen = 0UL,
     484              :         .totalLen = qryLen + retLen,
     485           23 :     };
     486           23 :     const auto ret = OperateConfigToServer(QueueSubEventType::QUERY_CONFIG_NUM, cfgParams, dataList, emptyVec, timeout);
     487           23 :     BQS_LOG_INFO("[DgwClient] Finish to query config number, ret is [%d].", ret);
     488           23 :     return ret;
     489           23 : }
     490              : 
     491           88 : int32_t DgwClient::OperateConfigToServer(
     492              :     const QueueSubEventType subEventId, const ConfigParams& cfgParams,
     493              :     std::list<std::pair<uintptr_t, size_t>>& dataList, std::vector<int32_t>& cfgRets, const int32_t timeout)
     494              : {
     495           88 :     if (isProxy_) {
     496            2 :         return OperateToServerOnOtherSide(subEventId, cfgParams, dataList, cfgRets, timeout);
     497              :     } else {
     498           86 :         return OperateToServerOnSameSide(subEventId, cfgParams, dataList, cfgRets, timeout);
     499              :     }
     500              : }
     501              : 
     502           91 : static int32_t SetDataAndEnqueueForMbuf(
     503              :     const std::list<std::pair<uintptr_t, size_t>>& dataList, Mbuf* const mbuf, const size_t mbufLen, uint32_t deviceId,
     504              :     uint32_t piplineQueueId)
     505              : {
     506              :     // check mbuf and mbufLen
     507           91 :     if ((mbuf == nullptr) || (mbufLen == 0UL) || (dataList.empty())) {
     508            1 :         BQS_LOG_ERROR("[DgwClient] mbuf nullptr or mbufLen equal zero or dataList empty:%d.", dataList.empty());
     509            1 :         return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
     510              :     }
     511              : 
     512              :     // get mbuf data addr
     513           90 :     void* mbufData = nullptr;
     514           90 :     auto drvRet = halMbufGetBuffAddr(mbuf, &mbufData);
     515           90 :     if ((drvRet != static_cast<int32_t>(DRV_ERROR_NONE)) || (mbufData == nullptr)) {
     516            1 :         BQS_LOG_ERROR("[DgwClient] Failed to get mbuf data, ret=[%d]", drvRet);
     517            1 :         return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     518              :     }
     519              : 
     520              :     // copy data to mbuf
     521           89 :     size_t offset = 0UL;
     522          231 :     for (auto& data : dataList) {
     523          143 :         const size_t restMbufLen = mbufLen - offset;
     524          143 :         if (data.second > restMbufLen) {
     525            1 :             BQS_LOG_ERROR(
     526              :                 "[DgwClient] dataLen[%zu] is invalid. mbufLen is [%zu], offset is [%zu].", data.second, mbufLen,
     527              :                 offset);
     528            1 :             return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
     529              :         }
     530              :         const auto cpyRet =
     531          142 :             memcpy_s(ValueToPtr(PtrToValue(mbufData) + offset), restMbufLen, ValueToPtr(data.first), data.second);
     532          142 :         if (cpyRet != EOK) {
     533            0 :             BQS_LOG_ERROR(
     534              :                 "[DgwClient] Memcpy failed, dataSize[%zu], mbufLen[%zu] ret=[%d]", data.second, restMbufLen, cpyRet);
     535            0 :             return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     536              :         }
     537          142 :         offset += data.second;
     538              :     }
     539              :     // set mbuf len
     540           88 :     drvRet = halMbufSetDataLen(mbuf, mbufLen);
     541           88 :     if (drvRet != static_cast<int32_t>(DRV_ERROR_NONE)) {
     542            1 :         BQS_LOG_ERROR("[DgwClient] Failed to set data len[%zu] for mbuf, ret=[%d]", mbufLen, drvRet);
     543            1 :         return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     544              :     }
     545              :     // mbuf enqueue
     546           87 :     const auto enqueueRet = halQueueEnQueue(deviceId, piplineQueueId, mbuf);
     547           87 :     if (enqueueRet != DRV_ERROR_NONE) {
     548            1 :         BQS_LOG_ERROR(
     549              :             "[DgwClient] Call halQueueEnQueue error, queue id[%u], ret=[%d]", piplineQueueId,
     550              :             static_cast<int32_t>(enqueueRet));
     551            1 :         return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     552              :     }
     553           86 :     BQS_LOG_INFO(
     554              :         "[DgwClient] Call halQueueEnQueue success, queue id[%u], ret=[%d]", piplineQueueId,
     555              :         static_cast<int32_t>(enqueueRet));
     556           86 :     return static_cast<int32_t>(BQS_STATUS_OK);
     557              : }
     558              : 
     559           92 : int32_t DgwClient::OperateToServerOnSameSide(
     560              :     const QueueSubEventType subEventId, const ConfigParams& cfgParams,
     561              :     std::list<std::pair<uintptr_t, size_t>>& dataList, std::vector<int32_t>& cfgRets, const int32_t timeout)
     562              : {
     563              :     // alloc mbuf
     564           92 :     Mbuf* mbuf = nullptr;
     565           92 :     const size_t mbufLen = cfgParams.totalLen;
     566           92 :     auto drvRet = halMbufAlloc(mbufLen, &mbuf);
     567           92 :     if ((drvRet != static_cast<int32_t>(DRV_ERROR_NONE)) || (mbuf == nullptr)) {
     568            1 :         BQS_LOG_ERROR("[DgwClient] failed to alloc mbuf, size[%zu], ret=[%d].", mbufLen, drvRet);
     569            1 :         return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     570              :     }
     571              :     // copy data to mbuf and mbuf enqueue
     572           91 :     Mbuf* dequeMbuf = nullptr;
     573           91 :     int32_t cmdRet = static_cast<int32_t>(BQS_STATUS_OK);
     574              :     {
     575           91 :         const std::unique_lock<std::mutex> eventLock(eventMutex_);
     576           91 :         auto ret = SetDataAndEnqueueForMbuf(dataList, mbuf, mbufLen, deviceId_, piplineQueueId_);
     577           91 :         if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     578              :             // reaching here means mbuf was not enqueued, so we should free
     579            5 :             (void)halMbufFree(mbuf);
     580            5 :             mbuf = nullptr;
     581            5 :             return ret;
     582              :         }
     583              : 
     584           86 :         ret = InformServer(subEventId, cmdRet, timeout);
     585           86 :         if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     586            0 :             return ret;
     587              :         }
     588              : 
     589           86 :         drvRet = halQueueDeQueue(deviceId_, piplineQueueId_, reinterpret_cast<void**>(&dequeMbuf));
     590           86 :         if ((drvRet != DRV_ERROR_NONE) || (dequeMbuf == nullptr)) {
     591            0 :             BQS_LOG_ERROR(
     592              :                 "halQueueDeQueue from queue[%u] in device[%u] failed, error[%d]", piplineQueueId_, deviceId_,
     593              :                 static_cast<int32_t>(drvRet));
     594            0 :             return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     595              :         }
     596           91 :     }
     597              : 
     598           86 :     void* dequeMbufData = nullptr;
     599           86 :     drvRet = halMbufGetBuffAddr(dequeMbuf, &dequeMbufData);
     600           86 :     if ((drvRet != static_cast<int32_t>(DRV_ERROR_NONE)) || (dequeMbufData == nullptr)) {
     601            0 :         BQS_LOG_ERROR("[DgwClient] Failed to get mbuf data, ret=[%d]", drvRet);
     602            0 :         (void)halMbufFree(dequeMbuf);
     603            0 :         dequeMbuf = nullptr;
     604            0 :         return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     605              :     }
     606              : 
     607           86 :     const uintptr_t dequeMbufDataAddr = PtrToValue(dequeMbufData);
     608           86 :     ExtractRetCode(subEventId, cfgParams, dequeMbufDataAddr, cfgRets, cmdRet);
     609              : 
     610           86 :     (void)halMbufFree(dequeMbuf);
     611           86 :     dequeMbuf = nullptr;
     612           86 :     return cmdRet;
     613              : }
     614              : 
     615            6 : int32_t DgwClient::OperateToServerOnOtherSide(
     616              :     const QueueSubEventType subEventId, const ConfigParams& cfgParams,
     617              :     std::list<std::pair<uintptr_t, size_t>>& dataList, std::vector<int32_t>& cfgRets, const int32_t timeout)
     618              : {
     619              :     // alloc memory
     620            6 :     const size_t mbufLen = cfgParams.totalLen;
     621            6 :     std::unique_ptr<char_t[]> body(new (std::nothrow) char_t[mbufLen], std::default_delete<char_t[]>());
     622            6 :     if (body == nullptr) {
     623            0 :         BQS_LOG_ERROR("[DgwClient] failed to alloc memory for data, size[%zu].", mbufLen);
     624            0 :         return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     625              :     }
     626              :     // copy data to mbuf
     627            6 :     size_t offset = 0UL;
     628           13 :     for (auto& data : dataList) {
     629            8 :         const size_t restMbufLen = mbufLen - offset;
     630            8 :         if (data.second > restMbufLen) {
     631            0 :             BQS_LOG_ERROR(
     632              :                 "[DgwClient] dataLen[%zu] is invalid. mbufLen is [%zu], offset is [%zu].", data.second, mbufLen,
     633              :                 offset);
     634            1 :             return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
     635              :         }
     636              :         const auto cpyRet =
     637            8 :             memcpy_s(ValueToPtr(PtrToValue(body.get()) + offset), restMbufLen, ValueToPtr(data.first), data.second);
     638            8 :         if (cpyRet != EOK) {
     639            1 :             BQS_LOG_ERROR(
     640              :                 "[DgwClient] Memcpy failed, dataSize[%zu], mbufLen[%zu] ret=[%d]", data.second, restMbufLen, cpyRet);
     641            1 :             return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     642              :         }
     643            7 :         offset += data.second;
     644              :     }
     645            5 :     const size_t totalLen = sizeof(struct buff_iovec) + sizeof(struct iovec_info);
     646            5 :     std::unique_ptr<char_t[]> vecUniquePtr(new (std::nothrow) char_t[totalLen], std::default_delete<char_t[]>());
     647            5 :     if (vecUniquePtr == nullptr) {
     648            0 :         BQS_LOG_ERROR("[DgwClient] failed to alloc memory for buffIovec, size[%zu].", totalLen);
     649            0 :         return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     650              :     }
     651            5 :     buff_iovec* const buffIovec = reinterpret_cast<buff_iovec*>(vecUniquePtr.get());
     652            5 :     buffIovec->context_base = nullptr;
     653            5 :     buffIovec->context_len = 0U;
     654            5 :     buffIovec->count = 1U;
     655            5 :     buffIovec->ptr[0U].iovec_base = body.get();
     656            5 :     buffIovec->ptr[0U].len = mbufLen;
     657              : 
     658            5 :     int32_t cmdRet = static_cast<int32_t>(BQS_STATUS_OK);
     659              :     {
     660            5 :         const std::unique_lock<std::mutex> eventLock(eventMutex_);
     661            5 :         auto drvRet = halQueueEnQueueBuff(deviceId_, piplineQueueId_, buffIovec, timeout);
     662            5 :         if (drvRet != DRV_ERROR_NONE) {
     663            1 :             BQS_LOG_ERROR(
     664              :                 "halQueueEnQueueBuff to queue[%u] in device[%u] failed, error[%d]", piplineQueueId_, deviceId_,
     665              :                 static_cast<int32_t>(drvRet));
     666            1 :             return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     667              :         }
     668              : 
     669            4 :         const auto ret = InformServer(subEventId, cmdRet, timeout);
     670            4 :         if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     671            1 :             return ret;
     672              :         }
     673              : 
     674            3 :         uint64_t respLen = 0U;
     675            3 :         drvRet = halQueuePeek(deviceId_, piplineQueueId_, &respLen, timeout);
     676            3 :         if ((drvRet != DRV_ERROR_NONE) || (respLen == 0U)) {
     677            2 :             BQS_LOG_ERROR(
     678              :                 "halQueuePeek from queue[%u] in device[%u] failed, ret[%d], respLen[%lu]", piplineQueueId_, deviceId_,
     679              :                 static_cast<int32_t>(drvRet), respLen);
     680            2 :             return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     681              :         }
     682              : 
     683            1 :         std::unique_ptr<char_t[]> respBody(new (std::nothrow) char_t[respLen], std::default_delete<char_t[]>());
     684            1 :         if (respBody == nullptr) {
     685            0 :             BQS_LOG_ERROR("[DgwClient] failed to alloc memory for response data, size[%lu].", respLen);
     686            0 :             return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     687              :         }
     688            1 :         buffIovec->context_base = nullptr;
     689            1 :         buffIovec->context_len = 0U;
     690            1 :         buffIovec->count = 1U;
     691            1 :         buffIovec->ptr[0U].iovec_base = respBody.get();
     692            1 :         buffIovec->ptr[0U].len = respLen;
     693            1 :         drvRet = halQueueDeQueueBuff(deviceId_, piplineQueueId_, buffIovec, timeout);
     694            1 :         if (drvRet != DRV_ERROR_NONE) {
     695            0 :             BQS_LOG_ERROR(
     696              :                 "halQueueDeQueueBuff from queue[%u] in device[%u] failed, ret[%d]", piplineQueueId_, deviceId_,
     697              :                 static_cast<int32_t>(drvRet));
     698            0 :             return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     699              :         }
     700              : 
     701            1 :         const uintptr_t dequeMbufDataAddr = PtrToValue(respBody.get());
     702            1 :         ExtractRetCode(subEventId, cfgParams, dequeMbufDataAddr, cfgRets, cmdRet);
     703            5 :     }
     704            1 :     return cmdRet;
     705            6 : }
     706              : 
     707           87 : void DgwClient::ExtractRetCode(
     708              :     const QueueSubEventType subEventId, const ConfigParams& cfgParams, const uintptr_t respPtr,
     709              :     std::vector<int32_t>& cfgRets, int32_t& cmdRet) const
     710              : {
     711              :     // create or destroy handle
     712           87 :     if ((subEventId == QueueSubEventType::DGW_CREATE_HCOM_HANDLE) ||
     713              :         (subEventId == QueueSubEventType::DGW_DESTORY_HCOM_HANDLE)) {
     714           16 :         if (cfgParams.info == nullptr) {
     715            0 :             cmdRet = static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     716              :         } else {
     717           16 :             (void)GetOperateHcomHandleRet(subEventId, *cfgParams.info, respPtr, cfgParams.cfgLen, cmdRet);
     718              :         }
     719           71 :     } else if (subEventId == QueueSubEventType::QUERY_CONFIG_NUM) { // qry config num get result outside
     720           23 :         if (cfgParams.query == nullptr) {
     721            0 :             cmdRet = static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     722              :         } else {
     723           23 :             (void)GetQryConfigNumRet(*cfgParams.query, respPtr, cmdRet);
     724              :         }
     725              :     } else {
     726           48 :         if (cfgParams.cfgInfo == nullptr) {
     727            0 :             cmdRet = static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     728              :         } else {
     729           48 :             (void)GetOperateConfigRet(*cfgParams.cfgInfo, respPtr, cfgParams.cfgLen, cfgRets, cmdRet);
     730              :         }
     731              :     }
     732           87 : }
     733              : 
     734           88 : int32_t DgwClient::InformServer(const QueueSubEventType subEventId, int32_t& cmdRet, const int32_t timeout)
     735              : {
     736              :     // send event to datagw server
     737           88 :     event_sync_msg syncMsg = {};
     738           88 :     QsProcMsgRsp procMsgRsp = {};
     739           88 :     const auto ret = SendEventToQsSync(&syncMsg, sizeof(event_sync_msg), subEventId, procMsgRsp, timeout);
     740           88 :     if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
     741            0 :         return ret;
     742              :     }
     743           88 :     cmdRet = procMsgRsp.retCode;
     744           88 :     return static_cast<int32_t>(BQS_STATUS_OK);
     745              : }
     746              : 
     747          114 : int32_t DgwClient::SendEventToQsSync(
     748              :     const void* const msg, const size_t msgLen, const QueueSubEventType subEventId, QsProcMsgRsp& qsProcMsgRsp,
     749              :     const int32_t timeout) const
     750              : {
     751          114 :     BQS_LOG_INFO("[DgwClient]SendEventToQsSync QsProcMsgRsp begin, timeout: %ds, deviceId: %u", timeout, deviceId_);
     752              :     event_reply drvAck;
     753          114 :     drvAck.buf = PtrToPtr<QsProcMsgRsp, char>(&qsProcMsgRsp);
     754          114 :     drvAck.buf_len = sizeof(QsProcMsgRsp);
     755              : 
     756          114 :     event_summary drvEventInfo = {};
     757          114 :     if (isProxy_) {
     758            2 :         drvEventInfo.dst_engine = static_cast<uint32_t>(CCPU_DEVICE);
     759              :     } else {
     760              :         // FlowGW host deployer client -- server used
     761          112 :         drvEventInfo.dst_engine = static_cast<uint32_t>(CCPU_LOCAL);
     762              :     }
     763          114 :     drvEventInfo.policy = ONLY;
     764          114 :     drvEventInfo.pid = qsPid_;
     765          114 :     drvEventInfo.grp_id = static_cast<uint32_t>(BIND_QUEUE_GROUP_ID);
     766          114 :     drvEventInfo.event_id = EVENT_QS_MSG;
     767          114 :     drvEventInfo.subevent_id = static_cast<uint32_t>(subEventId);
     768          114 :     drvEventInfo.msg_len = static_cast<uint32_t>(msgLen);
     769          114 :     drvEventInfo.msg = const_cast<char*>(static_cast<const char*>(msg));
     770              : 
     771          114 :     const int32_t timeOutMs = timeout > 0 ? timeout * 1000 : timeout;
     772          114 :     const auto drvRet = halEschedSubmitEventSync(deviceId_, &drvEventInfo, timeOutMs, &drvAck);
     773          114 :     if (drvRet != DRV_ERROR_NONE) {
     774            4 :         BQS_LOG_WARN("[DgwClient] Failed to submit event to qs, ret=[%d].", static_cast<int32_t>(drvRet));
     775            4 :         if (drvRet == DRV_ERROR_SCHED_WAIT_TIMEOUT) {
     776            2 :             return static_cast<int32_t>(BQS_STATUS_TIMEOUT);
     777              :         }
     778            2 :         return static_cast<int32_t>(BQS_STATUS_DRIVER_ERROR);
     779              :     }
     780          110 :     if (static_cast<size_t>(drvAck.reply_len) != sizeof(QsProcMsgRsp)) {
     781            0 :         BQS_LOG_ERROR(
     782              :             "[DgwClient] QsProcMsgRsp event_reply event message invalid, bufLen[%u], subEventId[%u]", drvAck.reply_len,
     783              :             static_cast<uint32_t>(subEventId));
     784            0 :         return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     785              :     }
     786          110 :     return static_cast<int32_t>(BQS_STATUS_OK);
     787              : }
     788              : 
     789           24 : int32_t DgwClient::GetQryConfigNumRet(ConfigQuery& query, const uintptr_t mbufData, int32_t& cmdRet) const
     790              : {
     791           24 :     if (cmdRet != static_cast<int32_t>(BQS_STATUS_OK)) {
     792              :         // return API operate result, not query config num ret
     793            1 :         return static_cast<int32_t>(BQS_STATUS_OK);
     794              :     }
     795              : 
     796           23 :     const uintptr_t retAddr = mbufData + sizeof(ConfigQuery);
     797           23 :     cmdRet = (PtrToPtr<void, CfgRetInfo>(ValueToPtr(retAddr)))->retCode;
     798              : 
     799           23 :     const ConfigQuery* cfgQry = PtrToPtr<void, ConfigQuery>(ValueToPtr(mbufData));
     800           23 :     if (query.mode == QueryMode::DGW_QUERY_MODE_GROUP) {
     801            8 :         query.qry.groupQry.endpointNum = cfgQry->qry.groupQry.endpointNum;
     802              :     } else {
     803           15 :         query.qry.routeQry.routeNum = cfgQry->qry.routeQry.routeNum;
     804              :     }
     805           23 :     return static_cast<int32_t>(BQS_STATUS_OK);
     806              : }
     807              : 
     808           49 : int32_t DgwClient::GetOperateConfigRet(
     809              :     ConfigInfo& cfgInfo, const uintptr_t mbufData, const size_t cfgLen, std::vector<int32_t>& cfgRets,
     810              :     int32_t& cmdRet) const
     811              : {
     812           49 :     int32_t ret = static_cast<int32_t>(BQS_STATUS_OK);
     813           49 :     switch (cfgInfo.cmd) {
     814           13 :         case ConfigCmd::DGW_CFG_CMD_BIND_ROUTE:
     815              :         case ConfigCmd::DGW_CFG_CMD_UNBIND_ROUTE: {
     816           13 :             ret = GetUpdateRouteRet(cfgInfo, mbufData, cfgLen, cfgRets, cmdRet);
     817           13 :             break;
     818              :         }
     819           18 :         case ConfigCmd::DGW_CFG_CMD_ADD_GROUP:
     820              :         case ConfigCmd::DGW_CFG_CMD_DEL_GROUP: {
     821           18 :             ret = GetUpdateGroupRet(cfgInfo, mbufData, cfgLen, cfgRets, cmdRet);
     822           18 :             break;
     823              :         }
     824            3 :         case ConfigCmd::DGW_CFG_CMD_QRY_GROUP: {
     825            3 :             ret = GetQryGroupRet(cfgInfo, mbufData, cfgLen, cfgRets, cmdRet);
     826            3 :             break;
     827              :         }
     828            6 :         case ConfigCmd::DGW_CFG_CMD_QRY_ROUTE: {
     829            6 :             ret = GetQryRouteRet(cfgInfo, mbufData, cfgLen, cfgRets, cmdRet);
     830            6 :             break;
     831              :         }
     832            8 :         case ConfigCmd::DGW_CFG_CMD_UPDATE_PROFILING:
     833              :         case ConfigCmd::DGW_CFG_CMD_SET_HCCL_PROTOCOL:
     834              :         case ConfigCmd::DGW_CFG_CMD_INIT_DYNAMIC_SCHEDULE:
     835              :         case ConfigCmd::DGW_CFG_CMD_STOP_SCHEDULE:
     836              :         case ConfigCmd::DGW_CFG_CMD_CLEAR_AND_RESTART_SCHEDULE: {
     837            8 :             const uintptr_t retAddr = mbufData + cfgLen;
     838           14 :             cmdRet = (cmdRet != static_cast<int32_t>(BQS_STATUS_OK)) ?
     839              :                          cmdRet :
     840            6 :                          PtrToPtr<void, CfgRetInfo>(ValueToPtr(retAddr))->retCode;
     841            8 :             cfgRets.push_back(cmdRet);
     842            8 :             break;
     843              :         }
     844            1 :         default: {
     845            1 :             BQS_LOG_WARN("[DgwClient] cmd[%d] is invalid.", static_cast<int32_t>(cfgInfo.cmd));
     846            1 :             cmdRet = static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
     847            1 :             ret = static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
     848            1 :             break;
     849              :         }
     850              :     }
     851           49 :     return ret;
     852              : }
     853              : 
     854           17 : int32_t DgwClient::GetOperateHcomHandleRet(
     855              :     const QueueSubEventType subEventId, HcomHandleInfo& info, const uintptr_t mbufData, const size_t cfgLen,
     856              :     int32_t& cmdRet) const
     857              : {
     858           17 :     constexpr int32_t ret = static_cast<int32_t>(BQS_STATUS_OK);
     859           17 :     const uintptr_t retAddr = mbufData + cfgLen;
     860           33 :     cmdRet = (cmdRet != static_cast<int32_t>(BQS_STATUS_OK)) ? cmdRet :
     861           16 :                                                                PtrToPtr<void, CfgRetInfo>(ValueToPtr(retAddr))->retCode;
     862           17 :     if (cmdRet != static_cast<int32_t>(BQS_STATUS_OK)) {
     863            2 :         return static_cast<int32_t>(BQS_STATUS_OK);
     864              :     }
     865              : 
     866           15 :     if (subEventId == QueueSubEventType::DGW_CREATE_HCOM_HANDLE) {
     867            8 :         info.hcomHandle = PtrToPtr<void, HcomHandleInfo>(ValueToPtr(mbufData))->hcomHandle;
     868              :     }
     869           15 :     return ret;
     870              : }
     871              : 
     872           50 : int32_t DgwClient::CalcResultLen(const ConfigInfo& cfgInfo, size_t& retLen) const
     873              : {
     874           50 :     retLen = 0UL;
     875           50 :     switch (cfgInfo.cmd) {
     876           13 :         case ConfigCmd::DGW_CFG_CMD_BIND_ROUTE:
     877              :         case ConfigCmd::DGW_CFG_CMD_UNBIND_ROUTE: {
     878           13 :             retLen += cfgInfo.cfg.routesCfg.routeNum * sizeof(CfgRetInfo);
     879           13 :             break;
     880              :         }
     881           36 :         case ConfigCmd::DGW_CFG_CMD_QRY_ROUTE:
     882              :         case ConfigCmd::DGW_CFG_CMD_QRY_GROUP:
     883              :         case ConfigCmd::DGW_CFG_CMD_ADD_GROUP:
     884              :         case ConfigCmd::DGW_CFG_CMD_DEL_GROUP:
     885              :         case ConfigCmd::DGW_CFG_CMD_UPDATE_PROFILING:
     886              :         case ConfigCmd::DGW_CFG_CMD_SET_HCCL_PROTOCOL:
     887              :         case ConfigCmd::DGW_CFG_CMD_INIT_DYNAMIC_SCHEDULE:
     888              :         case ConfigCmd::DGW_CFG_CMD_STOP_SCHEDULE:
     889              :         case ConfigCmd::DGW_CFG_CMD_CLEAR_AND_RESTART_SCHEDULE: {
     890           36 :             retLen += sizeof(CfgRetInfo);
     891           36 :             break;
     892              :         }
     893            1 :         default: {
     894            1 :             break;
     895              :         }
     896              :     }
     897           50 :     return static_cast<int32_t>(BQS_STATUS_OK);
     898              : }
     899              : 
     900              : // Create a handler for each EndpointType
     901            8 : static uint32_t HandleMemQueue(Endpoint& dstEndPoint, const Endpoint& srcEndPoint)
     902              : {
     903            8 :     dstEndPoint.type = bqs::EndpointType::QUEUE;
     904            8 :     dstEndPoint.attr.queueAttr.queueId = srcEndPoint.attr.memQueueAttr.queueId;
     905            8 :     return static_cast<int32_t>(BQS_STATUS_OK);
     906              : }
     907              : 
     908            1 : static uint32_t HandleGroup(Endpoint& dstEndPoint, const Endpoint& srcEndPoint)
     909              : {
     910            1 :     const size_t groupAttrSize = sizeof(srcEndPoint.attr.groupAttr);
     911            2 :     const auto cpyRet = memcpy_s(
     912            1 :         (void*)(&dstEndPoint.attr.groupAttr), groupAttrSize, (void*)(&srcEndPoint.attr.groupAttr), groupAttrSize);
     913            1 :     if (cpyRet != EOK) {
     914            0 :         BQS_LOG_ERROR("[HandleGroup] Memcpy failed, cpyLen[%zu], ret=[%d]", groupAttrSize, cpyRet);
     915            0 :         return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     916              :     }
     917            1 :     return static_cast<int32_t>(BQS_STATUS_OK);
     918              : }
     919              : 
     920            1 : static uint32_t HandleCommChannel(Endpoint& dstEndPoint, const Endpoint& srcEndPoint)
     921              : {
     922            1 :     const size_t channelAttrSize = sizeof(srcEndPoint.attr.channelAttr);
     923            2 :     const auto cpyRet = memcpy_s(
     924            1 :         (void*)(&dstEndPoint.attr.channelAttr), channelAttrSize, (void*)(&srcEndPoint.attr.channelAttr),
     925              :         channelAttrSize);
     926            1 :     if (cpyRet != EOK) {
     927            0 :         BQS_LOG_ERROR("[HandleCommChannel] Memcpy failed, cpyLen[%zu], ret=[%d]", channelAttrSize, cpyRet);
     928            0 :         return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     929              :     }
     930            1 :     return static_cast<int32_t>(BQS_STATUS_OK);
     931              : }
     932              : 
     933              : // Define the function pointer type
     934              : using EndpointHandler = uint32_t (*)(Endpoint&, const Endpoint&);
     935              : 
     936              : // Creating Table Mappings
     937              : std::map<bqs::EndpointType, EndpointHandler> g_endpointHandlers = {
     938              :     {bqs::EndpointType::MEM_QUEUE, &HandleMemQueue},
     939              :     {bqs::EndpointType::GROUP, &HandleGroup},
     940              :     {bqs::EndpointType::COMM_CHANNEL, &HandleCommChannel},
     941              : };
     942              : 
     943           11 : static uint32_t EndpointTransformMemQ2Q(Endpoint& dstEndPoint, Endpoint& srcEndPoint)
     944              : {
     945           11 :     if (srcEndPoint.type == bqs::EndpointType::MEM_QUEUE && srcEndPoint.attr.memQueueAttr.queueType == bqs::CLIENT_Q) {
     946            1 :         BQS_LOG_ERROR("[EndpointTransformMemQ2Q] CLIENT_Q interception");
     947            1 :         return static_cast<int32_t>(BQS_STATUS_ENDPOINT_MEM_TYPE_NOT_SUPPORT);
     948              :     }
     949           10 :     dstEndPoint.type = srcEndPoint.type;
     950           10 :     dstEndPoint.status = srcEndPoint.status;
     951           10 :     dstEndPoint.peerNum = srcEndPoint.peerNum;
     952           10 :     dstEndPoint.localId = srcEndPoint.localId;
     953           10 :     dstEndPoint.globalId = srcEndPoint.globalId;
     954           10 :     dstEndPoint.modelId = srcEndPoint.modelId;
     955              :     // localQ need transform
     956           10 :     auto it = g_endpointHandlers.find(srcEndPoint.type);
     957           10 :     if (it != g_endpointHandlers.end()) {
     958           10 :         return it->second(dstEndPoint, srcEndPoint);
     959              :     } else {
     960            0 :         BQS_LOG_ERROR("[EndpointTransformMemQ2Q] should not reach here");
     961            0 :         return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
     962              :     }
     963              : }
     964              : 
     965            1 : static int32_t EndpointTransformQ2MemQ(std::unique_ptr<Endpoint[]>& endpoints, uint32_t endpointNum)
     966              : {
     967            3 :     for (uint32_t rdx = 0; rdx < endpointNum; rdx++) {
     968            2 :         if (endpoints[rdx].type == bqs::EndpointType::QUEUE) {
     969            1 :             BQS_LOG_INFO("[EndpointTransformQ2MemQ] transfer q to memq");
     970            1 :             endpoints[rdx].type = bqs::EndpointType::MEM_QUEUE;
     971            1 :             endpoints[rdx].attr.memQueueAttr.queueId = endpoints[rdx].attr.queueAttr.queueId;
     972            1 :             endpoints[rdx].attr.memQueueAttr.queueType = 0U;
     973              :         }
     974              :     }
     975            1 :     return static_cast<int32_t>(BQS_STATUS_OK);
     976              : }
     977              : 
     978            2 : static int32_t GroupQueueTransform(
     979              :     Endpoint* endpoints, uint32_t endpointNum, std::unique_ptr<Endpoint[]>& spareEndpoints)
     980              : {
     981            2 :     bool isNeedTransform = false;
     982            6 :     for (uint32_t rdx = 0; rdx < endpointNum; rdx++) {
     983            4 :         if (endpoints[rdx].type == bqs::EndpointType::MEM_QUEUE) {
     984            3 :             isNeedTransform = true;
     985              :         }
     986              :     }
     987              : 
     988            2 :     if (!isNeedTransform) {
     989            0 :         BQS_LOG_INFO("[GroupQueueTransform] group no mem queue do not to transfer");
     990            0 :         return static_cast<int32_t>(BQS_STATUS_NO_NEED_MEM_QUEUE_TRANSFORM);
     991              :     }
     992              : 
     993            6 :     for (uint32_t rdx = 0; rdx < endpointNum; rdx++) {
     994            4 :         const uint32_t ret = EndpointTransformMemQ2Q(spareEndpoints[rdx], endpoints[rdx]);
     995            4 :         if (ret != static_cast<uint32_t>(BQS_STATUS_OK)) {
     996            0 :             BQS_LOG_ERROR("[GroupQueueTransform] transfer error ret=%u", ret);
     997            0 :             return static_cast<int32_t>(ret);
     998              :         }
     999              :     }
    1000            2 :     return static_cast<int32_t>(BQS_STATUS_OK);
    1001              : }
    1002              : 
    1003            5 : static int32_t MemQueueAttr2queueAttrTransform(Route* routes, uint32_t routeNum, std::unique_ptr<Route[]>& spareRoutes)
    1004              : {
    1005            5 :     bool isNeedTransform = false;
    1006           10 :     for (uint32_t rdx = 0; rdx < routeNum; rdx++) {
    1007            5 :         if (routes[rdx].src.type == bqs::EndpointType::MEM_QUEUE ||
    1008            1 :             routes[rdx].dst.type == bqs::EndpointType::MEM_QUEUE) {
    1009            4 :             isNeedTransform = true;
    1010              :         }
    1011              :     }
    1012              : 
    1013            5 :     if (!isNeedTransform) {
    1014            1 :         BQS_LOG_INFO("[MemQueueAttr2queueAttrTransform] no mem queue do not to transfer");
    1015            1 :         return static_cast<int32_t>(BQS_STATUS_NO_NEED_MEM_QUEUE_TRANSFORM);
    1016              :     }
    1017              : 
    1018            7 :     for (uint32_t rdx = 0; rdx < routeNum; rdx++) {
    1019            4 :         spareRoutes[rdx].status = routes[rdx].status;
    1020              :         // src copy
    1021            4 :         uint32_t ret = EndpointTransformMemQ2Q(spareRoutes[rdx].src, routes[rdx].src);
    1022            4 :         if (ret != static_cast<uint32_t>(BQS_STATUS_OK)) {
    1023            1 :             BQS_LOG_ERROR("[MemQueueAttr2queueAttrTransform] transfer error ret=%u", ret);
    1024            1 :             return static_cast<int32_t>(ret);
    1025              :         }
    1026              : 
    1027              :         // dst copy
    1028            3 :         ret = EndpointTransformMemQ2Q(spareRoutes[rdx].dst, routes[rdx].dst);
    1029            3 :         if (ret != static_cast<uint32_t>(BQS_STATUS_OK)) {
    1030            0 :             BQS_LOG_ERROR("[MemQueueAttr2queueAttrTransform] transfer error ret=%u", ret);
    1031            0 :             return static_cast<int32_t>(ret);
    1032              :         }
    1033              :     }
    1034            3 :     return static_cast<int32_t>(BQS_STATUS_OK);
    1035              : }
    1036              : 
    1037            1 : int32_t DgwClient::ChangeDynamicScheduleDeviceId(const ConfigInfo& cfgInfo)
    1038              : {
    1039            1 :     BQS_LOG_INFO("[DgwClient] begin to process dynamic schedule device id.");
    1040            1 :     if (cfgInfo.cfg.dynamicSchedCfgV2->requestQ.deviceType == 0) {
    1041            1 :         uint32_t logicDeviceId = cfgInfo.cfg.dynamicSchedCfgV2->requestQ.deviceId;
    1042              :         const auto cRet =
    1043            1 :             ChangeUserDeviceIdToLogicDeviceId(cfgInfo.cfg.dynamicSchedCfgV2->requestQ.deviceId, logicDeviceId);
    1044            1 :         if (cRet != static_cast<int32_t>(BQS_STATUS_OK)) {
    1045            0 :             BQS_LOG_ERROR("[DgwClient] ChangeUserDeviceIdToLogicDeviceId failed ret [%d]", cRet);
    1046            0 :             return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1047              :         }
    1048            1 :         cfgInfo.cfg.dynamicSchedCfgV2->requestQ.deviceId = logicDeviceId;
    1049              :     }
    1050            1 :     if (cfgInfo.cfg.dynamicSchedCfgV2->responseQ.deviceType == 0) {
    1051            1 :         uint32_t logicDeviceId = cfgInfo.cfg.dynamicSchedCfgV2->responseQ.deviceId;
    1052              :         const auto cRet =
    1053            1 :             ChangeUserDeviceIdToLogicDeviceId(cfgInfo.cfg.dynamicSchedCfgV2->responseQ.deviceId, logicDeviceId);
    1054            1 :         if (cRet != static_cast<int32_t>(BQS_STATUS_OK)) {
    1055            0 :             BQS_LOG_ERROR("[DgwClient] ChangeUserDeviceIdToLogicDeviceId failed ret [%d]", cRet);
    1056            0 :             return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1057              :         }
    1058            1 :         cfgInfo.cfg.dynamicSchedCfgV2->responseQ.deviceId = logicDeviceId;
    1059              :     }
    1060            1 :     return static_cast<int32_t>(BQS_STATUS_OK);
    1061              : }
    1062              : 
    1063          122 : int32_t DgwClient::ProcessEndpointDeviceId(Endpoint& endpoint) const
    1064              : {
    1065          122 :     BQS_LOG_INFO("[DgwClient] begin to process endpoint deviceId %u.", endpoint.resId);
    1066          122 :     if (isProxy_ && QSFeatureCtrl::IsSupportSetVisibleDevices(g_chipType)) {
    1067            2 :         const bool isHostQueue = ((endpoint.resId >> RESOURCE_ID_HOST_DEVICE_BIT_NUM) & 1U) ? true : false;
    1068            2 :         if (!isHostQueue) {
    1069            2 :             const uint32_t frontPart = endpoint.resId & ROUCE_ID_FRONT_PART_DATA_MASK;
    1070            2 :             const uint32_t rearPart = endpoint.resId & ROUCE_ID_DEVICE_ID_DATA_MASK;
    1071            2 :             uint32_t tmpRearPart = rearPart;
    1072            2 :             const auto cRet = ChangeUserDeviceIdToLogicDeviceId(rearPart, tmpRearPart);
    1073            2 :             if (cRet != static_cast<int32_t>(BQS_STATUS_OK)) {
    1074            1 :                 BQS_LOG_ERROR("[DgwClient] ChangeUserDeviceIdToLogicDeviceId failed ret [%d]", cRet);
    1075            1 :                 return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1076              :             }
    1077            1 :             endpoint.resId = static_cast<uint16_t>(frontPart) | static_cast<uint16_t>(tmpRearPart);
    1078              :         }
    1079              :     }
    1080          121 :     return static_cast<int32_t>(BQS_STATUS_OK);
    1081              : }
    1082              : 
    1083           63 : int32_t DgwClient::CalcConfigInfoLen(
    1084              :     const ConfigInfo& cfgInfo, size_t& cfgLen, std::list<std::pair<uintptr_t, size_t>>& dataList,
    1085              :     std::unique_ptr<Route[]>& spareRoutes, std::unique_ptr<Endpoint[]>& spareEndpoints) const
    1086              : {
    1087           63 :     switch (cfgInfo.cmd) {
    1088           27 :         case ConfigCmd::DGW_CFG_CMD_BIND_ROUTE:
    1089              :         case ConfigCmd::DGW_CFG_CMD_UNBIND_ROUTE:
    1090              :         case ConfigCmd::DGW_CFG_CMD_QRY_ROUTE: {
    1091           27 :             Route* routes = cfgInfo.cfg.routesCfg.routes;
    1092           27 :             if (routes == nullptr) {
    1093            1 :                 BQS_LOG_ERROR("routes is nullptr.");
    1094            1 :                 return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1095              :             }
    1096           26 :             const uint32_t routeNum = cfgInfo.cfg.routesCfg.routeNum;
    1097           26 :             if ((routeNum == 0U) || (routeNum > MAX_ROUTES_NUM)) {
    1098            1 :                 BQS_LOG_ERROR("route num[%u] is invalid, max allowed value is [%u].", routeNum, MAX_ROUTES_NUM);
    1099            1 :                 return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1100              :             }
    1101              : 
    1102           68 :             for (uint32_t rdx = 0; rdx < routeNum; rdx++) {
    1103           44 :                 const auto pRet = ProcessEndpointDeviceId(routes[rdx].src) + ProcessEndpointDeviceId(routes[rdx].dst);
    1104           44 :                 if (pRet != static_cast<int32_t>(BQS_STATUS_OK)) {
    1105            1 :                     return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1106              :                 }
    1107              :             }
    1108              : 
    1109           24 :             cfgLen += (sizeof(cfgInfo) + static_cast<size_t>(routeNum) * sizeof(Route));
    1110           24 :             (void)dataList.emplace_back(std::make_pair(PtrToValue(&cfgInfo), sizeof(ConfigInfo)));
    1111           24 :             if (!isServerOldVersion_) {
    1112           19 :                 (void)dataList.emplace_back(std::make_pair(PtrToValue(routes), routeNum * sizeof(Route)));
    1113              :             } else {
    1114            5 :                 BQS_LOG_INFO("[CalcConfigInfoLen] old version try to transfer mem queue");
    1115            5 :                 const auto cpyRet = memcpy_s(
    1116            5 :                     static_cast<void*>(spareRoutes.get()), routeNum * sizeof(Route), routes, routeNum * sizeof(Route));
    1117            5 :                 if (cpyRet != EOK) {
    1118            0 :                     BQS_LOG_ERROR(
    1119              :                         "[CalcConfigInfoLen] Memcpy failed, cpyLen[%zu], ret=[%d]", routeNum * sizeof(Route), cpyRet);
    1120            0 :                     return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
    1121              :                 }
    1122            5 :                 const int32_t ret = MemQueueAttr2queueAttrTransform(routes, routeNum, spareRoutes);
    1123            5 :                 if (ret != static_cast<int32_t>(BQS_STATUS_OK) &&
    1124              :                     ret != static_cast<int32_t>(BQS_STATUS_NO_NEED_MEM_QUEUE_TRANSFORM)) {
    1125            1 :                     BQS_LOG_ERROR("[CalcConfigInfoLen] ret is not okay or routes client is nullptr");
    1126            1 :                     return ret;
    1127              :                 }
    1128            4 :                 if (ret == static_cast<int32_t>(BQS_STATUS_NO_NEED_MEM_QUEUE_TRANSFORM)) {
    1129            1 :                     (void)dataList.emplace_back(std::make_pair(PtrToValue(routes), routeNum * sizeof(Route)));
    1130              :                 } else {
    1131            3 :                     (void)dataList.emplace_back(
    1132            6 :                         std::make_pair(PtrToValue(spareRoutes.get()), routeNum * sizeof(Route)));
    1133              :                 }
    1134              :             }
    1135           23 :             break;
    1136              :         }
    1137           18 :         case ConfigCmd::DGW_CFG_CMD_ADD_GROUP:
    1138              :         case ConfigCmd::DGW_CFG_CMD_QRY_GROUP: {
    1139           18 :             Endpoint* endpoints = cfgInfo.cfg.groupCfg.endpoints;
    1140           18 :             if (endpoints == nullptr) {
    1141            1 :                 BQS_LOG_ERROR("endpoints is nullptr.");
    1142            3 :                 return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1143              :             }
    1144              : 
    1145           51 :             for (uint32_t rdx = 0; rdx < cfgInfo.cfg.groupCfg.endpointNum; rdx++) {
    1146           35 :                 const auto pRet = ProcessEndpointDeviceId(endpoints[rdx]);
    1147           35 :                 if (pRet != static_cast<int32_t>(BQS_STATUS_OK)) {
    1148            1 :                     return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1149              :                 }
    1150              :             }
    1151              : 
    1152           16 :             const uint32_t endpointNum = cfgInfo.cfg.groupCfg.endpointNum;
    1153           16 :             if ((endpointNum == 0U) || (endpointNum > MAX_ENDPOINTS_NUM_IN_SINGLE_GROUP)) {
    1154            1 :                 BQS_LOG_ERROR("route num[%u] is invalid.", endpointNum);
    1155            1 :                 return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1156              :             }
    1157           15 :             const size_t endpointsLen = endpointNum * sizeof(Endpoint);
    1158           15 :             cfgLen += (sizeof(cfgInfo) + endpointsLen);
    1159           15 :             (void)dataList.emplace_back(std::make_pair(PtrToValue(&cfgInfo), sizeof(ConfigInfo)));
    1160           15 :             if (!isServerOldVersion_) {
    1161           13 :                 (void)dataList.emplace_back(std::make_pair(PtrToValue(endpoints), endpointsLen));
    1162              :             } else {
    1163            2 :                 BQS_LOG_INFO("[CalcConfigInfoLen] old version group try to transfer mem queue");
    1164            2 :                 const auto cpyRet = memcpy_s(
    1165            2 :                     static_cast<void*>(spareEndpoints.get()), endpointNum * sizeof(Endpoint), endpoints,
    1166            2 :                     endpointNum * sizeof(Endpoint));
    1167            2 :                 if (cpyRet != EOK) {
    1168            0 :                     BQS_LOG_ERROR("[CalcConfigInfoLen] Memcpy failed, cpyLen[%zu], ret=[%d]", endpointsLen, cpyRet);
    1169            0 :                     return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
    1170              :                 }
    1171            2 :                 const int32_t ret = GroupQueueTransform(endpoints, endpointNum, spareEndpoints);
    1172            2 :                 if (ret != static_cast<int32_t>(BQS_STATUS_OK) &&
    1173              :                     ret != static_cast<int32_t>(BQS_STATUS_NO_NEED_MEM_QUEUE_TRANSFORM)) {
    1174            0 :                     BQS_LOG_ERROR("[CalcConfigInfoLen] ret is not okay or routes client is nullptr");
    1175            0 :                     return ret;
    1176              :                 }
    1177            2 :                 if (ret == static_cast<int32_t>(BQS_STATUS_NO_NEED_MEM_QUEUE_TRANSFORM)) {
    1178            0 :                     (void)dataList.emplace_back(std::make_pair(PtrToValue(endpoints), endpointsLen));
    1179              :                 } else {
    1180            2 :                     (void)dataList.emplace_back(std::make_pair(PtrToValue(spareEndpoints.get()), endpointsLen));
    1181              :                 }
    1182              :             }
    1183           15 :             break;
    1184              :         }
    1185           11 :         case ConfigCmd::DGW_CFG_CMD_UPDATE_PROFILING:
    1186              :         case ConfigCmd::DGW_CFG_CMD_DEL_GROUP:
    1187              :         case ConfigCmd::DGW_CFG_CMD_SET_HCCL_PROTOCOL: {
    1188           11 :             cfgLen += sizeof(cfgInfo);
    1189           11 :             (void)dataList.emplace_back(std::make_pair(PtrToValue(&cfgInfo), sizeof(ConfigInfo)));
    1190           11 :             break;
    1191              :         }
    1192            4 :         case ConfigCmd::DGW_CFG_CMD_INIT_DYNAMIC_SCHEDULE: {
    1193            5 :             if (isProxy_ && QSFeatureCtrl::IsSupportSetVisibleDevices(g_chipType) &&
    1194            1 :                 cfgInfo.cfg.dynamicSchedCfgV2 != nullptr) {
    1195            1 :                 const auto cRet = ChangeDynamicScheduleDeviceId(cfgInfo);
    1196            1 :                 if (cRet != static_cast<int32_t>(BQS_STATUS_OK)) {
    1197            0 :                     return cRet;
    1198              :                 }
    1199              :             }
    1200              : 
    1201            4 :             cfgLen += sizeof(cfgInfo) + sizeof(DynamicSchedConfigV2);
    1202            4 :             (void)dataList.emplace_back(std::make_pair(PtrToValue(&cfgInfo), sizeof(ConfigInfo)));
    1203            4 :             (void)dataList.emplace_back(
    1204            4 :                 std::make_pair(PtrToValue(cfgInfo.cfg.dynamicSchedCfgV2), sizeof(DynamicSchedConfigV2)));
    1205            4 :             break;
    1206              :         }
    1207            2 :         case ConfigCmd::DGW_CFG_CMD_STOP_SCHEDULE:
    1208              :         case ConfigCmd::DGW_CFG_CMD_CLEAR_AND_RESTART_SCHEDULE: {
    1209            2 :             const size_t rootModelIdsLen = cfgInfo.cfg.reDeployCfg.rootModelNum * sizeof(uint32_t);
    1210            2 :             cfgLen += sizeof(cfgInfo) + rootModelIdsLen;
    1211            2 :             (void)dataList.emplace_back(std::make_pair(PtrToValue(&cfgInfo), sizeof(ConfigInfo)));
    1212            2 :             (void)dataList.emplace_back(std::make_pair(cfgInfo.cfg.reDeployCfg.rootModelIdsAddr, rootModelIdsLen));
    1213            2 :             break;
    1214              :         }
    1215            1 :         default: {
    1216            1 :             BQS_LOG_ERROR("calculate config info len failed, cmd[%d] is invalid.", static_cast<int32_t>(cfgInfo.cmd));
    1217            1 :             return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1218              :         }
    1219              :     }
    1220           55 :     return static_cast<int32_t>(BQS_STATUS_OK);
    1221              : }
    1222              : 
    1223           12 : int32_t DgwClient::CheckConfigNum(const ConfigQuery& query, ConfigInfo& cfgInfo)
    1224              : {
    1225           12 :     int32_t ret = static_cast<int32_t>(BQS_STATUS_OK);
    1226           12 :     switch (query.mode) {
    1227            4 :         case QueryMode::DGW_QUERY_MODE_GROUP: {
    1228            4 :             const uint32_t endpointNum = query.qry.groupQry.endpointNum;
    1229              :             // query config num
    1230            4 :             ConfigQuery tmpQry = query;
    1231            4 :             ret = QueryConfigNum(tmpQry);
    1232            4 :             if (ret == static_cast<int32_t>(BQS_STATUS_OK)) {
    1233              :                 // check endpointNum
    1234            4 :                 if ((endpointNum != tmpQry.qry.groupQry.endpointNum) || (endpointNum == 0U)) {
    1235            1 :                     BQS_LOG_ERROR(
    1236              :                         "[DgwClient] Param error! endpointNum in query is [%u], "
    1237              :                         "but endpointNum searched from dgw server is [%u].",
    1238              :                         endpointNum, tmpQry.qry.groupQry.endpointNum);
    1239            1 :                     ret = static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1240              :                 } else {
    1241              :                     // set to cfgInfo
    1242            3 :                     cfgInfo.cmd = ConfigCmd::DGW_CFG_CMD_QRY_GROUP;
    1243            3 :                     cfgInfo.cfg.groupCfg.endpointNum = endpointNum;
    1244              :                 }
    1245              :             }
    1246            4 :             break;
    1247              :         }
    1248            7 :         case QueryMode::DGW_QUERY_MODE_SRC_ROUTE:
    1249              :         case QueryMode::DGW_QUERY_MODE_DST_ROUTE:
    1250              :         case QueryMode::DGW_QUERY_MODE_SRC_DST_ROUTE:
    1251              :         case QueryMode::DGW_QUERY_MODE_ALL_ROUTE: {
    1252            7 :             uint32_t routeNum = query.qry.routeQry.routeNum;
    1253              :             // query config num
    1254            7 :             ConfigQuery tmpQry = query;
    1255            7 :             ret = QueryConfigNum(tmpQry);
    1256            7 :             if (ret == static_cast<int32_t>(BQS_STATUS_OK)) {
    1257              :                 // check routeNum
    1258            7 :                 if ((routeNum != tmpQry.qry.routeQry.routeNum) || (routeNum == 0U)) {
    1259            1 :                     BQS_LOG_ERROR(
    1260              :                         "[DgwClient] Param error! routeNum in query is [%u], "
    1261              :                         "but routeNum searched from dgw server is [%u].",
    1262              :                         routeNum, tmpQry.qry.routeQry.routeNum);
    1263            1 :                     ret = static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1264              :                 } else {
    1265              :                     // set to cfgInfo
    1266            6 :                     cfgInfo.cmd = ConfigCmd::DGW_CFG_CMD_QRY_ROUTE;
    1267            6 :                     cfgInfo.cfg.routesCfg.routeNum = routeNum;
    1268              :                 }
    1269              :             }
    1270            7 :             break;
    1271              :         }
    1272            1 :         default: {
    1273            1 :             ret = static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1274            1 :             BQS_LOG_ERROR("[DgwClient] query mode[%d] is invalid.", static_cast<int32_t>(query.mode));
    1275            1 :             break;
    1276              :         }
    1277              :     }
    1278           12 :     return ret;
    1279              : }
    1280              : 
    1281           13 : int32_t DgwClient::GetUpdateRouteRet(
    1282              :     const ConfigInfo& cfgInfo, const uintptr_t mbufData, const size_t cfgLen, std::vector<int32_t>& cfgRets,
    1283              :     int32_t& cmdRet) const
    1284              : {
    1285           13 :     bool failFlag = false;
    1286           13 :     const uintptr_t retAddr = mbufData + cfgLen;
    1287           13 :     CfgRetInfo* const results = PtrToPtr<void, CfgRetInfo>(ValueToPtr(retAddr));
    1288           13 :     const size_t routeNum = static_cast<size_t>(cfgInfo.cfg.routesCfg.routeNum);
    1289           40 :     for (size_t i = 0UL; i < routeNum; i++) {
    1290           53 :         const int32_t retCode = (cmdRet != static_cast<int32_t>(BQS_STATUS_OK)) ?
    1291              :                                     cmdRet :
    1292           26 :                                     PtrAdd<CfgRetInfo>(results, routeNum, i)->retCode;
    1293           27 :         cfgRets.push_back(retCode);
    1294           53 :         failFlag = ((!failFlag) && (retCode != static_cast<int32_t>(BQS_STATUS_OK))) ? true : failFlag;
    1295              :     }
    1296           13 :     cmdRet = (failFlag) ? static_cast<int32_t>(BQS_STATUS_FAILED) : cmdRet;
    1297           13 :     return static_cast<int32_t>(BQS_STATUS_OK);
    1298           26 : }
    1299              : 
    1300           18 : int32_t DgwClient::GetUpdateGroupRet(
    1301              :     ConfigInfo& cfgInfo, const uintptr_t mbufData, const size_t cfgLen, std::vector<int32_t>& cfgRets,
    1302              :     int32_t& cmdRet) const
    1303              : {
    1304           18 :     const uintptr_t retAddr = mbufData + cfgLen;
    1305           32 :     cmdRet = (cmdRet != static_cast<int32_t>(BQS_STATUS_OK)) ? cmdRet :
    1306           14 :                                                                PtrToPtr<void, CfgRetInfo>(ValueToPtr(retAddr))->retCode;
    1307           18 :     cfgRets.push_back(cmdRet);
    1308           18 :     if (cmdRet != static_cast<int32_t>(BQS_STATUS_OK)) {
    1309            4 :         return static_cast<int32_t>(BQS_STATUS_OK);
    1310              :     }
    1311           14 :     cfgInfo.cfg.groupCfg.groupId = PtrToPtr<void, ConfigInfo>(ValueToPtr(mbufData))->cfg.groupCfg.groupId;
    1312           14 :     return static_cast<int32_t>(BQS_STATUS_OK);
    1313              : }
    1314              : 
    1315            5 : int32_t DgwClient::GetQryGroupRet(
    1316              :     const ConfigInfo& cfgInfo, const uintptr_t mbufData, const size_t cfgLen, std::vector<int32_t>& cfgRets,
    1317              :     int32_t& cmdRet) const
    1318              : {
    1319              :     (void)cfgRets;
    1320            5 :     const uintptr_t retAddr = mbufData + sizeof(ConfigQuery) + cfgLen;
    1321           10 :     cmdRet = (cmdRet != static_cast<int32_t>(BQS_STATUS_OK)) ? cmdRet :
    1322            5 :                                                                PtrToPtr<void, CfgRetInfo>(ValueToPtr(retAddr))->retCode;
    1323              :     // cpy result to user memory
    1324            5 :     if (cmdRet == static_cast<int32_t>(BQS_STATUS_OK)) {
    1325            5 :         if (!isServerOldVersion_) {
    1326            3 :             Endpoint* endpoints = cfgInfo.cfg.groupCfg.endpoints;
    1327            3 :             const size_t endpointsLen = cfgInfo.cfg.groupCfg.endpointNum * sizeof(Endpoint);
    1328            3 :             const uintptr_t srcAddr = mbufData + sizeof(ConfigQuery) + sizeof(ConfigInfo);
    1329            3 :             const size_t srcLen = cfgLen - sizeof(ConfigInfo);
    1330            3 :             const auto cpyRet = memcpy_s(static_cast<void*>(endpoints), endpointsLen, ValueToPtr(srcAddr), srcLen);
    1331            3 :             if (cpyRet != EOK) {
    1332            0 :                 cmdRet = static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
    1333            0 :                 BQS_LOG_ERROR("Memcpy failed, srcLen[%zu], dstLen[%zu] ret=[%d]", srcLen, endpointsLen, cpyRet);
    1334            0 :                 return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
    1335              :             }
    1336              :         } else {
    1337            2 :             BQS_LOG_INFO("[GetQryGroupRet] old version need to transfer queue 2 mem queue");
    1338            2 :             std::unique_ptr<Endpoint[]> spareEndpoints(new (std::nothrow) Endpoint[cfgInfo.cfg.groupCfg.endpointNum]);
    1339            2 :             if (spareEndpoints == nullptr) {
    1340            0 :                 BQS_LOG_ERROR("[DgwClient] malloc failed on spareEndpoints.");
    1341            0 :                 return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
    1342              :             }
    1343              : 
    1344            2 :             const size_t endpointsLen = cfgInfo.cfg.groupCfg.endpointNum * sizeof(Endpoint);
    1345            2 :             const uintptr_t srcAddr = mbufData + sizeof(ConfigQuery) + sizeof(ConfigInfo);
    1346            2 :             const size_t srcLen = cfgLen - sizeof(ConfigInfo);
    1347            2 :             auto cpyRet = memcpy_s(static_cast<void*>(spareEndpoints.get()), endpointsLen, ValueToPtr(srcAddr), srcLen);
    1348            2 :             if (cpyRet != EOK) {
    1349            1 :                 cmdRet = static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
    1350            1 :                 BQS_LOG_ERROR("Memcpy failed, srcLen[%zu], dstLen[%zu] ret=[%d]", srcLen, endpointsLen, cpyRet);
    1351            1 :                 return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
    1352              :             }
    1353            1 :             const int32_t ret = EndpointTransformQ2MemQ(spareEndpoints, cfgInfo.cfg.groupCfg.endpointNum);
    1354            1 :             if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
    1355            0 :                 BQS_LOG_ERROR("ret is not okay or routes client is nullptr");
    1356            0 :                 return ret;
    1357              :             }
    1358            1 :             Endpoint* endpoints = cfgInfo.cfg.groupCfg.endpoints;
    1359              :             cpyRet =
    1360            1 :                 memcpy_s(static_cast<void*>(endpoints), endpointsLen, static_cast<void*>(spareEndpoints.get()), srcLen);
    1361            1 :             if (cpyRet != EOK) {
    1362            0 :                 cmdRet = static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
    1363            0 :                 BQS_LOG_ERROR("Memcpy failed, srcLen[%zu], dstLen[%zu] ret=[%d]", srcLen, endpointsLen, cpyRet);
    1364            0 :                 return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
    1365              :             }
    1366            2 :         }
    1367              :     }
    1368            4 :     return static_cast<int32_t>(BQS_STATUS_OK);
    1369              : }
    1370              : 
    1371            7 : int32_t DgwClient::GetQryRouteRet(
    1372              :     const ConfigInfo& cfgInfo, const uintptr_t mbufData, const size_t cfgLen, std::vector<int32_t>& cfgRets,
    1373              :     int32_t& cmdRet) const
    1374              : {
    1375              :     (void)cfgRets;
    1376            7 :     const uintptr_t retAddr = mbufData + sizeof(ConfigQuery) + cfgLen;
    1377           14 :     cmdRet = (cmdRet != static_cast<int32_t>(BQS_STATUS_OK)) ? cmdRet :
    1378            7 :                                                                PtrToPtr<void, CfgRetInfo>(ValueToPtr(retAddr))->retCode;
    1379              :     // cpy result to user memory
    1380            7 :     if (cmdRet == static_cast<int32_t>(BQS_STATUS_OK)) {
    1381            7 :         Route* routes = cfgInfo.cfg.routesCfg.routes;
    1382            7 :         const size_t routesLen = cfgInfo.cfg.routesCfg.routeNum * sizeof(Route);
    1383            7 :         const uintptr_t srcAddr = mbufData + sizeof(ConfigQuery) + sizeof(ConfigInfo);
    1384            7 :         const size_t srcLen = cfgLen - sizeof(ConfigInfo);
    1385            7 :         const auto cpyRet = memcpy_s(static_cast<void*>(routes), routesLen, ValueToPtr(srcAddr), srcLen);
    1386            7 :         if (cpyRet != EOK) {
    1387            1 :             cmdRet = static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
    1388            1 :             BQS_LOG_ERROR("Memcpy failed, srcLen[%zu], dstLen[%zu] ret=[%d]", srcLen, routesLen, cpyRet);
    1389            1 :             return static_cast<int32_t>(BQS_STATUS_INNER_ERROR);
    1390              :         }
    1391              :     }
    1392            6 :     return static_cast<int32_t>(BQS_STATUS_OK);
    1393              : }
    1394              : 
    1395              : // 由于兼容性问题,该接口废弃
    1396            5 : int32_t DgwClient::WaitConfigEffect(const uint64_t timeout)
    1397              : {
    1398            5 :     BQS_LOG_INFO("[DgwClient] Begin to waitConfigEffect.");
    1399            5 :     const std::lock_guard<std::mutex> lk(mutexForWaitConfig);
    1400              :     // check dgw client initialized
    1401            5 :     if (!initFlag_) {
    1402            1 :         BQS_LOG_ERROR("[DgwClient] please check whether datagw client has initialized successfully.");
    1403            1 :         return static_cast<int32_t>(BQS_STATUS_NOT_INIT);
    1404              :     }
    1405              : 
    1406              :     // 隔1S发送一次事件到SERVER端检测建链是否成功
    1407            4 :     int32_t cmdRet = static_cast<int32_t>(BQS_STATUS_FAILED);
    1408            6 :     for (uint64_t index = 0; index <= timeout; index++) {
    1409            5 :         event_sync_msg syncMsg = {};
    1410            5 :         QsProcMsgRsp procMsgRsp = {};
    1411            5 :         const int32_t ret = SendEventToQsSync(
    1412              :             &syncMsg, sizeof(event_sync_msg), QueueSubEventType::QUERY_LINKSTATUS, procMsgRsp,
    1413              :             static_cast<int32_t>(timeout));
    1414            5 :         if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
    1415            2 :             BQS_LOG_ERROR("[DgwClient] SendEventToQsSync failed ret[%d]", ret);
    1416            3 :             return ret;
    1417              :         }
    1418            3 :         cmdRet = procMsgRsp.retCode;
    1419            3 :         if (cmdRet == static_cast<int32_t>(BQS_STATUS_OK)) {
    1420            1 :             BQS_LOG_INFO("[DgwClient] WaitConfigEffect Success");
    1421            1 :             return static_cast<int32_t>(BQS_STATUS_OK);
    1422              :         } else {
    1423            2 :             (void)sleep(1);
    1424              :         }
    1425              :     }
    1426            1 :     BQS_LOG_ERROR("[DgwClient] WaitConfigEffect Failed");
    1427            1 :     return static_cast<int32_t>(BQS_STATUS_FAILED);
    1428            5 : }
    1429              : 
    1430            8 : int32_t DgwClient::WaitConfigEffect(const int32_t rsv, const int32_t timeout)
    1431              : {
    1432            8 :     BQS_LOG_INFO("[DgwClient] Begin to waitConfigEffect rsv value:%d, timeout:%ds", rsv, timeout);
    1433            8 :     if (rsv != 0) {
    1434            1 :         BQS_LOG_ERROR("[DgwClient] please check rsv value:%d", rsv);
    1435            1 :         return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1436              :     }
    1437            7 :     if (timeout <= 0) {
    1438            1 :         BQS_LOG_ERROR("[DgwClient] please check timeout value:%ds", timeout);
    1439            1 :         return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1440              :     }
    1441            6 :     const std::lock_guard<std::mutex> lk(mutexForWaitConfig);
    1442              :     // check dgw client initialized
    1443            6 :     if (!initFlag_) {
    1444            1 :         BQS_LOG_ERROR("[DgwClient] please check whether datagw client has initialized successfully.");
    1445            1 :         return static_cast<int32_t>(BQS_STATUS_NOT_INIT);
    1446              :     }
    1447              : 
    1448              :     // 隔1S发送一次事件到SERVER端检测建链是否成功
    1449            5 :     int32_t cmdRet = static_cast<int32_t>(BQS_STATUS_FAILED);
    1450           16 :     for (int32_t index = 0; index <= (QUERY_LINK_STATUS_UNIT / QUERY_LINK_STATUS_INTERVAL * timeout); index++) {
    1451           15 :         event_sync_msg syncMsg = {};
    1452           15 :         QsProcMsgRsp procMsgRsp = {};
    1453           15 :         const int32_t ret = SendEventToQsSync(
    1454              :             &syncMsg, sizeof(event_sync_msg), QueueSubEventType::QUERY_LINKSTATUS_V2, procMsgRsp,
    1455              :             static_cast<int32_t>(timeout));
    1456           15 :         if (ret != static_cast<int32_t>(BQS_STATUS_OK)) {
    1457            2 :             BQS_LOG_ERROR("[DgwClient] SendEventToQsSync failed ret[%d]", ret);
    1458            4 :             return ret;
    1459              :         }
    1460           13 :         cmdRet = procMsgRsp.retCode;
    1461           13 :         if (cmdRet == static_cast<int32_t>(BQS_STATUS_OK)) {
    1462            1 :             BQS_LOG_INFO("[DgwClient] WaitConfigEffect Success");
    1463            1 :             return static_cast<int32_t>(BQS_STATUS_OK);
    1464           12 :         } else if (cmdRet == static_cast<int32_t>(BQS_STATUS_PARAM_INVALID)) {
    1465            1 :             BQS_LOG_INFO("[DgwClient] WaitConfigEffect is not supported");
    1466            1 :             return static_cast<int32_t>(BQS_STATUS_NOT_SUPPORT);
    1467              :         } else {
    1468           11 :             (void)usleep(QUERY_LINK_STATUS_INTERVAL);
    1469              :         }
    1470              :     }
    1471            1 :     BQS_LOG_ERROR("[DgwClient] WaitConfigEffect Failed");
    1472            1 :     return static_cast<int32_t>(BQS_STATUS_FAILED);
    1473            6 : }
    1474              : 
    1475            4 : int32_t DgwClient::GetPlatformInfo(const uint32_t deviceId)
    1476              : {
    1477            4 :     BQS_LOG_INFO("[DgwClient] begin to GetPlatformInfo, deviceId=%u", deviceId);
    1478            4 :     int64_t hardwareVersion = 0;
    1479            4 :     const auto drvRet = halGetDeviceInfo(deviceId, MODULE_TYPE_SYSTEM, INFO_TYPE_VERSION, &hardwareVersion);
    1480            4 :     if (drvRet != DRV_ERROR_NONE) {
    1481            1 :         BQS_LOG_ERROR("get device info by halGetDeviceInfo failed, errorCode[%d] deviceId[%u]", drvRet, deviceId);
    1482            1 :         return static_cast<int32_t>(BQS_STATUS_FAILED);
    1483              :     }
    1484            3 :     g_chipType = AICPU_PLAT_GET_CHIP(static_cast<uint64_t>(hardwareVersion));
    1485            3 :     g_hadGetChipType = true;
    1486            3 :     BQS_LOG_INFO("[DgwClient] Get chip type [%u]", static_cast<uint32_t>(g_chipType));
    1487            3 :     return static_cast<int32_t>(BQS_STATUS_OK);
    1488              : }
    1489              : 
    1490           27 : bool DgwClient::IsNumeric(const std::string& str)
    1491              : {
    1492           27 :     if (str.empty()) {
    1493            2 :         return false;
    1494              :     }
    1495           59 :     for (const char c : str) {
    1496           35 :         if (!static_cast<bool>(isdigit(static_cast<unsigned char>(c)))) {
    1497            1 :             return false;
    1498              :         }
    1499              :     }
    1500           24 :     return true;
    1501              : }
    1502              : 
    1503            8 : void DgwClient::SplitString(const std::string& str, std::vector<std::string>& result)
    1504              : {
    1505            8 :     size_t start = 0;
    1506            8 :     size_t end = str.find(',');
    1507              : 
    1508           27 :     while (end != std::string::npos) {
    1509           21 :         std::string substr = str.substr(start, end - start);
    1510           21 :         if (!IsNumeric(substr)) {
    1511            2 :             BQS_LOG_WARN("[DgwClient] invalid device id [%s]", substr.c_str());
    1512            2 :             return;
    1513              :         }
    1514           19 :         result.push_back(substr);
    1515           19 :         start = end + 1U;
    1516           19 :         end = str.find(',', start);
    1517           21 :     }
    1518              : 
    1519            6 :     std::string substr = str.substr(start);
    1520            6 :     if (!IsNumeric(substr)) {
    1521            1 :         BQS_LOG_WARN("[DgwClient] invalid device id [%s]", substr.c_str());
    1522            1 :         return;
    1523              :     }
    1524            5 :     result.push_back(substr);
    1525            6 : }
    1526              : 
    1527           11 : bool DgwClient::GetVisibleDevices()
    1528              : {
    1529              :     // 标记hadGetVisibleDevices表示即将完成ASCEND_RT_VISIBLE_DEVICES解析
    1530           11 :     g_hadGetVisibleDevices = true;
    1531              :     // 获取并校验ASCEND_RT_VISIBLE_DEVICES环境变量配置
    1532           11 :     std::string inputStr;
    1533           11 :     bqs::GetEnvVal("ASCEND_RT_VISIBLE_DEVICES", inputStr);
    1534           11 :     BQS_LOG_INFO("[DgwClient] Get env ASCEND_RT_VISIBLE_DEVICES [%s].", inputStr.c_str());
    1535           11 :     if (inputStr.empty()) {
    1536            2 :         return false;
    1537              :     }
    1538              :     // 清空userDeviceInfo中的内容
    1539            9 :     g_userDeviceInfo.clear();
    1540              :     // 配置解析并校验
    1541            9 :     uint32_t deviceCnt = 0U;
    1542            9 :     const drvError_t drvRet = drvGetDevNum(&deviceCnt);
    1543            9 :     if (drvRet != DRV_ERROR_NONE) {
    1544            1 :         BQS_LOG_ERROR("[DgwClient] get device count failed, errorCode [%d]", drvRet);
    1545            1 :         return true;
    1546              :     }
    1547            8 :     std::vector<std::string> splitInputStr;
    1548            8 :     SplitString(inputStr, splitInputStr);
    1549            8 :     BQS_LOG_INFO("[DgwClient] splitInputStr size [%zu]", splitInputStr.size());
    1550           27 :     for (size_t i = 0U; i < splitInputStr.size(); i++) {
    1551           22 :         uint32_t tmpValue = 0U;
    1552              :         try {
    1553           22 :             tmpValue = static_cast<uint32_t>(std::stoi(splitInputStr[i]));
    1554            1 :         } catch (std::exception& e) {
    1555            1 :             BQS_LOG_ERROR("[DgwClient] splitInputStr [%s] is invalid, error: %s", splitInputStr[i].c_str(), e.what());
    1556            1 :             break;
    1557            1 :         }
    1558           21 :         if (tmpValue >= deviceCnt) {
    1559            1 :             BQS_LOG_ERROR(
    1560              :                 "[DgwClient] splitInputStr [%s] is exceed device count [%u]", splitInputStr[i].c_str(), deviceCnt);
    1561            1 :             break;
    1562              :         }
    1563           20 :         if (std::find(g_userDeviceInfo.begin(), g_userDeviceInfo.end(), tmpValue) != g_userDeviceInfo.end()) {
    1564            1 :             BQS_LOG_ERROR("[DgwClient] splitInputStr [%s] is repeat", splitInputStr[i].c_str());
    1565            1 :             break;
    1566              :         }
    1567           19 :         g_userDeviceInfo.push_back(tmpValue);
    1568              :     }
    1569            8 :     BQS_LOG_INFO("[DgwClient] g_userDeviceInfo size [%zu]", g_userDeviceInfo.size());
    1570            8 :     return true;
    1571           11 : }
    1572              : 
    1573            9 : int32_t DgwClient::ChangeUserDeviceIdToLogicDeviceId(const uint32_t userDevId, uint32_t& logicDevId)
    1574              : {
    1575            9 :     BQS_LOG_INFO("[DgwClient] begin to change user deviceId to logic deviceId, user deviceId=%u", userDevId);
    1576              :     // 先判断是不是有内容,避免重复解析,再获取环境变量、解析和校验
    1577            9 :     if (!g_hadGetVisibleDevices && !GetVisibleDevices()) {
    1578            0 :         return static_cast<int32_t>(BQS_STATUS_OK);
    1579              :     }
    1580              : 
    1581              :     // user device id匹配logic id
    1582            9 :     if (g_userDeviceInfo.empty()) {
    1583            4 :         return static_cast<int32_t>(BQS_STATUS_OK);
    1584            5 :     } else if (userDevId >= g_userDeviceInfo.size()) {
    1585            1 :         BQS_LOG_ERROR(
    1586              :             "[DgwClient] userDevId [%u] is exceed g_userDeviceInfo size [%zu]", userDevId, g_userDeviceInfo.size());
    1587            1 :         return static_cast<int32_t>(BQS_STATUS_PARAM_INVALID);
    1588              :     } else {
    1589            4 :         logicDevId = g_userDeviceInfo[userDevId];
    1590            4 :         BQS_LOG_INFO("[DgwClient] userDevId [%u] to logicDevId [%u]", userDevId, logicDevId);
    1591              :     }
    1592            4 :     return static_cast<int32_t>(BQS_STATUS_OK);
    1593              : }
    1594              : } // namespace bqs
        

Generated by: LCOV version 2.0-1