diff --git a/Ifish.xcodeproj/project.pbxproj b/Ifish.xcodeproj/project.pbxproj index 83aa041..33b9c5b 100644 --- a/Ifish.xcodeproj/project.pbxproj +++ b/Ifish.xcodeproj/project.pbxproj @@ -2310,6 +2310,12 @@ C05716712823D653004F113A /* XuanduoTimerListViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C057166F2823D652004F113A /* XuanduoTimerListViewController.m */; }; C0571674282674A4004F113A /* XuanduofishFeedViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C0571672282674A3004F113A /* XuanduofishFeedViewController.m */; }; C0571675282674A4004F113A /* XuanduofishFeedViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = C0571673282674A3004F113A /* XuanduofishFeedViewController.h */; }; + C0714E032A6FC3B300182CA8 /* JMAirKissEncoder.m in Sources */ = {isa = PBXBuildFile; fileRef = C0714DFD2A6FC3B300182CA8 /* JMAirKissEncoder.m */; }; + C0714E042A6FC3B300182CA8 /* JMAirKiss_Defines.h in Headers */ = {isa = PBXBuildFile; fileRef = C0714DFE2A6FC3B300182CA8 /* JMAirKiss_Defines.h */; }; + C0714E052A6FC3B300182CA8 /* JMAirKissConnection.h in Headers */ = {isa = PBXBuildFile; fileRef = C0714DFF2A6FC3B300182CA8 /* JMAirKissConnection.h */; }; + C0714E062A6FC3B300182CA8 /* JMAirKissEncoder.h in Headers */ = {isa = PBXBuildFile; fileRef = C0714E002A6FC3B300182CA8 /* JMAirKissEncoder.h */; }; + C0714E072A6FC3B300182CA8 /* JMAirKiss.h in Headers */ = {isa = PBXBuildFile; fileRef = C0714E012A6FC3B300182CA8 /* JMAirKiss.h */; }; + C0714E082A6FC3B300182CA8 /* JMAirKissConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = C0714E022A6FC3B300182CA8 /* JMAirKissConnection.m */; }; C072196A253AE2290065CF17 /* IfishP2PPlayBackListViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C0721969253AE2290065CF17 /* IfishP2PPlayBackListViewController.xib */; }; C072196D253B1F4F0065CF17 /* DatePickerCustomView.h in Headers */ = {isa = PBXBuildFile; fileRef = C072196B253B1F4F0065CF17 /* DatePickerCustomView.h */; }; C072196E253B1F4F0065CF17 /* DatePickerCustomView.m in Sources */ = {isa = PBXBuildFile; fileRef = C072196C253B1F4F0065CF17 /* DatePickerCustomView.m */; }; @@ -5829,6 +5835,12 @@ C057166F2823D652004F113A /* XuanduoTimerListViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XuanduoTimerListViewController.m; sourceTree = ""; }; C0571672282674A3004F113A /* XuanduofishFeedViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XuanduofishFeedViewController.m; sourceTree = ""; }; C0571673282674A3004F113A /* XuanduofishFeedViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XuanduofishFeedViewController.h; sourceTree = ""; }; + C0714DFD2A6FC3B300182CA8 /* JMAirKissEncoder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JMAirKissEncoder.m; sourceTree = ""; }; + C0714DFE2A6FC3B300182CA8 /* JMAirKiss_Defines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JMAirKiss_Defines.h; sourceTree = ""; }; + C0714DFF2A6FC3B300182CA8 /* JMAirKissConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JMAirKissConnection.h; sourceTree = ""; }; + C0714E002A6FC3B300182CA8 /* JMAirKissEncoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JMAirKissEncoder.h; sourceTree = ""; }; + C0714E012A6FC3B300182CA8 /* JMAirKiss.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JMAirKiss.h; sourceTree = ""; }; + C0714E022A6FC3B300182CA8 /* JMAirKissConnection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JMAirKissConnection.m; sourceTree = ""; }; C0721969253AE2290065CF17 /* IfishP2PPlayBackListViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = IfishP2PPlayBackListViewController.xib; sourceTree = ""; }; C072196B253B1F4F0065CF17 /* DatePickerCustomView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DatePickerCustomView.h; sourceTree = ""; }; C072196C253B1F4F0065CF17 /* DatePickerCustomView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DatePickerCustomView.m; sourceTree = ""; }; @@ -12205,6 +12217,7 @@ 88F5EEE81D48D04100CC7CAF /* libs */ = { isa = PBXGroup; children = ( + C0714DFC2A6FC3B300182CA8 /* airkiss */, C02D9E482794011100BBBF8D /* HTTPDNS */, C0EA883E23AF61D6003DC53B /* SDWebImage3.8.2 */, C024B7892379668B00509424 /* AvoidCrash */, @@ -12439,6 +12452,19 @@ path = HTTPDNS; sourceTree = ""; }; + C0714DFC2A6FC3B300182CA8 /* airkiss */ = { + isa = PBXGroup; + children = ( + C0714DFD2A6FC3B300182CA8 /* JMAirKissEncoder.m */, + C0714DFE2A6FC3B300182CA8 /* JMAirKiss_Defines.h */, + C0714DFF2A6FC3B300182CA8 /* JMAirKissConnection.h */, + C0714E002A6FC3B300182CA8 /* JMAirKissEncoder.h */, + C0714E012A6FC3B300182CA8 /* JMAirKiss.h */, + C0714E022A6FC3B300182CA8 /* JMAirKissConnection.m */, + ); + path = airkiss; + sourceTree = ""; + }; C0837A54244FF5500034FEC5 /* EGOCache */ = { isa = PBXGroup; children = ( @@ -13178,6 +13204,7 @@ CB484E1822B8C8C90075F050 /* old_codec_ids.h in Headers */, 2AC8750D26874F7500B6161C /* ESPTouchGenerator.h in Headers */, CB484CFC22B8B0AC0075F050 /* GCDAsyncSocket.h in Headers */, + C0714E042A6FC3B300182CA8 /* JMAirKiss_Defines.h in Headers */, CB7D6AB922954FB90014E5C7 /* FuncUserDefault.h in Headers */, 3D1C50C9221A9EE00096AE43 /* ContactDAO.h in Headers */, CB484D8522B8BE830075F050 /* P2PClient.h in Headers */, @@ -13211,6 +13238,7 @@ 2AC8751A26874F7500B6161C /* ESP_CRC8.h in Headers */, CB484DD922B8C8C80075F050 /* elian.h in Headers */, CB4820562334E30300A50C92 /* HKPieChartView.h in Headers */, + C0714E062A6FC3B300182CA8 /* JMAirKissEncoder.h in Headers */, C0E7D6C023A1D11500256A10 /* WXApiRequestHandler.h in Headers */, CBA615FC228E8E5A00ED380D /* MyMessageViewController.h in Headers */, CB484E1522B8C8C90075F050 /* avfft.h in Headers */, @@ -13249,6 +13277,8 @@ 3D1C507F221A9EDF0096AE43 /* KTPhotoBrowserDataSource.h in Headers */, 3D1C50E9221A9EE00096AE43 /* AlarmDAO.h in Headers */, C0EA8A2123AF74F4003DC53B /* MKAnnotationView+WebCache.h in Headers */, + C0714E052A6FC3B300182CA8 /* JMAirKissConnection.h in Headers */, + C0714E072A6FC3B300182CA8 /* JMAirKiss.h in Headers */, CBA61658228F9A6C00ED380D /* NSObject+YYModel.h in Headers */, 3D1C50C4221A9EE00096AE43 /* OBGradientView.h in Headers */, C0E7D6D223A1D16D00256A10 /* GetMessageFromWXResp+responseWithTextOrMediaMessage.h in Headers */, @@ -15506,6 +15536,7 @@ 8891330B1ECE999A00F52EBC /* IfishVideoModel.m in Sources */, 880707581DDD76480076F65A /* P2PShareViewBootomView.m in Sources */, 8835180C1E66916400FCBE56 /* UIControl+YT.m in Sources */, + C0714E032A6FC3B300182CA8 /* JMAirKissEncoder.m in Sources */, 8816721B1EA5DE2B00BEBF23 /* UMComSimplicityDiscoverViewController.m in Sources */, 3D1C50ED221A9EE00096AE43 /* RecommendInfoDAO.m in Sources */, 88C5989D1EA0965B00BDC812 /* LBLaunchImageAdView.m in Sources */, @@ -15682,6 +15713,7 @@ 3D8F649A21571D7C00F04BD3 /* HitbarWifeVc.m in Sources */, 8861258F1E2731CC004A6097 /* JuNengSoketBackUtls.m in Sources */, 88F5EFD11D48D08100CC7CAF /* MMProgressHUD+Animations.m in Sources */, + C0714E082A6FC3B300182CA8 /* JMAirKissConnection.m in Sources */, 881671AC1EA5DE2B00BEBF23 /* UMComUserDataController.m in Sources */, 88C1B2CD1DD1A58400C2C99A /* IfishKankanListModel.m in Sources */, 889133341ECEB94B00F52EBC /* VideoEditeVideoCell.m in Sources */, @@ -16025,7 +16057,7 @@ CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 2; + CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_TEAM = WFX8GD5HFX; ENABLE_BITCODE = NO; ENABLE_TESTABILITY = YES; @@ -16145,7 +16177,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 2; + CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_TEAM = WFX8GD5HFX; ENABLE_BITCODE = NO; ENABLE_TESTABILITY = YES; diff --git a/Ifish/.DS_Store b/Ifish/.DS_Store index ce30a78..7ee8bf9 100644 Binary files a/Ifish/.DS_Store and b/Ifish/.DS_Store differ diff --git a/Ifish/airkiss/JMAirKiss.h b/Ifish/airkiss/JMAirKiss.h new file mode 100644 index 0000000..eaf6b1d --- /dev/null +++ b/Ifish/airkiss/JMAirKiss.h @@ -0,0 +1,20 @@ +// +// JMAirKiss.h +// JMAirKiss +// +// Created by shengxiao on 16/3/2. +// Copyright © 2016年 shengxiao. All rights reserved. +// + +#import + +//! Project version number for JMAirKiss. +FOUNDATION_EXPORT double JMAirKissVersionNumber; + +//! Project version string for JMAirKiss. +FOUNDATION_EXPORT const unsigned char JMAirKissVersionString[]; + +// In this header, you should import all the public headers of your framework using statements like #import + +#import "GCDAsyncUdpSocket.h" +#import "JMAirKissConnection.h" diff --git a/Ifish/airkiss/JMAirKissConnection.h b/Ifish/airkiss/JMAirKissConnection.h new file mode 100644 index 0000000..f2d71af --- /dev/null +++ b/Ifish/airkiss/JMAirKissConnection.h @@ -0,0 +1,30 @@ +// +// JMAirKissConnection.h +// JMAirKiss +// +// Created by shengxiao on 16/3/2. +// Copyright © 2016年 shengxiao. All rights reserved. +// + +#import + +typedef void (^AirKissConnectionSuccess) (NSString*macAddress); +typedef void (^AirKissConnectionFailure) (void); + +@interface JMAirKissConnection : NSObject + +@property(nonatomic,copy) AirKissConnectionSuccess connectionSuccess; +@property(nonatomic,copy) AirKissConnectionFailure connectionFailure; + +/** + * AirKiss连接 + * + * @param ssidStr ssid + * @param pswStr psw + */ +- (void)connectAirKissWithSSID:(NSString *)ssidStr + password:(NSString *)password; + +- (void)closeConnection; + +@end diff --git a/Ifish/airkiss/JMAirKissConnection.m b/Ifish/airkiss/JMAirKissConnection.m new file mode 100644 index 0000000..2925df7 --- /dev/null +++ b/Ifish/airkiss/JMAirKissConnection.m @@ -0,0 +1,252 @@ +// +// JMAirKissConnection.m +// JMAirKiss +// +// Created by shengxiao on 16/3/2. +// Copyright © 2016年 shengxiao. All rights reserved. +// + +#import "JMAirKissConnection.h" +#import "JMAirKissEncoder.h" +#import "GCDAsyncUdpSocket.h" +#import "ESPTouchTaskParameter.h" +#import "ESP_NetUtil.h" + +#define kAirKiss_Port 10000 +#define kAirKiss_Host @"255.255.255.255" +#define kAirKiss_Limit_Return_Random_Num 20 + +@interface JMAirKissConnection() +{ + JMAirKissEncoder *_airKissEncoder; + NSTimer *_timer; // 超过1分钟未连接成功则表示失败 + + GCDAsyncUdpSocket *_clientUdpSocket; + GCDAsyncUdpSocket *_serverUdpSocket; + + long _tag; + int _returnRandomNum; + + BOOL _connectionDone; +} +@property (nonatomic,strong) ESPTaskParameter *_parameter; +@end + +@implementation JMAirKissConnection + +- (instancetype)init +{ + self = [super init]; + if (self) { + _airKissEncoder = [[JMAirKissEncoder alloc] init]; + _tag = 0; + _returnRandomNum = 0; + _connectionDone = false; + self._parameter = [[ESPTaskParameter alloc]init]; + + // check whether IPv4 and IPv6 is supported + NSString *localInetAddr4 = [ESP_NetUtil getLocalIPv4]; + if (![ESP_NetUtil isIPv4PrivateAddr:localInetAddr4]) { + localInetAddr4 = nil; + } + NSString *localInetAddr6 = [ESP_NetUtil getLocalIPv6]; + [self._parameter setIsIPv4Supported:localInetAddr4!=nil]; + [self._parameter setIsIPv6Supported:localInetAddr6!=nil]; + + [self setupClientUdpSocket]; + [self setupServerUdpSocket]; + } + return self; +} + +#pragma mark - Connection +/** + * AirKiss连接 + * + * @param ssidStr ssid + * @param pswStr psw + */ +- (void)connectAirKissWithSSID:(NSString *)ssidStr + password:(NSString *)password { + NSMutableArray *dataArray = [_airKissEncoder createAirKissEncorderWithSSID:ssidStr ? :@"" + password:password ? :@""]; + + _tag = 0; + _returnRandomNum = 0; + _connectionDone = false; + + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + dispatch_async(dispatch_get_main_queue(), ^{ + _timer = [NSTimer scheduledTimerWithTimeInterval:60 + target:self + selector:@selector(connectFailure) + userInfo:nil + repeats:NO]; + }); + + for (int i = 0;i < dataArray.count;i++) { + if (_connectionDone == true) { + break; + } + UInt16 length = [dataArray[i] unsignedShortValue]; + NSMutableData *mData = [NSMutableData data]; + UInt8 value = 0; + for (int j = 0; j < length; j++) { + [mData appendBytes:&value length:1]; + } + [_clientUdpSocket sendData:mData + toHost:kAirKiss_Host + port:kAirKiss_Port + withTimeout:-1 + tag:_tag]; + + [NSThread sleepForTimeInterval:0.004]; + + _tag++; + } + }); +} + +- (void)closeConnection { + _connectionDone = true; + + [_timer invalidate]; + _timer = nil; + + [_clientUdpSocket close]; + [_serverUdpSocket close]; + + _clientUdpSocket = nil; + _serverUdpSocket = nil; +} + +#pragma mark - Set up udp socket +- (void)setupClientUdpSocket +{ + NSError *error = nil; + + if (!_clientUdpSocket) { + _clientUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; + [_clientUdpSocket enableBroadcast:YES error:&error]; + } + + if (![_clientUdpSocket bindToPort:0 error:&error]) + { + return; + } + + if (![_clientUdpSocket beginReceiving:&error]) + { + return; + } +} + +- (void)setupServerUdpSocket { + if (!_serverUdpSocket) { + _serverUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; + [_serverUdpSocket enableBroadcast:YES error:nil]; + } + + NSError *error = nil; + + if (![_serverUdpSocket bindToPort:kAirKiss_Port error:&error]) + { + return; + } + + if (![_serverUdpSocket beginReceiving:&error]) + { + return; + } +} + +#pragma mark - Event Response +- (void)connectFailure { + [_timer invalidate]; + _timer = nil; + + _connectionDone = true; + + if (_connectionFailure) { + _connectionFailure(); + } +} + +#pragma mark - GCDAsyncUdpSocketDelegate +- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag +{ + // You could add checks here +} + +- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error +{ + // You could add checks here +} + +- (void)udpSocket:(GCDAsyncUdpSocket *)sock + didReceiveData:(NSData *)data + fromAddress:(NSData *)address +withFilterContext:(id)filterContext +{ + if (_serverUdpSocket == sock) { + if (_connectionDone) { + return; + } + + // 设备连接WIFI成功后会像10000端口发送至少20个UDP广播包所附带的随机数 + if (data != nil) { + UInt8 *bytes = (UInt8 *) [data bytes]; + if (bytes[0] == _airKissEncoder.randomChar) { + _returnRandomNum ++; + + if (_returnRandomNum >= 2) { + // 成功 + [_timer invalidate]; + _timer = nil; + + if (_returnRandomNum == kAirKiss_Limit_Return_Random_Num) { + _connectionDone = true; + NSData*MacData = [data subdataWithRange:NSMakeRange(1, data.length-1)]; + NSString*macstring = [self.class dataToHexString:MacData]; + if (_connectionSuccess) { + _connectionSuccess(macstring); + } + } + } + } + } + } +} +// NSdata进制转十六进制字符串 ++(NSString *) dataToHexString:(NSData*)data +{ + NSUInteger len = [data length]; + char * chars = (char *)[data bytes]; + NSMutableString * hexString = [[NSMutableString alloc] init]; + + for(NSUInteger i = 0; i < len; i++ ) + [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]]; + return hexString; +} + +// 十六进制字符串转NSdata进制 ++(NSData *) stringToHexData:(NSString*)string{ + + NSInteger len = [string length] / 2; // Target length + unsigned char *buf = malloc(len); + unsigned char *whole_byte = buf; + char byte_chars[3] = {'\0','\0','\0'}; + + int i; + for (i=0; i < [string length] / 2; i++) { + byte_chars[0] = [string characterAtIndex:i*2]; + byte_chars[1] = [string characterAtIndex:i*2+1]; + *whole_byte = strtol(byte_chars, NULL, 16); + whole_byte++; + } + NSData *data = [NSData dataWithBytes:buf length:len]; + free( buf ); + return data; + +} +@end diff --git a/Ifish/airkiss/JMAirKissEncoder.h b/Ifish/airkiss/JMAirKissEncoder.h new file mode 100644 index 0000000..cc458de --- /dev/null +++ b/Ifish/airkiss/JMAirKissEncoder.h @@ -0,0 +1,28 @@ +// +// JMAirKissEncoder.h +// UdpEchoClient +// +// Created by shengxiao on 16/2/27. +// +// + +#import + +@interface JMAirKissEncoder : NSObject +/** + * 创建AirKiss数据 + * + * @param ssid <#ssid description#> + * @param psd <#psd description#> + * + * @return 装载要发送的每条数据的长度的数组 + */ +- (NSMutableArray *)createAirKissEncorderWithSSID:(NSString *)ssid + password:(NSString *)password; + +/** + *@description 随机数 + */ +@property (nonatomic,readonly) UInt8 randomChar; + +@end diff --git a/Ifish/airkiss/JMAirKissEncoder.m b/Ifish/airkiss/JMAirKissEncoder.m new file mode 100644 index 0000000..46cbdcd --- /dev/null +++ b/Ifish/airkiss/JMAirKissEncoder.m @@ -0,0 +1,199 @@ +// +// JMAirKissEncoder.m +// UdpEchoClient +// +// Created by shengxiao on 16/2/27. +// +// + +#import "JMAirKissEncoder.h" + +#define kRandomChar arc4random() % 127 + +@interface JMAirKissEncoder() +{ + NSMutableArray *_mEncodedDatas; + UInt8 _mRandomChar; +} +@end + +@implementation JMAirKissEncoder + +- (instancetype)init { + self = [super init]; + if (self) { + _mEncodedDatas = [NSMutableArray array]; + } + + return self; +} + +/** + * 创建AirKiss数据 + * + * @param ssid <#ssid description#> + * @param psd <#psd description#> + * + * @return 装载要发送的每条数据的长度的数组 + */ +- (NSMutableArray *)createAirKissEncorderWithSSID:(NSString *)ssid + password:(NSString *)password { + [_mEncodedDatas removeAllObjects]; + int times = 5; + _mRandomChar = kRandomChar; + + while (times-- > 0) { + [self getLeadingPart]; + [self getMagicCodeWithSSID:ssid + password:password]; + + for (int i = 0; i < 15; i++) { + [self getPrefixCodeWithPSW:password]; + NSMutableData *data = [NSMutableData dataWithData:[password dataUsingEncoding:NSUTF8StringEncoding]];// 密码 + + [data appendBytes:&_mRandomChar length:1]; // 随机数 + [data appendData:[ssid dataUsingEncoding:NSUTF8StringEncoding]]; // ssid + + int size = 4; + int index = 0; + NSData *tempData = nil; + for (index = 0;index < (data.length / size); index++) { + // 以4为粒度 + tempData = [data subdataWithRange:NSMakeRange(index * size, size)]; + [self getSequenceWithIndex:index + data:tempData]; + } + + if ((data.length % size) != 0) { + tempData = [data subdataWithRange:NSMakeRange(index * size, data.length % size)]; + [self getSequenceWithIndex:index + data:tempData]; + } + } + } + + return _mEncodedDatas; +} + +/** + * 前导域数据 + */ +- (void)getLeadingPart { + for (int i = 0; i < 50; ++i) { + for (int j = 1; j <= 4; ++j) { + [_mEncodedDatas addObject:[NSNumber numberWithInt:j]]; + } + } +} + +/** + * magic code + */ +- (void)getMagicCodeWithSSID:(NSString *)ssid + password:(NSString *)password { + UInt8 length = ssid.length + password.length + 1; + UInt8 magicCode[4] = {0x00,0x00,0x00,0x00}; + magicCode[0] = 0x00 | (length >> 4 & 0xF); + + if (magicCode[0] == 0) { + magicCode[0] = 0x08; + } + + magicCode[1] = 0x10 | (length & 0xF); + + UInt8 *cipherBuffer = (UInt8*)[ssid UTF8String]; + UInt8 crc8 = CRC8(cipherBuffer, (int)ssid.length); + + magicCode[2] = 0x20 | (crc8 >> 4 & 0xF); + magicCode[3] = 0x30 | (crc8 & 0xF); + + for (int i = 0; i < 20; ++i) { + for (int j = 0; j < 4; ++j) { + [_mEncodedDatas addObject:[NSNumber numberWithUnsignedChar:magicCode[j]]]; + } + } +} + +/** + * prefix code + * + * @param psw <#psw description#> + */ +- (void)getPrefixCodeWithPSW:(NSString *)psw { + UInt8 length = psw.length; + UInt8 crc8 = CRC8(&length, 1); + + UInt8 prefixCode[4] = {0x00,0x00,0x00,0x00}; + + prefixCode[0] = 0x40 | (length >> 4 & 0xF); + prefixCode[1] = 0x50 | (length & 0xF); + prefixCode[2] = 0x60 | (crc8 >> 4 & 0xF); + prefixCode[3] = 0x70 | (crc8 & 0xF); + + for (int j = 0; j < 4; ++j) { + [_mEncodedDatas addObject:[NSNumber numberWithUnsignedChar:prefixCode[j]]]; + } +} + +/** + * sequence + * + * @param index <#index description#> + * @param data <#data description#> + */ +- (void)getSequenceWithIndex:(UInt8)index + data:(NSData *)data { + UInt8 newIndex = index & 0xFF; + NSMutableData *mData = [NSMutableData dataWithBytes:&newIndex + length:1]; + [mData appendData:data]; + + UInt8 *originUData = (UInt8 *)[data bytes]; + UInt8 *newUData = (UInt8 *)[mData bytes]; + + UInt8 crc8 = CRC8(newUData, (int)mData.length); + + [_mEncodedDatas addObject:[NSNumber numberWithUnsignedChar:(0x80 | crc8)]]; + [_mEncodedDatas addObject:[NSNumber numberWithUnsignedChar:(0x80 | index)]]; + + for (int i = 0;i < data.length;i++) { + [_mEncodedDatas addObject:[NSNumber numberWithUnsignedShort:(0x100 | originUData[i])]]; + } +} + +/** + * 获取CRC8值 + * + * @param data <#data description#> + * @param len <#len description#> + * + * @return <#return value description#> + */ +UInt8 CRC8(UInt8 * data, int len) +{ + UInt8 cFcs = 0; + int i, j; + + for( i = 0; i < len; i ++ ) { + cFcs ^= data[i]; + for(j = 0; j < 8; j ++) { + if(cFcs & 1) { + cFcs ^= 0x18; /* CRC (X(8) + X(5) + X(4) + 1) */ + cFcs >>= 1; + cFcs |= 0x80; + //cFcs = (BYTE)((cFcs >> 1) ^ AL2_FCS_COEF); + } else { + cFcs >>= 1; + } + } + } + + return cFcs; +} + +#pragma mark - Properties +- (UInt8)randomChar { + return _mRandomChar; +} + +@end diff --git a/Ifish/airkiss/JMAirKiss_Defines.h b/Ifish/airkiss/JMAirKiss_Defines.h new file mode 100644 index 0000000..0a83a81 --- /dev/null +++ b/Ifish/airkiss/JMAirKiss_Defines.h @@ -0,0 +1,64 @@ +// +// Defines.h +// JMAirKiss +// +// Created by shengxiao on 16/3/28. +// Copyright © 2016年 shengxiao. All rights reserved. +// + +#ifndef Defines_h +#define Defines_h + +#define kMagic_Num_0 0xFD +#define kMagic_Num_1 0x01 +#define kMagic_Num_2 0xFE +#define kMagic_Num_3 0xFC + +#define kHead_Length_0 0x00 +#define kHead_Length_1 0x20 + +#define kProto_Version_0 0x00 +#define kProto_Version_1 0x02 + +#define kCMD_Discorvery_Req_0 0x00 +#define kCMD_Discorvery_Req_1 0x00 +#define kCMD_Discorvery_Req_2 0x00 +#define kCMD_Discorvery_Req_3 0x01 + +#define kCMD_Discorvery_Resp_0 0x00 +#define kCMD_Discorvery_Resp_1 0x00 +#define kCMD_Discorvery_Resp_2 0x10 +#define kCMD_Discorvery_Resp_3 0x01 + +#define kCMD_Get_Dev_Pro_Req_0 0x00 +#define kCMD_Get_Dev_Pro_Req_1 0x00 +#define kCMD_Get_Dev_Pro_Req_2 0x00 +#define kCMD_Get_Dev_Pro_Req_3 0x03 + +#define kCMD_Get_Dev_Pro_Resp_0 0x00 +#define kCMD_Get_Dev_Pro_Resp_1 0x00 +#define kCMD_Get_Dev_Pro_Resp_2 0x10 +#define kCMD_Get_Dev_Pro_Resp_3 0x03 + +#define kCMD_User_Dev_Ser_Req_0 0x00 +#define kCMD_User_Dev_Ser_Req_1 0x00 +#define kCMD_User_Dev_Ser_Req_2 0x00 +#define kCMD_User_Dev_Ser_Req_3 0x07 + +#define kCMD_User_Dev_Ser_Resp_0 0x00 +#define kCMD_User_Dev_Ser_Resp_1 0x00 +#define kCMD_User_Dev_Ser_Resp_2 0x10 +#define kCMD_User_Dev_Ser_Resp_3 0x07 + +#define kPrefix_Data_Length 32 + +#define kTotalLength_Start_Index 8 +#define kTotalLength_Bytes_Num 4 + +#define kCMD_Start_Index 12 +#define kCMD_Bytes_Num 4 + +#define kCheckNum_Start_Index 20 +#define kCheckNum_Bytes_Num 4 + +#endif /* Defines_h */