1 /*
2 * Copyright (c) 2024 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 <cinttypes>
17 #include "cj_running_lock_ffi.h"
18 #include "cj_running_lock_impl.h"
19 #include "power_log.h"
20 #include "power_mgr_client.h"
21 #include "running_lock.h"
22 #include "running_lock_info.h"
23
24 using namespace OHOS::FFI;
25
26 namespace OHOS {
27 namespace CJSystemapi {
28 namespace RunningLockFfi {
29 using namespace OHOS::PowerMgr;
30
31 namespace {
32 const constexpr int32_t ERR_OTHER = -1;
33 const constexpr int64_t INVALID_FFIDATA_ID = 0;
34 }
35
36 extern "C" {
FfiOHOSRunningLockHold(int64_t id,int32_t timeout,int32_t * ret)37 void FfiOHOSRunningLockHold(int64_t id, int32_t timeout, int32_t *ret)
38 {
39 auto runningLock = FFI::FFIData::GetData<CJRunningLock>(id)->GetRunningLock();
40 if (runningLock == nullptr) {
41 POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock is nullptr, id=%{public}" PRId64, id);
42 *ret = static_cast<int32_t>(PowerErrors::ERR_PARAM_INVALID);
43 return;
44 }
45 runningLock->Lock(timeout);
46 }
47
FfiOHOSRunningLockIsHolding(int64_t id,int32_t * ret)48 bool FfiOHOSRunningLockIsHolding(int64_t id, int32_t *ret)
49 {
50 auto runningLock = FFI::FFIData::GetData<CJRunningLock>(id)->GetRunningLock();
51 if (runningLock == nullptr) {
52 POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock is nullptr, id=%{public}" PRId64, id);
53 *ret = static_cast<int32_t>(PowerErrors::ERR_PARAM_INVALID);
54 return false;
55 }
56 return runningLock->IsUsed();
57 }
58
FfiOHOSRunningLockUnhold(int64_t id,int32_t * ret)59 void FfiOHOSRunningLockUnhold(int64_t id, int32_t *ret)
60 {
61 auto runningLock = FFI::FFIData::GetData<CJRunningLock>(id)->GetRunningLock();
62 if (runningLock == nullptr) {
63 POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock is nullptr, id=%{public}" PRId64, id);
64 *ret = static_cast<int32_t>(PowerErrors::ERR_PARAM_INVALID);
65 return;
66 }
67 runningLock->UnLock();
68 }
69
FfiOHOSRunningLockIsSupported(int32_t num_type,int32_t * ret)70 bool FfiOHOSRunningLockIsSupported(int32_t num_type, int32_t *ret)
71 {
72 RunningLockType type = RunningLockType(num_type);
73 return (type == RunningLockType::RUNNINGLOCK_BACKGROUND) ||
74 (type == RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
75 }
76
FfiOHOSRunningLockCreate(char * name,int32_t num_type,int32_t * ret)77 int64_t FfiOHOSRunningLockCreate(char *name, int32_t num_type, int32_t *ret)
78 {
79 auto native = FFI::FFIData::Create<CJRunningLock>(name, num_type);
80 if (native == nullptr) {
81 POWER_HILOGE(FEATURE_RUNNING_LOCK, "create ffidata failed, name=%{public}s", name);
82 *ret = ERR_OTHER;
83 return INVALID_FFIDATA_ID;
84 }
85 *ret = static_cast<int32_t>(native->GetError());
86 return native->GetID();
87 }
88 }
89
90 } // namespace RunningLockFfi
91 } // namespace CJSystemapi
92 } // namespace OHOS