LCOV - code coverage report
Current view: top level - adcore/commopts - adx_comm_opt_manager.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 99.3 % 137 136
Test Date: 2026-08-12 11:04:53 Functions: 92.9 % 14 13

            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              : #include "adx_comm_opt_manager.h"
      11              : #include "log/adx_log.h"
      12              : namespace Adx {
      13            1 : AdxCommOptManager::~AdxCommOptManager()
      14              : {
      15            1 :     std::unique_lock<std::mutex> lock{commOptMapMtx_};
      16            1 :     commOptMap_.clear();
      17            1 : }
      18              : 
      19          100 : bool AdxCommOptManager::CommOptsRegister(std::unique_ptr<AdxCommOpt>& opt)
      20              : {
      21          100 :     if (opt == nullptr) {
      22            2 :         return false;
      23              :     }
      24              : 
      25           98 :     std::unique_lock<std::mutex> lock{commOptMapMtx_};
      26           98 :     if (commOptMap_.find(opt->GetOptType()) == commOptMap_.end()) {
      27            4 :         commOptMap_[opt->GetOptType()] = std::move(opt);
      28              :     }
      29           98 :     return true;
      30           98 : }
      31              : 
      32           32 : CommHandle AdxCommOptManager::OpenServer(OptType type, const std::map<std::string, std::string>& info)
      33              : {
      34           32 :     std::shared_ptr<AdxCommOpt> currCommOpt(nullptr);
      35              :     {
      36           32 :         std::unique_lock<std::mutex> lock{commOptMapMtx_};
      37           32 :         auto it = commOptMap_.find(type);
      38           32 :         if (it != commOptMap_.end()) {
      39           30 :             currCommOpt = it->second;
      40              :         }
      41           32 :     }
      42           32 :     if (currCommOpt != nullptr) {
      43           30 :         OptHandle opHandle = currCommOpt->OpenServer(info);
      44           30 :         return {type, opHandle, NR_COMPONENTS, -1, nullptr};
      45              :     }
      46              : 
      47            2 :     IDE_LOGW("commopt(%d) not registered", static_cast<int32_t>(type));
      48            2 :     return ADX_COMMOPT_INVALID_HANDLE(type);
      49           32 : }
      50              : 
      51           27 : int32_t AdxCommOptManager::CloseServer(const CommHandle& handle) const
      52              : {
      53           27 :     std::shared_ptr<AdxCommOpt> currCommOpt(nullptr);
      54              :     {
      55           27 :         std::unique_lock<std::mutex> lock{commOptMapMtx_};
      56           27 :         auto it = commOptMap_.find(handle.type);
      57           27 :         if (it != commOptMap_.end()) {
      58           25 :             currCommOpt = it->second;
      59              :         }
      60           27 :     }
      61           27 :     if (currCommOpt != nullptr) {
      62           25 :         return currCommOpt->CloseServer(handle.session);
      63              :     }
      64            2 :     return IDE_DAEMON_OK;
      65           27 : }
      66              : 
      67           36 : CommHandle AdxCommOptManager::OpenClient(OptType type, const std::map<std::string, std::string>& info)
      68              : {
      69           36 :     std::shared_ptr<AdxCommOpt> currCommOpt(nullptr);
      70              :     {
      71           36 :         std::unique_lock<std::mutex> lock{commOptMapMtx_};
      72           36 :         auto it = commOptMap_.find(type);
      73           36 :         if (it != commOptMap_.end()) {
      74           34 :             currCommOpt = it->second;
      75              :         }
      76           36 :     }
      77           36 :     if (currCommOpt != nullptr) {
      78           34 :         OptHandle opHandle = currCommOpt->OpenClient(info);
      79           34 :         return {type, opHandle, NR_COMPONENTS, -1, nullptr};
      80              :     }
      81              : 
      82            2 :     return ADX_COMMOPT_INVALID_HANDLE(type);
      83           36 : }
      84              : 
      85           31 : int32_t AdxCommOptManager::CloseClient(CommHandle& handle) const
      86              : {
      87           31 :     std::shared_ptr<AdxCommOpt> currCommOpt(nullptr);
      88              :     {
      89           31 :         std::unique_lock<std::mutex> lock{commOptMapMtx_};
      90           31 :         auto it = commOptMap_.find(handle.type);
      91           31 :         if (it != commOptMap_.end()) {
      92           29 :             currCommOpt = it->second;
      93              :         }
      94           31 :     }
      95           31 :     if (currCommOpt != nullptr) {
      96           29 :         return currCommOpt->CloseClient(handle.session);
      97              :     }
      98            2 :     return IDE_DAEMON_OK;
      99           31 : }
     100              : 
     101           18 : CommHandle AdxCommOptManager::Accept(const CommHandle& handle) const
     102              : {
     103           18 :     std::shared_ptr<AdxCommOpt> currCommOpt(nullptr);
     104              :     {
     105           18 :         std::unique_lock<std::mutex> lock{commOptMapMtx_};
     106           18 :         auto it = commOptMap_.find(handle.type);
     107           18 :         if (it != commOptMap_.end()) {
     108           16 :             currCommOpt = it->second;
     109              :         }
     110           18 :     }
     111           18 :     if (currCommOpt != nullptr) {
     112           16 :         OptHandle opHandle = currCommOpt->Accept(handle.session);
     113           16 :         return {handle.type, opHandle, NR_COMPONENTS, -1, nullptr};
     114              :     }
     115              : 
     116            2 :     return ADX_COMMOPT_INVALID_HANDLE(handle.type);
     117           18 : }
     118              : 
     119           48 : CommHandle AdxCommOptManager::Connect(const CommHandle& handle, const std::map<std::string, std::string>& info)
     120              : {
     121           48 :     std::shared_ptr<AdxCommOpt> currCommOpt(nullptr);
     122              :     {
     123           48 :         std::unique_lock<std::mutex> lock{commOptMapMtx_};
     124           48 :         auto it = commOptMap_.find(handle.type);
     125           48 :         if (it != commOptMap_.end()) {
     126           46 :             currCommOpt = it->second;
     127              :         }
     128           48 :     }
     129           48 :     if (currCommOpt != nullptr) {
     130           46 :         OptHandle opHandle = currCommOpt->Connect(handle.session, info);
     131           46 :         return {handle.type, opHandle, NR_COMPONENTS, -1, nullptr};
     132              :     }
     133              : 
     134            2 :     return ADX_COMMOPT_INVALID_HANDLE(handle.type);
     135           48 : }
     136              : 
     137           43 : int32_t AdxCommOptManager::Close(CommHandle& handle) const
     138              : {
     139           43 :     std::shared_ptr<AdxCommOpt> currCommOpt(nullptr);
     140              :     {
     141           43 :         std::unique_lock<std::mutex> lock{commOptMapMtx_};
     142           43 :         auto it = commOptMap_.find(handle.type);
     143           43 :         if (it != commOptMap_.end()) {
     144           41 :             currCommOpt = it->second;
     145              :         }
     146           43 :     }
     147           43 :     if (currCommOpt != nullptr) {
     148           41 :         return currCommOpt->Close(handle.session);
     149              :     }
     150            2 :     return IDE_DAEMON_OK;
     151           43 : }
     152              : 
     153           32 : int32_t AdxCommOptManager::Write(const CommHandle& handle, IdeSendBuffT buffer, int32_t length, int32_t flag)
     154              : {
     155           32 :     std::shared_ptr<AdxCommOpt> currCommOpt(nullptr);
     156              :     {
     157           32 :         std::unique_lock<std::mutex> lock{commOptMapMtx_};
     158           32 :         auto it = commOptMap_.find(handle.type);
     159           32 :         if (it != commOptMap_.end()) {
     160           30 :             currCommOpt = it->second;
     161              :         }
     162           32 :     }
     163           32 :     if (currCommOpt != nullptr) {
     164           30 :         return currCommOpt->Write(handle.session, buffer, length, flag);
     165              :     }
     166              : 
     167            2 :     return -1;
     168           32 : }
     169              : 
     170           61 : int32_t AdxCommOptManager::Read(const CommHandle& handle, IdeRecvBuffT buffer, int32_t& length, int32_t flag)
     171              : {
     172           61 :     std::shared_ptr<AdxCommOpt> currCommOpt(nullptr);
     173              :     {
     174           61 :         std::unique_lock<std::mutex> lock{commOptMapMtx_};
     175           61 :         auto it = commOptMap_.find(handle.type);
     176           61 :         if (it != commOptMap_.end()) {
     177           59 :             currCommOpt = it->second;
     178              :         }
     179           61 :     }
     180           61 :     if (currCommOpt != nullptr) {
     181           59 :         return currCommOpt->Read(handle.session, buffer, length, flag);
     182              :     }
     183              : 
     184            2 :     return -1;
     185           61 : }
     186              : 
     187           10 : SharedPtr<AdxDevice> AdxCommOptManager::GetDevice(OptType type)
     188              : {
     189           10 :     std::shared_ptr<AdxCommOpt> currCommOpt(nullptr);
     190              :     {
     191           10 :         std::unique_lock<std::mutex> lock{commOptMapMtx_};
     192           10 :         auto it = commOptMap_.find(type);
     193           10 :         if (it != commOptMap_.end()) {
     194           10 :             currCommOpt = it->second;
     195              :         }
     196           10 :     }
     197           10 :     if (currCommOpt != nullptr) {
     198           10 :         return currCommOpt->GetDevice();
     199              :     }
     200              : 
     201            0 :     return nullptr;
     202           10 : }
     203              : 
     204           14 : void AdxCommOptManager::Timer(OptType type) const
     205              : {
     206           14 :     std::shared_ptr<AdxCommOpt> currCommOpt(nullptr);
     207              :     {
     208           14 :         std::unique_lock<std::mutex> lock{commOptMapMtx_};
     209           14 :         auto it = commOptMap_.find(type);
     210           14 :         if (it != commOptMap_.end()) {
     211            9 :             currCommOpt = it->second;
     212              :         }
     213           14 :     }
     214           14 :     if (currCommOpt != nullptr) {
     215            9 :         currCommOpt->Timer();
     216              :     }
     217           14 : }
     218              : } // namespace Adx
        

Generated by: LCOV version 2.0-1