Skip to content

Commit 136f1de

Browse files
committed
fix: introduce a deviceUpdated event in order to support devices with multiple connection types
1 parent e5e472d commit 136f1de

9 files changed

+76
-31
lines changed

IOSDeviceLib/Constants.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static const char *kUnreachableStatus = "Unreachable";
4040
static const char *kConnectedStatus = "Connected";
4141

4242
static const char *kDeviceFound = "deviceFound";
43+
static const char *kDeviceUpdated = "deviceUpdated";
4344
static const char *kDeviceLost = "deviceLost";
4445
static const char *kDeviceTrusted = "deviceTrusted";
4546
static const char *kDeviceUnknown = "deviceUnknown";
@@ -70,7 +71,8 @@ static const char *kResponseCommandType = "responseCommandType";
7071
static const char *kResponsePropertyName = "responsePropertyName";
7172
static const char *kShouldWaitForResponse = "shouldWaitForResponse";
7273
static const char *kCommandType = "commandType";
73-
static const char *kConnectionType = "connectionType";
74+
static const char *kIsWiFiConnected = "isWiFiConnected";
75+
static const char *kIsUSBConnected = "isUSBConnected";
7476
static const char *kTimeout = "timeout";
7577
static const char *kPort = "port";
7678
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)(ServiceConnRef connection, const DeviceInfo*, CFStringRef *bundleIdentifier, CFDictionaryRef *params, void (*installCallback)(CFDictionaryRef*, void *));
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,7 +278,7 @@ extern "C"
276278
unsigned AMDeviceCreateHouseArrestService(const DeviceInfo*, CFStringRef identifier, void * unknown, AFCConnectionRef * handle);
277279
int AMDeviceGetConnectionID(const DeviceInfo*);
278280
int AMDeviceGetInterfaceType(const DeviceInfo*);
279-
unsigned AMDeviceSecureUninstallApplication(ServiceConnRef connection, const DeviceInfo*, CFStringRef bundleIdentifier, CFDictionaryRef params, void (*installCallback)(CFDictionaryRef*, void *));
281+
unsigned AMDeviceSecureUninstallApplication(ServiceConnRef connection, const DeviceInfo*, CFStringRef bundleIdentifier, CFDictionaryRef params, void (*installCallback)(CFDictionaryRef*, void *));
280282
unsigned AMDeviceUninstallApplication(HANDLE, CFStringRef, void*, void(*f)(), void*);
281283
unsigned AMDeviceStartSession(const DeviceInfo*);
282284
unsigned AMDeviceStopSession(const DeviceInfo*);

IOSDeviceLib/IOSDeviceLib.cpp

+55-23
Original file line numberDiff line numberDiff line change
@@ -134,7 +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));
137+
assert(AMDeviceIsPaired(device_info));
138138
UNLOCK_MUTEX_AND_RETURN_IF_FAILED_RESULT(AMDeviceValidatePairing(device_info), start_session_mutex);
139139
UNLOCK_MUTEX_AND_RETURN_IF_FAILED_RESULT(AMDeviceStartSession(device_info), start_session_mutex);
140140
}
@@ -243,18 +243,26 @@ void get_device_properties(std::string device_identifier, json &result)
243243
result["deviceName"] = get_device_property_value(device_identifier, "DeviceName");
244244
result["productVersion"] = get_device_property_value(device_identifier, kProductVersion);
245245
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"
246+
// available values:
247+
// "BluetoothAddress","BoardId","CPUArchitecture","ChipID","DeviceClass",
248+
// "DeviceColor","DeviceName","FirmwareVersion","HardwareModel",
249+
// "ModelNumber","ProductType","ProductVersion","UniqueDeviceID","WiFiAddress"
250250
}
251251

252252
inline bool has_complete_status(std::map<std::string, boost::any>& dict)
253253
{
254254
return boost::any_cast<std::string>(dict[kStatusKey]) == kComplete;
255255
}
256256

257-
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)
258266
{
259267
/*
260268
Interface type can be one of the followings:
@@ -265,11 +273,23 @@ void on_device_found(const DevicePointer* device_ptr, std::string device_identif
265273
*/
266274
int interface_type = AMDeviceGetInterfaceType(device_ptr->device_info);
267275
if (interface_type == kUSBInterfaceType || interface_type == kWIFIInterfaceType) {
268-
devices[device_identifier] = { device_ptr->device_info, nullptr };
269-
result[kEventString] = eventString;
270-
result[kConnectionType] = interface_type;
271-
get_device_properties(device_identifier, result);
272-
}
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);
292+
}
273293
}
274294

275295
void device_notification_callback(const DevicePointer* device_ptr)
@@ -281,21 +301,33 @@ void device_notification_callback(const DevicePointer* device_ptr)
281301
{
282302
case kADNCIMessageConnected:
283303
{
284-
on_device_found(device_ptr, device_identifier, kDeviceFound, result);
304+
on_device_found(device_ptr, device_identifier, result);
285305
break;
286306
}
287307
case kADNCIMessageDisconnected:
288308
{
289-
if (devices.count(device_identifier))
290-
{
291-
if (devices[device_identifier].apps_cache.size())
292-
{
293-
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;
294315
}
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+
}
295322

296-
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+
}
297329
}
298-
result[kEventString] = kDeviceLost;
330+
299331
break;
300332
}
301333
case kADNCIMessageUnknown:
@@ -305,7 +337,7 @@ void device_notification_callback(const DevicePointer* device_ptr)
305337
}
306338
case kADNCIMessageTrusted:
307339
{
308-
on_device_found(device_ptr, device_identifier, kDeviceTrusted, result);
340+
on_device_found(device_ptr, device_identifier, result);
309341
break;
310342
}
311343
}
@@ -510,9 +542,9 @@ void uninstall_application(std::string application_identifier, std::string devic
510542
}
511543

512544
CFStringRef appid_cfstring = create_CFString(application_identifier.c_str());
513-
DeviceInfo* deviceInfo = devices[device_identifier].device_info;
514-
CFDictionaryRef params = CFDictionaryCreate(NULL, {}, {}, 0, NULL, NULL);
515-
unsigned result = AMDeviceSecureUninstallApplication(serviceInfo.connection, deviceInfo, appid_cfstring, params, 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);
516548
CFRelease(appid_cfstring);
517549

518550
if (result)

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-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ declare global {
1010
productType?: string;
1111
productVersion?: string;
1212
status?: string;
13-
connectionType: number;
13+
isUSBConnected: number;
14+
isWiFiConnected: number;
1415
}
1516

1617
interface IDeviceId {
@@ -133,7 +134,7 @@ declare global {
133134
}
134135

135136
interface IOSDeviceLib extends NodeJS.EventEmitter {
136-
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;
137138
install(ipaPath: string, deviceIdentifiers: string[]): Promise<IDeviceResponse>[];
138139
uninstall(ipaPath: string, deviceIdentifiers: string[]): Promise<IDeviceResponse>[];
139140
list(listArray: IReadOperationData[]): Promise<IDeviceMultipleResponse>[];

0 commit comments

Comments
 (0)