usb-detection-sensor 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *.gemspec
7
+ log/*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 art+com AG/dirk luesebrink
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ # USB device detection
2
+
3
+ Detect USB devices when being connected and update a GOM sensor node
4
+ accordingly. This allows to automagically avtivate the device mirror overlay
5
+ on the horizon in home for example.
6
+
7
+ # DroidEx
8
+
9
+ http://github.com/commonsguy/droidex
10
+
11
+ ## Note on Patches/Pull Requests
12
+ - Fork the project.
13
+ - Make your feature addition or bug fix.
14
+ - Add tests for it. This is important so I don't break it in a future version
15
+ unintentionally.
16
+ - Commit, do not mess with rakefile, version, or history. (if you want to
17
+ have your own version, that is fine but bump version in a commit by itself
18
+ I can ignore when I pull)
19
+ - Send me a pull request. Bonus points for topic branches.
20
+
21
+ ## Copyright
22
+
23
+ Copyright (c) 2010 art+com/dirk luesebrink. See LICENSE for details.
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "usb-detection-sensor"
8
+ gem.summary = %Q{usb device connect/disconnect detection as GOM sensor}
9
+ gem.description = %Q{
10
+ Detect USB devices when being connected and update a GOM sensor node
11
+ accordingly. This allows to automagically avtivate the device mirror
12
+ overlay on the horizon in home for example.
13
+ }
14
+ gem.email = "dirk.luesebrink@gmail.com"
15
+ gem.homepage = "http://github.com/crux/usb-detection-sensor"
16
+ gem.authors = ["art+com/dirk luesebrink"]
17
+
18
+ gem.add_development_dependency "rspec"
19
+ gem.add_development_dependency "fakeweb"
20
+
21
+ gem.add_runtime_dependency "json_pure"
22
+ gem.add_runtime_dependency "applix"
23
+ gem.add_runtime_dependency "gom-script"
24
+ end
25
+ Jeweler::GemcutterTasks.new
26
+ rescue LoadError
27
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
28
+ end
29
+
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:rcov) do |t|
32
+ t.rcov_opts = %q[--exclude "spec"]
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ if File.exist?('VERSION')
42
+ version = File.read('VERSION')
43
+ else
44
+ version = ""
45
+ end
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "usb-detection-sensor #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.2
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'applix'
5
+
6
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
7
+ require 'usb-detection-sensor'
8
+
9
+ class Usage < ArgumentError; end
10
+
11
+ # TODO: refactoring this into gom-core logger
12
+ def redirect_log path
13
+ unless path == '-' # '-' means stdout means nothing
14
+ puts " -- redirecting stdout/stderr to: #{path}"
15
+ out = File.open(path, File::WRONLY|File::APPEND|File::CREAT)
16
+ out.sync = true
17
+ $stderr = $stdout = out
18
+ # first line after redirect
19
+ puts " -- logile redirect at #{Time.now}"
20
+ end
21
+ end
22
+
23
+ def main argv
24
+ options = Hash.from_argv argv
25
+ args = (options.delete :args)
26
+
27
+ handler = UsbDetectionSensor::CommandLineHandler.new options
28
+ redirect_log handler.logfile
29
+
30
+ if op = args.shift
31
+ handler.send op, *args
32
+ else
33
+ # TODO: process could become part of the udev rule script and this special
34
+ # case would went extinct
35
+ handler.process ENV
36
+ end
37
+
38
+ rescue Usage => e
39
+ puts <<-TXT
40
+
41
+ usage: #{__FILE__} [ <connect> | <disconnect> ]
42
+
43
+ ## #{e}
44
+ TXT
45
+ rescue => e
46
+ puts " ## #{e}\n -> #{e.backtrace.join "\n "}"
47
+ end
48
+
49
+ main ARGV
50
+ # vim: syntax=ruby
Binary file
@@ -0,0 +1,2 @@
1
+ SUBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666", RUN+="usb-udev-rules-handler --config=${CONFIG_FILE}"
2
+ SUBSYSTEM=="usb", ACTION=="remove", RUN+="usb-udev-rules-handler --config=${CONFIG_FILE}"
Binary file
Binary file
@@ -0,0 +1,8 @@
1
+ ---
2
+ :logfile: "-"
3
+ :java_exe: /usr/bin/java
4
+ :scale: 1
5
+ :android_root:
6
+ :gom_url: http://gom/sensors/usb-detect/home
7
+ :droid_pid_file: /tmp/droid.pid
8
+ :overlay_path: # nil default, e.g.: /areas/home/horizon:overlay-1
@@ -0,0 +1,3 @@
1
+ SUBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666", RUN+="/home/sonic/artcom/usb-detect/android_udev.sh"
2
+ SUBSYSTEM=="usb", ACTION=="remove", RUN+="/home/sonic/artcom/usb-detect/android_udev_remove.sh"
3
+
@@ -0,0 +1,6 @@
1
+ require 'applix/oattr'
2
+ require 'tempfile'
3
+ require 'json'
4
+ require 'gom-script'
5
+
6
+ require 'usb-detection-sensor/command-line-handler'
@@ -0,0 +1,172 @@
1
+ module UsbDetectionSensor
2
+ class CommandLineHandler
3
+
4
+ include OAttr
5
+ oattr :logfile, :gom_url, :gom_path, :java_exe, :droid_pid_file,
6
+ :overlay_path, :scale
7
+
8
+ # configuration precedence: Defaults -> --config=file -> commandline
9
+ def initialize options = {}
10
+ defaults = (YAML.load_file defaults_path)
11
+ @options = (YAML.load_file options[:config]) rescue {}
12
+ @options = defaults.merge(@options.merge options)
13
+ end
14
+
15
+ def process env
16
+ @env = env
17
+ #puts " -- ENV: #{@env.inspect}"
18
+ op = @env["ACTION"].downcase
19
+ self.send op
20
+ end
21
+
22
+ def info
23
+ puts " -- runtime env:\n#{@options.inspect}"
24
+ end
25
+
26
+ def ginfo
27
+ puts(gom.read "#{@gom_path}.json")
28
+ end
29
+
30
+ # 'add' gets called as udev rules action
31
+ def add
32
+ puts " -- connect: #{id_serial}"
33
+
34
+ wait_for_device
35
+ push_device_path # device_path
36
+ start_droid_exe
37
+ switch_horizon_overlay :grabber
38
+ end
39
+
40
+ # 'remove' gets called as udev rules action
41
+ def remove
42
+ switch_horizon_overlay :off
43
+ dev_type = @env["DEVTYPE"]
44
+ unless dev_type == "usb_device"
45
+ puts " .. ignoring remove for dev_type: #{dev_type}"
46
+ return
47
+ end
48
+ puts " -- disconnect: #{id_serial}"
49
+
50
+ stop_droid_exe
51
+ remove_device_path_attr
52
+ end
53
+
54
+ def switch_horizon_overlay mode
55
+ if overlay_path.nil?
56
+ puts " !! :overlay_path not defined"
57
+ return
58
+ end
59
+ gom.write overlay_path, mode.to_s
60
+ end
61
+
62
+ def id_serial
63
+ @id_serial ||= @env["ID_SERIAL"]
64
+ end
65
+
66
+ def dump_udev_rules
67
+ puts(File.read rules_path)
68
+ end
69
+
70
+ def dump_defaults
71
+ puts(File.read defaults_path)
72
+ end
73
+
74
+ # lazy load the GOM connection
75
+ def gom
76
+ return @gom unless @gom.nil?
77
+ @gom, @gom_path = Gom::Remote::Connection.init gom_url
78
+ @gom
79
+ end
80
+
81
+ def wait_for_device
82
+ puts = open("|#{adb} wait-for-device 2>&1").read
83
+ end
84
+
85
+ #def device_path
86
+ # @device_path ||= dev_config["device-path"]
87
+ #end
88
+ # write GOM specific device path for mobile: /areas/mobile/Generic-MD-I18
89
+ def push_device_path # device_path
90
+ device_path ||= dev_config["device-path"]
91
+ puts " .. push device-path: #{device_path}"
92
+ # http://gom/sensors/usb-detect/home:device_path &>/dev/null
93
+ (gom.write "#{@gom_path}:device_path", device_path)
94
+ rescue => e
95
+ puts " !! failed: #{e}"
96
+ end
97
+
98
+ def start_droid_exe
99
+ puts " .. start_droid_exe"
100
+ # java -cp DroidEx.jar:ddmlib.jar com.commonsware.droidex.DroidEx
101
+ clazz = "com.commonsware.droidex.DroidEx"
102
+ cmd = "#{java_exe} #{classpath} #{clazz}"
103
+ cmd = "DISPLAY=:0.0 #{cmd} -s #{scale} #{droid_log_redirect}"
104
+ puts " executing: #{cmd}"
105
+ pid = fork do
106
+ sid = Process.setsid
107
+ puts " session id: #{sid}"
108
+ system cmd
109
+ end
110
+ puts " droid pid: #{pid}"
111
+ File.open(droid_pid_file, "w") {|fd| fd.write pid; fd.flush}
112
+ Process.detach pid
113
+ end
114
+
115
+ def stop_droid_exe
116
+ puts " .. kill droid"
117
+ pid = Integer(File.read droid_pid_file)
118
+ Process.kill -15, pid
119
+ rescue => e
120
+ puts " !! stopping droid: #{e}"
121
+ end
122
+
123
+ def remove_device_path_attr
124
+ puts " .. remove device_path attribute"
125
+ gom.destroy "#{@gom_path}:device_path"
126
+ end
127
+
128
+ private
129
+
130
+ def overlay_path
131
+ #@overlay_path ||= (gom.read "#{@gom_path}:overlay_path.json")
132
+ @overlay_path ||= @options[:overlay_path]
133
+ end
134
+
135
+ def droid_log_redirect
136
+ (logfile == '-') && nil || (">> #{logfile} 2>&1")
137
+ end
138
+
139
+ # lazy loading device config file from well known sdcard location
140
+ def dev_config
141
+ @dev_config ||= begin
142
+ cmd = "#{adb} shell cat /sdcard/device_config.json"
143
+ puts " executing: #{cmd}"
144
+ json = open("|#{cmd} 2>&1").read
145
+ parsed = (JSON.parse json)
146
+ puts " dev config: #{parsed.inspect}"
147
+ parsed
148
+ end
149
+ end
150
+
151
+ def defaults_path
152
+ File.join(basedir, %w{etc defaults.yml})
153
+ end
154
+
155
+ def rules_path
156
+ File.join(basedir, %w{etc android-udev-rules})
157
+ end
158
+
159
+ def adb
160
+ @adb ||= "#{@options[:android_root]}/tools/adb"
161
+ end
162
+
163
+ def classpath
164
+ jars = Dir["#{basedir}/etc/*.jar"]
165
+ "-cp #{jars.join ':'}"
166
+ end
167
+
168
+ def basedir
169
+ File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
170
+ end
171
+ end
172
+ end
@@ -0,0 +1 @@
1
+ --backtrace --debugger
@@ -0,0 +1,17 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'usb-detection-sensor'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ require 'fakeweb'
9
+ FakeWeb.allow_net_connect = false
10
+
11
+ Spec::Runner.configure do |config|
12
+ config.before :each do
13
+ end
14
+
15
+ config.after :each do
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "UsbConnectGomSensor" do
4
+ it "should find class & module" do
5
+ UsbDetectionSensor.should_not == nil
6
+ end
7
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ include UsbDetectionSensor
4
+
5
+ describe "UsbConnectGomSensor::CommandLineHandler" do
6
+ it "should find class & module" do
7
+ CommandLineHandler.should_not == nil
8
+ end
9
+
10
+ describe "with a fake connection" do
11
+ before :each do
12
+ $gom = Gom::Remote.connection = Object.new
13
+ (Gom::Remote::Connection.stub! :init).and_return($gom, '/')
14
+ end
15
+
16
+ it "should should switch on overlays" do
17
+ @clh = CommandLineHandler.new :overlay_path => '/some:overlay-2'
18
+ $gom.should_receive(:write).with("/some:overlay-2", "grabber")
19
+ (@clh.switch_horizon_overlay :grabber)
20
+ end
21
+
22
+ it "should should switch off overlays" do
23
+ @clh = CommandLineHandler.new :overlay_path => '/some:overlay-1'
24
+ $gom.should_receive(:write).with("/some:overlay-1", "off")
25
+ (@clh.switch_horizon_overlay :off)
26
+ end
27
+
28
+ describe "with a default handler" do
29
+ before :each do
30
+ @clh = CommandLineHandler.new
31
+ end
32
+
33
+ it "should respond to remove requests" do
34
+ (@clh.respond_to? :info).should == true
35
+ end
36
+
37
+ it "should should not switch overlays" do
38
+ $gom.should_not_receive(:write)
39
+ (@clh.switch_horizon_overlay :grabber)
40
+ end
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: usb-detection-sensor
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 2
10
+ version: 0.2.2
11
+ platform: ruby
12
+ authors:
13
+ - art+com/dirk luesebrink
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-20 00:00:00 +01:00
19
+ default_executable: usb-udev-rules-handler
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: fakeweb
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: json_pure
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: applix
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :runtime
76
+ version_requirements: *id004
77
+ - !ruby/object:Gem::Dependency
78
+ name: gom-script
79
+ prerelease: false
80
+ requirement: &id005 !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ type: :runtime
90
+ version_requirements: *id005
91
+ description: "\n Detect USB devices when being connected and update a GOM sensor node\n accordingly. This allows to automagically avtivate the device mirror\n overlay on the horizon in home for example.\n "
92
+ email: dirk.luesebrink@gmail.com
93
+ executables:
94
+ - usb-udev-rules-handler
95
+ extensions: []
96
+
97
+ extra_rdoc_files:
98
+ - LICENSE
99
+ - README.markdown
100
+ files:
101
+ - .gitignore
102
+ - LICENSE
103
+ - README.markdown
104
+ - Rakefile
105
+ - VERSION
106
+ - bin/usb-udev-rules-handler
107
+ - etc/DroidEx.jar
108
+ - etc/android-udev-rules
109
+ - etc/args4j-2.0.16.jar
110
+ - etc/ddmlib.jar
111
+ - etc/defaults.yml
112
+ - etc/udev/rules.d/51-android.rules
113
+ - lib/usb-detection-sensor.rb
114
+ - lib/usb-detection-sensor/command-line-handler.rb
115
+ - spec/spec.opts
116
+ - spec/spec_helper.rb
117
+ - spec/usb-connect-gom-sensor_spec.rb
118
+ - spec/usb-detection-sensor/command-line-handler_spec.rb
119
+ has_rdoc: true
120
+ homepage: http://github.com/crux/usb-detection-sensor
121
+ licenses: []
122
+
123
+ post_install_message:
124
+ rdoc_options:
125
+ - --charset=UTF-8
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ requirements: []
147
+
148
+ rubyforge_project:
149
+ rubygems_version: 1.3.7
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: usb device connect/disconnect detection as GOM sensor
153
+ test_files:
154
+ - spec/spec_helper.rb
155
+ - spec/usb-connect-gom-sensor_spec.rb
156
+ - spec/usb-detection-sensor/command-line-handler_spec.rb