68 lines
1.9 KiB
C++
68 lines
1.9 KiB
C++
/*
|
|
* HipMessage.h
|
|
*
|
|
* Created on: 4 ìàð. 2020 ã.
|
|
* Author: user
|
|
*/
|
|
|
|
#ifndef UMLIBRARY_DRIVER_HIPMESSAGE_HH_
|
|
#define UMLIBRARY_DRIVER_HIPMESSAGE_HH_
|
|
|
|
#include "HipMessageControl.hh"
|
|
|
|
namespace driver {
|
|
|
|
struct Header {
|
|
union {
|
|
struct {
|
|
uint16_t address_value :5;
|
|
const uint16_t fixed_pattern :3;
|
|
uint16_t :8;
|
|
uint16_t command_value :6;
|
|
const uint16_t fixed1 :1;
|
|
uint16_t error_bit :1;
|
|
};
|
|
|
|
uint16_t bytes_2[2];
|
|
};
|
|
|
|
Header(uint16_t address, uint16_t command)
|
|
: address_value(address), command_value(command),
|
|
fixed_pattern(0x2), fixed1(0x1) {}
|
|
|
|
Header(char bytes[2]) { bytes_2[0] = bytes[0], bytes_2[1] = bytes[1]; }
|
|
};
|
|
|
|
//...???
|
|
class Message {
|
|
private:
|
|
static const unsigned short first_data_byte_pos = 2;
|
|
|
|
char * const data = nullptr; //âíåøíèé áóôåð äëÿ ïðèåìà è ïåðåäà÷è ñîîáùåíèé
|
|
const std::size_t data_size = 0; //ðàçìåð âíåøíåãî áóôåðà
|
|
|
|
public:
|
|
Message( char * data, std::size_t data_size ) : data(data), data_size(data_size) {}
|
|
|
|
Message & operator=( const Header & header );
|
|
|
|
operator Header() const { return Header(data); }
|
|
|
|
HipResult unpack_message( const MessageControl * message );
|
|
|
|
/// Ôóíêöèÿ ïîäãîòàâëèâàåò áóôåð äëÿ îòïðàâêè.
|
|
/// Âîçâðàùàåò false, åñëè ðàçìåð áóôåðà íåäîñòàòî÷åí.
|
|
bool pack_message( const MessageControl * message );
|
|
|
|
static std::size_t getRecMessageLen( const MessageControl * message ) { return message->rec_data_size + 3; }
|
|
|
|
static std::size_t getSendMessageLen( const MessageControl * message ) { return message->send_data_size + 3; }
|
|
|
|
static unsigned short calcControlSumm( const char * data, std::size_t data_len );
|
|
|
|
};
|
|
|
|
} /* namespace driver */
|
|
|
|
#endif /* UMLIBRARY_DRIVER_HIPMESSAGE_HH_ */
|