Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into linux
Browse files Browse the repository at this point in the history
  • Loading branch information
markjfisher committed Oct 20, 2024
2 parents d567c43 + c3cba87 commit 21239bf
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 47 deletions.
8 changes: 4 additions & 4 deletions source/SmartPortOverSlip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ void SmartPortOverSlip::handle_prodos_read(uint8_t drive_num, std::pair<int, int
auto id_connection = GetCommandListener().find_connection_with_device(device_id);

// Do a ReadRequest, and shove the 512 byte block into the required memory
ReadBlockRequest request(Requestor::next_request_number(), id_connection.first, 512);
ReadBlockRequest request(Requestor::next_request_number(), id_connection.first);
// $46-47 = Block Number
request.set_block_number_from_bytes(mem[0x46], mem[0x47], 0);
auto response = Requestor::send_request(request, id_connection.second.get());
Expand All @@ -360,7 +360,7 @@ void SmartPortOverSlip::handle_prodos_write(uint8_t drive_num, std::pair<int, in
auto device_id = drive_num == 1 ? disk_devices.first : disk_devices.second;
auto id_connection = GetCommandListener().find_connection_with_device(device_id);

WriteBlockRequest request(Requestor::next_request_number(), id_connection.first, 512);
WriteBlockRequest request(Requestor::next_request_number(), id_connection.first);
// $46-47 = Block Number
request.set_block_number_from_bytes(mem[0x46], mem[0x47], 0);
// put data into the request we're sending
Expand Down Expand Up @@ -420,7 +420,7 @@ void SmartPortOverSlip::device_count(const WORD sp_payload_loc)

void SmartPortOverSlip::read_block(const BYTE unit_number, Connection *connection, const WORD buffer_location, const WORD block_count_address)
{
ReadBlockRequest request(Requestor::next_request_number(), unit_number, 512);
ReadBlockRequest request(Requestor::next_request_number(), unit_number);
// Assume that (cmd_list_loc + 4 == block_count_address) holds 3 bytes for the block number. If it's in the payload, this is wrong and will have to be fixed.
request.set_block_number_from_ptr(mem, block_count_address);
auto response = Requestor::send_request(request, connection);
Expand All @@ -440,7 +440,7 @@ void SmartPortOverSlip::read_block(const BYTE unit_number, Connection *connectio

void SmartPortOverSlip::write_block(const BYTE unit_number, Connection *connection, const WORD sp_payload_loc, const WORD params_loc)
{
WriteBlockRequest request(Requestor::next_request_number(), unit_number, 512);
WriteBlockRequest request(Requestor::next_request_number(), unit_number);
// Assume that (cmd_list_loc + 4 == params_loc) holds 3 bytes for the block number. The payload contains the data to write
request.set_block_number_from_ptr(mem, params_loc);
request.set_block_data_from_ptr(mem, sp_payload_loc);
Expand Down
23 changes: 7 additions & 16 deletions source/devrelay/commands/ReadBlock.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
#ifdef DEV_RELAY_SLIP

#include <iostream>
#include "ReadBlock.h"
#include <iostream>

ReadBlockRequest::ReadBlockRequest(const uint8_t request_sequence_number, const uint8_t device_id, uint16_t block_size) : Request(request_sequence_number, CMD_READ_BLOCK, device_id), block_number_{}, block_size_(block_size) {}
ReadBlockRequest::ReadBlockRequest(const uint8_t request_sequence_number, const uint8_t device_id) : Request(request_sequence_number, CMD_READ_BLOCK, device_id), block_number_{} {}

std::vector<uint8_t> ReadBlockRequest::serialize() const
{
std::vector<uint8_t> request_data;
request_data.push_back(this->get_request_sequence_number());
request_data.push_back(this->get_command_number());
request_data.push_back(this->get_device_id());
request_data.push_back(this->get_block_size() & 0xFF);
request_data.push_back((this->get_block_size() >> 8) & 0xFF);
request_data.insert(request_data.end(), block_number_.begin(), block_number_.end());
return request_data;
}

std::unique_ptr<Response> ReadBlockRequest::deserialize(const std::vector<uint8_t> &data) const
{
if (data.size() != (block_size_ + 2))
if (data.size() != (512 + 2)) // 2 additional bytes are added to the block
{
std::cerr << "Not enough data to deserialize ReadBlockResponse" << std::endl;
return nullptr;
}

auto response = std::make_unique<ReadBlockResponse>(data[0], data[1], block_size_);
auto response = std::make_unique<ReadBlockResponse>(data[0], data[1]);
if (response->get_status() == 0)
{
response->set_block_data(data.begin() + 2, data.end());
Expand All @@ -35,8 +33,6 @@ std::unique_ptr<Response> ReadBlockRequest::deserialize(const std::vector<uint8_

const std::array<uint8_t, 3> &ReadBlockRequest::get_block_number() const { return block_number_; }

const uint16_t ReadBlockRequest::get_block_size() const { return block_size_; }

void ReadBlockRequest::set_block_number_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, block_number_.size(), block_number_.begin()); }

void ReadBlockRequest::set_block_number_from_bytes(uint8_t l, uint8_t m, uint8_t h)
Expand All @@ -52,7 +48,7 @@ void ReadBlockRequest::create_command(uint8_t *cmd_data) const {
}

std::unique_ptr<Response> ReadBlockRequest::create_response(uint8_t source, uint8_t status, const uint8_t *data, uint16_t num) const {
std::unique_ptr<ReadBlockResponse> response = std::make_unique<ReadBlockResponse>(get_request_sequence_number(), status, num);
std::unique_ptr<ReadBlockResponse> response = std::make_unique<ReadBlockResponse>(get_request_sequence_number(), status);
// Copy the return data if the status is OK
if (status == 0) {
std::vector<uint8_t> data_vector(data, data + num);
Expand All @@ -62,9 +58,7 @@ std::unique_ptr<Response> ReadBlockRequest::create_response(uint8_t source, uint
}


ReadBlockResponse::ReadBlockResponse(const uint8_t request_sequence_number, const uint8_t status, uint16_t block_size) : Response(request_sequence_number, status), block_size_(block_size) {
block_data_.resize(block_size_);
}
ReadBlockResponse::ReadBlockResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status), block_data_{} {}

std::vector<uint8_t> ReadBlockResponse::serialize() const
{
Expand All @@ -80,11 +74,8 @@ void ReadBlockResponse::set_block_data(std::vector<uint8_t>::const_iterator begi
std::copy(begin, end, block_data_.begin()); // NOLINT(performance-unnecessary-value-param)
}

const std::vector<uint8_t>& ReadBlockResponse::get_block_data() const {
const std::array<uint8_t, 512>& ReadBlockResponse::get_block_data() const {
return block_data_;
}

const uint16_t ReadBlockResponse::get_block_size() const { return block_size_; }


#endif
12 changes: 4 additions & 8 deletions source/devrelay/commands/ReadBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
class ReadBlockRequest : public Request
{
public:
ReadBlockRequest(uint8_t request_sequence_number, uint8_t device_id, uint16_t block_size);
ReadBlockRequest(uint8_t request_sequence_number, uint8_t device_id);
std::vector<uint8_t> serialize() const override;
std::unique_ptr<Response> deserialize(const std::vector<uint8_t> &data) const override;
const std::array<uint8_t, 3> &get_block_number() const;
void set_block_number_from_ptr(const uint8_t *ptr, size_t offset);
void set_block_number_from_bytes(uint8_t l, uint8_t m, uint8_t h);
const uint16_t get_block_size() const;

void create_command(uint8_t *output_data) const override;
void copy_payload(uint8_t *data) const override {}
Expand All @@ -26,21 +25,18 @@ class ReadBlockRequest : public Request

private:
std::array<uint8_t, 3> block_number_;
uint16_t block_size_;
};


class ReadBlockResponse : public Response
{
public:
explicit ReadBlockResponse(uint8_t request_sequence_number, uint8_t status, uint16_t block_size);
explicit ReadBlockResponse(uint8_t request_sequence_number, uint8_t status);
std::vector<uint8_t> serialize() const override;

void set_block_data(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end);
const std::vector<uint8_t>& get_block_data() const;
const uint16_t get_block_size() const;
const std::array<uint8_t, 512>& get_block_data() const;

private:
std::vector<uint8_t> block_data_;
uint16_t block_size_;
std::array<uint8_t, 512> block_data_;
};
8 changes: 2 additions & 6 deletions source/devrelay/commands/WriteBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
#include <iostream>
#include "WriteBlock.h"

WriteBlockRequest::WriteBlockRequest(const uint8_t request_sequence_number, const uint8_t device_id, const uint16_t block_size) : Request(request_sequence_number, CMD_WRITE_BLOCK, device_id), block_number_{}, block_data_{}, block_size_(block_size) {}
WriteBlockRequest::WriteBlockRequest(const uint8_t request_sequence_number, const uint8_t device_id) : Request(request_sequence_number, CMD_WRITE_BLOCK, device_id), block_number_{}, block_data_{} {}

std::vector<uint8_t> WriteBlockRequest::serialize() const
{
std::vector<uint8_t> request_data;
request_data.push_back(this->get_request_sequence_number());
request_data.push_back(this->get_command_number());
request_data.push_back(this->get_device_id());
request_data.push_back(this->get_block_size() & 0xFF);
request_data.push_back((this->get_block_size() >> 8) & 0xFF);
request_data.insert(request_data.end(), block_number_.begin(), block_number_.end());
request_data.insert(request_data.end(), block_data_.begin(), block_data_.end());

Expand All @@ -34,9 +32,7 @@ std::unique_ptr<Response> WriteBlockRequest::deserialize(const std::vector<uint8

const std::array<uint8_t, 3> &WriteBlockRequest::get_block_number() const { return block_number_; }

const std::vector<uint8_t> &WriteBlockRequest::get_block_data() const { return block_data_; }

const uint16_t WriteBlockRequest::get_block_size() const { return block_size_; }
const std::array<uint8_t, 512> &WriteBlockRequest::get_block_data() const { return block_data_; }

void WriteBlockRequest::set_block_number_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, block_number_.size(), block_number_.begin()); }

Expand Down
8 changes: 3 additions & 5 deletions source/devrelay/commands/WriteBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
class WriteBlockRequest : public Request
{
public:
WriteBlockRequest(uint8_t request_sequence_number, uint8_t device_id, uint16_t block_size);
WriteBlockRequest(uint8_t request_sequence_number, uint8_t device_id);
std::vector<uint8_t> serialize() const override;
std::unique_ptr<Response> deserialize(const std::vector<uint8_t> &data) const override;
const std::array<uint8_t, 3> &get_block_number() const;
void set_block_number_from_ptr(const uint8_t *ptr, size_t offset);

const std::vector<uint8_t> &get_block_data() const;
const std::array<uint8_t, 512> &get_block_data() const;
void set_block_data_from_ptr(const uint8_t *ptr, size_t offset);
void set_block_number_from_bytes(uint8_t l, uint8_t m, uint8_t h);
const uint16_t get_block_size() const;

void create_command(uint8_t *output_data) const override;
void copy_payload(uint8_t *data) const override;
Expand All @@ -29,8 +28,7 @@ class WriteBlockRequest : public Request

private:
std::array<uint8_t, 3> block_number_;
std::vector<uint8_t> block_data_;
uint16_t block_size_;
std::array<uint8_t, 512> block_data_;
};

class WriteBlockResponse : public Response
Expand Down
12 changes: 5 additions & 7 deletions source/devrelay/types/Request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,16 @@ std::unique_ptr<Request> Request::from_packet(const std::vector<uint8_t>& packet
}

case CMD_READ_BLOCK: {
auto bs = (packet[4] << 8) | packet[3];
auto readBlockRequest = std::make_unique<ReadBlockRequest>(packet[0], packet[2], bs);
readBlockRequest->set_block_number_from_ptr(packet.data(), 5);
auto readBlockRequest = std::make_unique<ReadBlockRequest>(packet[0], packet[2]);
readBlockRequest->set_block_number_from_ptr(packet.data(), 3);
request = std::move(readBlockRequest);
break;
}

case CMD_WRITE_BLOCK: {
auto bs = (packet[4] << 8) | packet[3];
auto writeBlockRequest = std::make_unique<WriteBlockRequest>(packet[0], packet[2], bs);
writeBlockRequest->set_block_number_from_ptr(packet.data(), 5);
writeBlockRequest->set_block_data_from_ptr(packet.data(), 8);
auto writeBlockRequest = std::make_unique<WriteBlockRequest>(packet[0], packet[2]);
writeBlockRequest->set_block_number_from_ptr(packet.data(), 3);
writeBlockRequest->set_block_data_from_ptr(packet.data(), 6);
request = std::move(writeBlockRequest);
break;
}
Expand Down
3 changes: 2 additions & 1 deletion source/devrelay/types/Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Response;
class Request : public Command
{
public:
Request(uint8_t request_sequence_number, uint8_t command_number, uint8_t device_id);
Request(const uint8_t request_sequence_number, const uint8_t command_number, const uint8_t device_id);

std::vector<uint8_t> serialize() const override = 0;
virtual std::unique_ptr<Response> deserialize(const std::vector<uint8_t> &data) const = 0;

Expand Down

0 comments on commit 21239bf

Please sign in to comment.