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 :
11 : #include "server_register.h"
12 : #include "memory_utils.h"
13 : #include "log/adx_log.h"
14 : using namespace Adx;
15 : namespace Adx {
16 6 : int32_t ServerRegister::RegisterComponent(int32_t serverType, std::unique_ptr<AdxComponent> &adxComponent)
17 : {
18 6 : std::lock_guard<std::mutex> lk(mtx_);
19 12 : return services_[serverType].ComponentAdd(adxComponent) ? IDE_DAEMON_OK : IDE_DAEMON_ERROR;
20 6 : }
21 :
22 3 : int32_t ServerRegister::UnRegisterComponent(int32_t serverType, ComponentType cmpt)
23 : {
24 3 : std::lock_guard<std::mutex> lk(mtx_);
25 6 : return services_[serverType].ComponentErase(cmpt) ? IDE_DAEMON_OK : IDE_DAEMON_ERROR;
26 3 : }
27 :
28 3 : int32_t ServerRegister::ComponentServerStartup(ServerInitInfo info) const
29 : {
30 3 : return ServerRegister::Instance().ServerManagerInit(info) ? IDE_DAEMON_OK : IDE_DAEMON_ERROR;
31 : }
32 :
33 1 : int32_t ServerRegister::ComponentServerCleanup(int32_t serverType)
34 : {
35 1 : std::lock_guard<std::mutex> lk(mtx_);
36 1 : (void)services_.erase(serverType);
37 2 : return (services_.count(serverType) == 0) ? IDE_DAEMON_OK : IDE_DAEMON_ERROR;
38 1 : }
39 :
40 4 : bool ServerRegister::ServerManagerInit(const ServerInitInfo info)
41 : {
42 4 : drvHdcServiceType type = static_cast<drvHdcServiceType>(info.serverType);
43 4 : auto it = services_.find(type);
44 4 : if (it == services_.end()) {
45 2 : IDE_LOGE("Find server failed, serverType:%d", static_cast<int32_t>(type));
46 2 : return false;
47 : }
48 2 : std::unique_ptr<AdxEpoll> epoll(CreateAdxEpoll(EpollType::EPOLL_HDC));
49 2 : IDE_CTRL_VALUE_FAILED(epoll != nullptr, return false, "create epoll error");
50 2 : bool ret = it->second.RegisterEpoll(epoll);
51 2 : IDE_CTRL_VALUE_FAILED(ret, return false, "register epoll error");
52 1 : std::unique_ptr<AdxCommOpt> opt(CreateAdxCommOpt(OptType::COMM_HDC));
53 1 : IDE_CTRL_VALUE_FAILED(opt != nullptr, return false, "create commopt error");
54 1 : ret = it->second.RegisterCommOpt(opt, std::to_string(type));
55 1 : IDE_CTRL_VALUE_FAILED(ret, return false, "register commopt error");
56 1 : ret = it->second.ComponentInit();
57 1 : IDE_CTRL_VALUE_FAILED(ret, return false, "component init error");
58 1 : it->second.SetMode(info.mode);
59 1 : it->second.SetDeviceId(info.deviceId);
60 2 : it->second.SetThreadName(std::string("adx_get_file_thread"));
61 1 : it->second.Start();
62 1 : (void)it->second.Join();
63 1 : return true;
64 2 : }
65 :
66 1 : static IdeThreadArg AdxServerProcess(const IdeThreadArg arg)
67 : {
68 1 : if (arg == nullptr) {
69 0 : return nullptr;
70 : }
71 1 : ServerInitInfo info = *static_cast<ServerInitInfo *>(arg);
72 1 : int32_t ret = ServerRegister::Instance().ComponentServerStartup(info);
73 1 : IDE_CTRL_VALUE_FAILED(ret == IDE_DAEMON_OK, return nullptr, "server process failed");
74 0 : return nullptr;
75 : }
76 : }
77 :
78 5 : int32_t AdxRegisterComponentFunc(drvHdcServiceType serverType, std::unique_ptr<AdxComponent> &adxComponent)
79 : {
80 5 : IDE_LOGI("Start to register, serverType:%d, componentType:%d", static_cast<int32_t>(serverType),
81 : static_cast<int32_t>(adxComponent->GetType()));
82 5 : return ServerRegister::Instance().RegisterComponent(serverType, adxComponent);
83 : }
84 :
85 4 : int32_t AdxComponentServerStartup(ServerInitInfo info)
86 : {
87 4 : int32_t serverType = static_cast<int32_t>(info.serverType);
88 4 : IDE_LOGI("Start to startup, serverType:%d", serverType);
89 : mmUserBlock_t funcBlock;
90 : #ifdef TINY_COMPILE
91 : static ServerInitInfo serverInfo{0, 0, 0};
92 : serverInfo = info;
93 : funcBlock.pulArg = static_cast<IdeThreadArg>(&serverInfo);
94 : #else
95 5 : static std::map<int32_t, ServerInitInfo> serverInfos;
96 4 : serverInfos[serverType] = info;
97 4 : funcBlock.pulArg = static_cast<IdeThreadArg>(&serverInfos[serverType]);
98 : #endif
99 4 : funcBlock.procFunc = AdxServerProcess;
100 4 : mmThread tid = 0;
101 4 : int32_t ret = Thread::CreateDetachTaskWithDefaultAttr(tid, funcBlock);
102 8 : return (ret != EN_OK) ? IDE_DAEMON_ERROR : IDE_DAEMON_OK;
103 : }
104 :
105 5 : int32_t AdxComponentServerCleanup(drvHdcServiceType serverType, ComponentType cmpt)
106 : {
107 5 : IDE_LOGI("Start to cleanup, serverType:%d, componentType:%d", static_cast<int32_t>(serverType),
108 : static_cast<int32_t>(cmpt));
109 5 : if (cmpt == ComponentType::NR_COMPONENTS) {
110 2 : return ServerRegister::Instance().ComponentServerCleanup(serverType);
111 : }
112 3 : return ServerRegister::Instance().UnRegisterComponent(serverType, cmpt);
113 : }
|