Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into linux
Browse files Browse the repository at this point in the history
  • Loading branch information
markjfisher committed Jan 28, 2024
2 parents c02f357 + 1b2358d commit 5ce2bfc
Show file tree
Hide file tree
Showing 65 changed files with 1,846 additions and 1,795 deletions.
52 changes: 52 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
###############################
# Git Line Endings #
###############################

# Set default behaviour to automatically normalize line endings.
* text=auto

# Source files
*.cpp text
*.c text
*.h text

# Force batch scripts to always use CRLF line endings so that if a repo is accessed
# in Windows via a file share from Linux, the scripts will work.
*.[cC][mM][dD] text eol=crlf
*.[bB][aA][tT] text eol=crlf

# Force bash scripts to always use LF line endings so that if a repo is accessed
# in Unix via a file share from Windows, the scripts will work.
*.sh text eol=lf

###############################
# Git Large File System (LFS) #
###############################

# Archives
*.7z filter=lfs diff=lfs merge=lfs -text
*.br filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.rar filter=lfs diff=lfs merge=lfs -text
*.tar filter=lfs diff=lfs merge=lfs -text
*.tz filter=lfs diff=lfs merge=lfs -text
*.xz filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text

# Documents
*.pdf filter=lfs diff=lfs merge=lfs -text

# Images
*.gif filter=lfs diff=lfs merge=lfs -text
*.ico filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
*.pdf filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.psd filter=lfs diff=lfs merge=lfs -text
*.webp filter=lfs diff=lfs merge=lfs -text

# Fonts
*.woff2 filter=lfs diff=lfs merge=lfs -text

# Other
*.exe filter=lfs diff=lfs merge=lfs -text
27 changes: 25 additions & 2 deletions source/.clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
BasedOnStyle: LLVM
ColumnLimit: 160
BreakBeforeBraces: Allman
ColumnLimit: 200
AllowShortBlocksOnASingleLine: true
AlwaysBreakAfterReturnType: None
IncludeBlocks: Preserve
SortIncludes: false
UseTab: Always
IndentWidth: 4
TabWidth: 4
AccessModifierOffset: -4
AlwaysBreakTemplateDeclarations: Yes
BreakInheritanceList: AfterColon
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: false
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
IndentPPDirectives: BeforeHash
22 changes: 11 additions & 11 deletions source/SPoverSLIP/CloseRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ CloseRequest::CloseRequest(const uint8_t request_sequence_number, const uint8_t

std::vector<uint8_t> CloseRequest::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_sp_unit());
return request_data;
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_sp_unit());
return request_data;
}

