-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoshset.cc
2121 lines (1906 loc) · 60.8 KB
/
toshset.cc
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
/* toshset.cc
*
* Copyright (C) 1999, 2000, 2001, 2002 Charles D. Schwieters
*
* Based on tools by Jonathan Buzzard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
static char const rcsid[]="$Id: toshset.cc,v 1.51 2010-02-12 16:30:51 schwitrs Exp $";
static char const rcsversion[]="$State: Exp $";
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sys/wait.h>
#include <ctype.h>
#include<errno.h>
#include<signal.h>
#include<paths.h>
#include<pwd.h>
#include <termios.h>
#include<sys/ioctl.h>
#ifdef LINUX
# include<features.h>
# include<sys/vt.h>
# include<sys/kd.h>
#else /* LINUX */
# include <term.h>
#endif
#include <iostream>
#include <streambuf>
#include <iomanip>
#include "cdsList.hh"
#include "cdsString.hh"
#include "cdsSStream.hh"
#include "cdsExcept.hh"
#ifdef __GLIBC__
#include<sys/perm.h>
#endif
#include "kernelInterface.h"
#include "sci.h"
#include "hci.h"
#include "toshibaIDs.hh"
#include "wildmat.h"
using namespace std;
enum { EMPTY, PRIMARY, AUXILLARY };
static int id;
static char versionString[80];
static int verbose=0;
static int longQuery=0;
static int fast=0;
/*
* Convert a single character to an interger, similar to atoi
*/
inline int ctoi(char *s)
{
return ((*s>='0') && (*s<='9')) ? *s-'0' : -1;
}
struct ValueSet {
const char* iString;
unsigned short sciCode;
const char* oString;
ValueSet(const char* input,
unsigned short sciCode,
const char* output) :
iString(input), sciCode(sciCode), oString(output) {}
};
struct Feature {
const char* name;
Feature(const char* name) : name(name) {}
virtual ~Feature() {}
// the next two return 0 on success, otherwise return an error code
// which is interpreted using error()
virtual int action(const char**) const=0;
virtual int query(OStringStream &os) const=0;
virtual const char* error(int) const=0;
};
struct ToggleFeature : public Feature {
int& toggleVar;
// const char* name;
ToggleFeature( int& toggleVar,
const char* name) :
Feature(name), toggleVar(toggleVar) {}
virtual ~ToggleFeature() {}
virtual int action(const char**) const
{ toggleVar = (toggleVar?0:1); return 1;}
virtual int query(OStringStream &os) const {return 1;}
virtual const char* error(int) const {return "";}
};
struct VersionFeature : public Feature {
VersionFeature() :
Feature("toshset version") {}
virtual ~VersionFeature() {}
virtual int action(const char**) const {return 1;}
virtual int query(OStringStream &os) const {
os << "toshset version: " << VERSION << ends; return 0;}
virtual const char* error(int) const {return "";}
};
struct AccessFeature : public Feature {
AccessFeature() :
Feature("HCI/SCI access") {}
virtual ~AccessFeature() {}
virtual int action(const char**) const {return 1;}
virtual int query(OStringStream &os) const {
os << "HCI/SCI access mode: "
<< (accessMode==ACCESS_DIRECT?"direct":"kernel") << ends; return 0;}
virtual const char* error(int) const {return "";}
};
struct ModelFeature : public Feature {
int id;
ModelFeature(int id) :
Feature("hardware model"), id(id) {}
virtual ~ModelFeature() {}
virtual int action(const char**) const {return 1;}
virtual int query(OStringStream &os) const {
os << "Toshiba Model: " << toshibaModelName(id) << ends; return 0;}
virtual const char* error(int) const {return "";}
};
struct SciFeature : public Feature {
unsigned short sciMode;
// const char* name;
SciFeature(unsigned short sciMode,const char* name) :
Feature(name), sciMode(sciMode) {}
CDSList<ValueSet*> values;
void addValue(const char* input,
unsigned short sciCode,
const char* output)
{ values.append(new ValueSet(input,sciCode,output)); }
virtual ~SciFeature()
{ for (int i=0 ; i<values.size() ; i++) delete values[i];}
virtual int action(const char**) const;
virtual int query(OStringStream &os) const;
const char* error(int code) const;
};
const char*
SciFeature::error(int code) const
{
const char* ret;
static char defret[80];
snprintf(defret,80,"%s (%d)","unknown error.",code);
ret = defret;
switch ( code ) {
case SCI_FAILURE : ret = "FAILURE" ; break;
case SCI_NOT_SUPPORTED : ret = "NOT_SUPPORTED" ; break;
case SCI_ALREADY_OPEN : ret = "ALREADY_OPEN" ; break;
case SCI_NOT_OPENED : ret = "NOT_OPENED" ; break;
case SCI_INPUT_ERROR : ret = "INPUT_ERROR" ; break;
case SCI_WRITE_PROTECTED : ret = "WRITE_PROTECTED" ; break;
case SCI_NOT_PRESENT : ret = "NOT_PRESENT" ; break;
case SCI_NOT_READY : ret = "NOT_READY" ; break;
case SCI_DEVICE_ERROR : ret = "DEVICE_ERROR" ; break;
case SCI_NOT_INSTALLED : ret = "NOT_INSTALLED" ; break;
}
return ret;
} /* SciFeature::error */
int
SciFeature::action(const char **s) const
{
SMMRegisters reg;
reg.ebx = sciMode;
if ( ! **s) {
cerr << "SCI error: argument required\n";
return 0;
}
int ret = -1;
for (int i=0 ; i<values.size() ; i++)
if ( strcmp( values[i]->iString , *s ) == 0 ) {
reg.ecx =values[i]->sciCode;
reg.edx = reg.esi = reg.edi = 0;
ret = SciSet( ® );
if ( ret == SCI_SUCCESS )
return 1;
}
// try setting indexed setting of option.
if ( ret<0 ) {
errno=0;
int i = strtol(*s,(char **)NULL,10);
if ( errno==0 && i>=0 && i<values.size() ) {
reg.ecx =values[i]->sciCode;
reg.edx = reg.esi = reg.edi = 0;
ret = SciSet( ® );
if ( ret == SCI_SUCCESS )
return 1;
}
}
if ( ret>=0 ) {
cerr << "SciFeature:action: error setting " << name << '\n';
cerr << "\tSciSet returned: " << error(ret) <<'\n';
return 0;
}
cerr << "SCIFeature: error when setting feature " << name
<< ":\n\terror in command-line option: " << *s << "\n";
cerr << "valid settings are:\n";
for (int i=0 ; i<values.size() ; i++)
cerr << "\t(" << i << ") " << values[i]->iString << '\n';
return 0;
} /* SciFeature::action */
int
SciFeature::query(OStringStream& os) const
{
SMMRegisters reg;
reg.ebx = sciMode;
int ret = SciGet( ® );
if ( ret == SCI_SUCCESS ) {
for (int i=0 ; i<values.size() ; i++)
if ( reg.ecx == values[i]->sciCode ) {
os << name << ": " << values[i]->oString << ends;
return 0;
}
cerr << "SciFeature::query: received an unexpected response for feature "
<< name << ": " << reg.ecx << '\n';
}
//else
// cerr << "SciFeature:query: error when querying feature " << name << '\n'
// << "\tSciGet returned " << error(ret) << '\n';
return ret;
} /* SciFeature::action */
struct TimeFeature : public SciFeature {
TimeFeature(unsigned short sciMode,const char* name) :
SciFeature(sciMode,name) {}
virtual ~TimeFeature()
{ for (int i=0 ; i<values.size() ; i++) delete values[i];}
virtual int action(const char**) const;
virtual int query(OStringStream& os) const;
};
int
TimeFeature::action(const char **s) const
{
if (!**s) {
cout << "<dis|HH:MM[/everyday|DD/MM[/YYYY]]> time/date to wake\n";
return 0;
}
SMMRegisters reg;
enum {
ALARM_TIME = 0x0001,
ALARM_DATE = 0x0002,
ALARM_EVERY = 0x0004,
ALARM_YEAR = 0x0008
};
int support = 0x0000;
reg.ebx = SCI_ALARM_TIME;
if (SciGet(®)==SCI_SUCCESS) {
support |= ALARM_TIME;
}
reg.ebx = SCI_ALARM_DATE;
if ( SciGet(®) == SCI_SUCCESS ) {
reg.edx &= 0xffff;
support |= ALARM_DATE;
if ( SCI_DATE_EVERYDAY(reg.edx) )
support |= ALARM_EVERY;
if (SCI_YEAR(reg.edx)!=1990)
support |= ALARM_YEAR;
}
if ( verbose )
cout << "TimeFeature: supported date elements: "
<< (support&ALARM_DATE?"month/day ":"")
<< (support&ALARM_YEAR?"year ":"")
<< (support&ALARM_EVERY?"everyday\n":"\n");
reg.ebx = sciMode;
int ret=0;
if ( strncmp(*s,"dis",3) == 0 ) {
reg.ecx = SCI_ALARM_DISABLED;
ret = SciSet( ® );
} else {
IStringStream istr(*s);
char delim;
char hour[4];
istr.get(hour,4,':'); istr.get( delim );
char minute[4];
istr.get(minute,4,'/'); istr.get( delim );
reg.ecx = SCI_TIME( atoi(hour) , atoi(minute) );
//cout << "setting time\n";
ret = SciSet( ® );
if ( delim == '/' && ret == SCI_SUCCESS && support&ALARM_DATE ) {
reg.ebx = SCI_ALARM_DATE;
//reg.ebx = sciMode;
reg.ecx = 0;
reg.edx = 0;
delim = '\0';
char day[9];
char month[3];
char year[9]; year[0]='\0';
istr.get(day,9,'/'); istr.get( delim );
if ( delim=='\0' && strcmp(day,"everyday")==0 ) {
if (support&ALARM_EVERY)
reg.ecx = 1;
else
cerr << "TimeFeature::action: everyday not supported\n";
} else {
delim = '\0';
istr.get(month,3,'/'); istr.get( delim );
if ( delim=='/' && support&ALARM_YEAR) {
istr.get(year,9,'/');
reg.ecx = SCI_FULLDATE( atoi(year) , atoi(month) , atoi(day) );
} else
reg.ecx = SCI_DATE( atoi(month) , atoi(day) );
}
//cout << "setting date to " << day << '/' << month << '/' << year << '\n';
if (reg.ecx) ret=SciSet( ® );
if ( ret != SCI_SUCCESS ) {
cerr << "TimeFeature:action: error when setting date for feature "
<< name << '\n'
<< "\tSciSet returned " << error(ret) << '\n';
return 0;
}
}
}
if ( ret == SCI_SUCCESS )
return 1;
else
cerr << "TimeFeature:action: error when setting feature " << name << '\n'
<< "\tSciSet returned " << error(ret) << '\n';
return 0;
} /* TimeFeature::action */
int
TimeFeature::query(OStringStream& os) const
{
SMMRegisters reg;
reg.ebx = sciMode;
int ret = SciGet( ® );
if ( ret == SCI_SUCCESS ) {
os << name << ": ";
if ( reg.ecx == SCI_ALARM_DISABLED )
os << "disabled";
else {
os << setfill('0') << setw(2) << SCI_HOUR(reg.ecx) << ':'
<< setfill('0') << setw(2) << SCI_MINUTE(reg.ecx) << ' ';
reg.ebx = SCI_ALARM_DATE;
SciGet( ® );
int year = SCI_YEAR(reg.ecx);
if ( SCI_DATE_EVERYDAY(reg.ecx) )
os << "everyday";
else {
os << SCI_DAY(reg.ecx) << '/'
<< SCI_MONTH(reg.ecx);
if ( year !=1990 )
os << year << '/';
}
}
os << ends;
return 0;
}
return ret;
//else
// cerr << "TimeFeature:query: error when querying feature " << name << '\n'
// << "\tSciGet returned " << error(ret) << '\n';
// return 0;
} /* SciFeature::action */
struct PasswdFeature : public SciFeature {
unsigned short passwdType;
PasswdFeature(unsigned short passwdType,const char* name) :
SciFeature(SCI_PASSWORD,name), passwdType(passwdType) {}
virtual ~PasswdFeature() {}
virtual int action(const char**) const;
virtual int query(OStringStream& os) const;
};
/*
* Put terminal into raw mode and then get a single keypress
*/
int GetScanCode()
{
struct termio new_key;
static struct termio saved;
unsigned char key[1];
int fd = open("/dev/tty", O_RDWR);
/* exit if unable to open the console */
if ( fd<0 ) {
cerr <<"unable to open console\n";
throw CDS::exception("unable to open console\n");
}
//terminal = 1;
/* get the current terminal state */
int keyboard;
ioctl(fd, KDGKBMODE, &keyboard);
ioctl(fd, TCGETA, &saved);
ioctl(fd, TCGETA, &new_key);
/* set appropriate terminal mode */
new_key.c_lflag &= ~ (ICANON | ECHO | ISIG);
new_key.c_iflag = 0;
new_key.c_cc[VMIN] = 1;
new_key.c_cc[VTIME] = 1;
ioctl(fd, TCSETAW, &new_key);
ioctl(fd, KDSKBMODE, K_RAW);
key[0] = 0;
read(fd, key, 1);
ioctl(fd, TCSETAW, &saved);
ioctl(fd, KDSKBMODE, keyboard);
keyboard = -1;
/* close the connection to the terminal */
close(fd);
// terminal = 0;
return (int) key[0];
}
/*
* Get a password from the user echoing astrix's
*/
void GetPassword(char *password)
{
/* blank the password */
for(int loop=0 ; loop<10 ; loop++)
*(password+loop) = 0;
fflush(stdout);
/* now get the password from the user*/
for (int loop=0;;) {
int scan = GetScanCode();
if ((scan<0x02) || (scan>0x39)) continue;
if ((scan==0x1d) || (scan==0x2a) || (scan==0x36)
|| (scan==0x37) || (scan==0x38)) continue;
if (scan==0x0f) continue;
if (scan==0x1c) {
cout << endl;
break;
}
if (scan==0x0e) {
if (loop>0) {
cout << "\b \b";
fflush(stdout);
loop--;
*(password+loop) = 0;
}
continue;
}
if (loop<10) {
*(password+loop) = scan;
cout << '*';
cout.flush();
loop++;
}
}
return;
}
static int
setPasswd(char* password,
unsigned short passwdType)
{
SMMRegisters reg;
reg.eax = 0xf4f4;
reg.ebx = SCI_PASSWORD;
reg.ecx = passwdType;
reg.edx = *(password+3) + *(password+2)*0x100 + *(password+1)*0x10000 +
*(password)*0x1000000;
reg.esi = *(password+7) + *(password+6)*0x100 + *(password+5)*0x10000 +
*(password+4)*0x1000000;
reg.edi = *(password+9) + *(password+8)*0x100;
int ret = SciSet( ® );
int trys = (int) (reg.edx & 0xffff);
if ( trys <1 )
cerr << "setPasswd: maximum password trys exceeded.\n"
<< "\tplease reboot or suspend/resume and try again.\n";
return ret;
} /* setPasswd */
int
PasswdFeature::action(const char **s) const
{
int fd = open("/dev/tty", O_RDWR);
if (fd<0) {
cerr << "PasswdFeature: unable to open console.\n";
return 0;
}
struct vt_mode vtm;
if (ioctl(fd, VT_GETMODE, &vtm)!=0) {
close(fd);
cerr << "PasswdFeature: must be run from the console.\n";
return 0;
}
close(fd);
SMMRegisters reg;
reg.ebx = sciMode;
reg.ecx = passwdType;
int ret = SciGet( ® );
if ( ret != SCI_SUCCESS )
return ret;
if ( reg.ecx == 0 ) {
// not registered
char password1[11];
cout << "enter new password:";
GetPassword(password1);
char password2[11];
cout << "reenter new password:";
GetPassword(password2);
if ( String(password1) != password2 ) {
cerr << "passwords do not match...\n";
return 1;
}
ret = setPasswd(password1,passwdType);
} else if (reg.ecx == 1) {
//registered
char password1[11];
cout << "enter current password to disable:";
GetPassword(password1);
ret = setPasswd(password1,passwdType);
} else
ret = SCI_FAILURE;
if ( ret == SCI_SUCCESS )
return 1;
else
cerr << "PasswdFeature:action: error when setting feature " << name << '\n'
<< "\tSciSetPasswd returned " << error(ret) << '\n';
return 0;
} /* PasswdFeature::action */
int
PasswdFeature::query(OStringStream& os) const
{
SMMRegisters reg;
reg.ebx = sciMode;
reg.ecx = passwdType;
int ret = SciGet( ® );
if ( ret == SCI_SUCCESS ) {
os << name << ": ";
switch ( reg.ecx ) {
case 0 : os << "not registered"; break;
case 1 : os << "registered"; break;
default : os << "unexpected response: " << reg.ebx;
}
os << ends;
return 0;
}
//else
// cerr << "PasswdFeature:query: error when querying feature " << name << '\n'
// << "\tSciGet returned " << error(ret) << '\n';
// return 0;
return ret;
} /* PasswdFeature::query */
struct PercentFeature : public SciFeature {
PercentFeature(unsigned short sciMode,const char* name) :
SciFeature(sciMode,name) {}
~PercentFeature() {}
int action(const char**) const {return 1;}
int query(OStringStream& os) const;
};
int
PercentFeature::query(OStringStream& os) const
{
SMMRegisters reg;
reg.ebx = sciMode;
int ret = SciGet( ® );
if ( ret == SCI_SUCCESS ) {
int percent = ((100*reg.ecx)/reg.edx);
os << name << ": " << percent << "\% " << ends;
}
return ret;
} /* Feature::action */
struct HciFeature : public Feature {
unsigned short hciMode;
HciFeature(unsigned short hciMode,const char* name) :
Feature(name), hciMode(hciMode) {}
CDSList<ValueSet*> values;
void addValue(const char* input,
unsigned short hciCode,
const char* output)
{ values.append(new ValueSet(input,hciCode,output)); }
virtual ~HciFeature()
{ for (int i=0 ; i<values.size() ; i++) delete values[i];}
virtual int action(const char**) const;
virtual int query(OStringStream& os) const;
virtual const char* error(int) const;
};
const char*
HciFeature::error(int code) const
{
const char* ret;
static char defret[80];
snprintf(defret,80,"%s (%d)","unknown error.",code);
ret = defret;
switch (code) {
case HCI_FAILURE : ret = "FAILURE" ; break;
case HCI_NOT_SUPPORTED : ret = "NOT_SUPPORTED" ; break;
case HCI_INPUT_ERROR : ret = "INPUT_ERROR" ; break;
case HCI_WRITE_PROTECTED : ret = "WRITE_PROTECTED" ; break;
case HCI_FIFO_EMPTY : ret = "FIFO_EMPTY" ; break;
case HCI_NOTREADY : ret = "NOTREADY" ; break;
}
return ret;
} /* HciFeature::error */
int
HciFeature::action(const char **s) const
{
SMMRegisters reg;
reg.eax = HCI_SET;
reg.ebx = hciMode;
reg.edx = 0;
if ( ! **s) {
cerr << "HCI error: argument required\n";
return 0;
}
int ret = -1;
for (int i=0 ; i<values.size() ; i++)
if ( strcmp( values[i]->iString , *s ) == 0 ) {
reg.ecx =values[i]->sciCode;
ret = HciFunction( ® );
if ( ret == HCI_SUCCESS )
return 1;
}
// try setting indexed setting of option.
int i = atoi(*s);
if ( ret<0 && isdigit(**s) && i>=0 && i<values.size() ) {
reg.ecx =values[i]->sciCode;
int ret = HciFunction( ® );
if ( ret == HCI_SUCCESS ) {
//sleep(1);
return 1;
}
}
if ( ret>=0 ) {
cerr << "HCI error setting " << name << '\n';
cerr << "\tHciFunction returned: " << error(ret) <<'\n';
return 0;
}
cerr << "HCI error setting " << name << '\n';
cerr << "valid settings are:\n";
for (int i=0 ; i<values.size() ; i++)
cerr << "\t(" << i << ") " << values[i]->iString << '\n';
return 0;
} /* Feature::action */
int
HciFeature::query(OStringStream& os) const
{
SMMRegisters reg;
reg.eax = HCI_GET;
reg.ebx = hciMode;
reg.ecx = 0;
reg.edx = 0;
int ret=0;
int retries=3;
while ( --retries ) {
ret = HciFunction( ® );
if ( ret != HCI_BUSY )
break;
}
// reg.ecx &= ~0x0080; //this is some sort of status bit
if ( ret == HCI_SUCCESS ) {
for (int i=0 ; i<values.size() ; i++)
if ( reg.ecx == values[i]->sciCode ) {
os << name << ": " << values[i]->oString << ends;
return 0;
}
cerr << "HciFeature::query: received an unexpected response for feature "
<< name << ": " << reg.ecx << '\n';
}
return ret;
} /* HciFeature::query */
// are these really constant??
const int HCI_LCD_BRIGHTNESS_BITS = 3;
const int HCI_LCD_BRIGHTNESS_SHIFT = (16-HCI_LCD_BRIGHTNESS_BITS);
const int HCI_LCD_BRIGHTNESS_LEVELS = (1 << HCI_LCD_BRIGHTNESS_BITS);
struct LCDIntensityFeature : public HciFeature {
LCDIntensityFeature(const char* name) :
HciFeature(HCI_LCD_BRIGHTNESS,name) {}
virtual int action(const char**) const;
virtual int query(OStringStream& os) const;
};
int
LCDIntensityFeature::action(const char **s) const
{
SMMRegisters reg;
reg.eax = HCI_SET;
reg.ebx = hciMode;
reg.edx = 0;
if ( ! **s) {
cerr << "HCI error: argument required\n";
return 0;
}
int ret = -1;
int val = atoi(*s);
if (val >=0 && val < HCI_LCD_BRIGHTNESS_LEVELS) {
reg.ecx =val << HCI_LCD_BRIGHTNESS_SHIFT;
ret = HciFunction( ® );
if ( ret == HCI_SUCCESS )
return 1;
}
if ( ret>=0 ) {
cerr << "HCI error setting " << name << '\n';
cerr << "\tHciFunction returned: " << error(ret) <<'\n';
return 0;
}
cerr << "HCI error setting " << name << '\n';
cerr << "valid settings are: 0-" << (HCI_LCD_BRIGHTNESS_LEVELS-1) << '\n';
return 0;
} /* LCDIntensityFeature::action */
int
LCDIntensityFeature::query(OStringStream& os) const
{
SMMRegisters reg;
reg.eax = HCI_GET;
reg.ebx = hciMode;
reg.ecx = 0;
reg.edx = 0;
int ret=0;
ret = HciFunction( ® );
// reg.ecx &= ~0x0080; //this is some sort of status bit
if ( ret == HCI_SUCCESS ) {
int value = reg.ecx >> HCI_LCD_BRIGHTNESS_SHIFT;
os << name << ": " << value << "/" << (HCI_LCD_BRIGHTNESS_LEVELS-1) << ends;
return 0;
}
return ret;
} /* LCDIntensityFeature::query */
struct WirelessFeature : public HciFeature {
int mode;
WirelessFeature(int mode,
const char* name) :
HciFeature(HCI_WIRELESS,name), mode(mode) {}
virtual int action(const char**) const;
virtual int query(OStringStream& os) const;
};
int
WirelessFeature::action(const char **s) const
{
SMMRegisters reg;
reg.eax = HCI_SET;
reg.ebx = hciMode;
reg.edx = mode;
if ( ! **s) {
cerr << "HCI error: argument required\n";
return 0;
}
int ret = -1;
for (int i=0 ; i<values.size() ; i++)
if ( strcmp( values[i]->iString , *s ) == 0 ) {
reg.ecx =values[i]->sciCode;
ret = HciFunction( ® );
if ( ret == HCI_SUCCESS )
return 1;
}
// try setting indexed setting of option.
int i = atoi(*s);
if ( ret<0 && strlen(*s)==1 && isdigit(**s) && i>=0 && i<values.size() ) {
reg.ecx =values[i]->sciCode;
int ret = HciFunction( ® );
if ( ret == HCI_SUCCESS ) {
//sleep(1);
return 1;
}
}
if ( ret>=0 ) {
cerr << "HCI error setting " << name << '\n';
cerr << "\tHciFunction returned: " << error(ret) <<'\n';
return 0;
}
cerr << "HCI error setting " << name << '\n';
cerr << "valid settings are:\n";
for (int i=0 ; i<values.size() ; i++)
cerr << "\t(" << i << ") " << values[i]->iString << '\n';
return 0;
} /* WirelessFeature::action */
int
WirelessFeature::query(OStringStream& os) const
{
SMMRegisters reg;
reg.eax = HCI_GET;
reg.ebx = hciMode;
reg.ecx = 1;
reg.edx = mode;
int ret=0;
ret = HciFunction( ® );
// cerr << "cx: " << reg.ecx <<endl;
// cerr << "dx: " << reg.edx <<endl;
if ( ret!=0 ) return ret;
if ( ret != HCI_SUCCESS )
return ret;
os << name << ": ";
if (!(reg.ecx & 0x0f))
os << "unavailable";
else
os << (reg.ecx&1 ? "on":"off");
os << ends;
return ret;
} /* WirelessFeature::query */
struct BlueToothFeature : public HciFeature {
BlueToothFeature(const char* name) :
HciFeature(HCI_WIRELESS,name) {}
virtual int action(const char**) const;
virtual int query(OStringStream& os) const;
};
int
BlueToothFeature::action(const char** c) const
{
SMMRegisters reg;
reg.eax = HCI_GET;
reg.ebx = hciMode;
reg.ecx = 0;
reg.edx = 0;
int ret=0;
ret = HciFunction( ® );
if ( ret!=0 ) {
// cerr << "error querying bluetooth\n";
return ret;
}
if (!(reg.ecx & 0x0f)) {
cerr << "Bluetooth unavailable\n";
return ret;
}
reg.eax = HCI_GET;
reg.ebx = hciMode;
reg.ecx = 0;
reg.edx = 1;
ret=0;
ret = HciFunction( ® );
if ( ret!=0 ) {
cerr << "error checking Bluetooth switch status\n";
return ret;
}
if(!(reg.ecx & 0x1)) {
cerr << "wireless switch is off\n";
return ret;
}
if ( (String(*c) == "on") ||
(String(*c) == "1") ) { // turn on
reg.eax = HCI_SET;
reg.ebx = hciMode;
reg.ecx = 1;
reg.edx = 0x80;
ret=0;
ret = HciFunction( ® );
if ( ret!=0 ) {
cerr << "error activating Bluetooth device\n";
return ret;
}
//cerr << "wireless switch is activated\n";
// HciFunction( 0 );
sleep(1);
reg.eax = HCI_SET;
reg.ebx = hciMode;
reg.ecx = 1;
reg.edx = 0x40;
ret=0;
ret = HciFunction( ® );
if ( ret!=0 ) {
cerr << "error attaching Bluetooth device\n";
return ret;
}
//cerr << "wireless switch is attached\n";
}
if ( (String(*c) == "off") ||
(String(*c) == "0") ) { // turn off
reg.eax = HCI_SET;
reg.ebx = hciMode;
reg.ecx = 0;
reg.edx = 0x40;
ret=0;
ret = HciFunction( ® );
if ( ret!=0 ) {
cerr << "error detaching Bluetooth device\n";
return ret;
}
sleep(1);
reg.eax = HCI_SET;
reg.ebx = hciMode;
reg.ecx = 0;
reg.edx = 0x80;
ret=0;
ret = HciFunction( ® );
if ( ret!=0 ) {
cerr << "error deactivating Bluetooth device\n";
return ret;
}
//cerr << "wireless switch is activated\n";
// HciFunction( 0 );
//cerr << "wireless switch is attached\n";
return ret;
}
return ret;
} /* BlueToothFeature::action */
int
BlueToothFeature::query(OStringStream& os) const
{
SMMRegisters reg;
reg.eax = HCI_GET;
reg.ebx = hciMode;
reg.ecx = 0;
reg.edx = 0;
int ret=0;
ret = HciFunction( ® );
if ( ret!=0 ) {
// cerr << "error querying bluetooth\n";
return ret;
}
if (!(reg.ecx & 0x0f)) {
cerr << "Bluetooth unavailable\n";
return ret;
}
reg.eax = HCI_GET;
reg.ebx = hciMode;
reg.ecx = 0;
reg.edx = 1;
ret=0;
ret = HciFunction( ® );
if ( ret!=0 ) {
cerr << "error checking Bluetooth switch status\n";
return ret;
}