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 17 : int32_t AdxHdcEpoll::EpollCreate(const int32_t size)
21 : {
22 17 : 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 16 : if (epEvent_ == nullptr) {
28 16 : epEvent_ = (struct drvHdcEvent *)ADX_SAFE_CALLOC(size, sizeof(struct drvHdcEvent));
29 16 : 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 16 : epMaxSize_ = size;
39 16 : drvError_t ret = drvHdcEpollCreate(size, &ep_);
40 16 : 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 15 : 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 16 : int32_t AdxHdcEpoll::EpollDestroy()
54 : {
55 16 : ADX_SAFE_FREE(epEvent_);
56 16 : if (ep_ != nullptr) {
57 15 : IDE_LOGI("start to close hdc epoll");
58 15 : drvError_t ret = drvHdcEpollClose(ep_);
59 15 : if (ret != DRV_ERROR_NONE) {
60 1 : IDE_LOGE("hdc epoll close error(%d)", ret);
61 1 : return IDE_DAEMON_ERROR;
62 : }
63 14 : ep_ = nullptr;
64 : }
65 15 : 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 36 : int32_t AdxHdcEpoll::EpollCtl(EpollHandle handle, EpollEvent &event, int32_t op)
77 : {
78 : int32_t ret;
79 36 : 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 34 : HDC_SESSION target = (HDC_SESSION)handle;
85 34 : 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 34 : epEvent.events = EpollEventToHdcEvent(event.events);
92 34 : epEvent.data = (uintptr_t)event.data;
93 :
94 34 : ret = drvHdcEpollCtl(ep_, op, target, &epEvent);
95 34 : if (ret != DRV_ERROR_NONE) {
96 3 : IDE_LOGE("hdc epoll ctl process error %d", ret);
97 3 : return IDE_DAEMON_ERROR;
98 : }
99 31 : 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 15 : int32_t AdxHdcEpoll::EpollAdd(EpollHandle target, EpollEvent &event)
110 : {
111 15 : 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 19 : int32_t AdxHdcEpoll::EpollDel(EpollHandle target, EpollEvent &event)
122 : {
123 19 : 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()
164 : {
165 10 : return epMaxSize_;
166 : }
167 :
168 34 : uint32_t AdxHdcEpoll::EpollEventToHdcEvent(uint32_t events) const
169 : {
170 34 : uint32_t transed = ((events & ADX_EPOLL_DATA_IN) != 0) ? HDC_EPOLL_DATA_IN : 0;
171 34 : transed |= ((events & ADX_EPOLL_CONN_IN) != 0) ? HDC_EPOLL_CONN_IN : 0;
172 34 : transed |= ((events & ADX_EPOLL_HANG_UP) != 0) ? HDC_EPOLL_SESSION_CLOSE : 0;
173 34 : return transed;
174 : }
175 :
176 27 : uint32_t AdxHdcEpoll::HdcEventToEpollEvent(uint32_t events) const
177 : {
178 27 : uint32_t transed = ((events & HDC_EPOLL_DATA_IN) != 0) ? ADX_EPOLL_DATA_IN : 0;
179 27 : transed |= ((events & HDC_EPOLL_CONN_IN) != 0) ? ADX_EPOLL_CONN_IN : 0;
180 27 : transed |= ((events & HDC_EPOLL_SESSION_CLOSE) != 0) ? ADX_EPOLL_HANG_UP : 0;
181 :
182 27 : if ((events & ~HDC_EPOLL_EVENT_MASK) != 0) {
183 0 : IDE_LOGW("hdc epoll get unrecognised events %u.", events);
184 : }
185 27 : return transed;
186 : }
187 : }
|