Skip to content

Commit 07d7743

Browse files
committed
Can receive a packet
1 parent c07adbb commit 07d7743

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

lib/bus/rs232/FujiBusPacket.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
#include "FujiBusPacket.h"
22

3-
enum {
4-
SLIP_END = 0xC0,
5-
SLIP_ESCAPE = 0xDB,
6-
SLIP_ESC_END = 0xDC,
7-
SLIP_ESC_ESC = 0xDD,
8-
};
9-
103
typedef struct {
114
uint8_t device; /* Destination Device */
125
uint8_t command; /* Command */
@@ -47,7 +40,7 @@ std::string FujiBusPacket::decodeSLIP(const std::string &input)
4740
break;
4841
}
4942

50-
for (idx = 0; idx < input.size(); idx++)
43+
for (idx++; idx < input.size(); idx++)
5144
{
5245
val = ptr[idx];
5346
if (val == SLIP_END)
@@ -96,11 +89,12 @@ std::string FujiBusPacket::encodeSLIP(const std::string &input)
9689
return output;
9790
}
9891

99-
uint8_t FujiBusPacket::calcChecksum(const std::string &buf)
92+
uint8_t FujiBusPacket::calcChecksum(const std::string &input)
10093
{
10194
uint16_t idx, chk;
95+
uint8_t *buf = (uint8_t *) input.data();
10296

103-
for (idx = chk = 0; idx < buf.size(); idx++)
97+
for (idx = chk = 0; idx < input.size(); idx++)
10498
chk = ((chk + buf[idx]) >> 8) + ((chk + buf[idx]) & 0xFF);
10599
return (uint8_t) chk;
106100
}
@@ -121,6 +115,8 @@ bool FujiBusPacket::parse(const std::string &input)
121115
return false;
122116

123117
decoded = decodeSLIP(input);
118+
if (decoded.size() < sizeof(fujibus_header))
119+
return false;
124120
hdr = (fujibus_header *) &decoded[0];
125121
if (hdr->length != decoded.size())
126122
return false;

lib/bus/rs232/FujiBusPacket.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
#include <string>
1010
#include <memory>
1111

12+
enum {
13+
SLIP_END = 0xC0,
14+
SLIP_ESCAPE = 0xDB,
15+
SLIP_ESC_END = 0xDC,
16+
SLIP_ESC_ESC = 0xDD,
17+
};
18+
1219
class FujiBusPacket
1320
{
1421
private:
@@ -22,7 +29,7 @@ class FujiBusPacket
2229
FujiCommandID command;
2330
unsigned int fieldSize;
2431
std::vector<unsigned int> fields;
25-
std::optional<std::string> data = nullptr;
32+
std::optional<std::string> data = std::nullopt;
2633

2734
FujiBusPacket(const std::string &input);
2835
std::string serialize();

lib/bus/rs232/rs232.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,16 @@ void systemBus::_rs232_process_cmd()
177177

178178
// Read CMD frame
179179
std::string packet;
180-
int val;
181-
while ((val = _port.read()) > -1)
180+
int val, count;
181+
for (count = 0; count < 2; )
182+
{
183+
val = _port.read();
184+
if (val < 0)
185+
break;
182186
packet.push_back(val);
187+
if (val == SLIP_END)
188+
count++;
189+
}
183190

184191
auto tempFrame = FujiBusPacket::fromSerialized(packet);
185192
if (!tempFrame)
@@ -253,12 +260,14 @@ void systemBus::service()
253260
{
254261
_modemDev->rs232_handle_modem();
255262
}
263+
#ifdef OBSOLETE
256264
else
257265
// Neither CMD nor active modem, so throw out any stray input data
258266
{
259267
//Debug_println("RS232 Srvc Flush");
260268
_port.discardInput();
261269
}
270+
#endif /* OBSOLETE */
262271

263272
// Handle interrupts from network protocols
264273
for (int i = 0; i < 8; i++)

lib/hardware/IOChannel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int IOChannel::read(void)
7070
int result = read(buf, 1);
7171

7272
if (result < 1)
73-
return result;
73+
return -1;
7474
return buf[0];
7575
}
7676

0 commit comments

Comments
 (0)