1cb69b360Sopenharmony_ci/* 2cb69b360Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3cb69b360Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4cb69b360Sopenharmony_ci * you may not use this file except in compliance with the License. 5cb69b360Sopenharmony_ci * You may obtain a copy of the License at 6cb69b360Sopenharmony_ci * 7cb69b360Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8cb69b360Sopenharmony_ci * 9cb69b360Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10cb69b360Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11cb69b360Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12cb69b360Sopenharmony_ci * See the License for the specific language governing permissions and 13cb69b360Sopenharmony_ci * limitations under the License. 14cb69b360Sopenharmony_ci */ 15cb69b360Sopenharmony_ci 16cb69b360Sopenharmony_ci#include <stdint.h> 17cb69b360Sopenharmony_ci 18cb69b360Sopenharmony_ci#include <iunknown.h> 19cb69b360Sopenharmony_ci#include <pthread.h> 20cb69b360Sopenharmony_ci#include <samgr_lite.h> 21cb69b360Sopenharmony_ci 22cb69b360Sopenharmony_ci#include "hilog_wrapper.h" 23cb69b360Sopenharmony_ci#include "power_manage_interface.h" 24cb69b360Sopenharmony_ci#include "power_mgr.h" 25cb69b360Sopenharmony_ci 26cb69b360Sopenharmony_cistatic pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; 27cb69b360Sopenharmony_cistatic PowerManageInterface *g_intf = NULL; 28cb69b360Sopenharmony_ci 29cb69b360Sopenharmony_cistatic PowerManageInterface *GetPowerManageInterface(void) 30cb69b360Sopenharmony_ci{ 31cb69b360Sopenharmony_ci if (g_intf != NULL) { 32cb69b360Sopenharmony_ci return g_intf; 33cb69b360Sopenharmony_ci } 34cb69b360Sopenharmony_ci pthread_mutex_lock(&g_mutex); 35cb69b360Sopenharmony_ci if (g_intf != NULL) { 36cb69b360Sopenharmony_ci pthread_mutex_unlock(&g_mutex); 37cb69b360Sopenharmony_ci return g_intf; 38cb69b360Sopenharmony_ci } 39cb69b360Sopenharmony_ci IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(POWER_MANAGE_SERVICE, POWER_MANAGE_FEATURE); 40cb69b360Sopenharmony_ci if (iUnknown == NULL) { 41cb69b360Sopenharmony_ci POWER_HILOGE("Failed to get power manage iUnknown"); 42cb69b360Sopenharmony_ci pthread_mutex_unlock(&g_mutex); 43cb69b360Sopenharmony_ci return NULL; 44cb69b360Sopenharmony_ci } 45cb69b360Sopenharmony_ci 46cb69b360Sopenharmony_ci int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_intf); 47cb69b360Sopenharmony_ci if ((ret != EC_SUCCESS) || (g_intf == NULL)) { 48cb69b360Sopenharmony_ci POWER_HILOGE("Failed to query power manage interface"); 49cb69b360Sopenharmony_ci pthread_mutex_unlock(&g_mutex); 50cb69b360Sopenharmony_ci return NULL; 51cb69b360Sopenharmony_ci } 52cb69b360Sopenharmony_ci pthread_mutex_unlock(&g_mutex); 53cb69b360Sopenharmony_ci 54cb69b360Sopenharmony_ci return g_intf; 55cb69b360Sopenharmony_ci} 56cb69b360Sopenharmony_ci 57cb69b360Sopenharmony_civoid InitIdentity(RunningLockEntry *entry) 58cb69b360Sopenharmony_ci{ 59cb69b360Sopenharmony_ci if (entry == NULL) { 60cb69b360Sopenharmony_ci return; 61cb69b360Sopenharmony_ci } 62cb69b360Sopenharmony_ci entry->identity.pid = 0; 63cb69b360Sopenharmony_ci entry->identity.token = (uint64_t)(uintptr_t)entry; 64cb69b360Sopenharmony_ci} 65cb69b360Sopenharmony_ci 66cb69b360Sopenharmony_ciBOOL AcquireRunningLockEntry(RunningLockEntry *entry, int32_t timeoutMs) 67cb69b360Sopenharmony_ci{ 68cb69b360Sopenharmony_ci int32_t ret = EC_FAILURE; 69cb69b360Sopenharmony_ci PowerManageInterface *intf = GetPowerManageInterface(); 70cb69b360Sopenharmony_ci if ((intf != NULL) && (intf->AcquireRunningLockEntryFunc != NULL)) { 71cb69b360Sopenharmony_ci ret = intf->AcquireRunningLockEntryFunc((IUnknown *)intf, entry, timeoutMs); 72cb69b360Sopenharmony_ci } 73cb69b360Sopenharmony_ci return (ret == EC_SUCCESS) ? TRUE : FALSE; 74cb69b360Sopenharmony_ci} 75cb69b360Sopenharmony_ci 76cb69b360Sopenharmony_ciBOOL ReleaseRunningLockEntry(RunningLockEntry *entry) 77cb69b360Sopenharmony_ci{ 78cb69b360Sopenharmony_ci int32_t ret = EC_FAILURE; 79cb69b360Sopenharmony_ci PowerManageInterface *intf = GetPowerManageInterface(); 80cb69b360Sopenharmony_ci if ((intf != NULL) && (intf->ReleaseRunningLockEntryFunc != NULL)) { 81cb69b360Sopenharmony_ci ret = intf->ReleaseRunningLockEntryFunc((IUnknown *)intf, entry); 82cb69b360Sopenharmony_ci } 83cb69b360Sopenharmony_ci return (ret == EC_SUCCESS) ? TRUE : FALSE; 84cb69b360Sopenharmony_ci} 85cb69b360Sopenharmony_ci 86cb69b360Sopenharmony_ciBOOL IsAnyRunningLockHolding() 87cb69b360Sopenharmony_ci{ 88cb69b360Sopenharmony_ci BOOL ret = FALSE; 89cb69b360Sopenharmony_ci PowerManageInterface *intf = GetPowerManageInterface(); 90cb69b360Sopenharmony_ci if ((intf != NULL) && (intf->IsAnyRunningLockHoldingFunc != NULL)) { 91cb69b360Sopenharmony_ci ret = intf->IsAnyRunningLockHoldingFunc((IUnknown *)intf); 92cb69b360Sopenharmony_ci } 93cb69b360Sopenharmony_ci return ret; 94cb69b360Sopenharmony_ci} 95cb69b360Sopenharmony_ci 96cb69b360Sopenharmony_civoid SuspendDevice(SuspendDeviceType reason, BOOL suspendImmed) 97cb69b360Sopenharmony_ci{ 98cb69b360Sopenharmony_ci PowerManageInterface *intf = GetPowerManageInterface(); 99cb69b360Sopenharmony_ci if ((intf != NULL) && (intf->SuspendDeviceFunc != NULL)) { 100cb69b360Sopenharmony_ci intf->SuspendDeviceFunc((IUnknown *)intf, reason, suspendImmed); 101cb69b360Sopenharmony_ci } 102cb69b360Sopenharmony_ci} 103cb69b360Sopenharmony_ci 104cb69b360Sopenharmony_civoid WakeupDevice(WakeupDeviceType reason, const char* details) 105cb69b360Sopenharmony_ci{ 106cb69b360Sopenharmony_ci const char* detailReason = (details != NULL) ? details : "No details"; 107cb69b360Sopenharmony_ci PowerManageInterface *intf = GetPowerManageInterface(); 108cb69b360Sopenharmony_ci if ((intf != NULL) && (intf->WakeupDeviceFunc != NULL)) { 109cb69b360Sopenharmony_ci intf->WakeupDeviceFunc((IUnknown *)intf, reason, detailReason); 110cb69b360Sopenharmony_ci } 111cb69b360Sopenharmony_ci} 112