Class: CanMessenger::Adapter::Base
- Inherits:
-
Object
- Object
- CanMessenger::Adapter::Base
- Defined in:
- lib/can_messenger/adapter/base.rb
Overview
Base adapter defines the interface for CAN bus adapters. Concrete adapters must implement all of the methods defined here.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#endianness ⇒ Object
readonly
Returns the value of attribute endianness.
-
#interface_name ⇒ Object
readonly
Returns the value of attribute interface_name.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
Instance Method Summary collapse
-
#build_can_frame(id:, data:, extended_id: false, can_fd: false) ⇒ Object
Build a frame ready to be written to the socket.
-
#initialize(interface_name:, logger:, endianness: :native) ⇒ Base
constructor
A new instance of Base.
-
#open_socket(can_fd: false) ⇒ Object
Open a socket for the underlying interface.
-
#parse_frame(frame:, can_fd: false) ⇒ Object
Parse a raw frame string into a message hash.
-
#receive_message(socket:, can_fd: false) ⇒ Object
Receive and parse a frame from the socket.
Constructor Details
#initialize(interface_name:, logger:, endianness: :native) ⇒ Base
Returns a new instance of Base.
24 25 26 27 28 29 |
# File 'lib/can_messenger/adapter/base.rb', line 24 def initialize(interface_name:, logger:, endianness: :native) @interface_name = interface_name @logger = logger normalized_endianness = normalize_endianness(endianness) @endianness = normalized_endianness == :native ? self.class.native_endianness : normalized_endianness end |
Instance Attribute Details
#endianness ⇒ Object (readonly)
Returns the value of attribute endianness.
8 9 10 |
# File 'lib/can_messenger/adapter/base.rb', line 8 def endianness @endianness end |
#interface_name ⇒ Object (readonly)
Returns the value of attribute interface_name.
8 9 10 |
# File 'lib/can_messenger/adapter/base.rb', line 8 def interface_name @interface_name end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/can_messenger/adapter/base.rb', line 8 def logger @logger end |
Class Method Details
.native_endianness ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/can_messenger/adapter/base.rb', line 10 def self.native_endianness native = pack_uint(1, "I") return :little if native == pack_uint(1, "V") return :big if native == pack_uint(1, "N") # Fallback for unusual platforms. native.bytes.first == 1 ? :little : :big end |
Instance Method Details
#build_can_frame(id:, data:, extended_id: false, can_fd: false) ⇒ Object
Build a frame ready to be written to the socket.
38 39 40 |
# File 'lib/can_messenger/adapter/base.rb', line 38 def build_can_frame(id:, data:, extended_id: false, can_fd: false) raise NotImplementedError, "build_can_frame must be implemented in subclasses" end |
#open_socket(can_fd: false) ⇒ Object
Open a socket for the underlying interface.
33 34 35 |
# File 'lib/can_messenger/adapter/base.rb', line 33 def open_socket(can_fd: false) raise NotImplementedError, "open_socket must be implemented in subclasses" end |
#parse_frame(frame:, can_fd: false) ⇒ Object
Parse a raw frame string into a message hash.
48 49 50 |
# File 'lib/can_messenger/adapter/base.rb', line 48 def parse_frame(frame:, can_fd: false) raise NotImplementedError, "parse_frame must be implemented in subclasses" end |
#receive_message(socket:, can_fd: false) ⇒ Object
Receive and parse a frame from the socket.
43 44 45 |
# File 'lib/can_messenger/adapter/base.rb', line 43 def (socket:, can_fd: false) raise NotImplementedError, "receive_message must be implemented in subclasses" end |