forked from AppleWin/AppleWin
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c136cc
commit 627fee2
Showing
70 changed files
with
1,410 additions
and
1,342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BasedOnStyle: LLVM | ||
ColumnLimit: 160 | ||
BreakBeforeBraces: Allman | ||
AllowShortBlocksOnASingleLine: true | ||
AlwaysBreakAfterReturnType: None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,26 @@ | ||
#include "stdafx.h" | ||
#include "CloseRequest.h" | ||
|
||
#include "CloseResponse.h" | ||
#include "SmartPortCodes.h" | ||
|
||
|
||
CloseRequest::CloseRequest(const uint8_t request_sequence_number, const uint8_t sp_unit) | ||
: Request(request_sequence_number, SP_CLOSE, sp_unit) {} | ||
CloseRequest::CloseRequest(const uint8_t request_sequence_number, const uint8_t sp_unit) : Request(request_sequence_number, SP_CLOSE, sp_unit) {} | ||
|
||
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 | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <cstdint> | ||
#include <vector> | ||
|
||
#include "Request.h" | ||
#include "Response.h" | ||
|
||
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
#include "stdafx.h" | ||
#include "CloseResponse.h" | ||
|
||
CloseResponse::CloseResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
#include "StdAfx.h" | ||
|
||
#include <vector> | ||
#include "Connection.h" | ||
#include <condition_variable> | ||
#include <cstdint> | ||
#include <mutex> | ||
#include <condition_variable> | ||
#include "Connection.h" | ||
#include <stdexcept> | ||
#include <vector> | ||
|
||
std::vector<uint8_t> Connection::wait_for_response(uint8_t request_id, std::chrono::seconds timeout) | ||
{ | ||
std::unique_lock<std::mutex> lock(responses_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 (!response_cv_.wait_for(lock, timeout, [this, request_id]() { return responses_.count(request_id) > 0; })) | ||
{ | ||
throw std::runtime_error("Timeout waiting for response"); | ||
} | ||
std::vector<uint8_t> response_data = responses_[request_id]; | ||
responses_.erase(request_id); | ||
return response_data; | ||
std::unique_lock<std::mutex> lock(responses_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 (!response_cv_.wait_for(lock, timeout, [this, request_id]() { return responses_.count(request_id) > 0; })) | ||
{ | ||
throw std::runtime_error("Timeout waiting for response"); | ||
} | ||
std::vector<uint8_t> response_data = responses_[request_id]; | ||
responses_.erase(request_id); | ||
return response_data; | ||
} | ||
|
||
std::vector<uint8_t> Connection::wait_for_request() | ||
{ | ||
std::unique_lock<std::mutex> lock(responses_mutex_); | ||
response_cv_.wait(lock, [this]() { return !responses_.empty(); }); | ||
const auto it = responses_.begin(); | ||
std::vector<uint8_t> request_data = it->second; | ||
responses_.erase(it); | ||
std::unique_lock<std::mutex> lock(responses_mutex_); | ||
response_cv_.wait(lock, [this]() { return !responses_.empty(); }); | ||
const auto it = responses_.begin(); | ||
std::vector<uint8_t> request_data = it->second; | ||
responses_.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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,37 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <atomic> | ||
#include <map> | ||
#include <chrono> | ||
#include <condition_variable> | ||
#include <cstdint> | ||
#include <map> | ||
#include <mutex> | ||
#include <condition_variable> | ||
#include <chrono> | ||
#include <thread> | ||
#include <vector> | ||
|
||
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 create_read_channel() = 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(); | ||
|
||
private: | ||
std::atomic<bool> is_connected_{false}; | ||
std::atomic<bool> is_connected_{false}; | ||
|
||
protected: | ||
std::map<uint8_t, std::vector<uint8_t>> responses_; | ||
std::thread reading_thread_; | ||
std::map<uint8_t, std::vector<uint8_t>> responses_; | ||
std::thread reading_thread_; | ||
|
||
std::mutex responses_mutex_; | ||
std::condition_variable response_cv_; | ||
std::mutex responses_mutex_; | ||
std::condition_variable response_cv_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
#include "stdafx.h" | ||
#include "ControlRequest.h" | ||
|
||
#include "ControlResponse.h" | ||
#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)) {} | ||
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)) | ||
{ | ||
} | ||
|
||
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 | ||
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; | ||
} |
Oops, something went wrong.