PMAssertions.h   [plain text]

 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * and read it before using this
 * file.
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * Copyright (c) 2002 Apple Computer, Inc.  All rights reserved. 
 * 29-Aug-02 ebold created
#ifndef _PMAssertions_h_
#define _PMAssertions_h_

#include <IOKit/pwr_mgt/IOPM.h>

#include <sys/queue.h>

/* ExternalMedia assertion
 * This assertion is only defined here in PM configd. 
 * It can only be asserted by PM configd; not by other user processes.
#define _kIOPMAssertionTypeExternalMediaCStr    "ExternalMedia"
#define _kIOPMAssertionTypeExternalMedia        CFSTR(_kIOPMAssertionTypeExternalMediaCStr)

#ifndef     kIOPMRootDomainWakeTypeNetwork
#define     kIOPMRootDomainWakeTypeNetwork          CFSTR("Network")

#ifndef     kIOPMRootDomainWakeTypeAlarm
#define     kIOPMRootDomainWakeTypeAlarm            CFSTR("Alarm")

#ifndef     kIOPMRootDomainWakeTypeMaintenance  
#define     kIOPMRootDomainWakeTypeMaintenance      CFSTR("Maintenance")

#ifndef     kIOPMRootDomainWakeTypeSleepTimer
#define     kIOPMRootDomainWakeTypeSleepTimer       CFSTR("SleepTimer")

#ifndef kIOPMRootDomainWakeTypeSleepService
#define kIOPMRootDomainWakeTypeSleepService         CFSTR("SleepService")

#ifndef kIOPMRootDomainWakeReasonRTC
#define  kIOPMRootDomainWakeReasonRTC        CFSTR("RTC")

#ifndef kIORootDomainWakeReasonDarkPME
#define kIORootDomainWakeReasonDarkPME          CFSTR("EC.DarkPME")

#ifndef kIOPMrootDomainWakeTypeLowBattery
#define kIOPMrootDomainWakeTypeLowBattery   CFSTR("LowBattery")

/* IOPMAssertion levels
 * Each assertion type has a corresponding bitfield index, here.
 * Also as found under the "Bitfields" property in assertion CFDictionaries.
typedef enum {
    // These must be consecutive integers beginning at 0
    kHighPerfIndex                  = 0,
    kPreventIdleIndex               = 1,
    kDisableInflowIndex             = 2,
    kInhibitChargeIndex             = 3,
    kDisableWarningsIndex           = 4,
    kPreventDisplaySleepIndex       = 5,
    kEnableIdleIndex                = 6,
    kNoRealPowerSourcesDebugIndex   = 7,
    kPreventSleepIndex              = 8,
    kExternalMediaIndex             = 9,
    kDeclareUserActivity            = 10,
    kPushServiceTaskIndex           = 11,
    kBackgroundTaskIndex            = 12,
    // Make sure this is the last enum element, as it tells us the total
    // number of elements in the enum definition
} kerAssertionType;

typedef struct assertion {
    LIST_ENTRY(assertion) link;
    CFMutableDictionaryRef props;       // client provided properties
    pid_t           pid;                // PID creating the assertion
    uint32_t        state;              // assertion state bits
    uint64_t        createTime;         // Time at which assertion is created
    uint64_t        timeout;            // absolute time at which assertion will timeout

    kerAssertionType    kassert;        // Assertion type, also index into gAssertionTypes
    IOPMAssertionID     assertionId;    // Assertion Id returned to client    

    uint32_t        mods;               // Modifcation bits for most recent SetProperties call

    uint32_t        retainCnt;          // Number of retain calls
} assertion_t;

/* State bits for assertion_t structure */
#define kAssertionStateTimed                0x1
#define kAssertionStateInactive             0x2
#define kAssertionStateValidOnBatt          0x4
#define kAssertionStateLogged               0x8
#define kAssertionLidStateModifier          0x10

/* Mods bits for assertion_t structure */
#define kAssertionModTimer              0x1
#define kAssertionModLevel              0x2
#define kAssertionModType               0x4
#define kAssertionModPowerConstraint    0x8
#define kAssertionModLidState           0x10

typedef enum {
    kAssertionOpEval,    // Evaluate for any changes to be sent to kernel due to enviromental changes
} assertionOps;

typedef struct assertionType assertionType_t;
typedef void (*assertionHandler_f)(assertionType_t *a, assertionOps op);

