forked from boxed/Waker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleTool.c
162 lines (134 loc) · 4.31 KB
/
SampleTool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <CoreServices/CoreServices.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include "BetterAuthorizationSampleLib.h"
#include "SampleCommon.h"
#define kAppKey "com.kodare.Waker"
void PrintParams(
AuthorizationRef auth,
const void * userData,
CFDictionaryRef request,
CFMutableDictionaryRef response,
aslclient asl,
aslmsg aslMsg)
{
CFIndex count;
CFIndex index;
const void **keys;
asl_log(asl, aslMsg, ASL_LEVEL_DEBUG, "waker: %li", CFDictionaryGetCount(request));
count = CFDictionaryGetCount( (CFDictionaryRef) request);
keys = (const void **) malloc( count * sizeof(const void *));
if (keys != NULL) {
CFDictionaryGetKeysAndValues( (CFDictionaryRef) request, keys, NULL);
for (index = 0; index < count; index++) {
char buffer[1024];
CFStringRef s = (CFStringRef)CFDictionaryGetValue( (CFDictionaryRef) request, keys[index]);
CFStringGetCString(keys[index], buffer, 1024, kCFStringEncodingASCII);
asl_log(asl, aslMsg, ASL_LEVEL_DEBUG, "waker: %s", buffer);
CFStringGetCString(s, buffer, 1024, kCFStringEncodingASCII);
asl_log(asl, aslMsg, ASL_LEVEL_DEBUG, "waker: %s", buffer);
}
free(keys);
}
}
/////////////////////////////////////////////////////////////////
static OSStatus DoSetWakeup(
AuthorizationRef auth,
const void * userData,
CFDictionaryRef request,
CFMutableDictionaryRef response,
aslclient asl,
aslmsg aslMsg
)
{
//OSStatus retval = noErr;
// Pre-conditions
int res;
assert(auth != NULL);
// userData may be NULL
assert(request != NULL);
assert(response != NULL);
// asl may be NULL
// aslMsg may be NULL
if ( !CFDictionaryContainsKey(request, CFSTR(kWakerSetWakeupEventKeyDate)) ) { return 10; }
res = IOPMSchedulePowerEvent((CFDateRef)CFDictionaryGetValue((CFDictionaryRef)request, CFSTR(kWakerSetWakeupEventKeyDate)), CFSTR(kAppKey), CFSTR(kIOPMAutoWake));
if (res == kIOReturnSuccess)
{
printf("Daily Wakeup Call successfully installed\n");
}
else
{
asl_log(asl, aslMsg, ASL_LEVEL_DEBUG, "Failed to set wakeup date. Errcode: %x", res);
return res;
}
return noErr;
}
static OSStatus DoCancelWakeup(
AuthorizationRef auth,
const void * userData,
CFDictionaryRef request,
CFMutableDictionaryRef response,
aslclient asl,
aslmsg aslMsg
)
{
//OSStatus retval = noErr;
// Pre-conditions
int res;
assert(auth != NULL);
// userData may be NULL
assert(request != NULL);
assert(response != NULL);
// asl may be NULL
// aslMsg may be NULL
if ( !CFDictionaryContainsKey(request, CFSTR(kWakerSetWakeupEventKeyDate)) ) { return 10; }
res = IOPMCancelScheduledPowerEvent((CFDateRef)CFDictionaryGetValue((CFDictionaryRef)request, CFSTR(kWakerSetWakeupEventKeyDate)), CFSTR(kAppKey), CFSTR(kIOPMAutoWake));
if (res == kIOReturnSuccess)
{
printf("Daily Wakeup Call successfully installed\n");
}
else
{
asl_log(asl, aslMsg, ASL_LEVEL_DEBUG, "Failed to set wakeup date. Errcode: %x", res);
return res;
}
return noErr;
}
static OSStatus SleepSystem(
AuthorizationRef auth,
const void * userData,
CFDictionaryRef request,
CFMutableDictionaryRef response,
aslclient asl,
aslmsg aslMsg
)
{
io_connect_t fb = IOPMFindPowerManagement(MACH_PORT_NULL);
IOPMSleepSystem(fb);
return noErr;
}
/////////////////////////////////////////////////////////////////
#pragma mark ***** Tool Infrastructure
/*
IMPORTANT
---------
This array must be exactly parallel to the kWakerCommandSet array
in "SampleCommon.c".
*/
static const BASCommandProc kWakerCommandProcs[] = {
DoSetWakeup,
DoCancelWakeup,
SleepSystem,
NULL
};
int main(int argc, char **argv)
{
// Go directly into BetterAuthorizationSampleLib code.
// IMPORTANT
// BASHelperToolMain doesn't clean up after itself, so once it returns
// we must quit.
return BASHelperToolMain(kWakerCommandSet, kWakerCommandProcs);
}