Skip to content

Commit

Permalink
Add ruby bindings and rspec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Hahn committed May 1, 2016
1 parent 7124f3e commit d666d3f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
37 changes: 37 additions & 0 deletions bindings/ruby/lib/ecutools.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'ffi'
require 'pp'

class EcutoolsError < StandardError; end

module Ecutools; end

module Ecutools::J2534
extend FFI::Library
ffi_lib 'c'
#ffi_lib '/usr/local/lib/libj2534.so'
ffi_lib '../../.libs/libj2534.so'
attach_function :PassThruScanForDevices, [ :pointer ], :long
attach_function :PassThruOpen, [ :pointer, :pointer ], :long
end

module Ecutools

class Api

def PassThruScanForDevices
pDeviceCount = FFI::MemoryPointer.new(:ulong, 1)
raise 'PassThruScanForDevices error' if Ecutools::J2534.PassThruScanForDevices(pDeviceCount) != 0
pDeviceCount.read_ulong
end

def PassThruOpen(name, deviceId)
pName = FFI::MemoryPointer.from_string(name)
pDeviceId = FFI::MemoryPointer.new(:ulong, 1)
rc = Ecutools::J2534.PassThruOpen(pName, pDeviceId)
raise EcutoolsError, "PassThruOpen: error=#{rc}" unless rc == 0
rc
end

end

end
21 changes: 21 additions & 0 deletions bindings/ruby/spec/ecutools_api_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

describe Ecutools::Api do

let(:ecutools) { Ecutools::Api.new }
let(:deviceName) { "J2534-1:ecutools"}
let(:deviceId) { 420 }

it 'PassThruScanForDevices returns 1 device' do
expect(ecutools.PassThruScanForDevices).to eq(1)
end

it 'PassThruOpen returns success when it finds VirtualDataLogger' do
expect(ecutools.PassThruOpen("VirtualDataLogger", deviceId)).to eq(0)
end

it 'PassThruOpen throws EcutoolsError when it doesnt find the requested device' do
expect { ecutools.PassThruOpen(deviceName, deviceId) }.to raise_error EcutoolsError
end

end
2 changes: 2 additions & 0 deletions bindings/ruby/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'ecutools'

0 comments on commit d666d3f

Please sign in to comment.