Skip to content

Commit e763e04

Browse files
authored
Parse serial bytes within the library and use callbacks (#6)
1 parent e8d5177 commit e763e04

File tree

4 files changed

+271
-0
lines changed

4 files changed

+271
-0
lines changed

.clang-format

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
Language: Cpp
2+
AccessModifierOffset: -1
3+
AlignAfterOpenBracket: Align
4+
AlignConsecutiveAssignments: false
5+
AlignConsecutiveDeclarations: false
6+
AlignEscapedNewlines: DontAlign
7+
AlignOperands: true
8+
AlignTrailingComments: true
9+
AllowAllParametersOfDeclarationOnNextLine: true
10+
AllowShortBlocksOnASingleLine: false
11+
AllowShortCaseLabelsOnASingleLine: false
12+
AllowShortFunctionsOnASingleLine: All
13+
AllowShortIfStatementsOnASingleLine: false
14+
AllowShortLoopsOnASingleLine: false
15+
AlwaysBreakAfterReturnType: None
16+
AlwaysBreakBeforeMultilineStrings: false
17+
AlwaysBreakTemplateDeclarations: MultiLine
18+
BinPackArguments: true
19+
BinPackParameters: true
20+
BraceWrapping:
21+
AfterClass: false
22+
AfterControlStatement: false
23+
AfterEnum: false
24+
AfterFunction: false
25+
AfterNamespace: false
26+
AfterObjCDeclaration: false
27+
AfterStruct: false
28+
AfterUnion: false
29+
AfterExternBlock: false
30+
BeforeCatch: false
31+
BeforeElse: false
32+
IndentBraces: false
33+
SplitEmptyFunction: true
34+
SplitEmptyRecord: true
35+
SplitEmptyNamespace: true
36+
BreakBeforeBinaryOperators: None
37+
BreakBeforeBraces: Attach
38+
BreakBeforeInheritanceComma: false
39+
BreakInheritanceList: BeforeColon
40+
BreakBeforeTernaryOperators: true
41+
BreakConstructorInitializersBeforeComma: false
42+
BreakConstructorInitializers: BeforeColon
43+
BreakAfterJavaFieldAnnotations: false
44+
BreakStringLiterals: true
45+
ColumnLimit: 120
46+
CommentPragmas: '^ IWYU pragma:'
47+
CompactNamespaces: false
48+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
49+
ConstructorInitializerIndentWidth: 4
50+
ContinuationIndentWidth: 4
51+
Cpp11BracedListStyle: true
52+
DerivePointerAlignment: false
53+
DisableFormat: false
54+
ExperimentalAutoDetectBinPacking: false
55+
FixNamespaceComments: true
56+
ForEachMacros:
57+
- foreach
58+
- Q_FOREACH
59+
- BOOST_FOREACH
60+
IncludeBlocks: Preserve
61+
IncludeCategories:
62+
- Regex: '^<ext/.*\.h>'
63+
Priority: 2
64+
- Regex: '^<.*\.h>'
65+
Priority: 1
66+
- Regex: '^<.*'
67+
Priority: 2
68+
- Regex: '.*'
69+
Priority: 3
70+
IncludeIsMainRegex: '([-_](test|unittest))?$'
71+
IndentCaseLabels: true
72+
IndentPPDirectives: None
73+
IndentWidth: 2
74+
IndentWrappedFunctionNames: false
75+
KeepEmptyLinesAtTheStartOfBlocks: false
76+
MacroBlockBegin: ''
77+
MacroBlockEnd: ''
78+
MaxEmptyLinesToKeep: 1
79+
NamespaceIndentation: None
80+
PenaltyBreakAssignment: 2
81+
PenaltyBreakBeforeFirstCallParameter: 1
82+
PenaltyBreakComment: 300
83+
PenaltyBreakFirstLessLess: 120
84+
PenaltyBreakString: 1000
85+
PenaltyBreakTemplateDeclaration: 10
86+
PenaltyExcessCharacter: 1000000
87+
PenaltyReturnTypeOnItsOwnLine: 2000
88+
PointerAlignment: Right
89+
RawStringFormats:
90+
- Language: Cpp
91+
Delimiters:
92+
- cc
93+
- CC
94+
- cpp
95+
- Cpp
96+
- CPP
97+
- 'c++'
98+
- 'C++'
99+
CanonicalDelimiter: ''
100+
BasedOnStyle: google
101+
- Language: TextProto
102+
Delimiters:
103+
- pb
104+
- PB
105+
- proto
106+
- PROTO
107+
EnclosingFunctions:
108+
- EqualsProto
109+
- EquivToProto
110+
- PARSE_PARTIAL_TEXT_PROTO
111+
- PARSE_TEST_PROTO
112+
- PARSE_TEXT_PROTO
113+
- ParseTextOrDie
114+
- ParseTextProtoOrDie
115+
CanonicalDelimiter: ''
116+
BasedOnStyle: google
117+
ReflowComments: true
118+
SortIncludes: false
119+
SortUsingDeclarations: false
120+
SpaceAfterCStyleCast: true
121+
SpaceAfterTemplateKeyword: false
122+
SpaceBeforeAssignmentOperators: true
123+
SpaceBeforeCpp11BracedList: false
124+
SpaceBeforeCtorInitializerColon: true
125+
SpaceBeforeInheritanceColon: true
126+
SpaceBeforeParens: ControlStatements
127+
SpaceBeforeRangeBasedForLoopColon: true
128+
SpaceInEmptyParentheses: false
129+
SpacesBeforeTrailingComments: 2
130+
SpacesInAngles: false
131+
SpacesInContainerLiterals: false
132+
SpacesInCStyleCastParentheses: false
133+
SpacesInParentheses: false
134+
SpacesInSquareBrackets: false
135+
Standard: Auto
136+
TabWidth: 2
137+
UseTab: Never

.vscode/settings.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"files.associations": {
3+
"*.js.erb": "javascript",
4+
"*.ejson": "json",
5+
"*.tf": "terraform",
6+
"cctype": "cpp",
7+
"clocale": "cpp",
8+
"cmath": "cpp",
9+
"cstdarg": "cpp",
10+
"cstddef": "cpp",
11+
"cstdio": "cpp",
12+
"cstdlib": "cpp",
13+
"cstring": "cpp",
14+
"ctime": "cpp",
15+
"cwchar": "cpp",
16+
"cwctype": "cpp",
17+
"any": "cpp",
18+
"array": "cpp",
19+
"atomic": "cpp",
20+
"hash_map": "cpp",
21+
"bit": "cpp",
22+
"*.tcc": "cpp",
23+
"chrono": "cpp",
24+
"cinttypes": "cpp",
25+
"compare": "cpp",
26+
"complex": "cpp",
27+
"concepts": "cpp",
28+
"condition_variable": "cpp",
29+
"cstdint": "cpp",
30+
"deque": "cpp",
31+
"list": "cpp",
32+
"map": "cpp",
33+
"set": "cpp",
34+
"string": "cpp",
35+
"unordered_map": "cpp",
36+
"unordered_set": "cpp",
37+
"vector": "cpp",
38+
"exception": "cpp",
39+
"algorithm": "cpp",
40+
"functional": "cpp",
41+
"iterator": "cpp",
42+
"memory": "cpp",
43+
"memory_resource": "cpp",
44+
"numeric": "cpp",
45+
"optional": "cpp",
46+
"random": "cpp",
47+
"ratio": "cpp",
48+
"string_view": "cpp",
49+
"system_error": "cpp",
50+
"tuple": "cpp",
51+
"type_traits": "cpp",
52+
"utility": "cpp",
53+
"fstream": "cpp",
54+
"initializer_list": "cpp",
55+
"iomanip": "cpp",
56+
"iosfwd": "cpp",
57+
"iostream": "cpp",
58+
"istream": "cpp",
59+
"limits": "cpp",
60+
"mutex": "cpp",
61+
"new": "cpp",
62+
"numbers": "cpp",
63+
"ostream": "cpp",
64+
"semaphore": "cpp",
65+
"shared_mutex": "cpp",
66+
"sstream": "cpp",
67+
"stdexcept": "cpp",
68+
"stop_token": "cpp",
69+
"streambuf": "cpp",
70+
"thread": "cpp",
71+
"cfenv": "cpp",
72+
"typeinfo": "cpp",
73+
"valarray": "cpp",
74+
"variant": "cpp"
75+
}
76+
}

