diff --git a/Brokenithm-iOS.xcodeproj/project.pbxproj b/Brokenithm-iOS.xcodeproj/project.pbxproj index d59e474..4393989 100644 --- a/Brokenithm-iOS.xcodeproj/project.pbxproj +++ b/Brokenithm-iOS.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 1968E72C24086C2200784829 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1968E72A24086C2200784829 /* Main.storyboard */; }; 1968E72E24086C2B00784829 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1968E72D24086C2B00784829 /* Assets.xcassets */; }; 1968E73424086C2B00784829 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 1968E73324086C2B00784829 /* main.m */; }; + 19F13E2A240A6F2200809B83 /* SocketDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 19F13E29240A6F2200809B83 /* SocketDelegate.m */; }; D023549DD3B09C46EBA2E321 /* libPods-Brokenithm-iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A7EE02147010843456A2152 /* libPods-Brokenithm-iOS.a */; }; /* End PBXBuildFile section */ @@ -25,6 +26,8 @@ 1968E72D24086C2B00784829 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 1968E73224086C2B00784829 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 1968E73324086C2B00784829 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + 19F13E22240A683200809B83 /* SocketDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SocketDelegate.h; sourceTree = ""; }; + 19F13E29240A6F2200809B83 /* SocketDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SocketDelegate.m; sourceTree = ""; }; 259804D4CC006A58255BC938 /* Pods-Brokenithm-iOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Brokenithm-iOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-Brokenithm-iOS/Pods-Brokenithm-iOS.release.xcconfig"; sourceTree = ""; }; 5A7EE02147010843456A2152 /* libPods-Brokenithm-iOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Brokenithm-iOS.a"; sourceTree = BUILT_PRODUCTS_DIR; }; B78B36B35C6A90E4F71F84B4 /* Pods-Brokenithm-iOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Brokenithm-iOS.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Brokenithm-iOS/Pods-Brokenithm-iOS.debug.xcconfig"; sourceTree = ""; }; @@ -71,6 +74,8 @@ 1968E72D24086C2B00784829 /* Assets.xcassets */, 1968E73224086C2B00784829 /* Info.plist */, 1968E73324086C2B00784829 /* main.m */, + 19F13E22240A683200809B83 /* SocketDelegate.h */, + 19F13E29240A6F2200809B83 /* SocketDelegate.m */, ); path = "Brokenithm-iOS"; sourceTree = ""; @@ -186,6 +191,7 @@ 1968E72924086C2200784829 /* ViewController.m in Sources */, 1968E73424086C2B00784829 /* main.m in Sources */, 1968E72624086C2200784829 /* AppDelegate.m in Sources */, + 19F13E2A240A6F2200809B83 /* SocketDelegate.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Brokenithm-iOS/SocketDelegate.h b/Brokenithm-iOS/SocketDelegate.h new file mode 100644 index 0000000..239d269 --- /dev/null +++ b/Brokenithm-iOS/SocketDelegate.h @@ -0,0 +1,26 @@ +// +// SocketDelegate.h +// Brokenithm-iOS +// +// Created by ester on 2020/2/29. +// Copyright © 2020 esterTion. All rights reserved. +// + +#ifndef SocketDelegate_h +#define SocketDelegate_h + +@class ViewController; + +#import +#import +#import "ViewController.h" + +@interface SocketDelegate : NSObject { + GCDAsyncSocket *server; + NSMutableArray *connectedSockets; +} +@property ViewController *parentVc; + +@end + +#endif /* SocketDelegate_h */ diff --git a/Brokenithm-iOS/SocketDelegate.m b/Brokenithm-iOS/SocketDelegate.m new file mode 100644 index 0000000..b8442b8 --- /dev/null +++ b/Brokenithm-iOS/SocketDelegate.m @@ -0,0 +1,78 @@ +// +// SocketDelegate.m +// Brokenithm-iOS +// +// Created by ester on 2020/2/29. +// Copyright © 2020 esterTion. All rights reserved. +// + +#import "SocketDelegate.h" + +@interface SocketDelegate () +@end + +@implementation SocketDelegate + +- (id)init { + server = [[GCDAsyncSocket alloc] initWithDelegate:(id)self delegateQueue:dispatch_get_main_queue()]; + { + NSError *error = nil; + if (![server acceptOnPort:24864 error:&error]) { + NSLog(@"error creating server: %@", error); + return nil; + } + } + connectedSockets = [[NSMutableArray alloc] initWithCapacity:1]; + return [super init]; +} + +- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket { + @synchronized(connectedSockets) + { + [connectedSockets addObject:newSocket]; + } + NSLog(@"got connection"); + NSString *initResponse = @"\x03WEL"; + NSData *initResp = [initResponse dataUsingEncoding:NSASCIIStringEncoding]; + [newSocket writeData:initResp withTimeout:-1 tag:0]; + [newSocket readDataToLength:1 withTimeout:5 tag:0]; +} +- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag {} +- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { + switch (tag) { + case 0: { + // length + [sock readDataToLength:((uint8_t*)data.bytes)[0] withTimeout:1 tag:1]; + break; + } + case 1: { + // data + if (data.length < 3) { + [sock disconnect]; + return; + } + NSData *msgData = [data subdataWithRange:NSMakeRange(0, 3)]; + NSString *message = [[NSString alloc] initWithData:msgData encoding:NSASCIIStringEncoding]; + if ([message isEqualToString:@"LED"] && data.length >= 99) { + NSLog(@"received led update"); + NSData *led = [data subdataWithRange:NSMakeRange(3, 96)]; + [self.parentVc updateLed:led]; + } + + [sock readDataToLength:1 withTimeout:5 tag:0]; + } + } +} +- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { + if (sock != server) + { + NSLog(@"connection ended"); + @synchronized(connectedSockets) + { + [connectedSockets removeObject:sock]; + } + } +} + +@end + diff --git a/Brokenithm-iOS/ViewController.h b/Brokenithm-iOS/ViewController.h index f22b820..423a80b 100644 --- a/Brokenithm-iOS/ViewController.h +++ b/Brokenithm-iOS/ViewController.h @@ -6,10 +6,21 @@ // Copyright © 2020 esterTion. All rights reserved. // +#pragma once + +@class SocketDelegate; + #import +#import +#import "SocketDelegate.h" -@interface ViewController : UIViewController +@interface ViewController : UIViewController { + SocketDelegate *server; +} +@property UIView *airIOView; +@property UIView *sliderIOView; +@property CAGradientLayer *ledBackground; +-(void)updateLed:(NSData*)rgbData; @end - diff --git a/Brokenithm-iOS/ViewController.m b/Brokenithm-iOS/ViewController.m index 4e65846..9eaf02d 100644 --- a/Brokenithm-iOS/ViewController.m +++ b/Brokenithm-iOS/ViewController.m @@ -10,17 +10,21 @@ @interface ViewController () -@property UIView *airIOView; -@property UIView *sliderIOView; -@property CAGradientLayer *ledBackground; - @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view, typically from a nib. + + // network permission + { + NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://captive.apple.com/"]]; + [NSURLConnection sendAsynchronousRequest:req + queue:[NSOperationQueue mainQueue] + completionHandler:^(NSURLResponse *resp, NSData *data, NSError *error) {}]; + } + CGRect screenSize = [UIScreen mainScreen].bounds; float screenWidth = screenSize.size.width; float screenHeight = screenSize.size.height; @@ -30,8 +34,6 @@ self.sliderIOView = [[UIView alloc] initWithFrame:CGRectMake(0, offsetY, screenWidth, sliderHeight)]; [self.view addSubview:self.airIOView]; [self.view addSubview:self.sliderIOView]; - //self.airIOView.backgroundColor = [UIColor blueColor]; - //self.sliderIOView.backgroundColor = [UIColor redColor]; self.ledBackground = [CAGradientLayer layer]; self.ledBackground.frame = CGRectMake(0, 0, screenWidth, sliderHeight); [self.sliderIOView.layer addSublayer:self.ledBackground]; @@ -67,11 +69,15 @@ [self.sliderIOView addSubview:sliderInput]; } + server = [[SocketDelegate alloc] init]; + server.parentVc = self; + NSLog(@"server created"); dispatch_async(dispatch_get_main_queue(), ^(){ char ledDataChar[32*3] = {0,254,254,0,254,254,0,254,254,0,0,0,254,254,254,254,254,254,254,254,254,0,0,0,10,10,10,10,10,10,10,10,10,0,0,0,0,254,128,0,254,128,0,254,128,0,254,128,0,254,128,0,254,128,0,254,128,0,0,0,0,0,254,0,0,254,0,0,254,0,0,254,0,0,254,0,0,0,0,0,254,0,0,254,0,0,254,0,0,254,0,0,254,0,0,0}; NSData *ledData = [NSData dataWithBytes:ledDataChar length:32*3]; [self updateLed:ledData]; + NSLog(@"displayed demo led"); }); }