require_relative "test_helper" require "maptool/can" require "timeout" class CanTest < Minitest::Test Frame = Maptool::Can::Frame def test_a_frame_round_trips_through_struct_can_frame frame = Frame.new(0x181, "\x01\x02\x03".b, false) bytes = frame.pack assert_equal 16, bytes.bytesize assert_equal frame, Frame.unpack(bytes) extended = Frame.new(0x18FEF100, "\xFF".b, true) assert_equal extended, Frame.unpack(extended.pack) assert_nil Frame.unpack([0x181 | Maptool::Can::RTR_FLAG, 0, ""].pack(Maptool::Can::FRAME)), "remote request: no data" end def test_canopen_roles_by_cob_id roles = [0x000, 0x080, 0x081, 0x181, 0x285, 0x20A, 0x585, 0x605, 0x77F].map { |id| Maptool::Can.canopen_role(id) } assert_equal [[:nmt, nil], [:sync, nil], [:emcy, 1], [:tpdo1, 1], [:tpdo2, 5], [:rpdo1, 10], [:sdo_tx, 5], [:sdo_rx, 5], [:heartbeat, 127]], roles assert_nil Maptool::Can.canopen_role(0x181, extended: true) assert_equal [0x01, 0x00], Maptool::Can.nmt_start.data.bytes end def test_signals_decode_by_id_mask_type_and_scale decoder = Maptool::Can::Decoder.new([ { "name" => "gnss.lat", "id" => "0x181", "byte" => 0, "type" => "i32", "scale" => 1e-7 }, { "name" => "gnss.lon", "id" => "0x181", "byte" => 4, "type" => "i32", "scale" => 1e-7 }, { "name" => "keypad.key2", "id" => "0x195", "byte" => 0, "bit" => 1, "type" => "bit" }, { "name" => "car.rpm", "id" => "0x18FEF100", "mask" => "0x00FFFF00", "byte" => 3, "type" => "u16", "scale" => 0.125 }, { "name" => "temp", "id" => "0x281", "byte" => 0, "type" => "i16", "scale" => 0.1, "offset" => -40 }, ]) gnss = decoder.decode(Frame.new(0x181, [491_700_000, 200_500_000].pack("l 1 }, decoder.decode(Frame.new(0x195, "\x02".b, false))) assert_equal({ "keypad.key2" => 0 }, decoder.decode(Frame.new(0x195, "\x01".b, false))) # Another source address (low byte) under the mask; a standard id never matches an extended signal. assert_equal({ "car.rpm" => 800.0 }, decoder.decode(Frame.new(0x18FEF1AB, "\0\0\0#{[6400].pack('S<')}".b, true))) assert_empty decoder.decode(Frame.new(0x0F1, "\0\0\0\0\0".b, false)) assert_in_delta(-15.0, decoder.decode(Frame.new(0x281, [250].pack("s<"), false))["temp"]) assert_empty decoder.decode(Frame.new(0x181, "\x01".b, false)), "too short for the i32s" assert_raises(ArgumentError) { Maptool::Can::Decoder.new([{ "name" => "x", "id" => 1, "type" => "u64" }]) } end def test_the_store_keeps_signals_and_node_heartbeat_states store = Maptool::Can::Store.new store.record("vcan0", Frame.new(0x705, "\x05".b, false), {}, at: 1.0) store.record("vcan0", Frame.new(0x181, "".b, false), { "gnss.lat" => 49.1 }, at: 2.0) snapshot = store.snapshot assert_equal({ frames: 2, last_frame_at: 2.0 }, snapshot[:interfaces]["vcan0"]) assert_equal({ seen_at: 1.0, state: "operational" }, snapshot[:nodes][5]) assert_equal({ value: 49.1, at: 2.0 }, snapshot[:signals]["gnss.lat"]) end # Over a real SocketCAN interface when there is one: # sudo ip link add dev vcan0 type vcan && sudo ip link set up vcan0 def test_frames_go_over_vcan0 skip "no vcan0" unless Socket.getifaddrs.any? { |i| i.name == "vcan0" } store = Maptool::Can::Store.new decoder = Maptool::Can::Decoder.new([{ "name" => "speed", "id" => "0x182", "type" => "u16", "scale" => 0.01 }]) received = Queue.new reader = Thread.new { Maptool::Can.listen("vcan0", decoder: decoder, store: store, retry_delay: 0.1) { |values| received << values } } sleep 0.2 until store.snapshot[:interfaces].dig("vcan0", :up) sender = Maptool::Can.open("vcan0") sender.write(Frame.new(0x182, [1234].pack("S<"), false).pack) assert_in_delta 12.34, Timeout.timeout(2) { received.pop }["speed"] ensure reader&.kill sender&.close end end