-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathIOKit.c
92 lines (77 loc) · 2.54 KB
/
IOKit.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
#include <stdio.h>
#include <string.h>
#include <mach/mach.h>
#include "IOKit.h"
struct ioconnectCache {
const char* serviceName;
io_connect_t conn;
};
#define IOCONNECT_CACHE_ENTRIES 10
struct ioconnectCache cache[IOCONNECT_CACHE_ENTRIES]={{NULL, 0}};
void __attribute__((destructor)) IOKit_destruct()
{
int i;
for (i=0; i < IOCONNECT_CACHE_ENTRIES && cache[i].conn != 0; i++) {
//printf("Closing %s\n", cache[i].serviceName);
IOServiceClose(cache[i].conn);
}
}
io_connect_t IOKit_getConnect(const char* serviceName)
{
IOReturn ret;
io_connect_t conn = 0;
int i;
for (i=0; i < IOCONNECT_CACHE_ENTRIES && cache[i].serviceName != NULL; i++) {
if (!strcmp(serviceName, cache[i].serviceName))
{
//printf("got cache for %s\n", serviceName);
return cache[i].conn;
}
}
CFMutableDictionaryRef dict = IOServiceMatching(serviceName);
io_service_t dev = IOServiceGetMatchingService(kIOMasterPortDefault, dict);
if(!dev) {
fprintf(stderr, "FAIL: Could not get %s service\n", serviceName);
return -1;
}
ret = IOServiceOpen(dev, mach_task_self(), 0, &conn);
IOObjectRelease(dev);
if(ret != kIOReturnSuccess) {
fprintf(stderr, "FAIL: Cannot open service %s\n", serviceName);
return -1;
}
if (i < 10) {
cache[i].serviceName = serviceName;
cache[i].conn = conn;
}
return conn;
}
IOReturn IOKit_call(const char* serviceName,
uint32_t selector,
const uint64_t *input,
uint32_t inputCnt,
const void *inputStruct,
size_t inputStructCnt,
uint64_t *output,
uint32_t *outputCnt,
void *outputStruct,
size_t *outputStructCnt)
{
IOReturn ret;
io_connect_t conn = IOKit_getConnect(serviceName);
ret = IOConnectCallMethod(conn,
selector,
input,
inputCnt,
inputStruct,
inputStructCnt,
output,
outputCnt,
outputStruct,
outputStructCnt);
if (ret != kIOReturnSuccess)
{
fprintf(stderr, "IOConnectCallMethod returned %x\n", ret);
}
return ret;
}