Class: Usbmux::PlistProtocol

Inherits:
BinaryProtocol show all
Defined in:
lib/usbmux/usbmux.rb

Constant Summary collapse

TYPE_RESULT =
'Result'
TYPE_CONNECT =
'Connect'
TYPE_LISTEN =
'Listen'
TYPE_DEVICE_ADD =
'Attached'
TYPE_DEVICE_REMOVE =
'Detached'
TYPE_PLIST =
8
VERSION =
1

Instance Attribute Summary

Attributes inherited from BinaryProtocol

#connected, #socket

Instance Method Summary collapse

Methods inherited from BinaryProtocol

#connected?, #initialize

Constructor Details

This class inherits a constructor from Usbmux::BinaryProtocol

Instance Method Details

#_pack(request, payload) ⇒ Object



173
174
175
# File 'lib/usbmux/usbmux.rb', line 173

def _pack(request, payload)
  payload + "\n"
end

#_unpack(response, payload) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/usbmux/usbmux.rb', line 177

def _unpack(response, payload)
  payload = CFPropertyList::List.new(:data => payload).value.value
  response = payload['MessageType'].value
  if response == TYPE_RESULT
    { 'MessageType' => response, 'Number' => payload['Number'].value }
  elsif response == TYPE_DEVICE_ADD
    properties = payload['Properties'].value
    {
      'MessageType' => response,
      'DeviceID' => payload['DeviceID'].value,
      'Properties' =>
      {
        'LocationID' => properties['LocationID'].value,
        'SerialNumber' => properties['SerialNumber'].value,
        'ProductID' => properties['ProductID'].value
      }
    }
  elsif response == TYPE_DEVICE_REMOVE
    { 'MessageType' => response, 'DeviceID' => payload['DeviceID'].value }
  else
    raise MuxError.new("Invalid incoming response type #{response}")
  end
end

#get_packetObject



164
165
166
167
168
169
170
171
# File 'lib/usbmux/usbmux.rb', line 164

def get_packet
  response, tag, payload = super()
  if response != TYPE_PLIST
    raise MuxError.new("Received non-plist type #{response}")
  end

  [payload['MessageType'], tag, payload]
end

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



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/usbmux/usbmux.rb', line 150

def send_packet(request, tag, payload = {})
  payload['ClientVersionString'] = 'usbmux.py by marcan'
  if request.is_a? Integer
    request = [TYPE_CONNECT, TYPE_LISTEN][request - 2]
  end
  payload['MessageType'] = request
  payload['ProgName'] = 'tcprelay'
  plist = CFPropertyList::List.new
  plist.value = CFPropertyList.guess(payload)
  xml = plist.to_str(CFPropertyList::List::FORMAT_XML, :formatted => true)

  super(TYPE_PLIST, tag, xml)
end