Skip to content

Commit

Permalink
Proxy mode for added for protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
paveldn committed Jan 30, 2024
1 parent d7eb9c9 commit c9784fb
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 76 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.vscode/
/bin/

# Prerequisites
*.d
Expand Down
53 changes: 34 additions & 19 deletions include/protocol/haier_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <chrono>
#include <functional>
#include <map>
#include <memory>
#include <queue>
#include "transport/protocol_transport.h"
#include "protocol/haier_message.h"
Expand All @@ -14,14 +15,20 @@ namespace haier_protocol

enum class HandlerError
{
HANDLER_OK = 0,
UNSUPPORTED_MESSAGE,
UNEXPECTED_MESSAGE,
UNSUPPORTED_SUBCOMMAND,
WRONG_MESSAGE_STRUCTURE,
RUNTIME_ERROR,
UNKNOWN_ERROR,
INVALID_ANSWER,
HANDLER_OK = 0,
UNSUPPORTED_MESSAGE,
UNEXPECTED_MESSAGE,
UNSUPPORTED_SUBCOMMAND,
WRONG_MESSAGE_STRUCTURE,
RUNTIME_ERROR,
UNKNOWN_ERROR,
INVALID_ANSWER,
};

enum class AnswerDestination
{
DIRECT_ANSWER,
PROXY_ANSWER
};

// Message handler type. Expected that function sends answer back.
Expand All @@ -36,16 +43,17 @@ using MessageHandler = std::function<HandlerError(FrameType, const uint8_t*, siz
// argument 2: Incoming message type
// argument 3: Incoming data buffer (nullptr if none)
// argument 4: Incoming data buffer size
// argument 5: Type of answer (direct or proxy)
// return: Result of processing
using AnswerHandler = std::function<HandlerError(FrameType, FrameType, const uint8_t*, size_t)>;
using AnswerHandler = std::function<HandlerError(FrameType, FrameType, const uint8_t*, size_t, AnswerDestination)>;

// Timeout handler type.
// argument 1: Request message type that caused this answer
// return: Result of processing
using TimeoutHandler = std::function<HandlerError(FrameType)>;

HandlerError default_message_handler(FrameType message_type, const uint8_t* data, size_t data_size);
HandlerError default_answer_handler(FrameType message_type, FrameType request_type, const uint8_t* data, size_t data_size);
HandlerError default_answer_handler(FrameType message_type, FrameType request_type, const uint8_t* data, size_t data_size, AnswerDestination dst);
HandlerError default_timeout_handler(FrameType message_type);

class ProtocolHandler
Expand All @@ -71,37 +79,44 @@ class ProtocolHandler
void remove_answer_handler(FrameType message_type);
void set_default_answer_handler(AnswerHandler handler);
void set_timeout_handler(FrameType message_type, TimeoutHandler handler);
void remove_timeout_handler(FrameType message_type);
void remove_timeout_handler(FrameType message_type);
void set_default_timeout_handler(TimeoutHandler handler);
void set_proxy(ProtocolStream&);
void remove_proxy();
bool has_proxy() const;
virtual void loop();
protected:
bool write_message_(const HaierMessage& message, bool use_crc);
size_t drop_extra_messages_(TransportLevelHandler& transport);
enum class ProtocolState
{
IDLE,
WAITING_FOR_ANSWER,
WAITING_FOR_PROXY_ANSWER,
};
struct OutgoingQueueItem
{
const HaierMessage message;
bool use_crc;
int number_of_retries;
std::chrono::milliseconds retry_interval;
bool proxy_message;
};
using OutgoingQueue = std::queue<OutgoingQueueItem>;
TransportLevelHandler transport_;
std::unique_ptr<TransportLevelHandler> proxy_transport_;
std::map<FrameType, MessageHandler> message_handlers_map_;
std::map<FrameType, AnswerHandler> answer_handlers_map_;
std::map<FrameType, TimeoutHandler> timeout_handlers_map_;
OutgoingQueue outgoing_messages_;
MessageHandler default_message_handler_;
AnswerHandler default_answer_handler_;
TimeoutHandler default_timeout_handler_;
ProtocolState state_;
bool processing_message_;
bool incoming_message_crc_status_;
bool answer_sent_;
FrameType last_message_type_;
MessageHandler default_message_handler_{default_message_handler};
AnswerHandler default_answer_handler_{default_answer_handler};
TimeoutHandler default_timeout_handler_{default_timeout_handler};
ProtocolState state_{ProtocolState::IDLE};
bool processing_message_{false};
bool incoming_message_crc_status_{false};
bool answer_sent_{false};
FrameType last_message_type_{FrameType::UNKNOWN_FRAME_TYPE};
std::chrono::milliseconds answer_timeout_interval_;
std::chrono::milliseconds cooldown_interval_;
std::chrono::steady_clock::time_point cooldown_time_point_;
Expand Down
8 changes: 4 additions & 4 deletions include/transport/protocol_transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ class TransportLevelHandler
TransportLevelHandler(const TransportLevelHandler&) = delete;
TransportLevelHandler& operator=(const TransportLevelHandler&) = delete;
explicit TransportLevelHandler(ProtocolStream& stream, size_t buffer_size = MAX_FRAME_SIZE + 0x10) noexcept;
virtual ~TransportLevelHandler();
uint8_t send_data(uint8_t frameType, const uint8_t* data, size_t data_size, bool use_crc=true);
size_t read_data();
void process_data();
size_t available() const noexcept { return this->incoming_queue_.size(); };
bool pop(TimestampedFrame& tframe);
void drop(size_t frames_count);
void reset_protocol() noexcept;
virtual ~TransportLevelHandler();
protected:
void clear_();
void drop_bytes_(size_t size);
ProtocolStream& stream_;
CircularBuffer<uint8_t> buffer_;
size_t pos_;
size_t sep_count_;
bool frame_start_found_;
size_t pos_{0};
size_t sep_count_{0};
bool frame_start_found_{false};
HaierFrame current_frame_;
std::chrono::steady_clock::time_point frame_start_;
std::queue<TimestampedFrame> incoming_queue_;
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "HaierProtocol",
"version": "0.9.25",
"version": "0.9.26",
"description": "A library to control Haier AC using serial protocol",
"keywords": "haier, air-conditioner, uart, serial",
"repository":
Expand Down
Loading

0 comments on commit c9784fb

Please sign in to comment.