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
|
#include <cstdio>
#include <cstdlib>
#include <readline/readline.h>
#include <string.h>
#include "cmd.h"
#include "i2ctcpv1.h"
#include "pb/types.h"
#include "rl.h"
#include "sock.h"
#include "parse.h"
#include "pb/bus.h"
char* consume_token(char* input, const char* ifs) {
strtok(input, ifs);
return strtok(NULL, "\0");
}
void cmd_exit(char*) {
exit(EXIT_SUCCESS);
}
void cmd_test(char*) {
const char* data = "Hello world!";
i2c_send(0x39, data, strlen(data));
}
void cmd_help(char*) {
printf("List of available commands:\n");
for (size_t i = 0; i < cmds_length; i++) {
struct cmd cmd = cmds[i];
printf(" %-*s", 10, cmd.name);
if (cmd.info != NULL)
printf(" %s", cmd.info);
printf("\n");
}
printf(
"\n"
"See man pbc(1) for more info about specific commands\n"
"Hint: you can use the TAB key to autocomplete commands\n"
);
}
void cmd_send(char * addr_str) {
char* data_str = consume_token(addr_str, IFS);
char* end;
uint16_t addr = strtol(addr_str, &end, 0);
if (addr_str + strlen(addr_str) != end) {
printf("address format error\n");
return;
}
char* data;
size_t data_size;
int err = strtodata(data_str, &data, &data_size);
if (err <= 0) {
printf("data format error at index %d:\n%s\n%*s^\n",
-err, data_str, -err, "");
return;
}
printf("sending char data[%lu = 0x%02lx] to 0x%02x\n", data_size, data_size, addr);
i2c_send(addr, data, data_size);
free(data);
}
void cmd_reset(char*) {
const char msg[] = {
PB_CMD_WRITE,
0x00,
PB_GS_IDLE,
};
i2c_send(BUSADDR_MAIN, msg, sizeof(msg));
}
void cmd_skip(char*) {
const char msg[] = {
PB_CMD_WRITE,
0x00,
PB_GS_SOLVED,
};
i2c_send(BUSADDR_MAIN, msg, sizeof(msg));
}
void cmd_ls(char*) {
return;
const char msg[] = {
PB_CMD_READ,
// TODO: which address is this?
};
i2c_send(BUSADDR_MAIN, msg, sizeof(msg));
}
extern bool i2c_dump_send;
extern bool i2c_dump_recv;
const char * dump_modes[] = {
"none",
"send",
"recv",
"both",
NULL,
};
void cmd_dump(char * mode) {
consume_token(mode, IFS);
mode += strspn(mode, IFS);
for (int i = 0; dump_modes[i] != NULL; i++) {
if (strcmp(mode, dump_modes[i]) == 0) {
i2c_dump_send = (i >> 0) & 1;
i2c_dump_recv = (i >> 1) & 1;
return;
}
}
printf("mode \"%s\" unknown\n", mode);
}
char** cmd_dump_complete(const char * text, int begin, int end) {
int word = rl_word(rl_line_buffer, begin);
if (word != 1) return NULL;
return rl_completion_matches(text, [](const char * text, int state) -> char * {
static size_t i = 0;
if (state == 0) i = 0;
while (dump_modes[i] != NULL) {
const char * mode = dump_modes[i++];
if (strncmp(text, mode, strlen(text)) == 0)
return strdup(mode);
}
return NULL;
});
return NULL;
}
|