1 /*
2  * Copyright (c) 2023 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 "system_operation.h"
17 
18 #include <fcntl.h>
19 #include <file_ex.h>
20 
21 #include "hdf_base.h"
22 #include "unique_fd.h"
23 
24 namespace OHOS {
25 namespace HDI {
26 namespace Power {
27 namespace V1_2 {
28 static constexpr const char * const WAKE_LOCK_PATH = "/sys/power/wake_lock";
29 static constexpr const char * const WAKE_UNLOCK_PATH = "/sys/power/wake_unlock";
30 
WriteWakeLock(const std::string & name)31 int32_t SystemOperation::WriteWakeLock(const std::string &name)
32 {
33     if (name.empty()) {
34         return HDF_ERR_INVALID_PARAM;
35     }
36     UniqueFd fd(TEMP_FAILURE_RETRY(open(WAKE_LOCK_PATH, O_RDWR | O_CLOEXEC)));
37     bool ret = SaveStringToFd(fd, name);
38     if (!ret) {
39         return HDF_FAILURE;
40     }
41     return HDF_SUCCESS;
42 }
43 
WriteWakeUnlock(const std::string & name)44 int32_t SystemOperation::WriteWakeUnlock(const std::string &name)
45 {
46     if (name.empty()) {
47         return HDF_ERR_INVALID_PARAM;
48     }
49     UniqueFd fd(TEMP_FAILURE_RETRY(open(WAKE_UNLOCK_PATH, O_RDWR | O_CLOEXEC)));
50     bool ret = SaveStringToFd(fd, name);
51     if (!ret) {
52         return HDF_FAILURE;
53     }
54     return HDF_SUCCESS;
55 }
56 } // namespace V1_2
57 } // namespace Power
58 } // namespace HDI
59 } // namespace OHOS
60