std::unique_ptr<Response> CloseRequest::deserialize(const std::vector<uint8_t> &data) const
{
if (data.size() < 2)
{
throw std::runtime_error("Not enough data to deserialize CloseResponse");
}
if (data.size() < 2)
{
throw std::runtime_error("Not enough data to deserialize CloseResponse");
}

auto response = std::make_unique<CloseResponse>(data[0], data[1]);
return response;
auto response = std::make_unique<CloseResponse>(data[0], data[1]);
return response;
}
6 changes: 3 additions & 3 deletions source/SPoverSLIP/CloseRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class CloseRequest : public Request
{
public:
CloseRequest(uint8_t request_sequence_number, uint8_t sp_unit);
std::vector<uint8_t> serialize() const override;
std::unique_ptr<Response> deserialize(const std::vector<uint8_t> &data) const override;
CloseRequest(uint8_t request_sequence_number, uint8_t sp_unit);
std::vector<uint8_t> serialize() const override;
std::unique_ptr<Response> deserialize(const std::vector<uint8_t> &data) const override;
};
8 changes: 4 additions & 4 deletions source/SPoverSLIP/CloseResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ CloseResponse::CloseResponse(const uint8_t request_sequence_number, const uint8_

std::vector<uint8_t> CloseResponse::serialize() const
{
std::vector<uint8_t> data;
data.push_back(this->get_request_sequence_number());
data.push_back(this->get_status());
return data;
std::vector<uint8_t> data;
data.push_back(this->get_request_sequence_number());
data.push_back(this->get_status());
return data;
}
4 changes: 2 additions & 2 deletions source/SPoverSLIP/CloseResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
class CloseResponse : public Response
{
public:
explicit CloseResponse(uint8_t request_sequence_number, uint8_t status);
std::vector<uint8_t> serialize() const override;
explicit CloseResponse(uint8_t request_sequence_number, uint8_t status);
std::vector<uint8_t> serialize() const override;
};
40 changes: 20 additions & 20 deletions source/SPoverSLIP/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@
// This is called after AppleWin sends a request to a device, and is waiting for the response
std::vector<uint8_t> Connection::wait_for_response(uint8_t request_id, std::chrono::seconds timeout)
{
std::unique_lock<std::mutex> lock(data_mutex_);
// mutex is unlocked as it goes into a wait, so then the inserting thread can
// add to map, and this can then pick it up when notified, or timeout.
if (!data_cv_.wait_for(lock, timeout, [this, request_id]() { return data_map_.count(request_id) > 0; }))
{
throw std::runtime_error("Timeout waiting for response");
}
std::vector<uint8_t> response_data = data_map_[request_id];
data_map_.erase(request_id);
return response_data;
std::unique_lock<std::mutex> lock(data_mutex_);
// mutex is unlocked as it goes into a wait, so then the inserting thread can
// add to map, and this can then pick it up when notified, or timeout.
if (!data_cv_.wait_for(lock, timeout, [this, request_id]() { return data_map_.count(request_id) > 0; }))
{
throw std::runtime_error("Timeout waiting for response");
}
std::vector<uint8_t> response_data = data_map_[request_id];
data_map_.erase(request_id);
return response_data;
}

// This is used by devices that are waiting for requests from AppleWin.
// The codebase is used both sides of the connection.
std::vector<uint8_t> Connection::wait_for_request()
{
std::unique_lock<std::mutex> lock(data_mutex_);
data_cv_.wait(lock, [this]() { return !data_map_.empty(); });
const auto it = data_map_.begin();
std::vector<uint8_t> request_data = it->second;
data_map_.erase(it);
std::unique_lock<std::mutex> lock(data_mutex_);
data_cv_.wait(lock, [this]() { return !data_map_.empty(); });
const auto it = data_map_.begin();
std::vector<uint8_t> request_data = it->second;
data_map_.erase(it);

return request_data;
return request_data;
}

void Connection::join()
{
if (reading_thread_.joinable())
{
reading_thread_.join();
}
if (reading_thread_.joinable())
{
reading_thread_.join();
}
}
28 changes: 14 additions & 14 deletions source/SPoverSLIP/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@
class Connection
{
public:
virtual ~Connection() = default;
virtual void send_data(const std::vector<uint8_t> &data) = 0;
virtual ~Connection() = default;
virtual void send_data(const std::vector<uint8_t> &data) = 0;

virtual void create_read_channel() = 0;
virtual void close_connection() = 0;
virtual void create_read_channel() = 0;
virtual void close_connection() = 0;

bool is_connected() const { return is_connected_; }
void set_is_connected(const bool is_connected) { is_connected_ = is_connected; }
bool is_connected() const { return is_connected_; }
void set_is_connected(const bool is_connected) { is_connected_ = is_connected; }

std::vector<uint8_t> wait_for_response(uint8_t request_id, std::chrono::seconds timeout);
std::vector<uint8_t> wait_for_request();
std::vector<uint8_t> wait_for_response(uint8_t request_id, std::chrono::seconds timeout);
std::vector<uint8_t> wait_for_request();

void join();
void join();

static constexpr std::array<uint8_t, 4> reboot_sequence = {0xFF, 0x00, 0x00, 0xFF};

private:
std::atomic<bool> is_connected_{false};
std::atomic<bool> is_connected_{false};

protected:
std::map<uint8_t, std::vector<uint8_t>> data_map_;
std::thread reading_thread_;
std::map<uint8_t, std::vector<uint8_t>> data_map_;
std::thread reading_thread_;

std::mutex data_mutex_;
std::condition_variable data_cv_;
std::mutex data_mutex_;
std::condition_variable data_cv_;
};
28 changes: 14 additions & 14 deletions source/SPoverSLIP/ControlRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
#include "SmartPortCodes.h"

ControlRequest::ControlRequest(const uint8_t request_sequence_number, const uint8_t sp_unit, const uint8_t control_code, std::vector<uint8_t> &data)
: Request(request_sequence_number, SP_CONTROL, sp_unit), control_code_(control_code), data_(std::move(data))
: Request(request_sequence_number, SP_CONTROL, sp_unit), control_code_(control_code), data_(std::move(data))
{
}

std::vector<uint8_t> ControlRequest::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_sp_unit());
request_data.push_back(this->get_control_code());
request_data.insert(request_data.end(), get_data().begin(), get_data().end());
return request_data;
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_sp_unit());
request_data.push_back(this->get_control_code());
request_data.insert(request_data.end(), get_data().begin(), get_data().end());
return request_data;
}