/* Structure per kernel assertion type */
struct assertionType {
    uint32_t        flags;              /* Specific to this assertion type */

    LIST_HEAD(, assertion) activeTimed;  /* Active assertions with timeout */
    LIST_HEAD(, assertion) active;       /* Active assertions without timeout */
    LIST_HEAD(, assertion) inactive;     /* timed out assertions/Level 0 assertions etc */

    kerAssertionType    kassert;
    dispatch_source_t   timer;          /* dispatch source for Per assertion timer */  
    dispatch_source_t   globalTimer;    /* dispatch source for all assertions of this type */

    uint64_t        globalTimeout;      /* Relative time at which assertion is timedout */
    uint32_t        forceTimedoutCnt;   /* Count of assertions turned off due to global timer */
    CFStringRef     uuid;               /* Assertion type specific UUID */

    assertionHandler_f  handler;        /* Function changing the required settings in the kernel for this assertion type */
    uint32_t            linkedTypes;    /* Assertion types that need same kernel settings as this one, but different behavior
                                           in powerd.  This field bits are indices into 'gAssertionTypes' array */

    // Fields changed by properties set on assertion. 
    // Not all fields are valid for all assertion types 
    uint32_t   validOnBattCount;        /* Count of assertions requesting to be active on Battery power */
    uint32_t   lidSleepCount;           /* Count of assertions changing clamshellSleep state(For kDeclareUserActivity only) */
} ;

/* Flag bits for assertionType_t structure */
#define kAssertionTypeNotValidOnBatt        0x1     /* By default, this assertion type is not valid on battery power */
#define kAssertionTypeGloballyTimed         0x2     /* A global timer releases all assertions of this type */
#define kAssertionTypeDisabled              0x4     /* All assertion requests of this type are ignored */

__private_extern__ void PMAssertions_prime(void);
__private_extern__ void createOnBootAssertions(void);
__private_extern__ void PMAssertions_SettingsHaveChanged(void);

__private_extern__ IOReturn _IOPMSetActivePowerProfilesRequiresRoot(
                                CFDictionaryRef which_profile, 
                                int uid, 
                                int gid);
__private_extern__ IOReturn _IOPMAssertionCreateRequiresRoot(
                                mach_port_t task_port, 
                                char *nameCStr,
                                char *assertionCStr,
                                int level, 
                                int *assertion_id);

__private_extern__ void _TaskPortInvalidatedCallout(CFMachPortRef port, void *info);

__private_extern__ void _PMAssertionsDriverAssertionsHaveChanged(uint32_t changedDriverAssertions);

__private_extern__ void _ProxyAssertions(const struct IOPMSystemCapabilityChangeParameters *capArgs);

__private_extern__ void PMAssertions_TurnOffAssertions_ApplePushServiceTask(void);

__private_extern__ IOReturn InternalCreateAssertion(
                                CFMutableDictionaryRef properties, 
                                IOPMAssertionID *outID);

__private_extern__ void InternalReleaseAssertion(
                                IOPMAssertionID *outID);

__private_extern__ void InternalEvaluateAssertions(void);

__private_extern__ void evalAllUserActivityAssertions(unsigned int dispSlpTimer);

__private_extern__ CFMutableDictionaryRef	_IOPMAssertionDescriptionCreate(
                            CFStringRef AssertionType, 
                            CFStringRef Name, 
                            CFStringRef Details,
                            CFStringRef HumanReadableReason,
                            CFStringRef LocalizationBundlePath,
                            CFTimeInterval Timeout,
                            CFStringRef TimeoutBehavior);

__private_extern__ CFStringRef processInfoGetName(pid_t p);
__private_extern__ void setSleepServicesTimeCap(uint32_t  timeoutInMS);
__private_extern__ bool systemBlockedInS0Dark( );
__private_extern__ bool checkForActivesByType(kerAssertionType type);
__private_extern__ bool checkForEntriesByType(kerAssertionType type);
__private_extern__ void disableAssertionType(kerAssertionType type);
__private_extern__ void enableAssertionType(kerAssertionType type);
__private_extern__ void applyToAllAssertionsSync(assertionType_t *assertType, 
      bool applyToInactives,  void (^performOnAssertion)(assertion_t *));
__private_extern__ void configAssertionType(kerAssertionType idx, bool initialConfig);
__private_extern__ void logASLAssertionTypeSummary( kerAssertionType type);