LCOV - code coverage report
Current view: top level - adcore/commopts - sock_comm_opt.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 90.7 % 75 68
Test Date: 2026-07-28 10:54:24 Functions: 84.6 % 13 11

            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 "sock_comm_opt.h"
      11              : #include "sock_api.h"
      12              : #include "log/adx_log.h"
      13              : #include "adx_sock_device.h"
      14              : namespace Adx {
      15              : /**
      16              :  * @brief      get comm opt name
      17              :  *
      18              :  * @return
      19              :  *      "LOCAL_SOCK"
      20              :  */
      21            0 : std::string SockCommOpt::CommOptName()
      22              : {
      23            0 :     return "LOCAL_SOCK";
      24              : }
      25              : 
      26              : /**
      27              :  * @brief      get opt type
      28              :  *
      29              :  * @return
      30              :  *      COMM_LOCAL
      31              :  */
      32            5 : OptType SockCommOpt::GetOptType()
      33              : {
      34            5 :     return OptType::COMM_LOCAL;
      35              : }
      36              : 
      37              : /**
      38              :  * @brief      open server
      39              :  * @param [in] info: map of server info
      40              :  *
      41              :  * @return
      42              :  *      handle: opt handle
      43              :  *      ADX_OPT_INVALID_HANDLE
      44              :  */
      45            6 : OptHandle SockCommOpt::OpenServer(const std::map<std::string, std::string> &info)
      46              : {
      47            6 :     OptHandle handle = ADX_OPT_INVALID_HANDLE;
      48            6 :     if (info.empty()) {
      49            1 :         return handle;
      50              :     }
      51              : 
      52            5 :     auto it = info.find(OPT_SERVICE_KEY);
      53            5 :     if (it == info.end()) {
      54            1 :         return ADX_OPT_INVALID_HANDLE;
      55              :     }
      56            4 :     int32_t fd =  SockServerCreate(it->second);
      57            4 :     if (fd < 0) {
      58            3 :         return handle;
      59              :     }
      60              : 
      61            1 :     handle = static_cast<OptHandle>(fd);
      62            1 :     return handle;
      63              : }
      64              : 
      65              : /**
      66              :  * @brief      close server
      67              :  * @param [in] handle: opt handle
      68              :  *
      69              :  * @return
      70              :  *      IDE_DAEMON_OK: close and destroy server success
      71              :  *      IDE_DAEMON_ERROR: close and destroy server failed
      72              :  */
      73            3 : int32_t SockCommOpt::CloseServer(const OptHandle &handle) const
      74              : {
      75            3 :     if (handle == ADX_OPT_INVALID_HANDLE) {
      76            1 :         return IDE_DAEMON_ERROR;
      77              :     }
      78              : 
      79            2 :     auto fd = static_cast<int32_t>(handle);
      80            2 :     return SockServerDestroy(fd);
      81              : }
      82              : 
      83              : /**
      84              :  * @brief      open client
      85              :  * @param [in] info: map of server info
      86              :  *
      87              :  * @return
      88              :  *      fd: opt handle
      89              :  */
      90            2 : OptHandle SockCommOpt::OpenClient(const std::map<std::string, std::string> &info)
      91              : {
      92              :     UNUSED(info);
      93            2 :     int32_t fd = SockClientCreate();
      94            2 :     return static_cast<OptHandle>(fd);
      95              : }
      96              : 
      97              : /**
      98              :  * @brief      close client
      99              :  * @param [in] handle: opt handle
     100              :  *
     101              :  * @return
     102              :  *      IDE_DAEMON_OK: close and destroy client success
     103              :  *      IDE_DAEMON_ERROR: close and destroy client failed
     104              :  */
     105            2 : int32_t SockCommOpt::CloseClient(OptHandle &handle) const
     106              : {
     107            2 :     if (handle == ADX_OPT_INVALID_HANDLE) {
     108            1 :         return IDE_DAEMON_ERROR;
     109              :     }
     110              : 
     111            1 :     auto fd = static_cast<int32_t>(handle);
     112            1 :     int32_t ret = SockClientDestory(fd);
     113            1 :     handle = ADX_OPT_INVALID_HANDLE;
     114            1 :     return ret;
     115              : }
     116              : 
     117              : /**
     118              :  * @brief      call sock accept
     119              :  * @param [in] handle: opt handle
     120              :  *
     121              :  * @return
     122              :  *      clientFd
     123              :  */
     124            3 : OptHandle SockCommOpt::Accept(const OptHandle handle) const
     125              : {
     126            3 :     if (handle == ADX_OPT_INVALID_HANDLE) {
     127            1 :         return ADX_OPT_INVALID_HANDLE;
     128              :     }
     129              : 
     130            2 :     IDE_LOGI("server %d in accept", static_cast<int32_t>(handle));
     131            2 :     int32_t fd = SockAccept(handle);
     132            2 :     if (fd < 0) {
     133            1 :         return ADX_OPT_INVALID_HANDLE;
     134              :     }
     135            1 :     IDE_LOGI("client %d", fd);
     136            1 :     return static_cast<OptHandle>(fd);
     137              : }
     138              : 
     139              : /**
     140              :  * @brief      sock connect
     141              :  * @param [in] handle: opt handle
     142              :  * @param [in] info: map of server info
     143              :  *
     144              :  * @return
     145              :  *      fd: opt handle
     146              :  *      ADX_OPT_INVALID_HANDLE
     147              :  */
     148            6 : OptHandle SockCommOpt::Connect(const OptHandle handle, const std::map<std::string, std::string> &info)
     149              : {
     150            6 :     if (handle == ADX_OPT_INVALID_HANDLE) {
     151            1 :         return ADX_OPT_INVALID_HANDLE;
     152              :     }
     153              : 
     154            5 :     auto it = info.find(OPT_SERVICE_KEY);
     155            5 :     if (it == info.end()) {
     156            1 :         return ADX_OPT_INVALID_HANDLE;
     157              :     }
     158            4 :     int32_t fd = SockConnect(handle, it->second);
     159            4 :     if (fd < 0) {
     160            2 :         return ADX_OPT_INVALID_HANDLE;
     161              :     }
     162              : 
     163            2 :     return static_cast<OptHandle>(fd);
     164              : }
     165              : 
     166              : /**
     167              :  * @brief      sock close
     168              :  * @param [in] handle: opt handle
     169              :  *
     170              :  * @return
     171              :  *      IDE_DAEMON_OK: close success
     172              :  *      IDE_DAEMON_ERROR: close failed
     173              :  */
     174            3 : int32_t SockCommOpt::Close(OptHandle &handle) const
     175              : {
     176            3 :     if (handle == ADX_OPT_INVALID_HANDLE) {
     177            1 :         return IDE_DAEMON_ERROR;
     178              :     }
     179              : 
     180            2 :     auto fd = static_cast<int32_t>(handle);
     181            2 :     handle = ADX_OPT_INVALID_HANDLE;
     182            2 :     return SockClose(fd);
     183              : }
     184              : 
     185              : /**
     186              :  * @brief      sock write
     187              :  * @param [in] handle: opt handle
     188              :  * @param [in] buffer: write buffer
     189              :  * @param [in] length: write length
     190              :  * @param [in] flag: write flag
     191              :  *
     192              :  * @return
     193              :  *      IDE_DAEMON_OK: sock write success
     194              :  *      IDE_DAEMON_ERROR: sock write failed
     195              :  */
     196           10 : int32_t SockCommOpt::Write(const OptHandle handle, IdeSendBuffT buffer, int32_t length, int32_t flag)
     197              : {
     198           10 :     IDE_CTRL_VALUE_FAILED(handle != ADX_OPT_INVALID_HANDLE, return IDE_DAEMON_ERROR,
     199              :         "sock write input invalid");
     200            6 :     IDE_CTRL_VALUE_FAILED(buffer != nullptr, return IDE_DAEMON_ERROR, "sock write input invalid");
     201            5 :     IDE_CTRL_VALUE_FAILED(length > 0, return IDE_DAEMON_ERROR, "sock write input invalid");
     202              : 
     203            4 :     return SockWrite(handle, buffer, length, flag);
     204              : }
     205              : 
     206              : /**
     207              :  * @brief      sock read
     208              :  * @param [in] handle: opt handle
     209              :  * @param [in] buffer: read buffer
     210              :  * @param [in] length: receive length
     211              :  * @param [in] flag: read flag
     212              :  *
     213              :  * @return
     214              :  *      IDE_DAEMON_OK: sock read success
     215              :  *      IDE_DAEMON_ERROR: sock read failed
     216              :  */
     217            6 : int32_t SockCommOpt::Read(const OptHandle handle, IdeRecvBuffT buffer, int32_t &length, int32_t /* flag */)
     218              : {
     219            6 :     IDE_CTRL_VALUE_FAILED(handle != ADX_OPT_INVALID_HANDLE, return IDE_DAEMON_ERROR,
     220              :         "sock write input invalid");
     221            4 :     IDE_CTRL_VALUE_FAILED(buffer != nullptr, return IDE_DAEMON_ERROR, "sock write input invalid");
     222              : 
     223            3 :     int32_t nonBlockFlag = 0;
     224            3 :     return SockRead(handle, buffer, &length, nonBlockFlag);
     225              : }
     226              : 
     227            1 : SharedPtr<AdxDevice> SockCommOpt::GetDevice()
     228              : {
     229            1 :     if (device_ == nullptr) {
     230              :         try {
     231            1 :             device_ = std::make_shared<AdxSockDevice>();
     232            0 :         } catch (...) {
     233            0 :             return nullptr;
     234            0 :         }
     235              :     }
     236              : 
     237            1 :     return device_;
     238              : }
     239              : 
     240            0 : void SockCommOpt::Timer(void) const
     241              : {
     242            0 : }
     243              : }
        

Generated by: LCOV version 2.0-1