Class: CanMessenger::Adapter::Base

Inherits:
Object
  • Object
show all
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

Socketcan

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#endiannessObject (readonly)

Returns the value of attribute endianness.



8
9
10
# File 'lib/can_messenger/adapter/base.rb', line 8

def endianness
  @endianness
end

#interface_nameObject (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

#loggerObject (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_endiannessObject



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.

Raises:

  • (NotImplementedError)


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.

Returns:

  • (Object)

    adapter-specific socket

Raises:

  • (NotImplementedError)


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.

Raises:

  • (NotImplementedError)


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.

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/can_messenger/adapter/base.rb', line 43

def receive_message(socket:, can_fd: false)
  raise NotImplementedError, "receive_message must be implemented in subclasses"
end