forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSProtocol.h
127 lines (112 loc) · 3.06 KB
/
BSProtocol.h
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
#ifndef BS_PROTOCOL_H
#define BS_PROTOCOL_H
#include "BSArgs.h"
#define BS_LINE_PREFIX ">>>>>bs_test_"
extern bool pretest();
namespace bs
{
namespace protocol
{
template<typename IO>
void output_test_start(IO& io, const char* file, size_t line, const char* name, const char* desc)
{
io.printf(BS_LINE_PREFIX "start file=\"%s\" line=%d name=\"%s\" desc=\"%s\"\n", file, line, name, desc);
}
template<typename IO>
void output_check_failure(IO& io, size_t line)
{
io.printf(BS_LINE_PREFIX "check_failure line=%d\n", line);
}
template<typename IO>
void output_test_end(IO& io, bool success, size_t checks, size_t failed_checks, size_t line = 0)
{
io.printf(BS_LINE_PREFIX "end line=%d result=%d checks=%d failed_checks=%d\n", line, success, checks, failed_checks);
}
template<typename IO>
void output_menu_begin(IO& io)
{
io.printf(BS_LINE_PREFIX "menu_begin\n");
}
template<typename IO>
void output_menu_item(IO& io, int index, const char* name, const char* desc)
{
io.printf(BS_LINE_PREFIX "item id=%d name=\"%s\" desc=\"%s\"\n", index, name, desc);
}
template<typename IO>
void output_menu_end(IO& io)
{
io.printf(BS_LINE_PREFIX "menu_end\n");
}
template<typename IO>
void output_setenv_result(IO& io, const char* key, const char* value)
{
io.printf(BS_LINE_PREFIX "setenv key=\"%s\" value=\"%s\"\n", key, value);
}
template<typename IO>
void output_getenv_result(IO& io, const char* key, const char* value)
{
(void) key;
io.printf(BS_LINE_PREFIX "getenv value=\"%s\"\n", value);
}
template<typename IO>
void output_pretest_result(IO& io, bool res)
{
io.printf(BS_LINE_PREFIX "pretest result=%d\n", res ? 1 : 0);
}
template<typename IO>
bool input_handle(IO& io, char* line_buf, size_t line_buf_size, int& test_num)
{
int cb_read = io.read_line(line_buf, line_buf_size);
if (cb_read == 0 || line_buf[0] == '\n')
{
return false;
}
char* argv[4];
size_t argc = split_args(line_buf, argv, sizeof(argv) / sizeof(argv[0]));
if (argc == 0)
{
return false;
}
if (strcmp(argv[0], "setenv") == 0)
{
if (argc != 3)
{
return false;
}
setenv(argv[1], argv[2], 1);
output_setenv_result(io, argv[1], argv[2]);
test_num = -1;
return false; /* we didn't get the test number yet, so return false */
}
if (strcmp(argv[0], "getenv") == 0)
{
if (argc != 2)
{
return false;
}
const char* value = getenv(argv[1]);
output_getenv_result(io, argv[1], (value != NULL) ? value : "");
return false;
}
if (strcmp(argv[0], "pretest") == 0)
{
if (argc != 1)
{
return false;
}
bool res = ::pretest();
output_pretest_result(io, res);
return false;
}
/* not one of the commands, try to parse as test number */
char* endptr;
test_num = (int) strtol(argv[0], &endptr, 10);
if (endptr != argv[0] + strlen(argv[0]))
{
return false;
}
return true;
}
} // ::protocol
} // ::bs
#endif //BS_PROTOCOL_H