-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptarg.c
1177 lines (998 loc) · 41.2 KB
/
optarg.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// smc_utils.c
// smc_utils
//
// Created by Mark Garrett on 26/11/2011.
// Copyright (c) 2011 Garetech Computer Solutions. All rights reserved.
//
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
// for sysctl()
#include <sys/types.h>
#include <sys/sysctl.h>
#include <assert.h>
//
// #include "smc_defs.h"
#include "smc_desc.h"
#include "smc_desc_t.h"
#include "smc_utils.h"
#include "fixedpoint.h"
#include "smc_types.h"
#include "reportfd.h"
kern_return_t smc_open(io_connect_t *connptr)
{
kern_return_t r0;
mach_port_t themaster;
CFMutableDictionaryRef AppleSMCdict;
io_iterator_t iointerator;
io_object_t ioobj;
r0 = IOMasterPort(MACH_PORT_NULL, &themaster);
if (r0 == kIOReturnSuccess) {
if ((AppleSMCdict = IOServiceMatching( "AppleSMC" ))) {
r0 = IOServiceGetMatchingServices( themaster, AppleSMCdict, &iointerator);
if (r0 == kIOReturnSuccess) {
ioobj = IOIteratorNext(iointerator); // Get the first item in the iterator.
IOObjectRelease(iointerator); // we done with iointerator, release it
if (ioobj) { // yep got one there
r0 = IOServiceOpen(ioobj, mach_task_self(), 0, connptr);
IOObjectRelease(ioobj); // conn is now setup we done with the entity
} else {
r0 = kIOReturnNoDevice;
}
}
} else {
r0 = kIOReturnNotFound; // IOServiceMatching nothing matched
}
}
return(r0);
}
kern_return_t smc_close(io_connect_t *conn)
{
kern_return_t r0;
r0 = IOServiceClose(*conn);
return(r0);
}
kern_return_t smc_io(io_connect_t conn, smc_io_data_t *in, smc_io_data_t *out )
{
size_t out_size;
kern_return_t r0;
out_size = sizeof(*out);
r0 = IOConnectCallStructMethod(
/* mach_port_t */ conn, // In
/* uint32_t */ 2, // In 0,1,3 did nothing
/* maybe this is the correct reference for the selector:
# ioclasscount | grep AppleSMCC
AppleSMCClient = 1,3 it varies
maybe we can do what ever ioclasscount does?
AppleSMCControl = 2
*/
/* const void* */ in, // In
/* size_t */ sizeof(*in), // In
/* void* */ out, // Out
/* size_t* */ &out_size); // In/Out
return(r0);
}
kern_return_t SMC_key_getinfo(io_connect_t conn, char *keyname,SMC_key_val_t *keyval )
{
smc_io_data_t in;
smc_io_data_t out;
kern_return_t r0;
r0=0;
bzero(&in, sizeof(in));
bzero(&out,sizeof(out));
in.cmd = kSMCGetKeyInfo;
htosmclstr((char *)&in.key, keyname); // turn key into SMC byte order stored in integer
r0 = smc_io(conn, &in, &out);
if (r0 == kIOReturnSuccess) {
if (out.io_sts == 0 ) {
// we have the key info in out
// UInt32 vsize; // 2
// SMC_type_t vtype; // fp88
// SMC_keynam_t vkey; // PC1C
// SMC_key_data_t vbytes; // 08 44
keyval->vsize = out.ksize; // TODO: probably need a endian swap
htosmclstr(keyval->vtype, out.ktype); //need to endian swap results
memcpy(keyval->vkey, keyname, 4);
memcpy(keyval->vbytes, out.bytes,sizeof(keyval->vbytes));
} else {
r0 = out.io_sts;
}
}
return(r0);
}
kern_return_t SMC_key_value_by_index_get(io_connect_t conn, UInt32 keyidx, SMC_key_val_t *keyval )
{
smc_io_data_t in;
smc_io_data_t out;
kern_return_t r0;
r0=0;
bzero(&in, sizeof(in));
bzero(&out,sizeof(out));
bzero(keyval, sizeof(SMC_key_val_t));
in.cmd = kSMCGetKeyFromIndex;
in.p1_lw = keyidx;
//htosmclstr((char *)&in.key, keyname); // turn key into SMC byte order stored in integer
r0 = smc_io(conn, &in, &out);
if (r0 == kIOReturnSuccess) {
if (out.io_sts == 0) {
// we have the key info in out
// UInt32 vsize; // 2
// SMC_type_t vtype; // fp88
// SMC_keynam_t vkey; // PC1C
// SMC_key_data_t vbytes; // 08 44
keyval->vsize = out.ksize; // TODO: probably need a endian swap
htosmclstr(keyval->vtype, out.ktype); //need to endian swap results
htosmclstr(keyval->vkey, out.key); //need to endian swap results
memcpy(keyval->vbytes, out.bytes,sizeof(keyval->vbytes));
r0 = SMC_key_value_get(conn, keyval->vkey, keyval);
} else {
r0 = out.io_sts;
}
}
return(r0);
}
//////////////////////////////////////////////////////////////////////////
kern_return_t SMC_key_write(io_connect_t conn, char *keyname, short val, SMC_key_val_t *keyval)
//////////////////////////////////////////////////////////////////////////
{
smc_io_data_t in;
smc_io_data_t out;
kern_return_t r0;
bzero(&in, sizeof(in));
bzero(&out,sizeof(out));
r0 = SMC_key_getinfo(conn, keyname, keyval); // find that the key name is valid
// also gets the defaults
if (r0 == kIOReturnSuccess) {
keyval->smc_defaults = smc_lookup_desc(keyval);
if (keyval->smc_defaults->d_attrib & KA_W) { // Only write key if the key attribute allows
htosmclstr(in.key , keyval->vkey); // convert key name to SMC order
htosmclstr(in.ktype, keyval->vtype); // convert type to SMC order
in.ksize = keyval->vsize; // convert to SMC byteorder
in.cmd = kSMCWriteKey;
in.bytes[0] = val >> 8;
in.bytes[1] = val & 0x0F;
r0 = smc_io(conn, &in, &out);
if (r0 == kIOReturnSuccess) {
if (out.io_sts == 0 ) {
// we have the key info in out
//memcpy(keyval->vbytes, out.bytes,sizeof(keyval->vbytes));
} else {
r0 = out.io_sts;
}
}
} else {
r0 = EACCES; // Read only key cant be written
}
}
return(r0);
}
///////////////////////////////////////////////////////////////////////////////
kern_return_t SMC_key_value_get(io_connect_t conn, char *keyname ,SMC_key_val_t *keyval )
///////////////////////////////////////////////////////////////////////////////
{
smc_io_data_t in;
smc_io_data_t out;
kern_return_t r0;
bzero(&in, sizeof(in));
bzero(&out,sizeof(out));
r0 = SMC_key_getinfo(conn, keyname, keyval);
if (r0 == kIOReturnSuccess) {
keyval->smc_defaults = smc_lookup_desc(keyval);
if (keyval->smc_defaults->d_attrib & KA_R) { // Only read key if the key attribute allows
htosmclstr(in.key , keyval->vkey);
htosmclstr(in.ktype, keyval->vtype);
in.ksize = keyval->vsize; // convert to SMC byteorder
in.cmd = kSMCReadKey;
r0 = smc_io(conn, &in, &out);
if (r0 == kIOReturnSuccess) {
if (out.io_sts == 0 ) {
// we have the key info in out
memcpy(keyval->vbytes, out.bytes,sizeof(keyval->vbytes));
} else {
r0 = out.io_sts;
}
}
} else {
r0 = EACCES; // Write only key cant be read
}
}
return(r0);
}
double smc_key_read_numeric(io_connect_t conn, char *key)
{
double rval;
kern_return_t r0;
SMC_key_val_t keyvalue;
r0 = SMC_key_value_get(conn, key, &keyvalue);
if (r0 == kIOReturnSuccess) {
rval = numeric2float(keyvalue.vtype, keyvalue.vbytes);
} else {
rval = nan("-4");
}
return(rval);
}
double report_temp(io_connect_t conn, char *key, char *desc)
{
double temp;
temp = smc_key_read_numeric(conn, key);
//reportfd_fldidx();
reportfd(F_DEBUG, "[%s]", key);
reportfd(F_ALWAYS|F_NL,desc, temp);
return(temp);
}
void report_fans(io_connect_t conn)
{
int numfans;
int fan;
int fan_mode;
fan_mode = (int) smc_key_read_numeric(conn, "FS!"); // 0=auto !=0 fixed
numfans = (int) smc_key_read_numeric(conn, "FNum");
reportfd(0, "# Found %d fans on this system\n", numfans);
for (fan=0;fan < numfans;fan++) {
report_fan(conn, fan);
newline();
}
reportfd(0, "Fan Control mode: %s\n", fan_mode?"Auto":"Fixed");
}
void report_fan(io_connect_t conn, int fan)
{
double rpm_act, rpm_min, rpm_max, rpm_safe, rpm_trgt;
char *desc;
SMC_key_val_t desc_kval;
kern_return_t r0;
char key_act[5] = { "F0Ac" };
char key_min[5] = { "F0Mn" };
char key_max[5] = { "F0Mx" };
char key_safe[5] = { "F0Sf" };
char key_trgt[5] = { "F0Tg" };
char key_desc[5] = { "F0ID" };
struct {
char ob;
char key[15];
} keystr;
char eolc;
bzero(&keystr, sizeof(keystr));
if (flag_test(F_DEBUG)) {
keystr.ob = '[';
}
if (flag_test(F_NL)) {
eolc = '\n';
} else {
eolc = ':';
}
if (fan < 0 || fan > 9 ) {
reportfd(F_NOISY, "# Illegal fan index must be 0-9: %d\n", fan);
return;
}
key_act[1] = '0'+fan;
key_min[1] = '0'+fan;
key_max[1] = '0'+fan;
key_safe[1] = '0'+fan;
key_trgt[1] = '0'+fan;
key_desc[1] = '0'+fan;
rpm_act = smc_key_read_numeric(conn, key_act);
rpm_min = smc_key_read_numeric(conn, key_min);
rpm_max = smc_key_read_numeric(conn, key_max);
rpm_safe = smc_key_read_numeric(conn, key_safe);
rpm_trgt = smc_key_read_numeric(conn, key_trgt);
r0 = SMC_key_value_get( conn, key_desc, &desc_kval);
if (r0 != kIOReturnSuccess) {
return;
};
desc = describe_fan(&desc_kval);
if (desc) {
sprintf((char *) keystr.key, "%s]", key_desc);
reportfd(F_ALWAYS, "%s Fan#%d-%s", (char *) &keystr, fan, desc );
free(desc);
}
sprintf((char *) keystr.key, "%s]", key_act);
reportfd(F_INDEX,"%sFan#%d [act] : %g %c" ,(char *) &keystr, fan, rpm_act, eolc);
sprintf((char *) keystr.key, "%s]", key_min);
reportfd(F_INDEX,"%sFan#%d [min] : %g %c" , (char *) &keystr, fan, rpm_min, eolc);
sprintf((char *) keystr.key, "%s]", key_max);
reportfd(F_INDEX,"%sFan#%d [max] : %g %c" , (char *) &keystr, fan, rpm_max, eolc);
sprintf((char *) keystr.key, "%s]", key_safe);
reportfd(F_INDEX,"%sFan#%d [safe]: %g %c" , (char *) &keystr, fan, rpm_safe, eolc);
sprintf((char *) keystr.key, "%s]", key_trgt);
reportfd(F_INDEX,"%sFan#%d [trgt]: %g %c" , (char *) &keystr, fan, rpm_trgt, eolc);
}
int smc_display_allkeys(io_connect_t conn, char *model)
{
UInt32 numkeys;
UInt32 keyidx;
SMC_key_val_t keyval;
numkeys = (int) smc_key_read_numeric(conn, "#KEY");
reportfd(F_NOISY, "# Found %d keys on this system: %s\n", numkeys, model);
for (keyidx=0;keyidx < numkeys;keyidx++) {
SMC_key_value_by_index_get(conn, keyidx, &keyval); // errors esp WO are ignored
smc_keyval_display(&keyval);
}
return(EINVAL);
}
kern_return_t smc_fan_set(io_connect_t conn, char *keyname, int rpm)
{
short val;
kern_return_t rval;
SMC_key_val_t key;
// TODO: print value and check conversion is correct
val = rpm << 2; // value is in fpe2 format so just shift left 2 int RPM
rval = SMC_key_write(conn, keyname, val, &key);
return(rval);
}
int smc_key_write(io_connect_t conn, char *key, char *value)
{
int rval;
int rpm;
rval = -1;
// TODO: obviously ;(
if ((memcmp(key, "F0M", 3)== 0) ||
(memcmp(key, "F1M", 3)== 0) ||
(memcmp(key, "F0A", 3)== 0) ||
(memcmp(key, "F1A", 3)== 0) ){
rpm = atoi(value);
rval = smc_fan_set(conn, key, rpm);
}
return(rval);
}
int smc_key_display(io_connect_t conn, char *keyptr)
{
kern_return_t r0;
SMC_key_val_t keyvalue;
char key[5]; // must be space padded eg. "SIP "
snprintf(key,sizeof(key), "%-4.4s", keyptr);
r0 = SMC_key_value_get(conn, key, &keyvalue);
if (r0 != kIOReturnSuccess) {
return(r0);
}
return (smc_keyval_display(&keyvalue));
}
int smc_keyval_display(SMC_key_val_t *keyvalue)
{
double fp;
unsigned short release;
ssize_t idx;
reportfd(F_DESCRIBE, "%s|", keyvalue->smc_defaults->d_desc);
reportfd(F_ALWAYS, "%-4.4s" , keyvalue->vkey);
if (flag_test(F_SHOWTYPE)) {
reportfd(F_SHOWTYPE, "|[%-4.4s]",keyvalue->vtype);
reportfd(F_SHOWSIZE, "|");
}
if (flag_test(F_SHOWSIZE)) {
reportfd(F_SHOWSIZE, "|");
reportfd(F_SHOWTYPE, "%lu", (unsigned long) keyvalue->vsize);
}
reportfd(F_ALWAYS, "|");
if (flag_test(F_VALUES)) {
if (flag_test(F_PRIVATE)) {
if (memcmp(keyvalue->vkey, "B0SN", 4) == 0 ) { // Blank out the System Serial#
strcpy(keyvalue->vbytes, "<Private>");
keyvalue->vsize = (SMC_keysize_t) strlen(keyvalue->vbytes);
}
}
if (memcmp(keyvalue->vkey, "ACID", 4) == 0 ) {
short pa_id;
short pa_wattage;
short pa_revision;
short pa_family;
unsigned int pa_serialno;
/* special case
mark@zed> ./smc_util -tsv ACID
ACID|[ch8*]|8|.#2.P...|( BA | 23 32 85| 50 05 10 A1 )
ACID|[ch8*]|8|.#2.P...|( BA | 23 32 85| 50 05 10 A1 )
0550
AC Charger Information:
Connected: Yes
ID: 0x0100
Wattage (W): 85
Revision: 0x0000
Family: 0x00ba 0x00 keyvalue->vbytes[0]
Serial Number: 0x00 85 32 23 0x00 keyvalue->vbytes[3,2,1]
Charging: No */
pa_id = 0; // TODO: NFI
pa_revision = 0; // TODO: NFI
pa_family = 0;
pa_serialno = 0;
pa_wattage = (keyvalue->vbytes[3] & 0xff); // BCD
pa_family |= keyvalue->vbytes[0]&0xff;
pa_serialno = 0;
pa_serialno = (pa_serialno<<8) | (keyvalue->vbytes[3] & 0xff);
pa_serialno = (pa_serialno<<8) | (keyvalue->vbytes[2] & 0xff);
pa_serialno = (pa_serialno<<8) | (keyvalue->vbytes[1] & 0xff);
if (pa_serialno || pa_family) {
reportfd(F_ALWAYS|F_NL, "AC PWR Adapter; Family: 0x%04x Serial Number: 0x%08x Wattage: %02X other bytes: %02x %02x %02x %02x",
pa_family,
pa_serialno,
pa_wattage, // BCD
(unsigned char) keyvalue->vbytes[4],
(unsigned char) keyvalue->vbytes[5],
(unsigned char) keyvalue->vbytes[6],
(unsigned char) keyvalue->vbytes[7]
);
}
else {
reportfd(F_ALWAYS|F_NL, "AC PWR Adapter; None connected");
}
}
else
if ((keyvalue->vtype[0] == 'f' && keyvalue->vtype[1] == 'p')
){
fp = numeric2float(keyvalue->vtype, keyvalue->vbytes);
reportfd(F_ALWAYS, "%g", fp);
if (flag_test(F_DUMPHEX)) {
dump_hex(keyvalue->vsize, keyvalue->vbytes);
}
//reportfd(F_DUMPHEX|F_BINARY,")");
//reportfd(F_ALWAYS|F_NL,"");
}
if ( (keyvalue->vtype[0] == 'f')
&& (keyvalue->vtype[1] == 'l')
&& (keyvalue->vtype[2] == 't')
&& (keyvalue->vtype[3] == ' ')
){
fp = numeric2float(keyvalue->vtype, keyvalue->vbytes);
reportfd(F_ALWAYS, "%g", fp);
if (flag_test(F_DUMPHEX)) {
assert(keyvalue->vsize == 4);
dump_hex(keyvalue->vsize, keyvalue->vbytes);
}
reportfd(F_ALWAYS|F_NL,"");
}
else if ((keyvalue->vtype[0] == 'u' && keyvalue->vtype[1] == 'i') ||
(keyvalue->vtype[0] == 's' && keyvalue->vtype[1] == 'i')) {
fp = numeric2float(keyvalue->vtype, keyvalue->vbytes);
if (keyvalue->vtype[0] == 's') {
reportfd(F_ALWAYS, "%d", (int) fp);
}
else {
reportfd(F_ALWAYS, "%u", (unsigned int) fp);
}
if (flag_test(F_DUMPHEX)) {
dump_hex(keyvalue->vsize, keyvalue->vbytes);
}
reportfd(F_ALWAYS,"");
}
else if (memcmp(keyvalue->vtype, "ch8*", 4) == 0) {
dump_ascii(keyvalue->vsize, keyvalue->vbytes);
dump_hex( keyvalue->vsize, keyvalue->vbytes);
}
else if (memcmp(keyvalue->vtype, "char", 4) == 0) {
dump_ascii( keyvalue->vsize, keyvalue->vbytes);
dump_hex( keyvalue->vsize, keyvalue->vbytes);
}
else if (memcmp(keyvalue->vtype,"{ala",4) == 0) { // ALS analog lux calculation information.
ushort als_m; // slope
short als_b; // Y intercept
ushort als_r; // region
als_m = readushort(&keyvalue->vbytes[0]);
als_b = readshort( &keyvalue->vbytes[2]);
als_r = readushort(&keyvalue->vbytes[4]);
reportfd(F_ALWAYS|F_NL, "ALS LUX; slope: %u y-intercept: %d region: %u ", als_m, als_b, als_r);
/* {ala \0\0\0\0 K_DESC_STR
struct ALSLuxLine {
UInt16 ui16ALSM; // Slope of line.
Int16 i16ALSB; // Y-Intercept of line.
UInt16 ui16ALSR; // Region.
} */
}
else if (memcmp(keyvalue->vtype,"{alc",4) == 0) { // ALSConfig structure contains global ALS configuration and tuning info.
ushort ALSI2CTime = readushort(&keyvalue->vbytes[0]); // Int interval (ms) for ALS I2C task
ushort ALSADCTime = readushort(&keyvalue->vbytes[2]); // Int interval (ms) for ALS ADC ISR.
ushort LMax = readushort(&keyvalue->vbytes[4]); // Maximum cd/m^2 for SIL.
ushort LMin = readushort(&keyvalue->vbytes[6]); // Minimum cd/m^2 for SIL.
ushort ELow = readushort(&keyvalue->vbytes[8]); // Low room illum threshold (lux).
ushort EHigh = readushort(&keyvalue->vbytes[10]); // High room illum threshold (lux).
ushort Reflect = readushort(&keyvalue->vbytes[12]); // Bezel reflection coefficient.
u_char ALSSensors = keyvalue->vbytes[14]; // Actual number of ALS sensors in system.
u_char LidDelay = keyvalue->vbytes[15]; // Delay after lid opens (in tenths of seconds)
reportfd(F_ALWAYS|F_NL, "ALSConfig; I2Ctsktim: %ums ADCtsktim: %ums LMax: %u LMin: %u ELow: %u EHigh: %u Reflect: %u Sensor#: %2u LidDly: %2.1fs %s",
ALSI2CTime,
ALSADCTime,
LMax, LMin,
ELow, EHigh,
Reflect,
ALSSensors,
(LidDelay/10.0),
"");
}
else if (memcmp(keyvalue->vtype,"{ali",4) == 0) { // ALSSensor structure contains sensor-specific information for this system
/*
ALI0|[{ali]|4|....|(04 00 0F 00 )
ALI1|[{ali]|4|.|(00 )
{ali \0\0\0\0 K_DESC_STR
enum ALSType { NoSensor, BS520, TSL2561CS, LX1973A, ISL29003 };
struct ALSSensor {
enum ALSType alstALSType; // Type of sensor.
Flag fValidWhenLidClosed; // TRUE if no lid or if sensor works with
// closed lid. FALSE otherwise.
Flag fControlSIL; // TRUE if the SIL brightness depends on
// this sensor's value. FALSE otherwise.
}
*/
int alstypeidx = keyvalue->vbytes[0]&0xff;
if (alstypeidx > 4 || alstypeidx < 0 ) {
alstypeidx = 0;
}
reportfd(F_ALWAYS|F_NL, "ALSSensor; type: %s sensorvalid: %s, SIL using this sensor: %s %02x ",
get_ALSType_str(alstypeidx),
keyvalue->vbytes[1]&0xff?"Yes":"No ",
keyvalue->vbytes[2]&0xff?"Yes":"No ",
keyvalue->vbytes[3]&0xff);
}
else if (memcmp(keyvalue->vtype,"{alr",4) == 0) { // ALS analog lux temperature coefficients.
double ALSTempBase = numeric2float("fpg0", &keyvalue->vbytes[0]); // Temperature baseline (deg C, FP16.0)
double ALSTempCoefV = numeric2float("fpc4", &keyvalue->vbytes[2]); // Temperature coeff (ADC Counts/deg C, FP12.4
double ALSTempInflV = numeric2float("fpg0", &keyvalue->vbytes[4]); // Thermal compensation inflection point voltage (ADCCounts, FP16.0)
double ALSTempLow = numeric2float("fpg0",&keyvalue->vbytes[6]); // Low temperature boundary (deg C, FP16.0)
double ALSTempHigh = numeric2float("fpg0",&keyvalue->vbytes[8]); // High temperature boundary (deg C, FP16.0)
reportfd(F_ALWAYS|F_NL, "ALSAnalog; TempBase: %.1fC TempCoef: %.4fV, TempInfl: %.1fV TempLow: %.1f TempHigh: %.1f ",
ALSTempBase,
ALSTempCoefV,
ALSTempInflV,
ALSTempLow,
ALSTempHigh);
}
else if (memcmp(keyvalue->vtype,"{alt",4) == 0) { // ALS analog lux calculation thresholds
ushort ALSThrshLow; // ADC threshold while in low gain
ushort ALSThrshHigh; // ADC threshold while in high gain
ALSThrshLow = readushort(&keyvalue->vbytes[0]);
ALSThrshHigh = readushort(&keyvalue->vbytes[2]);
reportfd(F_ALWAYS|F_NL, "ALS Anallux Thrsh; Low: %u High: %u ", ALSThrshLow, ALSThrshHigh);
}
else if (memcmp(keyvalue->vtype,"{alv",4) == 0) { // ALSValue structure contains latest ambient light info from x sensor
// ALV0|[{alv]|10|..........|(01 01 00 9C 00 1B 00 16 18 A4 )
// ALV1|[{alv]|10|...|(00 01 00 )
ushort fValid = keyvalue->vbytes[0]&0xff; // If TRUE, data in this struct is valid
ushort fHighGain = keyvalue->vbytes[1]&0xff; // If TRUE, Chan0/1 are high-gain
// readings. If FALSE, Chan0/1 are low-gain readings
ushort Chan0 = readushort(&keyvalue->vbytes[2]); // I2C channel 0 data or analog(ADC) data.
ushort Chan1 = readushort(&keyvalue->vbytes[4]); // I2C channel 1 data.
double RoomLux;
char RoomLuxStr[64];
if (keyvalue->vsize > 6 ) {
RoomLux = numeric2float("fpie", &keyvalue->vbytes[6]); // FP18.14
snprintf(RoomLuxStr, sizeof(RoomLuxStr), "RoomLux: %6.5f", RoomLux);
}
else {
RoomLux = 0.0;
RoomLuxStr[0] = '\0';
}
reportfd(F_ALWAYS|F_NL, "ALSValue; Valid: %s HighGain: %s Chan0: %u Chan1: %u %s ",
fValid?"Yes":"No ",
fHighGain?"Yes":"No ",
Chan0, Chan1,
RoomLuxStr);
}
else if (memcmp(keyvalue->vtype,"{fds",4) == 0) { // Fan Description
reportfd(F_ALWAYS|F_NL, "type: %s Zone: %d loc: %s - %s", fan_type(keyvalue->vbytes[0]), keyvalue->vbytes[1], fan_loc(keyvalue->vbytes[2]), &keyvalue->vbytes[4]);
/*
{fds \0\0\0\0 K_DESC_STR
Fan Diag description
typedef struct fanTypeDescStruct {
FanType type;
UInt8 ui8Zone;
LocationType location;
UChar rsvd; // padding to get us to 16 bytes
char strFunction[DIAG_FUNCTION_STR_LEN];
} FanTypeDescStruct;
FAN constants
+---+
z/ /|
/ / |
+---+ |
| | +
y| | /
| |/
+---+
x
typedef enum { LEFT_LOWER_FRONT, CENTER_LOWER_FRONT, RIGHT_LOWER_FRONT,
LEFT_MID_FRONT, CENTER_MID_FRONT, RIGHT_MID_FRONT,
LEFT_UPPER_FRONT, CENTER_UPPER_FRONT, RIGHT_UPPER_FRONT,
LEFT_LOWER_REAR, CENTER_LOWER_REAR, RIGHT_LOWER_REAR,
LEFT_MID_REAR, CENTER_MID_REAR, RIGHT_MID_REAR,
LEFT_UPPER_REAR, CENTER_UPPER_REAR, RIGHT_UPPER_REAR } LocationType;
typedef enum { FAN_PWM_TACH, FAN_RPM, PUMP_PWM, PUMP_RPM, FAN_PWM_NOTACH, EMPTY_PLACEHOLDER } FanType;
*/
}
else if (memcmp(keyvalue->vtype,"{lim",4) == 0) { // Plimits group is 3 UInt8 KPRIV_DESC_STR
reportfd(F_ALWAYS|F_NL, "ProcLimits; CPU: %u GPU: %u MEM: %u ", // MSB , middle, LSB
keyvalue->vbytes[0]&0xff,
keyvalue->vbytes[1]&0xff,
keyvalue->vbytes[2]&0xff);
}
else if (memcmp(keyvalue->vtype,"{lsc",4) == 0) { // K_DESC_STR LmsConfig structure provides overall system-specific config info for the SIL.
// LSCF|[{lsc]|10|......... |(00 C8 01 90 80 00 02 02 00 20 )
ushort modvBrightnessBreatheMin; // Breathe dwell PWM setting
ushort modvMaxChangePerTick; // Max PWM change per 1/152 sec
ushort ScaleConstant; // Scale constant (1.15 fixed-point representation) if not using ALS or TOD scaling
u_char lmsmScaleMode; // Scale by ALS, TOD, or constant
u_char RampDuration; // Ramp length (equals 152 * ramp time in seconds)
u_char fPowerSwitchOverridesSIL; // TRUE if pressing the power switch should force the SIL to full brightness
u_char MinTicksToTarget; // Slow the slew rate so that it takes at least this many ticks to reach the target from the prev PWM value.
const char *scmode[] = {
"kLmsScaleALS", // Use ALS autoscale
"kLmsScaleTOD", // Use TOD autoscale
"kLmsScaleConst" // Scale only by a constant
};
modvBrightnessBreatheMin = readushort(&keyvalue->vbytes[0]);
modvMaxChangePerTick = readushort(&keyvalue->vbytes[2]);
ScaleConstant = readushort(&keyvalue->vbytes[4]);
lmsmScaleMode = keyvalue->vbytes[6]&0xff;
RampDuration = keyvalue->vbytes[7]&0xff;
fPowerSwitchOverridesSIL = keyvalue->vbytes[8]&0xff;
MinTicksToTarget = keyvalue->vbytes[9]&0xff;
reportfd(F_ALWAYS|F_NL, "LmsConfig; BrightnessBreatheMin: %u MaxChangePerTick: %u (1/152 sec) ScaleConst: %u ScaleMode: %s RampDuration: %u PwrSwchOvrSIL: %u MinTicks2Trgt: %u ",
modvBrightnessBreatheMin,
modvMaxChangePerTick,
ScaleConstant,
scmode[lmsmScaleMode],
RampDuration,
fPowerSwitchOverridesSIL,
MinTicksToTarget);
/*
{lsd \0\0\0\0 K_DESC_STR
LmsDwell structures provide dwell fade-up/down configuration
struct LmsDwell {
UInt16 ui16MidToStartRatio; // Mid-step size / start-step size
UInt16 ui16MidToEndRatio; // Mid-step size / end-step size
UInt16 ui16StartTicks; // # of ticks using start-step size
UInt16 ui16EndTicks; // # of ticks using end-step size
}
{lsf \0\0\0\0 K_DESC_STR
LmsFlare structures provide flare config for non-breathing fade-up/down
See "{pwm" for details on PWMValue
struct LmsFlare {
PWMValue modvFlareCeiling; // Flare algorithm is active below this value.
PWMValue modvMinChange; // Minimum rate of change while flaring.
UInt16 ui16FlareAdjust; // Smaller value causes stronger flare as
} // PWM value descends below modvFlareCeiling.
{lsm \0\0\0\0 K_DESC_STR
LmsScaleMode enum
enum LmsScaleMode { kLmsScaleALS, // Use ALS autoscale
kLmsScaleTOD, // Use TOD autoscale
kLmsScaleConst // Scale only by a constant
}
{lso \0\0\0\0 K_DESC_STR
LmsOverrideBehavior structure provides a means to override the SIL's
behavior.
See "{lss" for details on LmsSelect
struct LmsOverrideBehavior {
LmsSelect lmssTargetBehavior; // Enumerated SIL behavior
Flag fRamp; // Set to 1 (LMS_RAMP) for a slew-rate
// controlled transition. Set to 0
// (LMS_NO_RAMP) for a step change.
}
{lss \0\0\0\0 K_DESC_STR
LmsSelect behavior enum
enum LmsSelect { kLmsOff, // SIL off
kLmsOn, // SIL on, autoscale OK
kLmsBreathe, // SIL breathing, autoscale OK
kLmsBrightNoScale // SIL on bright, no autoscale
// (for power switch override)
}
{msp \0\0\0\0 K_DESC_STR
SSMPowerState typedef
enum SSMPowerState { SSM_POWER_STATE_S0 = 0,
SSM_POWER_STATE_S3 = 1,
SSM_POWER_STATE_S4 = 2,
SSM_POWER_STATE_S5 = 3,
SSM_POWER_STATE_G3_AC = 4,
SSM_POWER_STATE_G3_HOT = 5,
SSM_POWER_STATE_QUERY = 6,
}
{mss \0\0\0\0 K_DESC_STR
SSMState typedef
enum SSMState { SSM_S0_DISP_WAKE = 0,
SSM_S0_DISP_SLEEP = 1,
SSM_G3_HOT = 2,
SSM_S3_SLEEP = 3,
SSM_S4_HIBER = 4,
SSM_S5_OFF = 5,
SSM_S0_ASP_WAIT = 6,
SSM_S0_IMVP_WAIT = 7,
SSM_S0_EARLY_DISP_SLEEP = 8,
SSM_S0_EARLY_DISP_WAKE = 9,
SSM_S3_EARLY = 10,
SSM_S4_EARLY = 11,
SSM_QUERY = 12,
SSM_ICH_RST = 13,
SSM_G2_BATTERY_DEAD = 14,
SSM_G2_POWER_WAIT = 15,
SSM_G2_RESET_WAIT = 16,
SSM_G3_AC = 17,
SSM_G2_ACPWR_WAIT = 18,
SSM_G2_ACRST_WAIT = 19
}
{pwm \0\0\0\0 K_DESC_STR
PWMValue typedef
typedef UInt16 PWMValue;
0xFFFF is full-on, 0x0 is full-off.
*/
}
else if (memcmp(keyvalue->vtype, "flag", 4) == 0) {
reportfd(F_ALWAYS|F_NL, "%03o",(unsigned char) keyvalue->vbytes[0]);
}
else if (memcmp(keyvalue->vtype, "hex_", 4) == 0) {
// dump_ascii(params, keyvalue->vsize, keyvalue->vbytes);
dump_hex(keyvalue->vsize, keyvalue->vbytes);
}
else if (memcmp(keyvalue->vtype, "{rev", 4) == 0) {
release = (short) (keyvalue->vbytes[4]<<8) | keyvalue->vbytes[5];
reportfd(F_ALWAYS|F_NL, "Version: %x.%x%x%x",
(unsigned char) keyvalue->vbytes[0],
(unsigned char) keyvalue->vbytes[1],
(unsigned char) keyvalue->vbytes[2], release);
}
else {
dump_ascii(keyvalue->vsize, keyvalue->vbytes);
dump_hex( keyvalue->vsize, keyvalue->vbytes);
}
}
else {
reportfd(F_ALWAYS, "\n");
}
return(0);
}
short readshort(char *sp)
{
short rval;
rval = (sp[0]&0xff) << 8 | (sp[1]&0xff);
return (rval);
}
ushort readushort(char *sp)
{
ushort rval;
rval = (sp[0]&0xff) << 8 | (sp[1]&0xff);
return (rval);
}
int dump_hex(int size, char *buf)
{
int idx;
int outbytes;
int r0;
outbytes = 0;
r0 = reportfd(F_ALWAYS, "|(");
if (r0 >= 0) {
outbytes = 2;
// F_BYTEORDER
if (flag_test(F_BYTEORDER)) {
for (idx=size-1;idx >=0;idx--) {
if (dump_hex_byte(size, buf, idx, -1) <= 0 ) {
break;
}
}
}
else {
for (idx=0;idx< size;idx++) {
if (dump_hex_byte(size, buf, idx, 1) <= 0 ) {
break;
}
}
}
if (r0 >= 0) {
r0 = reportfd(F_DUMPHEX, ") ");
if (r0 >= 0 ) {
outbytes += r0;
} else {
outbytes = r0;
}
}
}
else {
outbytes = r0;
}
if (flag_test(F_BINARY)) {
if (flag_test(F_BYTEORDER)) {
outbytes += dump_binary_h2l(size, buf);
outbytes += dump_binary_l2h(size, buf);
}
else {
outbytes += dump_binary_l2h(size, buf);
outbytes += dump_binary_h2l(size, buf);
}
}
r0 = reportfd(F_DUMPHEX|F_NL, "");
return (outbytes);
}
int dump_hex_byte(int size, char *buf, int idx, int order)
{
int r0;
r0 = reportfd(F_DUMPHEX, "%02X ", (unsigned char) buf[idx]);
if (r0>=0) {
if (isnull2end(size-idx, &buf[idx])) {
return(0); // 0 should be exit the calls loop ;
}
}
return (r0);
}
int dump_binary_l2h(int size, char *buf)
{
int idx, bidx;
int outbytes;
int r0;
int bit, byte;
outbytes = 0;
r0 = reportfd(F_ALWAYS, "|(");
if (r0 >= 0) {
outbytes = 2;
for (idx=0;idx< size;idx++) {
byte = buf[idx];
for (bidx = 0; bidx < 8; bidx++) {
bit = (byte&1)?1:0;
byte >>= 1;
r0 = reportfd(F_BINARY, "%d", bit);
if (r0>=0) {
outbytes += r0;
} else {
outbytes = r0;
break; // escape this loop there must have been an error
}
}
}
if (r0 >= 0) {
r0 = reportfd(F_DUMPHEX|F_NL, ")");
if (r0 >= 0 ) {
outbytes += r0;
}
else {
outbytes = r0;
}
}
}
else {
outbytes = r0;
}