-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmc_util.c
208 lines (183 loc) · 6.38 KB
/
smc_util.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
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
//
// smc_util.c
// smc_utils
//
// Created by Mark Garrett on 25/11/2011.
// Copyright (c) 2011 Garetech Computer Solutions. All rights reserved.
//
#include "smc_utils.h"
#include "reportfd.h"
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <stdlib.h>
static void dousage(char *progid);
static void dousage(char *progid)
{
// pDcqtsxnvdfw:lh?
// pDf
fprintf(stderr, "%s [-c] [-q] [-d] [-D] [-p] [-f] [-v] [-t] [-s] [-x] [-n] [-r <key> ] [ -w <key> ] [-l] [ <value>|keys... ]\n", progid);
fprintf(stderr, " -c suppress newline\n");
fprintf(stderr, " -q be quiet as a mouse\n");
fprintf(stderr, " -d increase debug output\n");
fprintf(stderr, " -D Display known descriptions\n");
fprintf(stderr, " -p Private/Paranoide, blank out the System Serial#\n");
fprintf(stderr, " -f display FAN specific data\n");
fprintf(stderr, " -v display values\n");
fprintf(stderr, " -t display [type](s)\n");
fprintf(stderr, " -s display :size\n");
fprintf(stderr, " -x dump values in (hexbytes)\n");
fprintf(stderr, " -n when normally a single line output line breaks between variables\n");
fprintf(stderr, " -w <key> write value to key\n");
fprintf(stderr, " -l list all keys to stdout\n");
fprintf(stderr, " with out options -w or -l the default is to read args[] as a series of keys to display\n");
exit(EINVAL);
}
gbl_params_t PARAMS;
int main(int argc, char *argv[])
{
char c;
int usage;
char *value;
char *key;
int idx;
direction_t direction;
kern_return_t r0;
gbl_params_t *params = &PARAMS;
usage = 0;
optarg = NULL;
bzero(params,sizeof(gbl_params_t));
params->progid = strdup(argv[0]);
params->dbg = 0;
params->fldidx = 1;
key = NULL;
value = NULL;
direction = RD;
params->outfd = stdout;
params->flg = F_ALWAYS;
params->newline = "\n";
params->model = get_sysmodel_dynamic();
params->flg |= F_NOISY; // by default noisy output
while (!usage && ((c=getopt(argc,argv, "pDcqtsxXOnvdfw:lh?")) != -1 )) {
switch(c) {
case 'p':
params->flg |= F_PRIVATE;
break;
case 'D':
params->flg |= F_DESCRIBE;
break;
case 'c':
params->newline = "";
break;
case 'q':
params->flg &= ~F_NOISY; // clear noisy
break;
case 't':
params->flg |= F_SHOWTYPE;
break;
case 's':
params->flg |= F_SHOWSIZE;
break;
case 'x':
params->flg |= F_DUMPHEX;
break;
case 'X':
params->flg |= F_DUMPHEX;
params->flg |= F_BINARY;
break;
case 'n':
params->flg |= F_NL;
break;
case 'O':
params->flg |= F_BYTEORDER; // flip byteorder
break;
case 'v':
params->flg |= F_VALUES;
break;
case 'd':
params->flg |= F_DEBUG;
params->dbg++;
break;
case 'f':
direction = FAN;
break;
case 'w':
key = strdup(optarg);
if (key == NULL ) {
if (flag_test(F_NOISY))
fprintf(stderr, "# Out of memory on args, impossible i say ;)\n");
exit(ENOMEM);
}
direction=WR;
break;
case 'l':
key = NULL; // a null value will list all
direction=LST;
break;
default:
usage++;
break;
}
}
argc -= (optind);
argv += (optind);
if (usage) {
dousage(params->progid);
exit(EINVAL);
}
if (flag_test(F_DEBUG)) {
fprintf(params->outfd, "# System Model: %s %s", params->model, params->newline);
}
r0 = smc_open(¶ms->conn);
if (r0 != kIOReturnSuccess) {
if (flag_test(F_NOISY))
fprintf(stderr, "# Error opening SMC subsystem: %d\n", r0);
} else {
switch (direction) {
case WR:
if (argc == 1) {
value = strdup(argv[0]);
if (value == NULL ) {
if (flag_test(F_NOISY))
fprintf(stderr, "# Out of memory on args, impossible i say ;)\n");
exit(ENOMEM);
}
r0 = smc_key_write(params->conn, key, value);
} else {
dousage(params->progid);
r0 = EINVAL;
}
break;
case FAN:
report_fans(params->conn);
break;
case TEMP:
break;
case LST:
r0 = smc_display_allkeys(params->conn, params->model);
break;
case RD:
for (idx=0;idx<argc;idx++) {
r0 = smc_key_display(params->conn, argv[idx]);
if (r0 != kIOReturnSuccess) {
if (flag_test(F_NOISY))
fprintf(stderr, "# Error displaying key: %s: %d\n", argv[idx], r0);
}
}
break;
case RW: // just to complete the values of direction, no function here
case UD:
if (flag_test(F_NOISY))
fprintf(stderr, "# internal error undefined direction\n");
exit(EINVAL);
break;
}
// TODO: r0 may hold a failure from an operation ?
r0 = smc_close(¶ms->conn);
if (r0 != kIOReturnSuccess) {
if (flag_test(F_NOISY))
fprintf(stderr, "# Error closing SMC subsystem: %d\n", r0);
}
}
exit(r0);
}