forked from gdawg/ios-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomms.c
113 lines (87 loc) · 2.66 KB
/
comms.c
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
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <plist/plist.h>
#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/lockdown.h>
#include <libimobiledevice/sbservices.h>
#include "ios-icons.h"
#include "comms.h"
#include "icons.h"
int idevice_errno = 0;
LUALIB_API int ios_connect(lua_State *L)
{
int rc;
const char* udid = NULL;
if (lua_gettop(L) > 0)
{
udid = lua_isnoneornil(L, -1) ? NULL : lua_tostring(L, 1);
lua_pop(L, 1);
}
SBConnection* c = (SBConnection*)lua_newuserdata(L, sizeof(SBConnection));
memset(c, 0, sizeof(SBConnection));
luaL_getmetatable(L, kSpringboardConnID);
lua_setmetatable(L, -2);
if (( IDEVICE_E_SUCCESS == (rc = idevice_new( &c->device, udid)))
&& LOCKDOWN_E_SUCCESS == (rc = lockdownd_client_new_with_handshake(
c->device,&c->lockdownClient, kClientId))
&& LOCKDOWN_E_SUCCESS == (rc = lockdownd_start_service(
c->lockdownClient, kSpringboardServices, &c->lockdownService))
&& SBSERVICES_E_SUCCESS == (rc = sbservices_client_new(
c->device, c->lockdownService, &c->sbClient)))
{
return 1;
}
else
{
ios_disconnect(L);
idevice_errno = rc;
lua_pushfstring(L, "%s code=%d", kConnectFail, rc);
lua_error(L);
return 0; // never reached
}
}
LUALIB_API int ios_disconnect(lua_State *L)
{
SBConnection* c = (SBConnection*)luaL_checkudata(L, 1, kSpringboardConnID);
if (c->sbClient != NULL) { sbservices_client_free(c->sbClient); }
if (c->lockdownService != NULL) { lockdownd_service_descriptor_free(c->lockdownService); }
if (c->lockdownClient != NULL) { lockdownd_client_free(c->lockdownClient); }
if (c->device != NULL) { idevice_free(c->device); }
memset(c, 0, sizeof(SBConnection));
lua_pop(L, 1); // connection
return 1;
}
void putDeviceNameOnStack(lua_State *L, int isToString) {
char* deviceName;
SBConnection* c = (SBConnection*)luaL_checkudata(L, 1, kSpringboardConnID);
if (c->lockdownClient == NULL) {
lua_pushstring(L, "disconnected");
} else {
if ( lockdownd_get_device_name(c->lockdownClient,
&deviceName) == LOCKDOWN_E_SUCCESS) {
if (isToString) {
lua_pushfstring(L, "ios[%s]", deviceName);
} else {
lua_pushstring(L, deviceName);
}
} else {
lua_pushstring(L, "unknown");
}
}
}
int conn_tostring(lua_State *L) {
putDeviceNameOnStack(L, 1);
return 1;
}
int ios_devicename(lua_State *L)
{
putDeviceNameOnStack(L, 0);
return 1;
}
int ios_errno(lua_State *L)
{
lua_pushinteger(L, idevice_errno);
return 1;
}