Class: Idevice::MobileBackup2Client

Inherits:
C::ManagedOpaquePointer show all
Includes:
LibHelpers
Defined in:
lib/idevice/mobilebackup2.rb

Overview

Used to backup and restore of all device data (mobilebackup2, iOS4+ only)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LibHelpers

included

Methods inherited from C::ManagedOpaquePointer

#initialize

Constructor Details

This class inherits a constructor from Idevice::C::ManagedOpaquePointer

Class Method Details

.attach(opts = {}) ⇒ Object


42
43
44
45
46
47
48
49
50
51
# File 'lib/idevice/mobilebackup2.rb', line 42

def self.attach(opts={})
  _attach_helper("com.apple.mobilebackup2", opts) do |idevice, ldsvc, p_mb2|
    err = C.mobilebackup2_client_new(idevice, ldsvc, p_mb2)
    raise MobileBackup2Error, "Mobile backup error: #{err}" if err != :SUCCESS

    mb2 = p_mb2.read_pointer
    raise MobileBackup2Error, "mobilebackup2_client_new returned a NULL client" if mb2.null?
    return new(mb2)
  end
end

.release(ptr) ⇒ Object


34
35
36
37
38
39
40
# File 'lib/idevice/mobilebackup2.rb', line 34

def self.release(ptr)
  C::Freelock.synchronize do
    unless ptr.null?
      C.mobilebackup2_client_free(ptr)
    end
  end
end

Instance Method Details

#receive_messageObject


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/idevice/mobilebackup2.rb', line 64

def receive_message
  FFI::MemoryPointer.new(:pointer) do |p_msg|
    FFI::MemoryPointer.new(:pointer) do |p_dlmessage|
      err = C.mobilebackup2_receive_message(self, p_msg, p_dlmessage)
      raise MobileBackup2Error, "Mobile backup error: #{err}" if err != :SUCCESS

      dlmessage = p_dlmessage.read_pointer
      msg = p_msg.read_pointer.read_plist_t
      begin
        raise MobileBackup2Error, "mobilebackup2_receive_message returned a null message plist" if msg.nil?
        unless dlmessage.null?
          msg[:dlmessage] = dlmessage.read_string
        end
        return msg
      ensure
        C.free(dlmessage) unless dlmessage.nil? or dlmessage.null?
      end
    end
  end
end

#receive_raw(len) ⇒ Object


96
97
98
99
100
101
102
103
104
105
# File 'lib/idevice/mobilebackup2.rb', line 96

def receive_raw(len)
  FFI::MemoryPointer.new(len) do |p_data|
    FFI::MemoryPointer.new(:uint32) do |p_rcvdbytes|
      err = C.mobilebackup2_receive_raw(self, p_data, p_data.size, p_rcvdbytes)
      raise MobileBackup2Error, "Mobile backup error: #{err}" if err != :SUCCESS

      return p_data.read_bytes(p_rcvdbytes.read_uint32)
    end
  end
end

#send_message(message, opts = nil) ⇒ Object

Raises:


53
54
55
56
57
58
59
60
61
62
# File 'lib/idevice/mobilebackup2.rb', line 53

def send_message(message, opts=nil)
  if message.nil? and opts.nil?
    raise ArgumentError, "Both message and options hash may not be nil"
  end
  opts = Plist_t.from_ruby(opts) unless opts.nil?
  err = C.mobilebackup2_send_message(self, message, opts)
  raise MobileBackup2Error, "Mobile backup error: #{err}" if err != :SUCCESS

  return true
end

#send_raw(data) ⇒ Object


85
86
87
88
89
90
91
92
93
94
# File 'lib/idevice/mobilebackup2.rb', line 85

def send_raw(data)
  FFI::MemoryPointer.from_bytes(data) do |p_data|
    FFI::MemoryPointer.new(:uint32) do |p_sentbytes|
      err = C.mobilebackup2_send_raw(self, p_data, p_data.size, p_sentbytes)
      raise MobileBackup2Error, "Mobile backup error: #{err}" if err != :SUCCESS

      return p_sentbytes.read_uint32
    end
  end
end

#send_request(request, target_identifier, source_identifier, opts = {}) ⇒ Object

Raises:


120
121
122
123
124
125
# File 'lib/idevice/mobilebackup2.rb', line 120

def send_request(request, target_identifier, source_identifier, opts={})
  err = C.mobilebackup2_send_request(self, request, target_id, source_id, Plist_t.from_ruby(opts))
  raise MobileBackup2Error, "Mobile backup error: #{err}" if err != :SUCCESS

  return true
end

#send_status_response(status_code, status_message = nil, opts = nil) ⇒ Object

Raises:


127
128
129
130
131
132
133
# File 'lib/idevice/mobilebackup2.rb', line 127

def send_status_response(status_code, status_message=nil, opts=nil)
  opts = Plist_t.from_ruby(opts) if opts
  err = C.mobilebackup2_send_status_response(self, status_code, status_message, opts)
  raise MobileBackup2Error, "Mobile backup error: #{err}" if err != :SUCCESS

  return true
end

#version_exchange(local_versions) ⇒ Object


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/idevice/mobilebackup2.rb', line 107

def version_exchange(local_versions)
  local_versions = local_versions.map{|x| x.to_f } # should throw an error if one is not a float/float-able
  FFI::MemoryPointer.new(FFI::TypeDefs[:double].size * local_versions.count) do |p_local_versions|
    p_local_versions.write_array_of_double(local_versions)
    FFI::MemoryPointer.new(:pointer) do |p_remote_version|
      err = C.mobilebackup2_version_exchange(self, p_local_versions, local_versions.count, p_remote_version)
      raise MobileBackup2Error, "Mobile backup error: #{err}" if err != :SUCCESS

      return p_remote_version.read_double
    end
  end
end