Skip to content

Commit

Permalink
use output formatter for airdrop scan
Browse files Browse the repository at this point in the history
  • Loading branch information
sunflsks committed Feb 6, 2022
1 parent 4db984b commit 324aec9
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions airdrop/airdrop-scan.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@
#include <stdio.h>
#include <string.h>

#include "../include/output.h"

CFMutableArrayRef discovered;

NSMutableArray* devices;

void airdropBrowserCallBack(SFBrowserRef browser, SFNodeRef node, CFStringRef protocol, SFBrowserFlags flags, SFBrowserError error, void *info) {
CFArrayRef children = SFBrowserCopyChildren(browser, node);

for (int i = 0; i < CFArrayGetCount(children); i++) {
SFNodeRef node = (SFNodeRef)CFArrayGetValueAtIndex(children, i);
if (![(__bridge NSArray *)discovered
containsObject:(__bridge id)node]) {
printf("name: '%s',", [(__bridge_transfer NSString *)SFNodeCopyComputerName(node) UTF8String]);
printf(" id: '%s'`\n", [(__bridge_transfer NSString *)SFNodeCopyRealName(node) UTF8String]);

NSString* name = (__bridge_transfer NSString *)SFNodeCopyComputerName(node);
NSString* ident = (__bridge_transfer NSString *)SFNodeCopyRealName(node);

[devices addObjectsFromArray:@[
@{ @"name" : name},
@{ @"id" : ident}
]];

CFArrayAppendValue(discovered, node);
}
}
Expand All @@ -28,6 +39,8 @@ int airdropscan(int argc, char **argv) {
int timeout = 30;
const char *errstr;

devices = [NSMutableArray array];

struct option opts[] = {
{ "timeout", required_argument, 0, 't' },
{ NULL, 0, NULL, 0 }
Expand All @@ -53,10 +66,14 @@ int airdropscan(int argc, char **argv) {
SFBrowserSetClient(browser, airdropBrowserCallBack, &context);
SFBrowserOpenNode(browser, 0, 0, 0);

fprintf(stderr, "scanning... timeout set for %d seconds\n", timeout);

CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout, false);

CFRelease(discovered);
SFBrowserInvalidate(browser);

[NCOutput printArray:devices withJSON:YES];

return 0;
}

0 comments on commit 324aec9

Please sign in to comment.