Skip to content

Commit e8c1a16

Browse files
authored
Fix serial protocol parser (#1301)
1 parent 838c900 commit e8c1a16

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

core/MyProtocol.cpp

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ bool protocolSerial2MyMessage(MyMessage &message, char *inputString)
3737

3838
// Extract command data coming on serial line
3939
for (str = strtok_r(inputString, ";", &p); // split using semicolon
40-
str && index < 6; // loop while str is not null an max 5 times
41-
str = strtok_r(NULL, ";", &p) // get subsequent tokens
40+
str && index < 5; // loop while str is not null an max 4 times
41+
str = strtok_r(NULL, ";", &p), index++ // get subsequent tokens
4242
) {
4343
switch (index) {
4444
case 0: // Radio id (destination)
@@ -57,34 +57,30 @@ bool protocolSerial2MyMessage(MyMessage &message, char *inputString)
5757
case 4: // Data type
5858
message.type = atoi(str);
5959
break;
60-
case 5: {// Variable value
61-
if (command == C_STREAM) {
62-
uint8_t bvalue[MAX_PAYLOAD];
63-
uint8_t blen = 0;
64-
while (*str) {
65-
uint8_t val;
66-
val = convertH2I(*str++) << 4;
67-
val += convertH2I(*str++);
68-
bvalue[blen] = val;
69-
blen++;
70-
}
71-
message.set(bvalue, blen);
72-
} else {
73-
char *value = str;
74-
// Remove trailing carriage return and newline character (if it exists)
75-
const uint8_t lastCharacter = strlen(value) - 1;
76-
if (value[lastCharacter] == '\r' || value[lastCharacter] == '\n') {
77-
value[lastCharacter] = '\0';
78-
}
79-
message.set(value);
80-
}
81-
break;
8260
}
61+
}
62+
// payload
63+
if (command == C_STREAM) {
64+
uint8_t bvalue[MAX_PAYLOAD];
65+
uint8_t blen = 0;
66+
while (*str) {
67+
uint8_t val;
68+
val = convertH2I(*str++) << 4;
69+
val += convertH2I(*str++);
70+
bvalue[blen] = val;
71+
blen++;
72+
}
73+
message.set(bvalue, blen);
74+
} else {
75+
char *value = str;
76+
// Remove trailing carriage return and newline character (if it exists)
77+
const uint8_t lastCharacter = strlen(value) - 1;
78+
if (value[lastCharacter] == '\r' || value[lastCharacter] == '\n') {
79+
value[lastCharacter] = '\0';
8380
}
84-
index++;
81+
message.set(value);
8582
}
86-
// Return true if input valid
87-
return (index == 6);
83+
return (index == 5);
8884
}
8985

9086
char *protocolMyMessage2Serial(MyMessage &message)
@@ -114,7 +110,7 @@ bool protocolMQTT2MyMessage(MyMessage &message, char *topic, uint8_t *payload,
114110
mSetEcho(message, false);
115111
for (str = strtok_r(topic + strlen(MY_MQTT_SUBSCRIBE_TOPIC_PREFIX) + 1, "/", &p);
116112
str && index < 5;
117-
str = strtok_r(NULL, "/", &p)
113+
str = strtok_r(NULL, "/", &p), index++
118114
) {
119115
switch (index) {
120116
case 0:
@@ -158,7 +154,6 @@ bool protocolMQTT2MyMessage(MyMessage &message, char *topic, uint8_t *payload,
158154
message.type = atoi(str);
159155
break;
160156
}
161-
index++;
162157
}
163158
// Return true if input valid
164159
return (index == 5);

0 commit comments

Comments
 (0)