summaryrefslogtreecommitdiffstats
path: root/swig/imobiledevice.i
blob: 2de69e77d3235966ea5faa1eb5b0763a77ba943d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
53
54
55
56
57
58
59
60
61
62
63
64
65
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
 /* swig.i */
 %module imobiledevice
 %feature("autodoc", "1");
 %{
 /* Includes the header in the wrapper code */
 #include <libimobiledevice/libimobiledevice.h>
 #include <libimobiledevice/lockdown.h>
 #include <libimobiledevice/mobilesync.h>
 #include <libimobiledevice/notification_proxy.h>
 #include <plist/plist.h>
 #include <plist/plist++.h>
 #include "../src/debug.h"

typedef struct {
    idevice_t dev;
} idevice;

typedef struct {
    idevice* dev;
    lockdownd_client_t client;
} Lockdownd;

typedef struct {
    idevice* dev;
    mobilesync_client_t client;
} MobileSync;

typedef struct {
    idevice* dev;
    np_client_t client;
} NotificationProxy;

//now declare funtions to handle creation and deletion of objects
static void my_delete_idevice(idevice* dev) {
	if (dev) {
		idevice_free(dev->dev);
		free(dev);
	}
}

static Lockdownd* my_new_Lockdownd(idevice* device) {
    if (!device) return NULL;
    Lockdownd* client = (Lockdownd*) malloc(sizeof(Lockdownd));
    client->dev = device;
    client->client = NULL;
    if (LOCKDOWN_E_SUCCESS == lockdownd_client_new_with_handshake(device->dev , &(client->client), NULL)) {
        return client;
    }
    else {
        free(client);
        return NULL;
    }
}

static void my_delete_Lockdownd(Lockdownd* lckd) {
    if (lckd) {
        lockdownd_client_free(lckd->client);
        free(lckd);
    }
}

static MobileSync* my_new_MobileSync(Lockdownd* lckd) {
	if (!lckd || !lckd->dev) return NULL;
	MobileSync* client = NULL;
	uint16_t port = 0;
	if (LOCKDOWN_E_SUCCESS == lockdownd_start_service(lckd->client, "com.apple.mobilesync", &port)) {
		client = (MobileSync*) malloc(sizeof(MobileSync));
		client->dev = lckd->dev;
		client->client = NULL;
		mobilesync_client_new(lckd->dev->dev, port, &(client->client));
	}
	return client;
}

static NotificationProxy* my_new_NotificationProxy(Lockdownd* lckd) {
    if (!lckd || !lckd->dev) return NULL;
    NotificationProxy* client = NULL;
    uint16_t port = 0;
	if (LOCKDOWN_E_SUCCESS == lockdownd_start_service(lckd->client, "com.apple.mobile.notification_proxy", &port)) {
        client = (NotificationProxy*) malloc(sizeof(NotificationProxy));
        client->dev = lckd->dev;
        client->client = NULL;
		np_client_new(lckd->dev->dev, port, &(client->client));
    }
    return client;
}

static PList::Node* new_node_from_plist(plist_t node)
{
	PList::Node* ret = NULL;
	plist_type subtype = plist_get_node_type(node);
	switch(subtype)
	{
	    case PLIST_DICT:
		ret = new PList::Dictionary(node);
		break;
	    case PLIST_ARRAY:
		ret = new PList::Array(node);
		break;
	    case PLIST_BOOLEAN:
		ret = new PList::Boolean(node);
		break;
	    case PLIST_UINT:
		ret = new PList::Integer(node);
		break;
	    case PLIST_REAL:
		ret = new PList::Real(node);
		break;
	    case PLIST_STRING:
		ret = new PList::String(node);
		break;
	    case PLIST_DATE:
		ret = new PList::Date(node);
		break;
	    case PLIST_DATA:
		ret = new PList::Data(node);
		break;
	    default:
		break;
	}
	return ret;
}

#ifdef SWIGPYTHON
static void NotificationProxyPythonCallback(const char *notification, void* user_data) {
    PyObject *func, *arglist;

    func = (PyObject *) user_data;
    arglist = Py_BuildValue("(s)",notification);

    PyEval_CallObject(func, arglist);

    Py_DECREF(arglist);
}
#endif
 %}

/* Parse the header file to generate wrappers */
%include "stdint.i"
%include "cstring.i"
%include "plist/swig/plist.i"

/* This needs to be here since if it's after
 * the structs, SWIG won't pick it up for %extend
 */
