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
|
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include "puzbusv1.h"
int send_message() {
const char* data = "Test message data!";
struct pb_msg output = {
.addr = 0x39,
.data = (char*) data,
.length = strlen(data),
};
char* packed;
size_t size;
if (!pb_write(&output, &packed, &size)) {
printf("error writing!\n");
return 1;
}
fwrite(packed, sizeof(packed[0]), size, stdout);
fflush(stdout);
return 0;
}
int read_message() {
freopen(NULL, "rb", stdin); // allow binary on stdin
struct pb_msg input;
char buf[8]; // extremely small buffer to test chunked message parsing
size_t bytes = 0;
while ((bytes = fread(buf, sizeof(buf[0]), sizeof(buf), stdin)) > 0) {
if (!pb_read(&input, buf, bytes)) continue;
printf("address: 0x%02x\n", input.addr);
printf("data: \"%.*s\"\n", input.length, input.data);
free(input.data);
return 0;
}
return 1;
}
int main() {
if (!isatty(fileno(stdout))) return send_message();
if (!isatty(fileno(stdin))) return read_message();
printf("please pipe some data in or out to use this program\n");
return 0;
}
|