Skip to content

Commit e221d70

Browse files
Merge pull request #56 from telerik/tachev/wifi-devices
feat: support Wi-Fi devices
2 parents d818a3f + 136f1de commit e221d70

9 files changed

+80
-24
lines changed

IOSDeviceLib/Constants.h

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static const unsigned kCFStringEncodingUTF8 = 0x08000100;
2424
static const unsigned kAMDNotFoundError = 0xe8000008;
2525
static const unsigned kAMDAPIInternalError = 0xe8000067;
2626
static const int kUSBInterfaceType = 1;
27+
static const int kWIFIInterfaceType = 2;
2728
static const unsigned kAppleServiceNotStartedErrorCode = 0xE8000063;
2829
static const unsigned kMountImageAlreadyMounted = 0xE8000076; // Note: This error code is actually named kAMDMobileImageMounterImageMountFailed but AppBuilder CLI and other sources think that getting this exit code during a mount is okay
2930
static const unsigned kIncompatibleSignature = 0xE8000033; // Note: This error code is actually named kAMDInvalidDiskImageError but CLI and other sources think that getting this exit code during a mount is okay
@@ -39,6 +40,7 @@ static const char *kUnreachableStatus = "Unreachable";
3940
static const char *kConnectedStatus = "Connected";
4041

4142
static const char *kDeviceFound = "deviceFound";
43+
static const char *kDeviceUpdated = "deviceUpdated";
4244
static const char *kDeviceLost = "deviceLost";
4345
static const char *kDeviceTrusted = "deviceTrusted";
4446
static const char *kDeviceUnknown = "deviceUnknown";
@@ -69,6 +71,8 @@ static const char *kResponseCommandType = "responseCommandType";
6971
static const char *kResponsePropertyName = "responsePropertyName";
7072
static const char *kShouldWaitForResponse = "shouldWaitForResponse";
7173
static const char *kCommandType = "commandType";
74+
static const char *kIsWiFiConnected = "isWiFiConnected";
75+
static const char *kIsUSBConnected = "isUSBConnected";
7276
static const char *kTimeout = "timeout";
7377
static const char *kPort = "port";
7478
static const char *kHost = "host";

