1 /*
2 * Copyright (c) 2021 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 <stdint.h>
17
18 #include <iunknown.h>
19 #include <pthread.h>
20 #include <samgr_lite.h>
21
22 #include "hilog_wrapper.h"
23 #include "power_manage_interface.h"
24 #include "power_mgr.h"
25
26 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
27 static PowerManageInterface *g_intf = NULL;
28
GetPowerManageInterface(void)29 static PowerManageInterface *GetPowerManageInterface(void)
30 {
31 if (g_intf != NULL) {
32 return g_intf;
33 }
34 pthread_mutex_lock(&g_mutex);
35 if (g_intf != NULL) {
36 pthread_mutex_unlock(&g_mutex);
37 return g_intf;
38 }
39 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(POWER_MANAGE_SERVICE, POWER_MANAGE_FEATURE);
40 if (iUnknown == NULL) {
41 POWER_HILOGE("Failed to get power manage iUnknown");
42 pthread_mutex_unlock(&g_mutex);
43 return NULL;
44 }
45
46 int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_intf);
47 if ((ret != EC_SUCCESS) || (g_intf == NULL)) {
48 POWER_HILOGE("Failed to query power manage interface");
49 pthread_mutex_unlock(&g_mutex);
50 return NULL;
51 }
52 pthread_mutex_unlock(&g_mutex);
53
54 return g_intf;
55 }
56
InitIdentity(RunningLockEntry * entry)57 void InitIdentity(RunningLockEntry *entry)
58 {
59 if (entry == NULL) {
60 return;
61 }
62 entry->identity.pid = 0;
63 entry->identity.token = (uint64_t)(uintptr_t)entry;
64 }
65
AcquireRunningLockEntry(RunningLockEntry * entry,int32_t timeoutMs)66 BOOL AcquireRunningLockEntry(RunningLockEntry *entry, int32_t timeoutMs)
67 {
68 int32_t ret = EC_FAILURE;
69 PowerManageInterface *intf = GetPowerManageInterface();
70 if ((intf != NULL) && (intf->AcquireRunningLockEntryFunc != NULL)) {
71 ret = intf->AcquireRunningLockEntryFunc((IUnknown *)intf, entry, timeoutMs);
72 }
73 return (ret == EC_SUCCESS) ? TRUE : FALSE;
74 }
75
ReleaseRunningLockEntry(RunningLockEntry * entry)76 BOOL ReleaseRunningLockEntry(RunningLockEntry *entry)
77 {
78 int32_t ret = EC_FAILURE;
79 PowerManageInterface *intf = GetPowerManageInterface();
80 if ((intf != NULL) && (intf->ReleaseRunningLockEntryFunc != NULL)) {
81 ret = intf->ReleaseRunningLockEntryFunc((IUnknown *)intf, entry);
82 }
83 return (ret == EC_SUCCESS) ? TRUE : FALSE;
84 }
85
IsAnyRunningLockHolding()86 BOOL IsAnyRunningLockHolding()
87 {
88 BOOL ret = FALSE;
89 PowerManageInterface *intf = GetPowerManageInterface();
90 if ((intf != NULL) && (intf->IsAnyRunningLockHoldingFunc != NULL)) {
91 ret = intf->IsAnyRunningLockHoldingFunc((IUnknown *)intf);
92 }
93 return ret;
94 }
95
SuspendDevice(SuspendDeviceType reason,BOOL suspendImmed)96 void SuspendDevice(SuspendDeviceType reason, BOOL suspendImmed)
97 {
98 PowerManageInterface *intf = GetPowerManageInterface();
99 if ((intf != NULL) && (intf->SuspendDeviceFunc != NULL)) {
100 intf->SuspendDeviceFunc((IUnknown *)intf, reason, suspendImmed);
101 }
102 }
103
WakeupDevice(WakeupDeviceType reason,const char * details)104 void WakeupDevice(WakeupDeviceType reason, const char* details)
105 {
106 const char* detailReason = (details != NULL) ? details : "No details";
107 PowerManageInterface *intf = GetPowerManageInterface();
108 if ((intf != NULL) && (intf->WakeupDeviceFunc != NULL)) {
109 intf->WakeupDeviceFunc((IUnknown *)intf, reason, detailReason);
110 }
111 }
112