Class: Usbmux::BinaryProtocol

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

Direct Known Subclasses

PlistProtocol

Constant Summary collapse

TYPE_RESULT =
1
TYPE_CONNECT =
2
TYPE_LISTEN =
3
TYPE_DEVICE_ADD =
4
TYPE_DEVICE_REMOVE =
5
VERSION =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ BinaryProtocol

Returns a new instance of BinaryProtocol.



70
71
72
73
# File 'lib/usbmux/usbmux.rb', line 70

def initialize(socket)
  @socket = socket
  @connected = false
end

Instance Attribute Details

#connectedObject

Returns the value of attribute connected.



68
69
70
# File 'lib/usbmux/usbmux.rb', line 68

def connected
  @connected
end

#socketObject

Returns the value of attribute socket.



68
69
70
# File 'lib/usbmux/usbmux.rb', line 68

def socket
  @socket
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/usbmux/usbmux.rb', line 101

def connected?
  @connected
end

#get_packetObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/usbmux/usbmux.rb', line 85

def get_packet
  if connected?
    raise MuxError.new('Mux is connected, cannot issue control packets')
  end
  dlen = @socket.receive(4)
  dlen = dlen.unpack('I').first
  body = @socket.receive(dlen - 4)
  version, response, tag = body[0..0xc].unpack('III')
  if version != self.class::VERSION
    raise MuxVersionError.new("Version mismatch: expected #{self.class::VERSION}, got #{version}")
  end
  payload = _unpack(response, body[0xc..-1])

  [response, tag, payload]
end

#send_packet(request, tag, payload = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/usbmux/usbmux.rb', line 75

def send_packet(request, tag, payload = {})
  payload = _pack(request, payload)
  if connected?
    raise MuxError.new('Mux is connected, cannot issue control packets')
  end
  length = 16 + payload.length
  data = [length, self.class::VERSION, request, tag].pack('IIII') + payload
  @socket.send(data)
end