更新EsptouchForIOS

This commit is contained in:
kai60
2020-06-10 20:27:25 +08:00
parent 1f69653c19
commit b8d6ad08ea
42 changed files with 1625 additions and 853 deletions
+44
View File
@@ -0,0 +1,44 @@
//
// ESPDataCode.h
// EspTouchDemo
//
// Created by fby on 4/9/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import <Foundation/Foundation.h>
#define DATA_CODE_LEN 6
/**
* one data format:(data code should have 2 to 65 data)
*
* control byte high 4 bits low 4 bits
* 1st 9bits: 0x0 crc(high) data(high)
* 2nd 9bits: 0x1 sequence header
* 3rd 9bits: 0x0 crc(low) data(low)
*
* sequence header: 0,1,2,...
*
* @author afunx
*
*/
@interface ESPDataCode : NSObject
{
@private
Byte _seqHeader;
@private
Byte _dataHigh;
@private
Byte _dataLow;
@private
Byte _crcHigh;
@private
Byte _crcLow;
}
- (NSData*) getBytes;
- (id) initWithU8: (UInt8) u8 andIndex: (int) index;
@end
+59
View File
@@ -0,0 +1,59 @@
//
// ESPDataCode.m
// EspTouchDemo
//
// Created by fby on 4/9/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import "ESPDataCode.h"
#import "ESP_ByteUtil.h"
#import "ESP_CRC8.h"
#define INDEX_MAX 127
@implementation ESPDataCode
- (id) initWithU8: (UInt8) u8 andIndex: (int) index
{
if (index > INDEX_MAX)
{
perror("index > INDEX_MAX");
}
self = [super init];
if (self)
{
NSData* u8Data = [ESP_ByteUtil splitUint8To2Bytes:u8];
[u8Data getBytes:&_dataHigh range:NSMakeRange(0, 1)];
[u8Data getBytes:&_dataLow range:NSMakeRange(1, 1)];
ESP_CRC8 *crc = [[ESP_CRC8 alloc]init];
[crc updateWithValue:u8];
[crc updateWithValue:index];
NSData* crcData = [ESP_ByteUtil splitUint8To2Bytes:[crc getValue]];
[crcData getBytes:&_crcHigh range:NSMakeRange(0, 1)];
[crcData getBytes:&_crcLow range:NSMakeRange(1, 1)];
_seqHeader = index;
}
return self;
}
- (NSData *) getBytes
{
Byte bytes[DATA_CODE_LEN];
bytes[0] = 0x00;
bytes[1] = [ESP_ByteUtil combine2bytesToOneWithHigh:_crcHigh andLow:_dataHigh];
bytes[2] = 0x01;
bytes[3] = _seqHeader;
bytes[4] = 0x00;
bytes[5] = [ESP_ByteUtil combine2bytesToOneWithHigh:_crcLow andLow:_dataLow];
NSData* data = [[NSData alloc]initWithBytes:bytes length:DATA_CODE_LEN];
return data;
}
- (NSString *)description
{
NSData* data = [self getBytes];
return [ESP_ByteUtil getHexStringByData:data];
}
@end
+38
View File
@@ -0,0 +1,38 @@
//
// ESPDatumCode.h
// EspTouchDemo
//
// Created by fby on 4/9/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ESPDatumCode : NSObject
{
@private
NSMutableArray *_dataCodes;
}
/**
* Constructor of DatumCode
*
* @param apSsid
* the Ap's ssid
* @param apBssid
* the Ap's bssid
* @param apPwd
* the Ap's password ssid
* @param ipAddrData
* the ip address of the phone or pad
* @param isSsidHidden
* whether the Ap's ssid is hidden
*
*/
- (id) initWithSsid: (NSData *) apSsid andApBssid: (NSData *) apBssid andApPwd: (NSData*) apPwd andInetAddrData: (NSData *) ipAddrData andIsSsidHidden: (BOOL) isSsidHidden;
- (NSData *) getBytes;
- (NSData *) getU16s;
@end
+168
View File
@@ -0,0 +1,168 @@
//
// ESPDatumCode.m
// EspTouchDemo
//
// Created by fby on 4/9/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import "ESPDatumCode.h"
#import "ESPDataCode.h"
#import "ESP_ByteUtil.h"
#import "ESP_CRC8.h"
#import "ESP_NetUtil.h"
// define by the Esptouch protocol, all of the datum code should add EXTRA_LEN to prevent 0
#define EXTRA_LEN 40
#define EXTRA_HEAD_LEN 5
@implementation ESPDatumCode
- (id) initWithSsid: (NSData *) apSsid andApBssid: (NSData *) apBssid andApPwd: (NSData*) apPwd andInetAddrData: (NSData *) ipAddrData andIsSsidHidden: (BOOL) isSsidHidden
{
self = [super init];
if (self)
{
// Data = total len(1 byte) + apPwd len(1 byte) + SSID CRC(1 byte) +
// BSSID CRC(1 byte) + TOTAL XOR(1 byte) + ipAddress(4 byte) + apPwd + apSsid apPwdLen <=
// 105 at the moment
// total xor
UInt8 totalXor = 0;
NSData *apPwdBytesData = apPwd;
NSData *apSsidBytesData = apSsid;
Byte apPwdBytes[[apPwdBytesData length]];
Byte apSsidBytes[[apSsidBytesData length]];
[apPwdBytesData getBytes:apPwdBytes length:[apPwdBytesData length]];
[apSsidBytesData getBytes:apSsidBytes length:[apSsidBytesData length]];
Byte apPwdLen = [apPwdBytesData length];
ESP_CRC8 *crc = [[ESP_CRC8 alloc]init];
[crc updateWithBuf:apSsidBytes Nbytes:(int)sizeof(apSsidBytes)];
Byte apSsidCrc = [crc getValue];
[crc reset];
NSData *apBssidData = apBssid;
int apBssidDataLen = (int)[apBssidData length];
Byte apBssidBytes[apBssidDataLen];
[apBssidData getBytes:apBssidBytes length:[apBssidData length]];
[crc updateWithBuf:apBssidBytes Nbytes:apBssidDataLen];
UInt8 apBssidCrc = [crc getValue];
UInt8 apSsidLen = sizeof(apSsidBytes);
// only support ipv4 at the moment
UInt8 ipLen = [ipAddrData length];
Byte ipAddrUint8s[ipLen];
[ipAddrData getBytes:ipAddrUint8s length:[ipAddrData length]];
UInt8 _totalLen = EXTRA_HEAD_LEN + ipLen + apPwdLen + apSsidLen;
UInt8 totalLen = isSsidHidden ? (EXTRA_HEAD_LEN + ipLen + apPwdLen + apSsidLen):(EXTRA_HEAD_LEN + ipLen + apPwdLen);
// build data codes
_dataCodes = [[NSMutableArray alloc]initWithCapacity:totalLen + apBssidDataLen];
ESPDataCode *dataCode = [[ESPDataCode alloc]initWithU8:_totalLen andIndex:0];
[_dataCodes addObject:dataCode];
totalXor ^= _totalLen;
dataCode = [[ESPDataCode alloc]initWithU8:apPwdLen andIndex:1];
[_dataCodes addObject:dataCode];
totalXor ^= apPwdLen;
dataCode = [[ESPDataCode alloc]initWithU8:apSsidCrc andIndex:2];
[_dataCodes addObject:dataCode];
totalXor ^= apSsidCrc;
dataCode = [[ESPDataCode alloc]initWithU8:apBssidCrc andIndex:3];
[_dataCodes addObject:dataCode];
totalXor ^= apBssidCrc;
// ESPDataCode 4 is nil
for (int i = 0; i < ipLen; i++)
{
dataCode = [[ESPDataCode alloc]initWithU8:ipAddrUint8s[i] andIndex:i + EXTRA_HEAD_LEN];
[_dataCodes addObject:dataCode];
totalXor ^= ipAddrUint8s[i];
}
for (int i = 0; i < apPwdLen; i++)
{
dataCode = [[ESPDataCode alloc]initWithU8:apPwdBytes[i] andIndex:i + EXTRA_HEAD_LEN + ipLen];
[_dataCodes addObject:dataCode];
totalXor ^= apPwdBytes[i];
}
// totalXor will xor apSsidChars no matter whether the ssid is hidden
for (int i = 0; i < apSsidLen; i++)
{
totalXor ^= apSsidBytes[i];
}
if (isSsidHidden)
{
for (int i = 0; i < apSsidLen; i++)
{
dataCode = [[ESPDataCode alloc]initWithU8:apSsidBytes[i] andIndex:i + EXTRA_HEAD_LEN + ipLen + apPwdLen];
[_dataCodes addObject:dataCode];
}
}
// add total xor last
dataCode = [[ESPDataCode alloc]initWithU8:totalXor andIndex:4];
[_dataCodes insertObject:dataCode atIndex:4];
// add bssid
NSUInteger bssidInsertIndex = EXTRA_HEAD_LEN;
for (int i = 0; i < apBssidDataLen; i++) {
int index = totalLen + i;
Byte b = apBssidBytes[i];
ESPDataCode *dc = [[ESPDataCode alloc] initWithU8:b andIndex:index];
if (bssidInsertIndex >= _dataCodes.count) {
[_dataCodes addObject:dc];
} else {
[_dataCodes insertObject:dc atIndex:bssidInsertIndex];
}
bssidInsertIndex += 4;
}
}
return self;
}
- (NSData *) getBytes
{
Byte datumCode[[_dataCodes count] * DATA_CODE_LEN];
for (int i = 0; i < [_dataCodes count]; i++)
{
ESPDataCode *dataCode = [_dataCodes objectAtIndex:i];
NSData *bytesData = [dataCode getBytes];
Byte bytes[DATA_CODE_LEN];
[bytesData getBytes:bytes length:DATA_CODE_LEN];
void *dest = datumCode + i * DATA_CODE_LEN * sizeof(Byte);
void *src = bytes;
memcpy(dest, src, DATA_CODE_LEN);
}
return [[NSData alloc]initWithBytes:datumCode length:sizeof(datumCode)];
}
- (NSData *) getU16s
{
NSData *dataBytes = [self getBytes];
NSInteger totalLen = [dataBytes length];
Byte bytes[totalLen];
[dataBytes getBytes:bytes length:totalLen];
NSInteger len = totalLen / 2;
UInt16 dataU16s[len];
Byte high, low;
for (int i = 0; i < len; i++)
{
high = bytes[i * 2];
low = bytes[i * 2 + 1];
dataU16s[i] = [ESP_ByteUtil combine2bytesToU16WithHigh:high andLow:low] + EXTRA_LEN;
}
return [[NSData alloc]initWithBytes:dataU16s length:totalLen];
}
- (NSString *)description
{
NSData* data = [self getBytes];
return [ESP_ByteUtil getHexStringByData:data];
}
@end
+15
View File
@@ -0,0 +1,15 @@
//
// ESPGuideCode.h
// EspTouchDemo
//
// Created by fby on 4/9/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ESPGuideCode : NSObject
- (NSData *) getU16s;
@end
+34
View File
@@ -0,0 +1,34 @@
//
// ESPGuideCode.m
// EspTouchDemo
//
// Created by fby on 4/9/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import "ESPGuideCode.h"
#import "ESP_ByteUtil.h"
#define GUIDE_CODE_LEN 4
@implementation ESPGuideCode
- (NSData *) getU16s
{
UInt16 guideU16s[GUIDE_CODE_LEN];
guideU16s[0] = 515;
guideU16s[1] = 514;
guideU16s[2] = 513;
guideU16s[3] = 512;
NSData* data = [[NSData alloc]initWithBytes:guideU16s length:GUIDE_CODE_LEN*2];
return data;
}
- (NSString *)description
{
NSData* data = [self getU16s];
return [ESP_ByteUtil getHexStringByData:data];
}
@end
@@ -0,0 +1,46 @@
//
// ESPTouchGenerator.h
// EspTouchDemo
//
// Created by fby on 4/9/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ESPTouchGenerator : NSObject
{
@private
NSMutableArray *_gcBytes2;
@private
NSMutableArray *_dcBytes2;
}
/**
* Constructor of EsptouchGenerator, it will cost some time(maybe a bit much)
*
* @param apSsid
* the Ap's ssid
* @param apBssid
* the Ap's bssid
* @param apPwd
* the Ap's password
* @param ipAddrData
* the ip address of the phone or pad
* @param isSsidHidden
* whether the Ap's ssid is hidden
*/
- (id) initWithSsid: (NSData *)apSsid andApBssid: (NSData *)apBssid andApPassword: (NSData *)apPwd andInetAddrData: (NSData *)ipAddrData andIsSsidHidden: (BOOL)isSsidHidden;
/**
* Get guide code by the format of byte[][]
* @return guide code by the format of byte[][]
*/
- (NSArray *) getGCBytes2;
/**
* Get data code by the format of byte[][]
* @return data code by the format of byte[][]
*/
- (NSArray *) getDCBytes2;
@end
@@ -0,0 +1,60 @@
//
// ESPTouchGenerator.m
// EspTouchDemo
//
// Created by fby on 4/9/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import "ESPTouchGenerator.h"
#import "ESP_ByteUtil.h"
#import "ESPGuideCode.h"
#import "ESPDatumCode.h"
@implementation ESPTouchGenerator
- (id) initWithSsid: (NSData *)apSsid andApBssid: (NSData *)apBssid andApPassword: (NSData *)apPwd andInetAddrData: (NSData *)ipAddrData andIsSsidHidden:(BOOL) isSsidHidden
{
self = [super init];
if (self)
{
// generate guide code
ESPGuideCode *gc = [[ESPGuideCode alloc]init];
NSData *gcData1 = [gc getU16s];
NSUInteger gcData1Len = [gcData1 length];
UInt16 gcU16_1[gcData1Len/2];
[gcData1 getBytes:gcU16_1 length:gcData1Len];
_gcBytes2 = [[NSMutableArray alloc]initWithCapacity:gcData1Len];
for (int i = 0; i < gcData1Len/2; i++)
{
NSData* data = [ESP_ByteUtil genSpecBytesWithU16:gcU16_1[i]];
[_gcBytes2 addObject:data];
}
// generate data code
ESPDatumCode *dc = [[ESPDatumCode alloc]initWithSsid:apSsid andApBssid:apBssid andApPwd:apPwd andInetAddrData:ipAddrData andIsSsidHidden:isSsidHidden];
NSData *dcData1 = [dc getU16s];
NSUInteger dcDataLen = [dcData1 length];
UInt16 dcU16_1[dcDataLen/2];
[dcData1 getBytes:dcU16_1 length:dcDataLen];
_dcBytes2 = [[NSMutableArray alloc]initWithCapacity:dcDataLen];
for (int i = 0; i < dcDataLen/2; i++)
{
NSData* data = [ESP_ByteUtil genSpecBytesWithU16:dcU16_1[i]];
[_dcBytes2 addObject:data];
}
}
return self;
}
- (NSArray *) getGCBytes2
{
return _gcBytes2;
}
- (NSArray *) getDCBytes2
{
return _dcBytes2;
}
@end