Class: Usbmux::MuxConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/usbmux/usbmux.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket_path, protocol_class) ⇒ MuxConnection

Returns a new instance of MuxConnection.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/usbmux/usbmux.rb', line 204

def initialize(socket_path, protocol_class)
  @socket_path = socket_path
  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
    family = Socket::PF_INET
    address = Addrinfo.tcp('127.0.0.1', 27015)
  else
    family = Socket::PF_UNIX
    address = Addrinfo.unix('/var/run/usbmuxd')
  end
  @socket = SafeStreamSocket.new(family, address)
  @protocol = protocol_class.new(@socket)
  @pkttag = 1
  @devices = []
end

Instance Attribute Details

#devicesObject

Returns the value of attribute devices.



203
204
205
# File 'lib/usbmux/usbmux.rb', line 203

def devices
  @devices
end

#pkttagObject

Returns the value of attribute pkttag.



203
204
205
# File 'lib/usbmux/usbmux.rb', line 203

def pkttag
  @pkttag
end

#protocolObject

Returns the value of attribute protocol.



203
204
205
# File 'lib/usbmux/usbmux.rb', line 203

def protocol
  @protocol
end

#socketObject

Returns the value of attribute socket.



203
204
205
# File 'lib/usbmux/usbmux.rb', line 203

def socket
  @socket
end

#socket_pathObject

Returns the value of attribute socket_path.



203
204
205
# File 'lib/usbmux/usbmux.rb', line 203

def socket_path
  @socket_path
end

Instance Method Details

#closeObject



256
257
258
# File 'lib/usbmux/usbmux.rb', line 256

def close
  @socket.close
end

#connect(device, port) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/usbmux/usbmux.rb', line 243

def connect(device, port)
  payload = {
    'DeviceID' => device.id,
    'PortNumber' => (( port << 8) & 0xFF00) | (port >> 8)
  }
  ret = _exchange(@protocol.class::TYPE_CONNECT, payload)
  if ret != 0
    raise MuxError.new("Connect failed: error #{ret}")
  end
  @protocol.connected = true
  @socket.sock
end

#listenObject



219
220
221
222
223
224
# File 'lib/usbmux/usbmux.rb', line 219

def listen
  ret = _exchange(@protocol.class::TYPE_LISTEN)
  if ret != 0
    raise MuxError.new("Listen failed: error #{ret}")
  end
end

#process(timeout = nil) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/usbmux/usbmux.rb', line 226

def process(timeout = nil)
  if @protocol.connected?
    raise MuxError.new('Socket is connected, cannot process listener events')
  end
  rlo, wlo, xlo = IO.select([@socket.sock], [], [@socket.sock], timeout)

  # IO.select returns nil when timeout is reached. Check for nil before
  unless rlo.nil?
    if xlo.length > 0
      @socket.close
      raise MuxError.new("Exception in listener socket")
    elsif rlo.length > 0
      _process_packet
    end
  end
end