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 : #include "device_comm.h"
12 :
13 : #include "tsd_hdc_client.h"
14 : #include "basic_define.h"
15 : #include "tsd_util_func.h"
16 : #include "tsd_log.h"
17 :
18 : namespace tsd {
19 : /**
20 : * @ingroup DeviceComm
21 : * @brief DeviceComm构造函数
22 : */
23 113 : DeviceComm::DeviceComm(const uint32_t devId, const DeviceCommType commType) : deviceId_(devId), commType_(commType) {}
24 :
25 : /**
26 : * @ingroup DeviceComm
27 : * @brief 静态工厂函数,根据 commType 生成具体子类实例并维护单例
28 : * @param [in] devId : 建立连接的Device设备ID
29 : * @param [in] commType : 设备通信类型
30 : * @return DeviceComm单例类实例
31 : */
32 108 : std::shared_ptr<DeviceComm> DeviceComm::GetInstance(const uint32_t devId, const DeviceCommType commType)
33 : {
34 : // 校验Client端,device ID范围[0-128)
35 108 : if (devId >= MAX_DEVNUM_PER_HOST) {
36 3 : TSD_ERROR("deviceId=%u is not supported, not in [0-128]", devId);
37 3 : return nullptr;
38 : }
39 105 : uint64_t curIndex = KeyCompose(devId, commType);
40 105 : std::shared_ptr<DeviceComm> deviceCommPtr = nullptr;
41 : {
42 105 : std::recursive_mutex* deviceCommMutex = MutexForDeviceCommMap();
43 105 : std::map<uint64_t, std::shared_ptr<DeviceComm>>* deviceCommMap = DeviceCommMap();
44 105 : std::unordered_map<uint32_t, CreatorFunc>* creatorMap = CreatorMap();
45 162 : TSD_CHECK(deviceCommMutex != nullptr, nullptr, "Fail to create deviceComm mutex");
46 105 : TSD_CHECK(deviceCommMap != nullptr, nullptr, "Fail to create deviceComm map");
47 105 : TSD_CHECK(creatorMap != nullptr, nullptr, "Fail to create creator map");
48 105 : const std::lock_guard<std::recursive_mutex> lk(*deviceCommMutex);
49 105 : const std::map<uint64_t, std::shared_ptr<DeviceComm>>::iterator iter = deviceCommMap->find(curIndex);
50 105 : if (iter != deviceCommMap->end()) {
51 55 : return iter->second;
52 : }
53 : const std::unordered_map<uint32_t, CreatorFunc>::iterator creatorIter =
54 50 : creatorMap->find(static_cast<uint32_t>(commType));
55 50 : if (creatorIter == creatorMap->end()) {
56 1 : TSD_ERROR("DeviceCommType=%u is not supported", static_cast<uint32_t>(commType));
57 1 : return nullptr;
58 : }
59 49 : deviceCommPtr = creatorIter->second(devId);
60 49 : TSD_CHECK((deviceCommPtr != nullptr), nullptr, "Fail to create deviceCommPtr");
61 48 : deviceCommMap->emplace(curIndex, deviceCommPtr);
62 105 : }
63 48 : return deviceCommPtr;
64 105 : }
65 :
66 5 : bool DeviceComm::Register(DeviceCommType type, CreatorFunc creator)
67 : {
68 5 : std::unordered_map<uint32_t, CreatorFunc>* creatorMap = CreatorMap();
69 5 : TSD_CHECK(creatorMap != nullptr, false, "Fail to create creator map");
70 5 : creatorMap->emplace(static_cast<uint32_t>(type), std::move(creator));
71 5 : return true;
72 : }
73 : } // namespace tsd
|