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_hdc_epoll.h"
11 : #include "log/hdc_log.h"
12 : #include "mmpa_api.h"
13 : #include "extra_config.h"
14 : namespace Adx {
15 : /**
16 : * @brief create epoll object with the size
17 : * @param [in] size: epoll event size
18 : * @return epoll handle
19 : */
20 20 : int32_t AdxHdcEpoll::EpollCreate(const int32_t size)
21 : {
22 20 : if (size <= 0) {
23 1 : IDE_LOGE("hdc epoll create size(%d bytes) input error", size);
24 1 : return IDE_DAEMON_ERROR;
25 : }
26 :
27 19 : if (epEvent_ == nullptr) {
28 19 : epEvent_ = (struct drvHdcEvent*)ADX_SAFE_CALLOC(size, sizeof(struct drvHdcEvent));
29 19 : if (epEvent_ == nullptr) {
30 0 : IDE_LOGE("hdc epoll calloc error.");
31 0 : return IDE_DAEMON_ERROR;
32 : }
33 : } else {
34 0 : IDE_LOGW("hdc epoll has been created.");
35 0 : return IDE_DAEMON_OK;
36 : }
37 :
38 19 : epMaxSize_ = size;
39 19 : drvError_t ret = drvHdcEpollCreate(size, &ep_);
40 19 : if (ret != DRV_ERROR_NONE || ep_ == nullptr) {
41 1 : IDE_LOGE("hdc epoll create error(%d).", ret);
42 1 : ADX_SAFE_FREE(epEvent_);
43 1 : return IDE_DAEMON_ERROR;
44 : }
45 18 : return IDE_DAEMON_OK;
46 : }
47 :
48 : /**
49 : * @brief destroy epoll object
50 : * @param [in] ep: epoll handle
51 : * @return !=0: failure ==0: success
52 : */
53 18 : int32_t AdxHdcEpoll::EpollDestroy()
54 : {
55 18 : ADX_SAFE_FREE(epEvent_);
56 18 : if (ep_ != nullptr) {
57 17 : IDE_LOGI("start to close hdc epoll");
58 17 : drvError_t ret = drvHdcEpollClose(ep_);
59 17 : if (ret != DRV_ERROR_NONE) {
60 1 : IDE_LOGE("hdc epoll close error(%d)", ret);
61 1 : return IDE_DAEMON_ERROR;
62 : }
63 16 : ep_ = nullptr;
64 : }
65 17 : return IDE_DAEMON_OK;
66 : }
67 :
68 : /**
69 : * @brief add epoll event
70 : * @param [in] ep: epoll handle
71 : * @param [in] op: operate
72 : * @param [in] handle: target handle, server handle or session handle
73 : * @param [in] event: epoll event
74 : * @return !=0: failure ==0: success
75 : */
76 44 : int32_t AdxHdcEpoll::EpollCtl(EpollHandle handle, EpollEvent& event, int32_t op)
77 : {
78 : int32_t ret;
79 44 : if (ep_ == nullptr || handle == ADX_INVALID_HANDLE) {
80 2 : IDE_LOGE("hdc epoll ctl input error");
81 2 : return IDE_DAEMON_ERROR;
82 : }
83 :
84 42 : HDC_SESSION target = (HDC_SESSION)handle;
85 42 : if (target == nullptr) {
86 0 : IDE_LOGE("hdc epoll ctl handle error");
87 0 : return IDE_DAEMON_ERROR;
88 : }
89 :
90 : struct drvHdcEvent epEvent;
91 42 : epEvent.events = EpollEventToHdcEvent(event.events);
92 42 : epEvent.data = (uintptr_t)event.data;
93 :
94 42 : ret = drvHdcEpollCtl(ep_, op, target, &epEvent);
95 42 : if (ret != DRV_ERROR_NONE) {
96 6 : IDE_LOGE("hdc epoll ctl process error %d", ret);
97 6 : return IDE_DAEMON_ERROR;
98 : }
99 36 : return IDE_DAEMON_OK;
100 : }
101 :
102 : /**
103 : * @brief add epoll event
104 : * @param [in] ep: epoll handle
105 : * @param [in] target: target handle, server handle or session handle
106 : * @param [in] event: epoll event
107 : * @return !=0: failure ==0: success
108 : */
109 18 : int32_t AdxHdcEpoll::EpollAdd(EpollHandle target, EpollEvent& event)
110 : {
111 18 : return EpollCtl(target, event, HDC_EPOLL_CTL_ADD);
112 : }
113 :
114 : /**
115 : * @brief del epoll event
116 : * @param [in] ep: epoll handle
117 : * @param [in] target: target handle, server handle or session handle
118 : * @param [in] event: epoll event
119 : * @return !=0: failure ==0: success
120 : */
121 24 : int32_t AdxHdcEpoll::EpollDel(EpollHandle target, EpollEvent& event)
122 : {
123 24 : return EpollCtl(target, event, HDC_EPOLL_CTL_DEL);
124 : }
125 :
126 : /**
127 : * @brief hdc epoll wait
128 : * @param [in] ep: epoll handle
129 : * @param [in] event: epoll event
130 : * @param [in] maxEvents: max events num, which epoll wait
131 : * @param [in] timeout: timeout
132 : * @return event num
133 : */
134 11 : int32_t AdxHdcEpoll::EpollWait(std::vector<EpollEvent>& events, int32_t size, int32_t timeout)
135 : {
136 : int32_t ret;
137 11 : if (ep_ == nullptr || size < epMaxSize_) {
138 2 : IDE_LOGW("hdc epoll wait init");
139 2 : return IDE_DAEMON_ERROR;
140 : }
141 :
142 9 : int32_t eventNum = 0;
143 9 : ret = drvHdcEpollWait(ep_, epEvent_, epMaxSize_, timeout, &eventNum);
144 9 : if (ret != DRV_ERROR_NONE) {
145 0 : IDE_LOGE("hdc epoll wait process error");
146 0 : return IDE_DAEMON_ERROR;
147 : }
148 :
149 36 : for (int32_t i = 0; i < eventNum && i < epMaxSize_; i++) {
150 27 : events[i].events = HdcEventToEpollEvent(epEvent_[i].events);
151 27 : events[i].data = epEvent_[i].data;
152 : }
153 9 : return eventNum;
154 : }
155 :
156 19 : int32_t AdxHdcEpoll::EpollErrorHandle()
157 : {
158 19 : IDE_LOGW("process epoll other state");
159 19 : mmSleep(DEFAULT_EPOLL_TIMEOUT);
160 19 : return IDE_DAEMON_OK;
161 : }
162 :
163 10 : int32_t AdxHdcEpoll::EpollGetSize() { return epMaxSize_; }
164 :
165 42 : uint32_t AdxHdcEpoll::EpollEventToHdcEvent(uint32_t events) const
166 : {
167 42 : uint32_t transed = ((events & ADX_EPOLL_DATA_IN) != 0) ? HDC_EPOLL_DATA_IN : 0;
168 42 : transed |= ((events & ADX_EPOLL_CONN_IN) != 0) ? HDC_EPOLL_CONN_IN : 0;
169 42 : transed |= ((events & ADX_EPOLL_HANG_UP) != 0) ? HDC_EPOLL_SESSION_CLOSE : 0;
170 42 : return transed;
171 : }
172 :
173 27 : uint32_t AdxHdcEpoll::HdcEventToEpollEvent(uint32_t events) const
174 : {
175 27 : uint32_t transed = ((events & HDC_EPOLL_DATA_IN) != 0) ? ADX_EPOLL_DATA_IN : 0;
176 27 : transed |= ((events & HDC_EPOLL_CONN_IN) != 0) ? ADX_EPOLL_CONN_IN : 0;
177 27 : transed |= ((events & HDC_EPOLL_SESSION_CLOSE) != 0) ? ADX_EPOLL_HANG_UP : 0;
178 :
179 27 : if ((events & ~HDC_EPOLL_EVENT_MASK) != 0) {
180 0 : IDE_LOGW("hdc epoll get unrecognised events %u.", events);
181 : }
182 27 : return transed;
183 : }
184 : } // namespace Adx
|