Skip to content

Commit e7260dd

Browse files
authored
Temporary hack to make rs232bus read() work until devices learn to do packets (#1066)
* Temporary hack to make rs232bus read() work until devices learn to do packets * Fix a bunch of problems with packet decoding
1 parent 9a90b6c commit e7260dd

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

lib/bus/rs232/FujiBusPacket.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ bool FujiBusPacket::parse(const std::string &input)
114114
if (decoded.size() < sizeof(fujibus_header))
115115
return false;
116116
hdr = (fujibus_header *) &decoded[0];
117+
Debug_printv("Header: dev:%02x cmd:%02x len:%d chk:%02x fld:%02x",
118+
hdr->device, hdr->command, hdr->length, hdr->checksum, hdr->fields);
119+
117120
if (hdr->length != decoded.size())
118121
return false;
119122

@@ -131,7 +134,8 @@ bool FujiBusPacket::parse(const std::string &input)
131134
fieldCount = numFieldsTable[hdr->fields & FUJI_FIELD_COUNT_MASK];
132135
if (fieldCount)
133136
{
134-
_fieldSize = fieldSizeTable[fieldCount];
137+
_fieldSize = fieldSizeTable[hdr->fields & FUJI_FIELD_COUNT_MASK];
138+
135139
for (idx = 0; idx < fieldCount; idx++)
136140
{
137141
for (val = jdx = 0; jdx < _fieldSize; jdx++)
@@ -161,6 +165,7 @@ std::string FujiBusPacket::serialize()
161165
hdr.command = _command;
162166
hdr.length = sizeof(hdr);
163167
hdr.checksum = 0;
168+
hdr.fields = 0;
164169

165170
std::string output(sizeof(hdr), '\0');
166171

lib/bus/rs232/FujiBusPacket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class FujiBusPacket
4242
fujiDeviceID_t device() { return _device; }
4343
fujiCommandID_t command() { return _command; }
4444
unsigned int param(unsigned int index) { return _params[index]; }
45+
unsigned int paramCount() { return _params.size(); }
4546
std::optional<std::string> data() const { return _data; }
4647
};
4748

lib/bus/rs232/rs232.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ void virtualDevice::bus_to_computer(uint8_t *buf, uint16_t len, bool err)
9797
len = length
9898
Returns checksum
9999
*/
100+
#ifdef OBSOLETE
100101
uint8_t virtualDevice::bus_to_peripheral(uint8_t *buf, unsigned short len)
101102
{
102103
// Retrieve data frame from computer
@@ -130,13 +131,27 @@ uint8_t virtualDevice::bus_to_peripheral(uint8_t *buf, unsigned short len)
130131

131132
return ck_rcv;
132133
}
134+
#else
135+
uint8_t virtualDevice::bus_to_peripheral(uint8_t *buf, unsigned short len)
136+
{
137+
// FIXME - This is a terrible hack to allow devices to continue
138+
// directly read/write the bus instead of upgrading them to work
139+
// with packets.
140+
size_t rlen = SYSTEM_BUS.read(buf, len);
141+
return rs232_checksum(buf, rlen);
142+
}
143+
#endif /* OBSOLETE */
133144

134145
// RS232 NAK
135146
void virtualDevice::rs232_nak()
136147
{
148+
#ifdef OBSOLETE
137149
SYSTEM_BUS.write('N');
138150
SYSTEM_BUS.flushOutput();
139151
Debug_println("NAK!");
152+
#else
153+
rs232_error();
154+
#endif /* OBSOLETE */
140155
}
141156

142157
// RS232 ACK
@@ -165,10 +180,13 @@ void virtualDevice::rs232_complete()
165180
// RS232 ERROR
166181
void virtualDevice::rs232_error()
167182
{
168-
abort();
183+
#ifdef OBSOLETE
169184
fnSystem.delay_microseconds(DELAY_T5);
170185
SYSTEM_BUS.write('E');
171186
Debug_println("ERROR!");
187+
#else
188+
SYSTEM_BUS.sendReplyPacket(_devnum, false, nullptr, 0);
189+
#endif /* OBSOLETE */
172190
}
173191

174192
#ifdef OBSOLETE
@@ -219,6 +237,12 @@ void systemBus::_rs232_process_cmd()
219237
tempFrame->device(), tempFrame->command(),
220238
tempFrame->data() ? tempFrame->data()->size() : -1);
221239

240+
// FIXME - This is a terrible hack to allow devices to continue
241+
// directly read/write the bus instead of upgrading them to work
242+
// with packets.
243+
_lastPacketReceived = tempFrame.get();
244+
_lastReadPosition = 0;
245+
222246
if (tempFrame->device() == FUJI_DEVICEID_DISK && _fujiDev != nullptr
223247
&& _fujiDev->boot_config)
224248
{
@@ -500,8 +524,20 @@ void systemBus::sendReplyPacket(fujiDeviceID_t source, bool ack, void *data, siz
500524
/* Convert direct bus access into bus packets? */
501525
size_t systemBus::read(void *buffer, size_t length)
502526
{
503-
abort();
504-
return _port->read(buffer, length);
527+
// FIXME - This is a terrible hack to allow devices to continue
528+
// directly read/write the bus instead of upgrading them to work
529+
// with packets.
530+
// Assuming 'data()' returns the optional:
531+
auto optional_data = _lastPacketReceived->data();
532+
533+
if (!optional_data.has_value())
534+
return 0;
535+
536+
size_t avail = optional_data.value().size() - _lastReadPosition;
537+
avail = std::min(avail, (size_t) length);
538+
memcpy(buffer, optional_data.value().data() + _lastReadPosition, avail);
539+
_lastReadPosition += avail;
540+
return avail;
505541
}
506542

507543
size_t systemBus::read()

lib/bus/rs232/rs232.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ struct rs232_message_t
199199
class systemBus
200200
{
201201
private:
202+
// FIXME - DO NOT DO CACHE THE LAST PACKET RECEIVED. This is a
203+
// terrible hack to allow devices to continue directly read/write
204+
// the bus instead of upgrading them to work with packets.
205+
FujiBusPacket *_lastPacketReceived;
206+
size_t _lastReadPosition;
207+
202208
std::forward_list<virtualDevice *> _daisyChain;
203209

204210
int _command_frame_counter = 0;

lib/device/rs232/network.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ void rs232Network::rs232_read(uint16_t length)
246246

247247
// And send off to the computer
248248
bus_to_computer((uint8_t *)receiveBuffer->data(), length, err);
249-
receiveBuffer->clear();
249+
250+
// Remove from receive buffer and shrink.
251+
receiveBuffer->erase(0, length);
252+
receiveBuffer->shrink_to_fit();
250253
}
251254

252255
/**
@@ -870,7 +873,12 @@ void rs232Network::rs232_process(FujiBusPacket &packet)
870873
rs232_write(packet.param(0));
871874
break;
872875
case FUJICMD_STATUS:
873-
rs232_status(static_cast<FujiStatusReq>(packet.param(0)));
876+
{
877+
FujiStatusReq reqType = STATUS_NETWORK_CONNERR;
878+
if (packet.paramCount() >= 2)
879+
reqType = (FujiStatusReq) packet.param(1);
880+
rs232_status(reqType);
881+
}
874882
break;
875883
case FUJICMD_PARSE:
876884
rs232_ack();

0 commit comments

Comments
 (0)