1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "accessibility_element_operator_async_mng.h"
17
18 #include <sys/time.h>
19 #include <unistd.h>
20 #include "hilog_wrapper.h"
21
22 namespace OHOS {
23 namespace Accessibility {
24 std::recursive_mutex AccessibilityElementAsyncOperatorMng::mutex_ = {};
25 int AccessibilityElementAsyncOperatorMng::sequence_ = 0;
26
SearchElementResultTimer(const int sequence)27 bool AccessibilityElementAsyncOperatorMng::SearchElementResultTimer(const int sequence)
28 {
29 std::lock_guard<std::recursive_mutex> lock(mutex_);
30 struct timeval getTime {};
31 gettimeofday(&getTime, NULL);
32 uint64_t startTime = (uint64_t)(getTime.tv_sec * SECOND_TO_MILLIS + getTime.tv_usec);
33 HILOG_DEBUG("element sequence[%{public}d]", sequence);
34
35 do {
36 if (feedbackSequence_ == sequence) {
37 return true;
38 }
39 gettimeofday(&getTime, NULL);
40 uint64_t endTime = (uint64_t)(getTime.tv_sec * SECOND_TO_MILLIS + getTime.tv_usec);
41 uint64_t waitTime = (uint64_t)(endTime - startTime);
42 if (TIMEOUT_OPERATOR_MILLIS < waitTime) {
43 completed_ = true;
44 HILOG_ERROR("Failed to wait sequence[%{public}d], feedbackSequence_[%{public}d]",
45 sequence, feedbackSequence_);
46 return false;
47 }
48 usleep(SLEEP_MILLIS);
49 } while (true);
50 HILOG_DEBUG("Response [sequence:%{public}d] end", sequence);
51 return true;
52 }
53
UpdateSearchFeedback(int feedbackSequence)54 void AccessibilityElementAsyncOperatorMng::UpdateSearchFeedback(int feedbackSequence)
55 {
56 feedbackSequence_ = feedbackSequence;
57 }
58
SetOperationStatus(bool status)59 void AccessibilityElementAsyncOperatorMng::SetOperationStatus(bool status)
60 {
61 completed_ = status;
62 }
63
GetOperationStatus()64 bool AccessibilityElementAsyncOperatorMng::GetOperationStatus()
65 {
66 HILOG_DEBUG("[completed_:%{public}d]", completed_);
67 return completed_;
68 }
69
RecordSearchSequence()70 int AccessibilityElementAsyncOperatorMng::RecordSearchSequence()
71 {
72 std::lock_guard<std::recursive_mutex> lock(mutex_);
73 HILOG_INFO("[sequence_:%{public}d]", sequence_);
74 sequence_++;
75 sequence_ = sequence_ % MAX_REQUEST;
76 HILOG_INFO("[sequence_:%{public}d] end", sequence_);
77 return sequence_;
78 }
79 } // namespace Accessibility
80 } // namespace OHOS