summaryrefslogtreecommitdiff
path: root/psutil/arch/osx/smc.c
blob: bda6210c450a156f7659eaf8ff0f79bf359010a5 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Interface to SMC API, needed in order to collect sensors stats.
 */

#include <stdio.h>
#include <string.h>
#include <IOKit/IOKitLib.h>

#include "smc.h"

UInt32 _strtoul(char *str, int size, int base)
{
    UInt32 total = 0;
    int i;

    for (i = 0; i < size; i++) {
        if (base == 16)
            total += str[i] << (size - 1 - i) * 8;
        else
            total += (unsigned char) (str[i] << (size - 1 - i) * 8);
    }
    return total;
}


float _strtof(unsigned char *str, int size, int e)
{
    float total = 0;
    int i;

    for (i = 0; i < size; i++) {
        if (i == (size - 1))
            total += (str[i] & 0xff) >> e;
        else
            total += str[i] << (size - 1 - i) * (8 - e);
    }

    total += (str[size-1] & 0x03) * 0.25;

    return total;
}


void _ultostr(char *str, UInt32 val)
{
    str[0] = '\0';
    sprintf(str, "%c%c%c%c",
        (unsigned int) val >> 24,
        (unsigned int) val >> 16,
        (unsigned int) val >> 8,
        (unsigned int) val);
}


kern_return_t SMCOpen(io_connect_t * pconn)
{
    kern_return_t result;
    io_iterator_t iterator;
    io_object_t   device;

    CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC");
    result = IOServiceGetMatchingServices(
        kIOMasterPortDefault,
        matchingDictionary,
        &iterator
    );
    if (result != kIOReturnSuccess) {
        return 1;
    }

    device = IOIteratorNext(iterator);
    IOObjectRelease(iterator);
    if (device == 0) {
        return 1;
    }

    result = IOServiceOpen(device, mach_task_self(), 0, pconn);
    IOObjectRelease(device);
    if (result != kIOReturnSuccess) {
        return 1;
    }

    return kIOReturnSuccess;
}


kern_return_t SMCClose(io_connect_t conn)
{
    return IOServiceClose(conn);
}


kern_return_t SMCCall(io_connect_t conn,
    int index, SMCKeyData_t *inputStructure, SMCKeyData_t *outputStructure)
{
    size_t   structureInputSize;
    size_t   structureOutputSize;

    structureInputSize = sizeof(SMCKeyData_t);
    structureOutputSize = sizeof(SMCKeyData_t);

    return IOConnectCallStructMethod(
        conn, index, inputStructure, structureInputSize, outputStructure,
        &structureOutputSize);
}


kern_return_t SMCReadKey(io_connect_t conn, UInt32Char_t key, SMCVal_t *val) {
    kern_return_t result;
    SMCKeyData_t  inputStructure;
    SMCKeyData_t  outputStructure;

    memset(&inputStructure, 0, sizeof(SMCKeyData_t));
    memset(&outputStructure, 0, sizeof(SMCKeyData_t));
    memset(val, 0, sizeof(SMCVal_t));

    inputStructure.key = _strtoul(key, 4, 16);
    inputStructure.data8 = SMC_CMD_READ_KEYINFO;

    result = SMCCall(
        conn,
        KERNEL_INDEX_SMC,
        &inputStructure,
        &outputStructure
    );
    if (result != kIOReturnSuccess)
        return result;

    val->dataSize = outputStructure.keyInfo.dataSize;
    _ultostr(val->dataType, outputStructure.keyInfo.dataType);
    inputStructure.keyInfo.dataSize = val->dataSize;
    inputStructure.data8 = SMC_CMD_READ_BYTES;

    result = SMCCall(
        conn,
        KERNEL_INDEX_SMC,
        &inputStructure,
        &outputStructure
    );
    if (result != kIOReturnSuccess)
        return result;

    memcpy(val->bytes, outputStructure.bytes, sizeof(outputStructure.bytes));

    return kIOReturnSuccess;
}


double SMCGetTemperature(io_connect_t conn, char *key) {
    SMCVal_t val;
    int intValue;
    kern_return_t result;

    result = SMCReadKey(conn, key, &val);
    if (result == kIOReturnSuccess) {
        if (val.dataSize > 0) {
            if (strcmp(val.dataType, DATATYPE_SP78) == 0) {
                intValue = val.bytes[0] * 256 + (unsigned char)val.bytes[1];
                return intValue / 256.0;
            }
        }
    }
    return 0.0;
}


float SMCGetFanSpeed(int fanNum)
{
    SMCVal_t val;
    kern_return_t result;
    UInt32Char_t  key;
    io_connect_t conn;

    result = SMCOpen(&conn);
    if (result != kIOReturnSuccess) {
        return -1;
    }

    sprintf(key, SMC_KEY_FAN_SPEED, fanNum);
    result = SMCReadKey(conn, key, &val);
     if (result != kIOReturnSuccess) {
        SMCClose(conn);
        return -1;
    }
    SMCClose(conn);
    return _strtof((unsigned char *)val.bytes, val.dataSize, 2);
}


int SMCGetFanNumber(char *key)
{
    SMCVal_t val;
    kern_return_t result;
    io_connect_t conn;

    result = SMCOpen(&conn);
    if (result != kIOReturnSuccess) {
        return 0;
    }

    result = SMCReadKey(conn, key, &val);
     if (result != kIOReturnSuccess) {
        SMCClose(conn);
        return 0;
    }
    SMCClose(conn);
    return _strtoul((char *)val.bytes, val.dataSize, 10);
}