Skip to content

Commit

Permalink
[Swift] Strong typing for PTUSBHub notification user info dictionaries
Browse files Browse the repository at this point in the history
In ObjC, clients were hardcoding `@"DeviceID"` et al when pulling values out of the device attach and detach notifications.
Now they can use `PTUSBHubNotificationKeyDeviceID` etc.

In Swift, instead of hardcoding `"DeviceID"` we can now do this:

```
@objc func onAttach(notification: Notification) {
		guard let userInfo = notification.userInfo as? [PTUSBHubNotificationKey: Any],
					let deviceID = userInfo[.deviceID] as? Int else {
			return
		}
		...
}
```
  • Loading branch information
jonathandann committed Dec 9, 2020
1 parent 6b6f123 commit d589b1e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Peertalk macOS Example/PTAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,22 @@ - (void)startListeningForDevices {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

[nc addObserverForName:PTUSBDeviceDidAttachNotification object:PTUSBHub.sharedHub queue:nil usingBlock:^(NSNotification *note) {
NSNumber *deviceID = [note.userInfo objectForKey:@"DeviceID"];
NSNumber *deviceID = [note.userInfo objectForKey:PTUSBHubNotificationKeyDeviceID];
//NSLog(@"PTUSBDeviceDidAttachNotification: %@", note.userInfo);
NSLog(@"PTUSBDeviceDidAttachNotification: %@", deviceID);

dispatch_async(self->notConnectedQueue_, ^{
if (!self->connectingToDeviceID_ || ![deviceID isEqualToNumber:self->connectingToDeviceID_]) {
[self disconnectFromCurrentChannel];
self->connectingToDeviceID_ = deviceID;
self->connectedDeviceProperties_ = [note.userInfo objectForKey:@"Properties"];
self->connectedDeviceProperties_ = [note.userInfo objectForKey:PTUSBHubNotificationKeyProperties];
[self enqueueConnectToUSBDevice];
}
});
}];

[nc addObserverForName:PTUSBDeviceDidDetachNotification object:PTUSBHub.sharedHub queue:nil usingBlock:^(NSNotification *note) {
NSNumber *deviceID = [note.userInfo objectForKey:@"DeviceID"];
NSNumber *deviceID = [note.userInfo objectForKey:PTUSBHubNotificationKeyDeviceID];
//NSLog(@"PTUSBDeviceDidDetachNotification: %@", note.userInfo);
NSLog(@"PTUSBDeviceDidDetachNotification: %@", deviceID);

Expand Down
9 changes: 7 additions & 2 deletions peertalk/PTUSBHub.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// };
// }
//
FOUNDATION_EXPORT NSString * const PTUSBDeviceDidAttachNotification;
FOUNDATION_EXPORT NSNotificationName const PTUSBDeviceDidAttachNotification NS_SWIFT_NAME(deviceDidAttach);

// PTUSBDeviceDidDetachNotification
// Posted when a device has been detached.
Expand All @@ -28,7 +28,12 @@ FOUNDATION_EXPORT NSString * const PTUSBDeviceDidAttachNotification;
// MessageType = Detached;
// }
//
FOUNDATION_EXPORT NSString * const PTUSBDeviceDidDetachNotification;
FOUNDATION_EXPORT NSNotificationName const PTUSBDeviceDidDetachNotification NS_SWIFT_NAME(deviceDidDetach);

typedef NSString * PTUSBHubNotificationKey NS_TYPED_ENUM;
FOUNDATION_EXPORT PTUSBHubNotificationKey const PTUSBHubNotificationKeyDeviceID NS_SWIFT_NAME(deviceID);
FOUNDATION_EXPORT PTUSBHubNotificationKey const PTUSBHubNotificationKeyMessageType NS_SWIFT_NAME(messageType);
FOUNDATION_EXPORT PTUSBHubNotificationKey const PTUSBHubNotificationKeyProperties NS_SWIFT_NAME(properties);

// NSError domain
FOUNDATION_EXPORT NSString * const PTUSBHubErrorDomain;
Expand Down
19 changes: 11 additions & 8 deletions peertalk/PTUSBHub.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ static void usbmux_packet_free(usbmux_packet_t *upacket) {
}


NSString * const PTUSBDeviceDidAttachNotification = @"PTUSBDeviceDidAttachNotification";
NSString * const PTUSBDeviceDidDetachNotification = @"PTUSBDeviceDidDetachNotification";
NSNotificationName const PTUSBDeviceDidAttachNotification = @"PTUSBDeviceDidAttachNotification";
NSNotificationName const PTUSBDeviceDidDetachNotification = @"PTUSBDeviceDidDetachNotification";

PTUSBHubNotificationKey const PTUSBHubNotificationKeyDeviceID = @"DeviceID";
PTUSBHubNotificationKey const PTUSBHubNotificationKeyMessageType = @"MessageType";
PTUSBHubNotificationKey const PTUSBHubNotificationKeyProperties = @"Properties";

static NSString *kPlistPacketTypeListen = @"Listen";
static NSString *kPlistPacketTypeConnect = @"Connect";


// Represents a channel of communication between the host process and a remote
// (device) system. In practice, a PTUSBChannel is connected to a usbmuxd
// endpoint which is configured to either listen for device changes (the
Expand Down Expand Up @@ -232,7 +235,7 @@ - (void)connectToDevice:(NSNumber*)deviceID port:(int)port onStart:(void(^)(NSEr

NSDictionary *packet = [PTUSBChannel packetDictionaryWithPacketType:kPlistPacketTypeConnect
payload:[NSDictionary dictionaryWithObjectsAndKeys:
deviceID, @"DeviceID",
deviceID, PTUSBHubNotificationKeyDeviceID,
[NSNumber numberWithInt:port], @"PortNumber",
nil]];

Expand All @@ -245,7 +248,7 @@ - (void)connectToDevice:(NSNumber*)deviceID port:(int)port onStart:(void(^)(NSEr


- (void)handleBroadcastPacket:(NSDictionary*)packet {
NSString *messageType = [packet objectForKey:@"MessageType"];
NSString *messageType = [packet objectForKey:PTUSBHubNotificationKeyMessageType];

if ([@"Attached" isEqualToString:messageType]) {
[[NSNotificationCenter defaultCenter] postNotificationName:PTUSBDeviceDidAttachNotification object:self userInfo:packet];
Expand Down Expand Up @@ -279,12 +282,12 @@ + (NSDictionary*)packetDictionaryWithPacketType:(NSString*)messageType payload:(

if (bundleName) {
packet = [NSDictionary dictionaryWithObjectsAndKeys:
messageType, @"MessageType",
messageType, PTUSBHubNotificationKeyMessageType,
bundleName, @"ProgName",
bundleVersion, @"ClientVersionString",
nil];
} else {
packet = [NSDictionary dictionaryWithObjectsAndKeys:messageType, @"MessageType", nil];
packet = [NSDictionary dictionaryWithObjectsAndKeys:messageType, PTUSBHubNotificationKeyMessageType, nil];
}

if (payload) {
Expand Down Expand Up @@ -345,7 +348,7 @@ - (BOOL)openOnQueue:(dispatch_queue_t)queue error:(NSError**)error onEnd:(void(^
// Connect socket
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, "/var/run/usbmuxd");
strcpy(addr.sun_path, "/private/var/run/usbmuxd");
socklen_t socklen = sizeof(addr);
if (connect(fd, (struct sockaddr*)&addr, socklen) == -1) {
if (error) *error = [[NSError alloc] initWithDomain:NSPOSIXErrorDomain code:errno userInfo:nil];
Expand Down

0 comments on commit d589b1e

Please sign in to comment.