#ifdef SWIGPYTHON
%typemap(in) (PyObject *pyfunc) {
    if (!PyCallable_Check($input)) {
        PyErr_SetString(PyExc_TypeError, "Need a callable object!");
        return NULL;
    }
    $1 = $input;
}
%typemap(in) (const char **string_list) {
    /* Check if it's a list */
    if (PyList_Check($input)) {
        int size = PyList_Size($input);
        int i = 0;
        $1 = (char **) malloc((size+1)*sizeof(char *));
        for (i = 0; i < size; i++) {
            PyObject *o = PyList_GetItem($input,i);
            if (PyString_Check(o)) {
                $1[i] = PyString_AsString(PyList_GetItem($input,i));
            } else {
                PyErr_SetString(PyExc_TypeError,"List must contain strings");
                free($1);
                return NULL;
            }
        }
        $1[i] = 0;
    } else if (PyTuple_Check($input)) {
        int size = PyTuple_Size($input);
        int i = 0;
        $1 = (char **) malloc((size+1)*sizeof(char *));
        for (i = 0; i < size; i++) {
            PyObject *o = PyTuple_GetItem($input,i);
            if (PyString_Check(o)) {
                $1[i] = PyString_AsString(PyTuple_GetItem($input,i));
            } else {
                PyErr_SetString(PyExc_TypeError,"List must contain strings");
                free($1);
                return NULL;
            }
        }
        $1[i] = 0;
    } else {
        PyErr_SetString(PyExc_TypeError, "not a list or tuple");
        return NULL;
    }
}
%typemap(freearg) (const char **string_list) {
    free((char *) $1);
}
#endif

 typedef struct {
	idevice_t dev;
 } idevice;

typedef struct {
    idevice* dev;
    lockdownd_client_t client;
} Lockdownd;

typedef struct {
    idevice* dev;
    mobilesync_client_t client;
} MobileSync;

typedef struct {
    idevice* dev;
    np_client_t client;
} NotificationProxy;


%extend idevice {             // Attach these functions to struct idevice
	idevice() {
		idevice* device = (idevice*) malloc(sizeof(idevice));
		device->dev = NULL;
		return device;
	}

	~idevice() {
		my_delete_idevice($self);
	}

	void set_debug_level(int level) {
		idevice_set_debug_level(level);
	}

	int init_device_by_uuid(char* uuid) {
		if (IDEVICE_E_SUCCESS == idevice_new(&($self->dev), uuid))
			return 1;
		return 0;
	}

	int init_device() {
		if (IDEVICE_E_SUCCESS == idevice_new(&($self->dev), NULL))
			return 1;
		return 0;
	}

	%newobject get_uuid;
	char* get_uuid(){
		char* uuid = NULL;
		idevice_get_uuid($self->dev, &uuid);
		return uuid;
	}

	Lockdownd* get_lockdown_client() {
		return my_new_Lockdownd($self);
	}
};

%extend Lockdownd {             // Attach these functions to struct Lockdownd
	Lockdownd(idevice* device) {
		return my_new_Lockdownd(device);
	}

	~Lockdownd() {
		my_delete_Lockdownd($self);
	}

	void send(PList::Node* node) {
		lockdownd_send($self->client, node->GetPlist());
	}

	PList::Node* receive() {
		plist_t node = NULL;
		lockdownd_receive($self->client, &node);
		return new_node_from_plist(node);
	}

	MobileSync* get_mobilesync_client() {
		return my_new_MobileSync($self);
	}

    NotificationProxy* get_notification_proxy_client() {
        return my_new_NotificationProxy($self);
    }
};

%extend MobileSync {             // Attach these functions to struct MobileSync
	MobileSync(Lockdownd* lckd) {
		return my_new_MobileSync(lckd);
	}

	~MobileSync() {
		mobilesync_client_free($self->client);
		free($self);
	}

	void send(PList::Node* node) {
		mobilesync_send($self->client, node->GetPlist());
	}

	PList::Node* receive() {
		plist_t node = NULL;
		mobilesync_receive($self->client, &node);
		return new_node_from_plist(node);
	}
};

#define NP_SYNC_WILL_START           "com.apple.itunes-mobdev.syncWillStart"
#define NP_SYNC_DID_START            "com.apple.itunes-mobdev.syncDidStart"
#define NP_SYNC_DID_FINISH           "com.apple.itunes-mobdev.syncDidFinish"
#define NP_SYNC_LOCK_REQUEST         "com.apple.itunes-mobdev.syncLockRequest"

%extend NotificationProxy {
    NotificationProxy(Lockdownd* lckd) {
        return my_new_NotificationProxy(lckd);
    }

    ~NotificationProxy() {
        np_client_free($self->client);
        free($self);
    }

    int16_t post_notification(const char* notification) {
        return np_post_notification($self->client, notification);
    }

    int16_t observe_notification(const char* notification) {
        return np_observe_notification($self->client, notification);
    }

    int16_t observe_notifications(const char** string_list) {
        return np_observe_notifications($self->client, string_list);
    }
};

#ifdef SWIGPYTHON
%extend NotificationProxy {
    int16_t set_callback(PyObject *pyfunc) {
        int16_t res;
        res = np_set_notify_callback($self->client, NotificationProxyPythonCallback, (void *) pyfunc);
        Py_INCREF(pyfunc);
        return res;
    }
};
#endif