src/improv.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,52 @@ ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_c
4848
return improv_command;
4949
}
5050

51+
bool parse_improv_serial_byte(size_t position, uint8_t byte, const uint8_t *buffer,
52+
std::function<bool(ImprovCommand)> &&callback, std::function<void(Error)> &&on_error) {
53+
if (position == 0)
54+
return byte == 'I';
55+
if (position == 1)
56+
return byte == 'M';
57+
if (position == 2)
58+
return byte == 'P';
59+
if (position == 3)
60+
return byte == 'R';
61+
if (position == 4)
62+
return byte == 'O';
63+
if (position == 5)
64+
return byte == 'V';
65+
66+
if (position == 6)
67+
return byte == IMPROV_SERIAL_VERSION;
68+
69+
if (position <= 8)
70+
return true;
71+
72+
uint8_t type = buffer[7];
73+
uint8_t data_len = buffer[8];
74+
75+
if (position <= 8 + data_len)
76+
return true;
77+
78+
if (position == 8 + data_len + 1) {
79+
uint8_t checksum = 0x00;
80+
for (size_t i = 0; i < position; i++)
81+
checksum += buffer[i];
82+
83+
if (checksum != byte) {
84+
on_error(ERROR_INVALID_RPC);
85+
return false;
86+
}
87+
88+
if (type == TYPE_RPC) {
89+
auto command = parse_improv_data(&buffer[9], data_len, false);
90+
return callback(command);
91+
}
92+
}
93+
94+
return false;
95+
}
96+
5197
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::string> &datum, bool add_checksum) {
5298
std::vector<uint8_t> out;
5399
uint32_t length = 0;

src/improv.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#endif // ARDUINO
66

77
#include <cstdint>
8+
#include <functional>
89
#include <string>
910
#include <vector>
1011

@@ -44,6 +45,14 @@ enum Command : uint8_t {
4445
};
4546

4647
static const uint8_t CAPABILITY_IDENTIFY = 0x01;
48+
static const uint8_t IMPROV_SERIAL_VERSION = 1;
49+
50+
enum ImprovSerialType : uint8_t {
51+
TYPE_CURRENT_STATE = 0x01,
52+
TYPE_ERROR_STATE = 0x02,
53+
TYPE_RPC = 0x03,
54+
TYPE_RPC_RESPONSE = 0x04
55+
};
4756

4857
struct ImprovCommand {
4958
Command command;
@@ -54,6 +63,9 @@ struct ImprovCommand {
5463
ImprovCommand parse_improv_data(const std::vector<uint8_t> &data, bool check_checksum = true);
5564
ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_checksum = true);
5665

66+
bool parse_improv_serial_byte(size_t position, uint8_t byte, const uint8_t *buffer,
67+
std::function<bool(ImprovCommand)> &&callback, std::function<void(Error)> &&on_error);
68+
5769
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::string> &datum,
5870
bool add_checksum = true);
5971
#ifdef ARDUINO

0 commit comments

Comments
 (0)