LCOV - code coverage report
Current view: top level - basic_component/device_comm - device_comm.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 12 12
Test Date: 2026-07-28 10:52:48 Functions: 83.3 % 6 5

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2026 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #ifndef TSD_BASIC_COMPONENT_DEVICE_COMM_DEVICE_COMM_H
      12              : #define TSD_BASIC_COMPONENT_DEVICE_COMM_DEVICE_COMM_H
      13              : 
      14              : #include <map>
      15              : #include <memory>
      16              : #include <mutex>
      17              : #include <functional>
      18              : #include <unordered_map>
      19              : 
      20              : #include "proto/tsd_message.pb.h"
      21              : #include "tsd/status.h"
      22              : #include "tsd_version_verify.h"
      23              : 
      24              : namespace tsd {
      25              : /**
      26              :  * @ingroup DeviceComm
      27              :  * @brief 设备通信类型枚举,用于 DeviceComm 工厂模式实例化
      28              :  */
      29              : enum class DeviceCommType : uint32_t { HDC = 0, DEVICE_COMM_TYPE_TOTAL_NUM };
      30              : 
      31          147 : inline uint64_t KeyCompose(const uint32_t& devId, const DeviceCommType& type)
      32              : {
      33          147 :     return ((static_cast<uint64_t>(devId)) << 32U) | (static_cast<uint64_t>(type));
      34              : }
      35              : 
      36              : /**
      37              :  * @ingroup DeviceComm
      38              :  * @brief 设备通信基类,对外提供与device建立连接、通信的统一接口。
      39              :  *        外部模块只依赖此基类,具体连接方式由派生类实现。
      40              :  */
      41              : class DeviceComm {
      42              : public:
      43              :     // 工厂函数类型:接收 devId,返回具体子类实例
      44              :     using CreatorFunc = std::function<std::shared_ptr<DeviceComm>(uint32_t)>;
      45              : 
      46              :     // 注册接口,供各子类在静态初始化阶段调用
      47              :     static bool Register(DeviceCommType type, CreatorFunc creator);
      48              :     /**
      49              :      * @ingroup DeviceComm
      50              :      * @brief 根据devId和commType获得单例类实例,内部根据commType进行工厂模式生成
      51              :      * @param [in] devId : 设备device ID
      52              :      * @param [in] commType : 设备通信类型
      53              :      * @return DeviceComm单例类实例
      54              :      */
      55              :     static std::shared_ptr<DeviceComm> GetInstance(const uint32_t devId, const DeviceCommType commType);
      56              : 
      57              :     /**
      58              :      * @ingroup DeviceComm
      59              :      * @brief 初始化与device的连接通道
      60              :      * @return TSD_OK:成功,或者其他错误码
      61              :      */
      62              :     virtual TSD_StatusT CommInit(const uint32_t clientPid, const bool isAdcEnv) = 0;
      63              : 
      64              :     /**
      65              :      * @ingroup DeviceComm
      66              :      * @brief 创建会话
      67              :      * @param [out] sessionId : 会话ID
      68              :      * @return TSD_OK:成功 或者其他错误码
      69              :      */
      70              :     virtual TSD_StatusT CommCreateSession(uint32_t& sessionId) = 0;
      71              : 
      72              :     /**
      73              :      * @ingroup DeviceComm
      74              :      * @brief 关闭与device的连接,释放相关资源
      75              :      */
      76              :     virtual void CommDestroy() = 0;
      77              : 
      78              :     /**
      79              :      * @ingroup DeviceComm
      80              :      * @brief 接收device发送过来的数据
      81              :      * @param [in] sessionId : 某个连接sessionId
      82              :      * @param [in] ignoreRecvErr : 是否忽略接收错误
      83              :      * @param [in] timeout : 超时时间(毫秒)
      84              :      * @return TSD_OK:成功 或者其他错误码
      85              :      */
      86              :     virtual TSD_StatusT CommRecvData(
      87              :         const uint32_t sessionId, const bool ignoreRecvErr = false, const uint32_t timeout = 0U) = 0;
      88              : 
      89              :     /**
      90              :      * @ingroup DeviceComm
      91              :      * @brief 获取设备连接状态
      92              :      * @param [out] sessStat : 会话连接状态
      93              :      * @return TSD_OK:成功 或者其他错误码
      94              :      */
      95              :     virtual TSD_StatusT CommGetConctStatus(int32_t& sessStat) = 0;
      96              : 
      97              :     /**
      98              :      * @ingroup DeviceComm
      99              :      * @brief 向device发送消息
     100              :      * @param [in] sessionId : 会话ID
     101              :      * @param [in] msg : 待发送消息
     102              :      * @return TSD_OK:成功 或者其他错误码
     103              :      */
     104              :     virtual TSD_StatusT CommSendMsg(const uint32_t sessionId, const HDCMessage& msg) = 0;
     105              : 
     106              :     /**
     107              :      * @ingroup DeviceComm
     108              :      * @brief 获取版本校验信息
     109              :      * @param [in] sessionId : 会话ID
     110              :      * @param [out] inspector : 版本校验对象
     111              :      * @return TSD_OK:成功 或者其他错误码
     112              :      */
     113              :     virtual TSD_StatusT CommGetVersionVerify(const uint32_t sessionId, std::shared_ptr<VersionVerify>& inspector) = 0;
     114              : 
     115              :     /**
     116              :      * @ingroup DeviceComm
     117              :      * @brief 析构函数
     118              :      */
     119          102 :     virtual ~DeviceComm() = default;
     120              : 
     121              :     DeviceComm(const DeviceComm&) = delete;
     122              :     DeviceComm(DeviceComm&&) = delete;
     123              :     DeviceComm& operator=(const DeviceComm&) = delete;
     124              :     DeviceComm& operator=(DeviceComm&&) = delete;
     125              : 
     126              : protected:
     127              :     /**
     128              :      * @ingroup DeviceComm
     129              :      * @brief 构造函数
     130              :      * @param [in] devId : 设备device ID
     131              :      * @param [in] commType : 设备通信类型
     132              :      */
     133              :     DeviceComm(const uint32_t devId, const DeviceCommType commType);
     134              : 
     135              :     // deviceId 和 DeviceComm 指针对象的 Map
     136          143 :     static std::map<uint64_t, std::shared_ptr<DeviceComm>>* DeviceCommMap()
     137              :     {
     138              :         static std::map<uint64_t, std::shared_ptr<DeviceComm>>* instance =
     139          143 :             new (std::nothrow) std::map<uint64_t, std::shared_ptr<DeviceComm>>();
     140          143 :         return instance;
     141              :     }
     142              :     // deviceCommMap_ 的锁
     143          143 :     static std::recursive_mutex* MutexForDeviceCommMap()
     144              :     {
     145          143 :         static std::recursive_mutex* instance = new (std::nothrow) std::recursive_mutex();
     146          143 :         return instance;
     147              :     }
     148              :     // 注册表:DeviceCommType -> 工厂函数
     149          110 :     static std::unordered_map<uint32_t, CreatorFunc>* CreatorMap()
     150              :     {
     151              :         static std::unordered_map<uint32_t, CreatorFunc>* instance =
     152          110 :             new (std::nothrow) std::unordered_map<uint32_t, CreatorFunc>();
     153          110 :         return instance;
     154              :     }
     155              :     // 设备 ID
     156              :     uint32_t deviceId_;
     157              :     // 设备通信类型
     158              :     DeviceCommType commType_;
     159              : };
     160              : } // namespace tsd
     161              : #endif // TSD_BASIC_COMPONENT_DEVICE_COMM_DEVICE_COMM_H
        

Generated by: LCOV version 2.0-1