std::unique_ptr<Response> ControlRequest::deserialize(const std::vector<uint8_t> &data) const
{
if (data.size() < 2)
{
throw std::runtime_error("Not enough data to deserialize ControlResponse");
}
if (data.size() < 2)
{
throw std::runtime_error("Not enough data to deserialize ControlResponse");
}

auto response = std::make_unique<ControlResponse>(data[0], data[1]);
return response;
auto response = std::make_unique<ControlResponse>(data[0], data[1]);
return response;
}
14 changes: 7 additions & 7 deletions source/SPoverSLIP/ControlRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
class ControlRequest : public Request
{
public:
ControlRequest(const uint8_t request_sequence_number, const uint8_t sp_unit, const uint8_t control_code, std::vector<uint8_t> &data);
std::vector<uint8_t> serialize() const override;
std::unique_ptr<Response> deserialize(const std::vector<uint8_t> &data) const override;
ControlRequest(const uint8_t request_sequence_number, const uint8_t sp_unit, const uint8_t control_code, std::vector<uint8_t> &data);
std::vector<uint8_t> serialize() const override;
std::unique_ptr<Response> deserialize(const std::vector<uint8_t> &data) const override;

const std::vector<uint8_t> &get_data() const { return data_; }
uint8_t get_control_code() const { return control_code_; }
const std::vector<uint8_t> &get_data() const { return data_; }
uint8_t get_control_code() const { return control_code_; }

private:
uint8_t control_code_;
std::vector<uint8_t> data_;
uint8_t control_code_;
std::vector<uint8_t> data_;
};
8 changes: 4 additions & 4 deletions source/SPoverSLIP/ControlResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ ControlResponse::ControlResponse(const uint8_t request_sequence_number, const ui

std::vector<uint8_t> ControlResponse::serialize() const
{
std::vector<uint8_t> data;
data.push_back(this->get_request_sequence_number());
data.push_back(this->get_status());
return data;
std::vector<uint8_t> data;
data.push_back(this->get_request_sequence_number());
data.push_back(this->get_status());
return data;
}
4 changes: 2 additions & 2 deletions source/SPoverSLIP/ControlResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
class ControlResponse : public Response
{
public:
explicit ControlResponse(uint8_t request_sequence_number, uint8_t status);
std::vector<uint8_t> serialize() const override;
explicit ControlResponse(uint8_t request_sequence_number, uint8_t status);
std::vector<uint8_t> serialize() const override;
};
22 changes: 11 additions & 11 deletions source/SPoverSLIP/FormatRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ FormatRequest::FormatRequest(const uint8_t request_sequence_number, const uint8_

std::vector<uint8_t> FormatRequest::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_sp_unit());
return request_data;
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_sp_unit());
return request_data;
}

std::unique_ptr<Response> FormatRequest::deserialize(const std::vector<uint8_t> &data) const
{
if (data.size() < 2)
{
throw std::runtime_error("Not enough data to deserialize FormatResponse");
}
if (data.size() < 2)
{
throw std::runtime_error("Not enough data to deserialize FormatResponse");
}

auto response = std::make_unique<FormatResponse>(data[0], data[1]);
return response;
auto response = std::make_unique<FormatResponse>(data[0], data[1]);
return response;
}
6 changes: 3 additions & 3 deletions source/SPoverSLIP/FormatRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class FormatRequest : public Request
{
public:
FormatRequest(const uint8_t request_sequence_number, const uint8_t sp_unit);
std::vector<uint8_t> serialize() const override;
std::unique_ptr<Response> deserialize(const std::vector<uint8_t> &data) const override;
FormatRequest(const uint8_t request_sequence_number, const uint8_t sp_unit);
std::vector<uint8_t> serialize() const override;
std::unique_ptr<Response> deserialize(const std::vector<uint8_t> &data) const override;
};
8 changes: 4 additions & 4 deletions source/SPoverSLIP/FormatResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ FormatResponse::FormatResponse(const uint8_t request_sequence_number, const uint

std::vector<uint8_t> FormatResponse::serialize() const
{
std::vector<uint8_t> data;
data.push_back(this->get_request_sequence_number());
data.push_back(this->get_status());
return data;
std::vector<uint8_t> data;
data.push_back(this->get_request_sequence_number());
data.push_back(this->get_status());
return data;
}
Loading

0 comments on commit 5ce2bfc

Please sign in to comment.