tochka 0.1.1

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,105 @@
1
+ module Tochka
2
+ require "wiringpi"
3
+
4
+ class PiTFTButton
5
+ PIN1 = 4
6
+ PIN2 = 3
7
+ PIN3 = 2
8
+ PIN4 = 1
9
+
10
+ PIN_MODE_INPUT=0
11
+
12
+ def initialize
13
+ if ENV['BUTTON_MOCK'] != nil
14
+ dummy_init
15
+ else
16
+ real_init
17
+ end
18
+
19
+ @prev = [false, false, false, false]
20
+
21
+ end
22
+
23
+ def dummy_init
24
+ # do nothing
25
+ end
26
+
27
+ def real_init
28
+ @io = WiringPi::GPIO.new
29
+
30
+ [PIN1, PIN2, PIN3, PIN4].each do |pin|
31
+ @io.pin_mode(pin, PIN_MODE_INPUT)
32
+ @io.pull_up_dn_control(pin, WiringPi::PUD_UP)
33
+ end
34
+
35
+ backlight_on()
36
+ end
37
+
38
+ def button_all
39
+ return [button1, button2, button3, button4]
40
+ end
41
+
42
+ def button1
43
+ read_button(PIN1)
44
+ end
45
+
46
+ def button2
47
+ read_button(PIN2)
48
+ end
49
+
50
+ def button3
51
+ read_button(PIN3)
52
+ end
53
+
54
+ def button4
55
+ read_button(PIN4)
56
+ end
57
+
58
+ def button_all_edge
59
+ buttons = button_all()
60
+ edges = []
61
+ 0.upto(3) do |n|
62
+ if buttons[n] == true and buttons[n] != @prev[n]
63
+ edges[n] = true
64
+ else
65
+ edges[n] = false
66
+ end
67
+ end
68
+
69
+ @prev = buttons
70
+ return edges
71
+ end
72
+
73
+ def backlight_on
74
+ set_backlight(1)
75
+ end
76
+
77
+ def backlight_off
78
+ set_backlight(0)
79
+ end
80
+
81
+ private
82
+ def read_button pin
83
+ return @io.digital_read(pin) == 0
84
+ end
85
+
86
+ def set_backlight val
87
+ File.open("/sys/class/gpio/gpio508/value", "w") do |f|
88
+ f.write(val.to_s)
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ if __FILE__ == $0
95
+ pitftb = Tochka::PiTFTButton.new
96
+ while true
97
+ buttons = pitftb.button_all_edge
98
+ 0.upto(3) do |n|
99
+ bn = n + 1
100
+ if buttons[n] == true
101
+ print "Button #{bn} is On"
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ module Tochka
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,113 @@
1
+ module Tochka
2
+ require "open3"
3
+
4
+ require "tochka/athsurvey"
5
+
6
+ class Wlan
7
+ attr_reader :duration, :current_channel, :file_size, :channel_walk, :utilization, :utilization_channel
8
+ CHAN = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
9
+ 34, 36, 38, 40, 42, 44, 46, 48,
10
+ 52, 56, 60, 64,
11
+ 100, 104, 108, 112, 116,
12
+ 120, 124, 128, 132, 136, 140,
13
+ 149, 153, 157, 161, 165]
14
+ LIMIT_IDX=CHAN.length
15
+
16
+ def initialize ifname=DEFAULT_IFNAME
17
+ @th_shark = nil
18
+ @ifname = ifname || DEFAULT_IFNAME
19
+ init_status()
20
+ @black_list = []
21
+ @athsurvey = Tochka::Athsurvey.new(@ifname)
22
+ end
23
+
24
+ def run_capture fpath
25
+ init_device()
26
+ init_status()
27
+ start_time = Time.now.to_i
28
+
29
+ stdin, stdout, stderr, @th_tshark = *Open3.popen3(
30
+ "tshark -i #{@ifname} -F pcapng -w #{fpath}")
31
+
32
+ while @th_tshark.alive?
33
+ sleep 1
34
+
35
+ # update status
36
+ @duration = Time.now.to_i - start_time
37
+ @file_size = File.size?(fpath) || 0
38
+
39
+ # do something here to run before channel transition
40
+ ary = @athsurvey.current_data()
41
+ @utilization_channel = ary[0]
42
+ @utilization = ary[3]
43
+
44
+ prev_channel = @current_channel
45
+ @current_chanenl = move_channel(@current_channel)
46
+ @channel_walk += 1
47
+ $log.debug("channel moved to #{@current_channel} from #{prev_channel} (dur=#{@duration}, size=#{@file_size}, walk=#{@channel_walk}, utilization=#{@utilization} uch=#{@utilization_channel})")
48
+ end
49
+ rescue => e
50
+ $log.warn("run_capture detected unknown error (#{e})")
51
+ end
52
+
53
+ def stop_capture
54
+ if @th_tshark == nil
55
+ $log.err("tried to kill tshark, but it's not executed? (or already dead?)")
56
+ return
57
+ end
58
+
59
+ Process.kill("INT", @th_tshark.pid)
60
+ end
61
+
62
+ private
63
+ def init_status
64
+ @duration = 0
65
+ @current_channel = 1
66
+ @file_size = 0
67
+ @channel_walk = 0
68
+ end
69
+
70
+ def init_device
71
+ unless system("ip link set #{@ifname} down")
72
+ raise "failed to turn down #{@ifname}"
73
+ end
74
+ unless system("iw #{@ifname} set monitor fcsfail otherbss control")
75
+ raise "failed to set #{@ifname} to monitor mode"
76
+ end
77
+ unless system("ip link set #{@ifname} up")
78
+ raise "failed to turn up #{@ifname}"
79
+ end
80
+ unless system("iw wlan0 set channel #{@current_channel}")
81
+ raise "failed to set channel #{@current_channel} on #{@ifname}"
82
+ end
83
+ end
84
+
85
+ def move_channel current
86
+ next_channel = pick_channel(current)
87
+
88
+ while !system("iw #{@ifname} set channel #{next_channel}")
89
+ # what if we got unplugged ifname? => should we die?
90
+ $log.debug("channel transition failed, added to black list (channel=#{next_channel})")
91
+ @black_list << next_channel
92
+ sleep 1
93
+ next_channel = pick_channel(next_channel)
94
+ end
95
+
96
+ @current_channel = next_channel
97
+ end
98
+
99
+ def pick_channel current
100
+ idx = CHAN.index(current)
101
+ idx += 1
102
+ next_channel = CHAN[idx % LIMIT_IDX]
103
+
104
+ # we have black list
105
+ while @black_list.include?(next_channel)
106
+ idx += 1
107
+ next_channel = CHAN[idx % LIMIT_IDX]
108
+ end
109
+
110
+ return next_channel
111
+ end
112
+ end
113
+ end
data/lib/tochka.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "tochka/version"
2
+ require "tochka/agent"
3
+ require "tochka/channel"
4
+ require "tochka/log"
5
+ require "tochka/pitft_button"
6
+ require "tochka/wlan"
7
+ require "tochka/command/tochka-miniui"
8
+ require "tochka/command/tochkad"
9
+
10
+ module Tochka
11
+ end
data/tochka.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tochka/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tochka"
8
+ spec.version = Tochka::VERSION
9
+ spec.authors = ["enukane"]
10
+ spec.email = ["enukane@glenda9.org"]
11
+
12
+ spec.summary = %q{Wi-Fi packet capture software set on Raspberry Pi.}
13
+ spec.homepage = "https://github.com/enukane/tochka"
14
+
15
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
16
+ # delete this section to allow pushing this gem to any host.
17
+ #if spec.respond_to?(:metadata)
18
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
19
+ #else
20
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
21
+ #end
22
+
23
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.10"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec"
31
+
32
+ spec.add_dependency 'wiringpi', "~> 2.0.0"
33
+ spec.add_dependency 'rubysdl', "~> 2.2.0" # requires sge, ttf enabled
34
+ spec.add_dependency 'pidfile', "~> 0.3.0"
35
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tochka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - enukane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: wiringpi
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubysdl
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.2.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.2.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pidfile
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.3.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.3.0
97
+ description:
98
+ email:
99
+ - enukane@glenda9.org
100
+ executables:
101
+ - tochka-miniui
102
+ - tochkad
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - exe/tochka-miniui
115
+ - exe/tochkad
116
+ - ext/tochka-miniui
117
+ - ext/tochkad
118
+ - lib/tochka.rb
119
+ - lib/tochka/agent.rb
120
+ - lib/tochka/athsurvey.rb
121
+ - lib/tochka/channel.rb
122
+ - lib/tochka/command/tochka-miniui.rb
123
+ - lib/tochka/command/tochkad.rb
124
+ - lib/tochka/log.rb
125
+ - lib/tochka/pitft_button.rb
126
+ - lib/tochka/version.rb
127
+ - lib/tochka/wlan.rb
128
+ - tochka.gemspec
129
+ homepage: https://github.com/enukane/tochka
130
+ licenses: []
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.4.5
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Wi-Fi packet capture software set on Raspberry Pi.
152
+ test_files: []