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

Generated by: LCOV version 2.0-1