1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License").  You may not use this file except in compliance with the
9 * License.  Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23cc test2.c -o pm_callback -Wall -Wno-four-char-constants -framework IOKit -framework CoreFoundation
24*/
25
26#include <ctype.h>
27#include <stdlib.h>
28#include <stdio.h>
29
30#include <mach/mach_port.h>
31#include <mach/mach_interface.h>
32#include <mach/mach_init.h>
33
34#include <IOKit/pwr_mgt/IOPMLib.h>
35#include <IOKit/IOMessage.h>
36
37#include "config.h"
38#include "wintypes.h"
39#include "pcsclite.h"
40#include "debuglog.h"
41#include "readerfactory.h"
42#include "thread_generic.h"
43#include "hotplug.h"
44
45
46static io_connect_t		root_port;
47static IONotificationPortRef	notify;
48static io_object_t 		anIterator;
49
50PCSCLITE_THREAD_T       pmgmtThread;
51extern PCSCLITE_MUTEX   usbNotifierMutex;
52
53void PMPowerRegistrationThread();
54
55
56void PMPowerEventCallback(void * x,io_service_t y,natural_t messageType,void * messageArgument)
57{
58
59    switch ( messageType ) {
60    case kIOMessageCanSystemSleep:
61          IOAllowPowerChange(root_port,(long)messageArgument);
62          break;
63    case kIOMessageSystemWillSleep:
64          DebugLogA("PMPowerEventCallback: system will sleep");
65          SYS_MutexLock(&usbNotifierMutex);
66	// see WrapRFSuspendAllReaders
67    //      RFSuspendAllReaders();
68          IOAllowPowerChange(root_port,(long)messageArgument);
69          DebugLogA("PMPowerEventCallback: system allowed to sleep");
70          break;
71    case kIOMessageSystemHasPoweredOn:
72        DebugLogA("PMPowerEventCallback: system has powered on");
73    // see WrapRFSuspendAllReaders
74	//    HPSearchHotPluggables();
75     //   RFAwakeAllReaders();
76        SYS_MutexUnLock(&usbNotifierMutex);
77        break;
78	case kIOMessageSystemWillPowerOn:
79        DebugLogA("PMPowerEventCallback: system will power on");
80		break;
81	default:
82		DebugLogB("PMPowerEventCallback: unknown event: %d", messageType);
83		break;
84    }
85
86}
87
88void PMPowerRegistrationThread() {
89
90    root_port = IORegisterForSystemPower (0,&notify,PMPowerEventCallback,&anIterator);
91
92    if ( root_port == 0 ) {
93            printf("IORegisterForSystemPower failed\n");
94            return;
95    }
96
97    CFRunLoopAddSource(CFRunLoopGetCurrent(),
98                        IONotificationPortGetRunLoopSource(notify),
99                        kCFRunLoopDefaultMode);
100
101    CFRunLoopRun();
102}
103
104ULONG PMRegisterForPowerEvents()
105{
106	LONG rv;
107	DebugLogA("PMRegisterForPowerEvents");
108	rv = SYS_ThreadCreate(&pmgmtThread, THREAD_ATTR_DEFAULT, (LPVOID) PMPowerRegistrationThread, NULL);
109	return 0;
110}
111
112
113
114