Skip to content

Commit 659b27b

Browse files
authored
Update xec.cpp
1 parent 376c6ad commit 659b27b

File tree

1 file changed

+168
-68
lines changed

1 file changed

+168
-68
lines changed

xec.cpp

Lines changed: 168 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
//#ifndef _COLORS_
2+
//#define _COLORS_
3+
//
4+
///* FOREGROUND */
5+
//#define _reset "\x1B[0m"
6+
//#define KRED "\x1B[31m"
7+
//#define KGRN "\x1B[32m"
8+
//#define KYEL "\x1B[33m"
9+
//#define KBLU "\x1B[34m"
10+
//#define KMAG "\x1B[35m"
11+
//#define KCYN "\x1B[36m"
12+
//#define KWHT "\x1B[37m"
13+
//#define KLRD "\033[1;31m"
14+
//#define KLGN "\033[1;32m"
15+
//#define _inverse "\033[1;7m"
16+
//
17+
//#define FRED(x) KRED x _reset
18+
//#define FGRN(x) KGRN x _reset
19+
//#define FYEL(x) KYEL x _reset
20+
//#define FBLU(x) KBLU x _reset
21+
//#define FMAG(x) KMAG x _reset
22+
//#define FCYN(x) KCYN x _reset
23+
//#define FWHT(x) KWHT x _reset
24+
//
25+
//#define BOLD(x) "\x1B[1m" x _reset
26+
//#define UNDL(x) "\x1B[4m" x _reset
27+
//
28+
//#endif /* _COLORS_ */
29+
130
#include <iostream>
231
#include <stdio.h>
332
#include <string.h>
@@ -6,11 +35,67 @@
635
#include <cctype>
736
#include <sstream>
837
#include <string>
9-
1038
using namespace std;
1139

1240
int ec_fd;
1341

