Class: ProconBypassMan::DeviceConnection::Executer
- Inherits:
-
Object
- Object
- ProconBypassMan::DeviceConnection::Executer
- Defined in:
- lib/procon_bypass_man/device_connection/executor.rb
Defined Under Namespace
Classes: Value
Constant Summary collapse
- GADGET_PATH =
'/dev/hidg0'
Class Method Summary collapse
Instance Method Summary collapse
- #add(expected_to_receive:, read_from:, call_block_if_receive: nil, &block) ⇒ Object
- #blocking_read_with_timeout_from_procon ⇒ Object
- #drain_all ⇒ Object
- #from_device(item) ⇒ Object
- #init_devices ⇒ Object
-
#initialize(throw_error_if_timeout: false, throw_error_if_mismatch: false) ⇒ Executer
constructor
A new instance of Executer.
- #procon ⇒ Object
- #switch ⇒ Object
-
#to_device(item) ⇒ Object
fromの対になる.
- #write_to_procon(data) ⇒ Object
Constructor Details
#initialize(throw_error_if_timeout: false, throw_error_if_mismatch: false) ⇒ Executer
Returns a new instance of Executer.
54 55 56 57 58 59 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 54 def initialize(throw_error_if_timeout: false, throw_error_if_mismatch: false) @queue = [] @initialized_devices = false @throw_error_if_timeout = throw_error_if_timeout @throw_error_if_mismatch = throw_error_if_mismatch end |
Class Method Details
.execute! ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 19 def self.execute! s = new_with_default_args s.add(expected_to_receive: [ ["0000"], ["0000"], ["8005"], ["0000"], ], read_from: :switch) # 1. Sends current connection status, and if the Joy-Con are connected, s.add(expected_to_receive: [["8001"]], read_from: :switch) s.add(expected_to_receive: [/^8101/], read_from: :procon) # <<< 81010003176d96e7a5480000000, macaddressとコントローラー番号を返す # 2. Sends handshaking packets over UART to the Joy-Con or Pro Controller Broadcom chip. This command can only be called once per session. s.add(expected_to_receive: [["8002"]], read_from: :switch) s.add(expected_to_receive: [/^8102/], read_from: :procon) # 3 s.add(expected_to_receive: [/^0100/], read_from: :switch) s.add(expected_to_receive: [/^21/], read_from: :procon, call_block_if_receive: /^8101/) do |this| begin ProconBypassMan.logger.info "(start special route)" this.blocking_read_with_timeout_from_procon # <<< 810100032dbd42e9b698000 this.write_to_procon("8002") this.blocking_read_with_timeout_from_procon # <<< 8102 this.write_to_procon("01000000000000000000033000000000000000000000000000000000000000000000000000000000000000000000000000") this.blocking_read_with_timeout_from_procon # <<< 21 rescue ProconBypassMan::SafeTimeout::Timeout, Timeout::Error raise ProconBypassMan::DeviceConnection::TimeoutErrorInConditionalRoute end end # 4. Forces the Joy-Con or Pro Controller to only talk over USB HID without any timeouts. This is required for the Pro Controller to not time out and revert to Bluetooth. s.add(expected_to_receive: [["8004"]], read_from: :switch) s.drain_all return [s.switch, s.procon] end |
.new_with_default_args ⇒ Object
15 16 17 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 15 def self.new_with_default_args new(throw_error_if_timeout: true) end |
Instance Method Details
#add(expected_to_receive:, read_from:, call_block_if_receive: nil, &block) ⇒ Object
61 62 63 64 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 61 def add(expected_to_receive: , read_from: , call_block_if_receive: nil, &block) values = expected_to_receive @queue << Value.new(values: values, read_from: read_from, call_block_if_receive: call_block_if_receive, &block) end |
#blocking_read_with_timeout_from_procon ⇒ Object
187 188 189 190 191 192 193 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 187 def blocking_read_with_timeout_from_procon Timeout.timeout(4) do raw_data = procon.read(64) ProconBypassMan.logger.info "<<< #{raw_data.unpack("H*").first}" return raw_data end end |
#drain_all ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 66 def drain_all debug_log_buffer = [] unless @initialized_devices init_devices end while(item = @queue.shift) item.values.each do |value| raw_data = nil timer = ProconBypassMan::SafeTimeout.new begin timer.throw_if_timeout! raw_data = from_device(item).read_nonblock(64) debug_log_buffer << "read_from(#{item.read_from}): #{raw_data.unpack("H*")}" rescue IO::EAGAINWaitReadable # debug_log_buffer << "read_from(#{item.read_from}): IO::EAGAINWaitReadable" retry end if item.call_block_if_receive ProconBypassMan.logger.info "call block if receive: #{item.call_block_if_receive}, actual: #{raw_data.unpack("H*")} from: #{item.read_from}" if item.call_block_if_receive =~ raw_data.unpack("H*").first raw_data = item.block.call(self) end end result = case value when String, Array value == raw_data.unpack("H*") when Regexp value =~ raw_data.unpack("H*").first else raise "#{value}は知りません" end if result ProconBypassMan.logger.info "OK(expected: #{value}, got: #{raw_data.unpack("H*")}) from: #{item.read_from}" debug_log_buffer << "OK(expected: #{value}, got: #{raw_data.unpack("H*")}) from: #{item.read_from}" else ProconBypassMan.logger.info "NG(expected: #{value}, got: #{raw_data.unpack("H*")}) from: #{item.read_from}" debug_log_buffer << "NG(expected: #{value}, got: #{raw_data.unpack("H*")}) from: #{item.read_from}" raise ProconBypassMan::DeviceConnection::BytesMismatchError.new(debug_log_buffer) if @throw_error_if_mismatch end to_device(item).write_nonblock(raw_data) end end rescue ProconBypassMan::SafeTimeout::Timeout, Timeout::Error => e ProconBypassMan.logger.error "timeoutになりました(#{e.})" procon.write_nonblock(['8006'].pack("H*")) # 再実行時のケーブルの再接続を不要にするワークアラウンド. リセットしているらしい compressed_buffer_text = ProconBypassMan::CompressArray.new(debug_log_buffer).compress.join("\n") ProconBypassMan::SendErrorCommand.execute(error: compressed_buffer_text, stdout: false) raise ProconBypassMan::SafeTimeout::Timeout if @throw_error_if_timeout end |
#from_device(item) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 122 def from_device(item) case item.read_from when :switch switch when :procon procon else raise end end |
#init_devices ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 154 def init_devices unless SudoNeedPasswordChecker.execute! raise ProconBypassMan::DeviceConnection::SetupIncompleteError end if @initialized_devices return end ProconBypassMan::UsbDeviceController.init ProconBypassMan::UsbDeviceController.reset if path = ProconBypassMan::DeviceProconFinder.find ShellRunner.execute("sudo chmod 777 #{path}") @procon = File.open(path, "w+b") ProconBypassMan.logger.info "proconのデバイスファイルは#{path}を使います" else raise(ProconBypassMan::DeviceConnection::NotFoundProconError) end begin ShellRunner.execute("sudo chmod 777 #{GADGET_PATH}") @gadget = File.open(GADGET_PATH, "w+b") rescue Errno::ENXIO => e # /dev/hidg0 をopenできないときがある ProconBypassMan::SendErrorCommand.execute(error: "Errno::ENXIOが起きたのでresetします.\n #{e.}", stdout: false) ProconBypassMan::UsbDeviceController.reset retry end @initialized_devices = true end |
#procon ⇒ Object
149 150 151 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 149 def procon @procon end |
#switch ⇒ Object
145 146 147 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 145 def switch @gadget end |
#to_device(item) ⇒ Object
fromの対になる
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 134 def to_device(item) case item.read_from when :switch procon when :procon switch else raise end end |
#write_to_procon(data) ⇒ Object
195 196 197 198 |
# File 'lib/procon_bypass_man/device_connection/executor.rb', line 195 def write_to_procon(data) ProconBypassMan.logger.info ">>> #{data}" procon.write_nonblock([data].pack("H*")) end |