IOSDeviceLib/Declarations.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ struct DeviceData {
113113
std::map<const char*, ServiceInfo> services;
114114
int sessions;
115115
std::map<std::string, ApplicationCache> apps_cache;
116+
int isWiFiConnected;
117+
int isUSBConnected;
116118

117119
void kill_device_server()
118120
{
@@ -168,7 +170,7 @@ typedef void(__cdecl *cfdictionary_get_keys_and_values)(CFDictionaryRef, const v
168170
typedef CFStringRef(__cdecl *cfstring_create_with_cstring)(void*, const char*, unsigned);
169171
typedef CFArrayRef(__cdecl *cfarray_create)(void*, const void**, long, void**);
170172
typedef unsigned(__cdecl *device_secure_operation_with_path)(int, const DeviceInfo*, CFURLRef, CFDictionaryRef, void(*f)(), int);
171-
typedef unsigned(__cdecl *device_secure_operation_with_bundle_id)(int, const DeviceInfo*, CFStringRef, int, void(*f)(), int);
173+
typedef unsigned(__cdecl *device_secure_operation_with_bundle_id)(ServiceConnRef connection, const DeviceInfo*, CFStringRef bundleIdentifier, CFDictionaryRef params, void (*installCallback)(CFDictionaryRef*, void *));
172174
typedef void(__cdecl *cfrelease)(CFStringRef);
173175

174176
typedef CFDictionaryRef(__cdecl *cfdictionary_create)(void *, void*, void*, int, void*, void*);
@@ -276,8 +278,8 @@ extern "C"
276278
unsigned AMDeviceCreateHouseArrestService(const DeviceInfo*, CFStringRef identifier, void * unknown, AFCConnectionRef * handle);
277279
int AMDeviceGetConnectionID(const DeviceInfo*);
278280
int AMDeviceGetInterfaceType(const DeviceInfo*);
281+
unsigned AMDeviceSecureUninstallApplication(ServiceConnRef connection, const DeviceInfo*, CFStringRef bundleIdentifier, CFDictionaryRef params, void (*installCallback)(CFDictionaryRef*, void *));
279282
unsigned AMDeviceUninstallApplication(HANDLE, CFStringRef, void*, void(*f)(), void*);
280-
unsigned AMDeviceSecureUninstallApplication(int, const DeviceInfo*, CFStringRef, int, void(*f)(), int);
281283
unsigned AMDeviceStartSession(const DeviceInfo*);
282284
unsigned AMDeviceStopSession(const DeviceInfo*);
283285
unsigned AMDeviceConnect(const DeviceInfo*);

IOSDeviceLib/IOSDeviceLib.cpp

+58-18
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ int start_session(std::string& device_identifier)
134134
{
135135
const DeviceInfo* device_info = devices[device_identifier].device_info;
136136
UNLOCK_MUTEX_AND_RETURN_IF_FAILED_RESULT(AMDeviceConnect(device_info), start_session_mutex);
137+
assert(AMDeviceIsPaired(device_info));
137138
UNLOCK_MUTEX_AND_RETURN_IF_FAILED_RESULT(AMDeviceValidatePairing(device_info), start_session_mutex);
138139
UNLOCK_MUTEX_AND_RETURN_IF_FAILED_RESULT(AMDeviceStartSession(device_info), start_session_mutex);
139140
}
@@ -242,14 +243,26 @@ void get_device_properties(std::string device_identifier, json &result)
242243
result["deviceName"] = get_device_property_value(device_identifier, "DeviceName");
243244
result["productVersion"] = get_device_property_value(device_identifier, kProductVersion);
244245
result["deviceColor"] = get_device_property_value(device_identifier, "DeviceColor");
246+
// available values:
247+
// "BluetoothAddress","BoardId","CPUArchitecture","ChipID","DeviceClass",
248+
// "DeviceColor","DeviceName","FirmwareVersion","HardwareModel",
249+
// "ModelNumber","ProductType","ProductVersion","UniqueDeviceID","WiFiAddress"
245250
}
246251

247252
inline bool has_complete_status(std::map<std::string, boost::any>& dict)
248253
{
249254
return boost::any_cast<std::string>(dict[kStatusKey]) == kComplete;
250255
}
251256

252-
void on_device_found(const DevicePointer* device_ptr, std::string device_identifier, std::string eventString, json &result)
257+
258+
void update_device_result(std::string device_identifier, json &result)
259+
{
260+
result[kIsUSBConnected] = devices[device_identifier].isUSBConnected;
261+
result[kIsWiFiConnected] = devices[device_identifier].isWiFiConnected;
262+
get_device_properties(device_identifier, result);
263+
}
264+
265+
void on_device_found(const DevicePointer* device_ptr, std::string device_identifier, json &result)
253266
{
254267
/*
255268
Interface type can be one of the followings:
@@ -259,10 +272,23 @@ void on_device_found(const DevicePointer* device_ptr, std::string device_identif
259272
2 - wifi interface type
260273
*/
261274
int interface_type = AMDeviceGetInterfaceType(device_ptr->device_info);
262-
if (interface_type == kUSBInterfaceType) {
263-
devices[device_identifier] = { device_ptr->device_info, nullptr };
264-
result[kEventString] = eventString;
265-
get_device_properties(device_identifier, result);
275+
if (interface_type == kUSBInterfaceType || interface_type == kWIFIInterfaceType) {
276+
if (devices.count(device_identifier)) {
277+
devices[device_identifier].device_info = device_ptr->device_info;
278+
result[kEventString] = kDeviceUpdated;
279+
} else {
280+
devices[device_identifier] = { device_ptr->device_info, nullptr };
281+
result[kEventString] = kDeviceFound;
282+
}
283+
284+
285+
if (interface_type == kUSBInterfaceType) {
286+
devices[device_identifier].isUSBConnected = 1;
287+
} else {
288+
devices[device_identifier].isWiFiConnected = 1;
289+
}
290+
291+
update_device_result(device_identifier, result);
266292
}
267293
}
268294

@@ -275,21 +301,33 @@ void device_notification_callback(const DevicePointer* device_ptr)
275301
{
276302
case kADNCIMessageConnected:
277303
{
278-
on_device_found(device_ptr, device_identifier, kDeviceFound, result);
304+
on_device_found(device_ptr, device_identifier, result);
279305
break;
280306
}
281307
case kADNCIMessageDisconnected:
282308
{
283-
if (devices.count(device_identifier))
284-
{
285-
if (devices[device_identifier].apps_cache.size())
286-
{
287-
cleanup_file_resources(device_identifier);
309+
if (devices.count(device_identifier)) {
310+
int interface_type = AMDeviceGetInterfaceType(device_ptr->device_info);
311+
if (interface_type == kUSBInterfaceType) {
312+
devices[device_identifier].isUSBConnected = 0;
313+
} else if (interface_type == kWIFIInterfaceType) {
314+
devices[device_identifier].isWiFiConnected = 0;
288315
}
316+
317+
if (!devices[device_identifier].isUSBConnected && !devices[device_identifier].isWiFiConnected) {
318+
if (devices[device_identifier].apps_cache.size())
319+
{
320+
cleanup_file_resources(device_identifier);
321+
}
289322

290-
devices.erase(device_identifier);
323+
devices.erase(device_identifier);
324+
result[kEventString] = kDeviceLost;
325+
} else {
326+
result[kEventString] = kDeviceUpdated;
327+
update_device_result(device_identifier, result);
328+
}
291329
}
292-
result[kEventString] = kDeviceLost;
330+
293331
break;
294332
}
295333
case kADNCIMessageUnknown:
@@ -299,7 +337,7 @@ void device_notification_callback(const DevicePointer* device_ptr)
299337
}
300338
case kADNCIMessageTrusted:
301339
{
302-
on_device_found(device_ptr, device_identifier, kDeviceTrusted, result);
340+
on_device_found(device_ptr, device_identifier, result);
303341
break;
304342
}
305343
}
@@ -497,14 +535,16 @@ void uninstall_application(std::string application_identifier, std::string devic
497535
return;
498536
}
499537

500-
HANDLE socket = start_secure_service(device_identifier, kInstallationProxy, method_id, true, false).socket;
501-
if (!socket)
538+
ServiceInfo serviceInfo = start_secure_service(device_identifier, kInstallationProxy, method_id, true, false);
539+
if (!serviceInfo.socket)
502540
{
503541
return;
504542
}
505543

506544
CFStringRef appid_cfstring = create_CFString(application_identifier.c_str());
507-
unsigned result = AMDeviceUninstallApplication(socket, appid_cfstring, NULL, [] {}, NULL);
545+
DeviceInfo* deviceInfo = devices[device_identifier].device_info;
546+
CFDictionaryRef params = CFDictionaryCreate(NULL, {}, {}, 0, NULL, NULL);
547+
unsigned result = AMDeviceSecureUninstallApplication(serviceInfo.connection, deviceInfo, appid_cfstring, params, NULL);
508548
CFRelease(appid_cfstring);
509549

510550
if (result)
@@ -1137,7 +1177,7 @@ void post_notification(std::string device_identifier, PostNotificationInfo post_
11371177
void await_notification_response(std::string device_identifier, AwaitNotificationResponseInfo await_notification_response_info, std::string method_id)
11381178
{
11391179
ServiceConnRef connection = serviceConnections[(int)await_notification_response_info.socket];
1140-
std::string invalid_connection_error_message = "Invalid connectionId: " + std::to_string(await_notification_response_info.socket);
1180+
std::string invalid_connection_error_message = "Invalid socket: " + std::to_string(await_notification_response_info.socket);
11411181
PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT(connection == nullptr, invalid_connection_error_message.c_str(), device_identifier, method_id);
11421182

11431183
ServiceInfo currentNotificationProxy = devices[device_identifier].services[kNotificationProxy];

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const dl = new DeviceLib.IOSDeviceLib(device => {
2424
console.log("An error occurred ;(", err);
2525
});
2626
});
27+
}, device => {
28+
console.log("Device UPDATED!", device);
2729
}, device => {
2830
console.log("Device LOST!", device);
2931
});

constants.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
exports.DataEventName = "data";
44
exports.DeviceFoundEventName = "deviceFound";
5+
exports.DeviceUpdatedEventName = "deviceUpdated";
56
exports.DeviceLostEventName = "deviceLost";
67

78
exports.DeviceEventEnum = {
89
kDeviceFound: "deviceFound",
10+
kDeviceUpdated: "deviceUpdated",
911
kDeviceLost: "deviceLost",
1012
kDeviceTrusted: "deviceTrusted",
1113
kDeviceUnknown: "deviceUnknown"

ios-device-lib-stdio-handler.js

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class IOSDeviceLibStdioHandler extends EventEmitter {
4848
case Constants.DeviceEventEnum.kDeviceFound:
4949
this.emit(Constants.DeviceFoundEventName, message);
5050
break;
51+
case Constants.DeviceEventEnum.kDeviceUpdated:
52+
this.emit(Constants.DeviceUpdatedEventName, message);
53+
break;
5154
case Constants.DeviceEventEnum.kDeviceLost:
5255
this.emit(Constants.DeviceLostEventName, message);
5356
break;

ios-device-lib.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ const Events = {
2727
};
2828

2929
class IOSDeviceLib extends EventEmitter {
30-
constructor(onDeviceFound, onDeviceLost, options) {
30+
constructor(onDeviceFound, onDeviceUpdated, onDeviceLost, options) {
3131
super();
3232
this._options = options || {};
3333
this._iosDeviceLibStdioHandler = new IOSDeviceLibStdioHandler(this._options);
3434
this._iosDeviceLibStdioHandler.startReadingData();
3535
this._iosDeviceLibStdioHandler.on(Constants.DeviceFoundEventName, onDeviceFound);
36+
this._iosDeviceLibStdioHandler.on(Constants.DeviceUpdatedEventName, onDeviceUpdated);
3637
this._iosDeviceLibStdioHandler.on(Constants.DeviceLostEventName, onDeviceLost);
3738
}
3839

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ios-device-lib",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"description": "",
55
"types": "./typings/ios-device-lib.d.ts",
66
"main": "index.js",
@@ -28,4 +28,4 @@
2828
"istanbul": "0.4.5",
2929
"mocha": "5.2.0"
3030
}
31-
}
31+
}

typings/interfaces.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ declare global {
1010
productType?: string;
1111
productVersion?: string;
1212
status?: string;
13+
isUSBConnected: number;
14+
isWiFiConnected: number;
1315
}
1416

1517
interface IDeviceId {
@@ -132,7 +134,7 @@ declare global {
132134
}
133135

134136
interface IOSDeviceLib extends NodeJS.EventEmitter {
135-
new(onDeviceFound: (found: IDeviceActionInfo) => void, onDeviceLost: (found: IDeviceActionInfo) => void): IOSDeviceLib;
137+
new(onDeviceFound: (found: IDeviceActionInfo) => void, onDeviceUpdated: (updated: IDeviceActionInfo) => void, onDeviceLost: (found: IDeviceActionInfo) => void): IOSDeviceLib;
136138
install(ipaPath: string, deviceIdentifiers: string[]): Promise<IDeviceResponse>[];
137139
uninstall(ipaPath: string, deviceIdentifiers: string[]): Promise<IDeviceResponse>[];
138140
list(listArray: IReadOperationData[]): Promise<IDeviceMultipleResponse>[];

0 commit comments

Comments
 (0)