42+
int max_waits = 20, wait_faults;
43+
44+
//(void)&ttyf;
45+
//int ttyf = isatty(1) ? TTY : ORDINARY;
46+
47+
string _reset, _bright, _underline, _blink, _inverse, _black, _red, _green,
48+
_yellow, _blue, _magenta, _cyan, _white, _black_bg, _red_bg, _green_bg,
49+
_yellow_bg, _blue_bg, _magenta_bg, _cyan_bg, _white_bg;
50+
51+
extern char **environ;
52+
53+
int run(int argc, char **argv);
54+
55+
int main(int argc, char **argv, char **envp) {
56+
57+
bool isColored = 0;
58+
for (size_t i = 1; i < argc; ++i) {
59+
if (argv[i] == std::string("-c") ) {
60+
isColored = 1;
61+
}
62+
}
63+
64+
// for (char **env = envp; *env != 0; env++)
65+
// {
66+
// char *thisEnv = *env;
67+
// printf("%s\n", thisEnv);
68+
// }
69+
70+
if (isatty(STDOUT_FILENO) ^ isColored) {
71+
_reset = "\x1b[0m";
72+
_bright = "\x1b[1m";
73+
_underline = "\x1b[4m";
74+
_blink = "\x1b[5m";
75+
_inverse = "\x1b[7m";
76+
77+
_black = "\x1b[30m";
78+
_red = "\x1b[31m";
79+
_green = "\x1b[32m";
80+
_yellow = "\x1b[33m";
81+
_blue = "\x1b[34m";
82+
_magenta = "\x1b[35m";
83+
_cyan = "\x1b[36m";
84+
_white = "\x1b[37m";
85+
86+
_black_bg = "\x1b[40m";
87+
_red_bg = "\x1b[41m";
88+
_green_bg = "\x1b[42m";
89+
_yellow_bg = "\x1b[43m";
90+
_blue_bg = "\x1b[44m";
91+
_magenta_bg = "\x1b[45m";
92+
_cyan_bg = "\x1b[46m";
93+
_white_bg = "\x1b[47m";
94+
}
95+
96+
run(argc, argv);
97+
}
98+
1499
int err(char *txt) {
15100
throw std::invalid_argument(txt);
16101
}
@@ -32,7 +117,7 @@ enum Status : int {
32117
output_buffer_full = 0x01,
33118
input_buffer_full = 0x02,
34119
command = 0x08,
35-
burst_mode = 0x10,
120+
bu_reset_mode = 0x10,
36121
sci_event_pending = 0x20,
37122
smi_event_pending = 0x40
38123
};
@@ -44,8 +129,8 @@ enum Ports : int {
44129
enum Command : int {
45130
_read = 0x80,
46131
_write = 0x81,
47-
burst_enable = 0x82,
48-
burst_disable = 0x83,
132+
bu_reset_enable = 0x82,
133+
bu_reset_disable = 0x83,
49134
query = 0x84
50135
};
51136

@@ -75,81 +160,80 @@ byte ec_write(uint8_t port, byte data) {
75160
return data;
76161
}
77162

78-
bool ec_wait_status(int port, Status status) {
79-
int timeline = 200;
80-
while (timeline > 0) {
81-
timeline--;
82-
byte f = (byte) ec_read(0x66);
83-
if (((int) status && (int) f) == 0)
84-
return int(1000 - timeline);
163+
//bool ec_wait_status(int port, Status status) {
164+
// int timeline = 200;
165+
// while (timeline > 0) {
166+
// timeline--;
167+
// byte f = (byte) ec_read(0x66);
168+
// if (((int) status && (int) f) == 0)
169+
// return int(1000 - timeline);
170+
// }
171+
// return false;
172+
//}
173+
174+
bool ec_wait_status(int status, bool set) {
175+
int timeout = 500;
176+
while (timeout > 0) {
177+
timeout--;
178+
byte val = ec_read(Ports::command_port);
179+
val = set ? (byte) ~(int) val : val;
180+
if (((int) status & (int) val) == 0) {
181+
return true;
182+
}
85183
}
86184
return false;
87185
}
88186

89-
#ifndef _COLORS_
90-
#define _COLORS_
91-
92-
/* FOREGROUND */
93-
#define RST "\x1B[0m"
94-
#define KRED "\x1B[31m"
95-
#define KGRN "\x1B[32m"
96-
#define KYEL "\x1B[33m"
97-
#define KBLU "\x1B[34m"
98-
#define KMAG "\x1B[35m"
99-
#define KCYN "\x1B[36m"
100-
#define KWHT "\x1B[37m"
101-
#define KGRY "\033[1;30m"
102-
#define KLRD "\033[1;31m"
103-
#define KLGN "\033[1;32m"
104-
#define KRVR "\033[1;7m"
105-
106-
#define FRED(x) KRED x RST
107-
#define FGRN(x) KGRN x RST
108-
#define FYEL(x) KYEL x RST
109-
#define FBLU(x) KBLU x RST
110-
#define FMAG(x) KMAG x RST
111-
#define FCYN(x) KCYN x RST
112-
#define FWHT(x) KWHT x RST
113-
114-
#define BOLD(x) "\x1B[1m" x RST
115-
#define UNDL(x) "\x1B[4m" x RST
116-
117-
#endif /* _COLORS_ */
187+
bool ec_wait_read() {
188+
if (wait_faults > max_waits) {
189+
return true;
190+
} else if (ec_wait_status(Status::output_buffer_full, true)) {
191+
wait_faults = 0;
192+
return true;
193+
} else {
194+
wait_faults++;
195+
return false;
196+
}
197+
}
198+
199+
bool ec_wait_write() {
200+
return ec_wait_status(Status::input_buffer_full, false);
201+
}
118202

119203
int monitor() {
120204

121205
cout << "";
122206
for (uint16_t x = 0; x < 16; x++) {
123207
string append = ((int) x < 16 ? "0" : "");
124-
cout << KRVR << append << uppercase << std::hex << x << " ";
208+
cout << _inverse << append << uppercase << std::hex << x << " ";
125209
}
126-
cout << RST;
210+
cout << _reset;
127211
cout << endl << "──┼";
128212
for (uint16_t x = 0; x < 16 * 3; x++) {
129213
cout << "";
130214
}
131215
cout << endl;
132216

133217
for (int i = 0; i < 256; i++) {
134-
ec_wait_status(Ports::command_port, Status::input_buffer_full);
218+
ec_wait_write();
135219
ec_write(Ports::command_port, (byte) Command::_read);
136-
ec_wait_status(Ports::command_port, Status::input_buffer_full);
220+
ec_wait_write();
137221
ec_write(Ports::data_port, (byte) (i));
138-
ec_wait_status(Ports::command_port, Status::output_buffer_full);
222+
ec_wait_read();
139223
byte data = ec_read(Ports::data_port);
140224

141225
if (i == 0)
142-
cout << KRVR << "00" << RST << "";
226+
cout << _inverse << "00" << _reset << "";
143227

144228
if ((i % 16) == 0 && i != 0) {
145229
cout << endl;
146-
cout << RST << KRVR << uppercase << i << RST << "";
230+
cout << _reset << _inverse << uppercase << i << _reset << "";
147231
}
148232

149-
150233
string append = ((int) data < 16 ? "0" : "");
151234

152-
cout << uppercase << ((int) data == 0xFF ? KLGN : (int) data == 0x00 ? KGRY : KLRD);
235+
cout << uppercase
236+
<< ((int) data == 0xFF ? (_green + _bright) : (int) data == 0x00 ? (_black + _bright) : (_red + _bright));
153237
cout << append << uppercase << std::hex << (int) data << " ";
154238
//cout << std::hex << append << (int) data << " ";
155239
}
@@ -165,10 +249,27 @@ int help() {
165249
cout << endl;
166250

167251
cout << "<example>" << endl;
168-
cout << " watch -n 1 -c -d sudo xec monitor : print EC table with changes highlight every second" << endl;
252+
cout
253+
<< " watch -n 1 -c -d sudo xec monitor : print EC table with changes highlight every second"
254+
<< endl;
255+
cout << endl;
256+
257+
cout << "<arguments>" << endl;
258+
cout
259+
<< " -c : enable colors"
260+
<< endl;
261+
}
262+
263+
#include <cstdlib>
264+
#include <iostream>
265+
266+
int arg_to_int(char *arg) {
267+
int c = (int) atol(arg);
268+
c = (c > (int) 0 ? c : (int) std::stoul(arg, nullptr, 16));
269+
return c;
169270
}
170271

171-
int main(int argc, char **argv) {
272+
int run(int argc, char **argv) {
172273

173274
ec_init();
174275

@@ -178,16 +279,13 @@ int main(int argc, char **argv) {
178279
if (argv[2] == NULL)
179280
err("no address");
180281

181-
unsigned int hex = stoul(argv[2], nullptr, 16);
182-
183-
ec_wait_status(Ports::command_port, Status::input_buffer_full);
282+
ec_wait_write();
184283
ec_write(Ports::command_port, (byte) Command::_read);
185-
ec_wait_status(Ports::command_port, Status::input_buffer_full);
186-
ec_write(Ports::data_port, (byte) hex);
187-
//ec_wait_status(Ports::command_port, Status::input_buffer_full);
188-
ec_wait_status(Ports::command_port, Status::output_buffer_full);
284+
ec_wait_write();
285+
ec_write(Ports::data_port, (byte) arg_to_int(argv[2]));
286+
ec_wait_read();
189287
cout << argv[2] << ": " << uppercase << std::hex << "0x"
190-
<< (int)ec_read(Ports::data_port) << endl;
288+
<< (int) ec_read(Ports::data_port) << endl;
191289

192290
} else if (strcmp(argv[1], "write") == 0) {
193291

@@ -196,23 +294,25 @@ int main(int argc, char **argv) {
196294
if (argv[3] == NULL)
197295
err("no 2 address");
198296

199-
200-
unsigned int hex0 = stoul(argv[2], nullptr, 16);
201-
unsigned int hex1 = stoul(argv[3], nullptr, 16);
202-
203-
ec_wait_status(Ports::command_port, Status::input_buffer_full);
297+
ec_wait_write();
204298
ec_write(Ports::command_port, (byte) Command::_write);
205-
ec_wait_status(Ports::command_port, Status::input_buffer_full);
206-
ec_write(Ports::data_port, (byte) hex0);
207-
ec_wait_status(Ports::command_port, Status::input_buffer_full);
208-
ec_write(Ports::data_port, (byte) hex1);
299+
ec_wait_write();
300+
ec_write(Ports::data_port, (byte) arg_to_int(argv[2]));
301+
ec_wait_write();
302+
ec_write(Ports::data_port, (byte) arg_to_int(argv[3]));
303+
ec_wait_read();
209304
ec_read(Ports::data_port);
210305

211306
cout << uppercase << std::hex << argv[2] << ": " << argv[3] << endl;
212307
} else if (strcmp(argv[1], "monitor") == 0) {
213308
monitor();
214309
} else if (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "") == 0) {
215310
help();
311+
} else if (strcmp(argv[1], "cti") == 0 || strcmp(argv[1], "") == 0) {
312+
313+
cout << arg_to_int(argv[2]) << endl;
314+
cout << argv[2] << endl;
315+
216316
} else {
217317
cout << "unknown command" << endl;
218318
return 0;

0 commit comments

Comments
 (0)