更新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
+24
View File
@@ -0,0 +1,24 @@
//
// ESPTouchDelegate.h
// EspTouchDemo
//
// Created by fby on 8/14/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ESPTouchResult.h"
@protocol ESPTouchDelegate <NSObject>
/**
* when new esptouch result is added, the listener will call
* onEsptouchResultAdded callback
*
* @param result
* the Esptouch result
*/
-(void) onEsptouchResultAddedWithResult: (ESPTouchResult *) result;
@end
+36
View File
@@ -0,0 +1,36 @@
//
// ESPTouchResult.h
// EspTouchDemo
//
// Created by fby on 4/14/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ESPTouchResult : NSObject
// it is used to check whether the esptouch task is executed suc
@property (nonatomic,assign) BOOL isSuc;
// it is used to store the device's bssid
@property (nonatomic,strong) NSString * bssid;
// it is used to check whether the esptouch task is cancelled by user
@property (atomic,assign) BOOL isCancelled;
// it is used to store the device's ip address
@property (atomic) NSData * ipAddrData;
/**
* Constructor of EsptouchResult
*
* @param isSuc whether the esptouch task is executed suc
* @param bssid the device's bssid
* @param ipAddrData the device's ip address
*/
- (id) initWithIsSuc: (BOOL) isSuc andBssid: (NSString *) bssid andInetAddrData: (NSData *) ipAddrData;
- (NSString *) getAddressString;
@end
+44
View File
@@ -0,0 +1,44 @@
//
// ESPTouchResult.m
// EspTouchDemo
//
// Created by fby on 4/14/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import "ESPTouchResult.h"
#import "ESP_NetUtil.h"
@implementation ESPTouchResult
- (id) initWithIsSuc: (BOOL) isSuc andBssid: (NSString *) bssid andInetAddrData: (NSData *) ipAddrData
{
self = [super init];
if (self)
{
self.isSuc = isSuc;
self.bssid = bssid;
self.isCancelled = NO;
self.ipAddrData = ipAddrData;
}
return self;
}
- (NSString *)getAddressString {
NSString *ipAddrDataStr = [ESP_NetUtil descriptionInetAddr4ByData:self.ipAddrData];
if (ipAddrDataStr==nil) {
ipAddrDataStr = [ESP_NetUtil descriptionInetAddr6ByData:self.ipAddrData];
}
return ipAddrDataStr;
}
- (NSString *)description
{
NSString *ipAddrDataStr = [self getAddressString];
return [[NSString alloc]initWithFormat:@"[isSuc: %@,isCancelled: %@,bssid: %@,inetAddress: %@]",self.isSuc? @"YES":@"NO",
self.isCancelled? @"YES":@"NO"
,self.bssid
,ipAddrDataStr];
}
@end
+115
View File
@@ -0,0 +1,115 @@
//
// ESPTouchTask.h
// EspTouchDemo
//
// Created by fby on 4/14/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ESPTouchResult.h"
#import "ESPTouchDelegate.h"
#import "ESPAES.h"
#define ESPTOUCH_VERSION @"SDK-v1.0.0"
#define DEBUG_ON YES
@interface ESPTouchTask : NSObject
@property (atomic,assign) BOOL isCancelled;
- (id)initWithApSsid:(NSString *)apSsid andApBssid:(NSString *)apBssid andApPwd:(NSString *)apPwd andAES:(ESPAES *)aes;
/**
* Constructor of EsptouchTask
*
* @param apSsid
* the Ap's ssid
* @param apBssid
* the Ap's bssid
* @param apPwd
* the Ap's password
*/
- (id) initWithApSsid: (NSString *)apSsid andApBssid: (NSString *) apBssid andApPwd: (NSString *)apPwd;
/**
* Deprecated
*/
- (id) initWithApSsid: (NSString *)apSsid andApBssid: (NSString *) apBssid andApPwd: (NSString *)apPwd andIsSsidHiden: (BOOL) isSsidHidden __deprecated_msg("Use initWithApSsid:(NSString *) andApBssid:(NSString *) andApPwd:(NSString *) instead.");
/**
* Constructor of EsptouchTask
*
* @param apSsid
* the Ap's ssid
* @param apBssid
* the Ap's bssid
* @param apPwd
* the Ap's password
* @param timeoutMillisecond (it should be >= 15000+6000)
* millisecond of total timeout
*/
- (id) initWithApSsid: (NSString *)apSsid andApBssid: (NSString *) apBssid andApPwd: (NSString *)apPwd andTimeoutMillisecond: (int) timeoutMillisecond;
/**
* Constructor of EsptouchTask
*
* @param apSsid
* the Ap's ssid
* @param apBssid
* the Ap's bssid
* @param apPwd
* the Ap's password
* @param isSsidHidden
* whether the Ap's ssid is hidden
* @param timeoutMillisecond (it should be >= 15000+6000)
* millisecond of total timeout
*/
- (id) initWithApSsid: (NSString *)apSsid andApBssid: (NSString *) apBssid andApPwd: (NSString *)apPwd andIsSsidHiden: (BOOL) isSsidHidden andTimeoutMillisecond: (int) timeoutMillisecond __deprecated_msg("Use initWithApSsid:(NSString *) andApBssid:(NSString *) andApPwd:(NSString *) andTimeoutMillisecond:(int) instead.");
/**
* Interrupt the Esptouch Task when User tap back or close the Application.
*/
- (void) interrupt;
/**
* Note: !!!Don't call the task at UI Main Thread
*
* Smart Config v2.4 support the API
*
* @return the ESPTouchResult
*/
- (ESPTouchResult*) executeForResult;
/**
* Note: !!!Don't call the task at UI Main Thread
*
* Smart Config v2.4 support the API
*
* It will be blocked until the client receive result count >= expectTaskResultCount.
* If it fail, it will return one fail result will be returned in the list.
* If it is cancelled while executing,
* if it has received some results, all of them will be returned in the list.
* if it hasn't received any results, one cancel result will be returned in the list.
*
* @param expectTaskResultCount
* the expect result count(if expectTaskResultCount <= 0,
* expectTaskResultCount = INT32_MAX)
* @return the NSArray of EsptouchResult
* @throws RuntimeException
*/
- (NSArray*) executeForResults:(int) expectTaskResultCount;
/**
* set the esptouch delegate, when one device is connected to the Ap, it will be called back
* @param esptouchDelegate when one device is connected to the Ap, it will be called back
*/
- (void) setEsptouchDelegate: (NSObject<ESPTouchDelegate> *) esptouchDelegate;
/**
* Set boradcast or multicast when post config info
* @param broadcast YES is boradcast, NO is multicast
*/
- (void) setPackageBroadcast: (BOOL) broadcast;
@end
+520
View File
@@ -0,0 +1,520 @@
//
// ESPTouchTask.m
// EspTouchDemo
//
// Created by fby on 4/14/15.
// Copyright (c) 2015 fby. All rights reserved.
//
// The usage of NSCondition refer to: https://gist.github.com/prachigauriar/8118909
#import "ESPTouchTask.h"
#import "ESP_ByteUtil.h"
#import "ESPTouchGenerator.h"
#import "ESPUDPSocketClient.h"
#import "ESPUDPSocketServer.h"
#import "ESP_NetUtil.h"
#import "ESPTouchTaskParameter.h"
#define ONE_DATA_LEN 3
@interface ESPTouchTask ()
@property (nonatomic,strong) NSData *_apSsid;
@property (nonatomic,strong) NSData *_apBssid;
@property (nonatomic,strong) NSData *_apPwd;
@property (atomic,assign) BOOL _isSuc;
@property (atomic,assign) BOOL _isInterrupt;
@property (nonatomic,strong) ESPUDPSocketClient *_client;
@property (nonatomic,strong) ESPUDPSocketServer *_server;
@property (atomic,strong) NSMutableArray *_esptouchResultArray;
@property (atomic,strong) NSCondition *_condition;
@property (nonatomic,assign) __block BOOL _isWakeUp;
@property (nonatomic,assign) volatile BOOL _isExecutedAlready;
@property (nonatomic,assign) BOOL _isSsidHidden;
@property (nonatomic,strong) ESPTaskParameter *_parameter;
@property (atomic,strong) NSMutableDictionary *_bssidTaskSucCountDict;
@property (atomic,strong) NSCondition *_esptouchResultArrayCondition;
@property (nonatomic,assign) __block UIBackgroundTaskIdentifier _backgroundTask;
@property (nonatomic,strong) id<ESPTouchDelegate> _esptouchDelegate;
@property (nonatomic,strong) NSData *_localInetAddrData;
@end
@implementation ESPTouchTask
- (id)initWithApSsid:(NSString *)apSsid andApBssid:(NSString *)apBssid andApPwd:(NSString *)apPwd andAES:(ESPAES *)aes
{
self = [super init];
NSLog(@"Welcome Esptouch %@",ESPTOUCH_VERSION);
if (apSsid==nil||[apSsid isEqualToString:@""])
{
perror("ESPTouchTask initWithApSsid() apSsid shouldn't be null or empty");
return self;
}
// the apSsid should be null or empty
assert(apSsid!=nil&&![apSsid isEqualToString:@""]);
if (apPwd == nil)
{
apPwd = @"";
}
if (self)
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask init");
}
if (aes == nil) {
self._apSsid = [ESP_ByteUtil getBytesByNSString:apSsid];
self._apPwd = [ESP_ByteUtil getBytesByNSString:apPwd];
} else {
self._apSsid = [aes AES128EncryptData:[ESP_ByteUtil getBytesByNSString:apSsid]];
self._apPwd = [aes AES128EncryptData:[ESP_ByteUtil getBytesByNSString:apPwd]];
}
self._apBssid = [ESP_NetUtil parseBssid2bytes:apBssid];
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];
// create udp client and udp server
self._client = [[ESPUDPSocketClient alloc]init];
self._server = [[ESPUDPSocketServer alloc]initWithPort: [self._parameter getPortListening]
AndSocketTimeout: [self._parameter getWaitUdpTotalMillisecond]];
// update listening port for IPv6
[self._parameter setListeningPort6:self._server.port];
if (DEBUG_ON) {
NSLog(@"ESPTouchTask app server port is %d",self._server.port);
}
if (localInetAddr4!=nil) {
self._localInetAddrData = [ESP_NetUtil getLocalInetAddress4ByAddr:localInetAddr4];
} else {
int localPort = [self._parameter getPortListening];
self._localInetAddrData = [ESP_NetUtil getLocalInetAddress6ByPort:localPort];
}
if (DEBUG_ON)
{
// for ESPTouchGenerator only receive 4 bytes for local address no matter IPv4 or IPv6
NSLog(@"ESPTouchTask executeForResult() localInetAddr: %@", [ESP_NetUtil descriptionInetAddr4ByData:self._localInetAddrData]);
}
self._isSuc = NO;
self._isInterrupt = NO;
self._isWakeUp = NO;
self._isExecutedAlready = NO;
self._condition = [[NSCondition alloc]init];
self._isSsidHidden = YES;
self._esptouchResultArray = [[NSMutableArray alloc]init];
self._bssidTaskSucCountDict = [[NSMutableDictionary alloc]init];
self._esptouchResultArrayCondition = [[NSCondition alloc]init];
}
return self;
}
- (id) initWithApSsid: (NSString *)apSsid andApBssid: (NSString *) apBssid andApPwd: (NSString *)apPwd {
return [self initWithApSsid:apSsid andApBssid:apBssid andApPwd:apPwd andAES:nil];
}
- (id) initWithApSsid: (NSString *)apSsid andApBssid: (NSString *) apBssid andApPwd: (NSString *)apPwd andIsSsidHiden: (BOOL) isSsidHidden
{
return [self initWithApSsid:apSsid andApBssid:apBssid andApPwd:apPwd];
}
- (id) initWithApSsid: (NSString *)apSsid andApBssid: (NSString *) apBssid andApPwd: (NSString *)apPwd andTimeoutMillisecond: (int) timeoutMillisecond
{
ESPTouchTask *_self = [self initWithApSsid:apSsid andApBssid:apBssid andApPwd:apPwd];
if (_self)
{
[_self._parameter setWaitUdpTotalMillisecond:timeoutMillisecond];
}
return _self;
}
- (id) initWithApSsid: (NSString *)apSsid andApBssid: (NSString *) apBssid andApPwd: (NSString *)apPwd andIsSsidHiden: (BOOL) isSsidHidden andTimeoutMillisecond: (int) timeoutMillisecond
{
return [self initWithApSsid:apSsid andApBssid:apBssid andApPwd:apPwd andTimeoutMillisecond:timeoutMillisecond];
}
- (void) __putEsptouchResultIsSuc: (BOOL) isSuc AndBssid: (NSString *)bssid AndInetAddr:(NSData *)inetAddr
{
[self._esptouchResultArrayCondition lock];
// check whether the result receive enough UDP response
BOOL isTaskSucCountEnough = NO;
NSNumber *countNumber = [self._bssidTaskSucCountDict objectForKey:bssid];
int count = 0;
if (countNumber != nil)
{
count = [countNumber intValue];
}
++count;
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __putEsptouchResult(): count = %d",count);
}
countNumber = [[NSNumber alloc]initWithInt:count];
[self._bssidTaskSucCountDict setObject:countNumber forKey:bssid];
isTaskSucCountEnough = count >= [self._parameter getThresholdSucBroadcastCount];
if (!isTaskSucCountEnough)
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __putEsptouchResult(): count = %d, isn't enough", count);
}
[self._esptouchResultArrayCondition unlock];
return;
}
// check whether the result is in the mEsptouchResultList already
BOOL isExist = NO;
for (id esptouchResultId in self._esptouchResultArray)
{
ESPTouchResult *esptouchResultInArray = esptouchResultId;
if ([esptouchResultInArray.bssid isEqualToString:bssid])
{
isExist = YES;
break;
}
}
// only add the result who isn't in the mEsptouchResultList
if (!isExist)
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __putEsptouchResult(): put one more result");
}
ESPTouchResult *esptouchResult = [[ESPTouchResult alloc]initWithIsSuc:isSuc andBssid:bssid andInetAddrData:inetAddr];
[self._esptouchResultArray addObject:esptouchResult];
if (self._esptouchDelegate != nil)
{
[self._esptouchDelegate onEsptouchResultAddedWithResult:esptouchResult];
}
}
[self._esptouchResultArrayCondition unlock];
}
-(NSArray *) __getEsptouchResultList
{
[self._esptouchResultArrayCondition lock];
if ([self._esptouchResultArray count] == 0)
{
ESPTouchResult *esptouchResult = [[ESPTouchResult alloc]initWithIsSuc:NO andBssid:nil andInetAddrData:nil];
esptouchResult.isCancelled = self.isCancelled;
[self._esptouchResultArray addObject:esptouchResult];
}
[self._esptouchResultArrayCondition unlock];
return self._esptouchResultArray;
}
- (void) beginBackgroundTask
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask beginBackgroundTask() entrance");
}
self._backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask beginBackgroundTask() endBackgroundTask");
}
[self endBackgroundTask];
}];
}
- (void) endBackgroundTask
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask endBackgroundTask() entrance");
}
[[UIApplication sharedApplication] endBackgroundTask: self._backgroundTask];
self._backgroundTask = UIBackgroundTaskInvalid;
}
- (void) __listenAsyn: (const int) expectDataLen
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
[self beginBackgroundTask];
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __listenAsyn() start an asyn listen task, current thread is: %@", [NSThread currentThread]);
}
NSTimeInterval startTimestamp = [[NSDate date] timeIntervalSince1970];
// NSString *apSsidAndPwd = [NSString stringWithFormat:@"%@%@",self._apSsid,self._apPwd];
Byte expectOneByte = [self._apSsid length] + [self._apPwd length] + 9;
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __listenAsyn() expectOneByte: %d",expectOneByte);
}
Byte receiveOneByte = -1;
NSData *receiveData = nil;
while ([self._esptouchResultArray count] < [self._parameter getExpectTaskResultCount] && !self._isInterrupt)
{
if ([self._parameter isIPv4Supported]) {
receiveData = [self._server receiveSpecLenBytes4:expectDataLen];
} else {
receiveData = [self._server receiveSpecLenBytes6:expectDataLen];
}
if (receiveData != nil)
{
[receiveData getBytes:&receiveOneByte length:1];
}
else
{
receiveOneByte = -1;
}
if (receiveOneByte == expectOneByte)
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __listenAsyn() receive correct broadcast");
}
// change the socket's timeout
NSTimeInterval consume = [[NSDate date] timeIntervalSince1970] - startTimestamp;
int timeout = (int)([self._parameter getWaitUdpTotalMillisecond] - consume*1000);
if (timeout < 0)
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __listenAsyn() esptouch timeout");
}
break;
}
else
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __listenAsyn() socketServer's new timeout is %d milliseconds",timeout);
}
[self._server setSocketTimeout:timeout];
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __listenAsyn() receive correct broadcast");
}
if (receiveData != nil)
{
NSString *bssid =
[ESP_ByteUtil parseBssid:(Byte *)[receiveData bytes]
Offset:[self._parameter getEsptouchResultOneLen]
Count:[self._parameter getEsptouchResultMacLen]];
NSData *inetAddrData =
[ESP_NetUtil parseInetAddrByData:receiveData
andOffset:[self._parameter getEsptouchResultOneLen] + [self._parameter getEsptouchResultMacLen]
andCount:[self._parameter getEsptouchResultIpLen]];
[self __putEsptouchResultIsSuc:YES AndBssid:bssid AndInetAddr:inetAddrData];
}
}
}
else
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __listenAsyn() receive rubbish message, just ignore");
}
}
}
self._isSuc = [self._esptouchResultArray count] >= [self._parameter getExpectTaskResultCount];
[self __interrupt];
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __listenAsyn() finish");
}
[self endBackgroundTask];
});
}
- (void) interrupt
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask interrupt()");
}
self.isCancelled = YES;
[self __interrupt];
}
- (void) __interrupt
{
self._isInterrupt = YES;
[self._client interrupt];
[self._server interrupt];
// notify the ESPTouchTask to wake up from sleep mode
[self __notify];
}
- (BOOL) __execute: (ESPTouchGenerator *)generator
{
NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
NSTimeInterval currentTime = startTime;
NSTimeInterval lastTime = currentTime - [self._parameter getTimeoutTotalCodeMillisecond];
NSArray *gcBytes2 = [generator getGCBytes2];
NSArray *dcBytes2 = [generator getDCBytes2];
int index = 0;
while (!self._isInterrupt)
{
if (currentTime - lastTime >= [self._parameter getTimeoutTotalCodeMillisecond]/1000.0)
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __execute() send gc code ");
}
// send guide code
while (!self._isInterrupt && [[NSDate date] timeIntervalSince1970] - currentTime < [self._parameter getTimeoutGuideCodeMillisecond]/1000.0)
{
[self._client sendDataWithBytesArray2:gcBytes2
ToTargetHostName:[self._parameter getTargetHostname]
WithPort:[self._parameter getTargetPort]
andInterval:[self._parameter getIntervalGuideCodeMillisecond]];
// check whether the udp is send enough time
if ([[NSDate date] timeIntervalSince1970] - startTime > [self._parameter getWaitUdpSendingMillisecond]/1000.0)
{
break;
}
}
lastTime = currentTime;
}
else
{
[self._client sendDataWithBytesArray2:dcBytes2
Offset:index
Count:ONE_DATA_LEN
ToTargetHostName:[self._parameter getTargetHostname]
WithPort:[self._parameter getTargetPort]
andInterval:[self._parameter getIntervalDataCodeMillisecond]];
index = (index + ONE_DATA_LEN) % [dcBytes2 count];
}
currentTime = [[NSDate date] timeIntervalSince1970];
// check whether the udp is send enough time
if ([[NSDate date] timeIntervalSince1970] - startTime > [self._parameter getWaitUdpSendingMillisecond]/1000.0)
{
break;
}
}
return self._isSuc;
}
- (void) __checkTaskValid
{
if (self._isExecutedAlready)
{
perror("ESPTouchTask __checkTaskValid() fail, the task could be executed only once");
}
// !!!NOTE: the esptouch task could be executed only once
assert(!self._isExecutedAlready);
self._isExecutedAlready = YES;
}
- (ESPTouchResult *) executeForResult
{
return [[self executeForResults:1] objectAtIndex:0];
}
- (NSArray*) executeForResults:(int) expectTaskResultCount
{
// set task result count
if (expectTaskResultCount <= 0)
{
expectTaskResultCount = INT32_MAX;
}
[self._parameter setExpectTaskResultCount:expectTaskResultCount];
[self __checkTaskValid];
// generator the esptouch byte[][] to be transformed, which will cost
// some time(maybe a bit much)
ESPTouchGenerator *generator = [[ESPTouchGenerator alloc]initWithSsid:self._apSsid andApBssid:self._apBssid andApPassword:self._apPwd andInetAddrData:self._localInetAddrData andIsSsidHidden:self._isSsidHidden];
// listen the esptouch result asyn
[self __listenAsyn:[self._parameter getEsptouchResultTotalLen]];
BOOL isSuc = NO;
for (int i = 0; i < [self._parameter getTotalRepeatTime]; i++)
{
isSuc = [self __execute:generator];
if (isSuc)
{
return [self __getEsptouchResultList];
}
}
if (!self._isInterrupt)
{
[self __sleep: [self._parameter getWaitUdpReceivingMillisecond]];
[self __interrupt];
}
return [self __getEsptouchResultList];
}
// sleep some milliseconds
- (BOOL) __sleep :(long) milliseconds
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __sleep() start");
}
NSDate *date = [NSDate dateWithTimeIntervalSinceNow: milliseconds/1000.0];
[self._condition lock];
BOOL signaled = NO;
while (!self._isWakeUp && (signaled = [self._condition waitUntilDate:date]))
{
}
[self._condition unlock];
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __sleep() end, receive signal is %@", signaled ? @"YES" : @"NO");
}
return signaled;
}
// notify the sleep thread to wake up
- (void) __notify
{
if (DEBUG_ON)
{
NSLog(@"ESPTouchTask __notify()");
}
[self._condition lock];
self._isWakeUp = YES;
[self._condition signal];
[self._condition unlock];
}
- (void) setEsptouchDelegate: (NSObject<ESPTouchDelegate> *) esptouchDelegate
{
self._esptouchDelegate = esptouchDelegate;
}
- (void)setPackageBroadcast:(BOOL)broadcast {
[self._parameter setBroadcast:broadcast];
}
@end
+173
View File
@@ -0,0 +1,173 @@
//
// ESPTaskParameter.h
// EspTouchDemo
//
// Created by fby on 5/20/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ESPTaskParameter : NSObject
/**
* get interval millisecond for guide code(the time between each guide code sending)
* @return interval millisecond for guide code(the time between each guide code sending)
*/
- (long) getIntervalGuideCodeMillisecond;
/**
* get interval millisecond for data code(the time between each data code sending)
* @return interval millisecond for data code(the time between each data code sending)
*/
- (long) getIntervalDataCodeMillisecond;
/**
* get timeout millisecond for guide code(the time how much the guide code sending)
* @return timeout millisecond for guide code(the time how much the guide code sending)
*/
- (long) getTimeoutGuideCodeMillisecond;
/**
* get timeout millisecond for data code(the time how much the data code sending)
* @return timeout millisecond for data code(the time how much the data code sending)
*/
- (long) getTimeoutDataCodeMillisecond;
/**
* get timeout millisecond for total code(guide code and data code altogether)
* @return timeout millisecond for total code(guide code and data code altogether)
*/
- (long) getTimeoutTotalCodeMillisecond;
/**
* get total repeat time for executing esptouch task
* @return total repeat time for executing esptouch task
*/
- (int) getTotalRepeatTime;
/**
* the length of the Esptouch result 1st byte is the total length of ssid and
* password, the other 6 bytes are the device's bssid
*/
/**
* get esptouchResult length of one
* @return length of one
*/
- (int) getEsptouchResultOneLen;
/**
* get esptouchResult length of mac
* @return length of mac
*/
- (int) getEsptouchResultMacLen;
/**
* get esptouchResult length of ip
* @return length of ip
*/
- (int) getEsptouchResultIpLen;
/**
* get esptouchResult total length
* @return total length
*/
- (int) getEsptouchResultTotalLen;
/**
* get port for listening(used by server)
* @return port for listening(used by server)
*/
- (int) getPortListening;
/**
* get target hostname
* @return target hostame(used by client)
*/
- (NSString *) getTargetHostname;
/**
* get target port
* @return target port(used by client)
*/
- (int) getTargetPort;
/**
* get millisecond for waiting udp receiving(receiving without sending)
* @return millisecond for waiting udp receiving(receiving without sending)
*/
- (int) getWaitUdpReceivingMillisecond;
/**
* get millisecond for waiting udp sending(sending including receiving)
* @return millisecond for waiting udep sending(sending including receiving)
*/
- (int) getWaitUdpSendingMillisecond;
/**
* get millisecond for waiting udp sending and receiving
* @return millisecond for waiting udp sending and receiving
*/
- (int) getWaitUdpTotalMillisecond;
/**
* get the threshold for how many correct broadcast should be received
* @return the threshold for how many correct broadcast should be received
*/
- (int) getThresholdSucBroadcastCount;
/**
* set the millisecond for waiting udp sending and receiving
* @param waitUdpTotalMillisecond the millisecond for waiting udp sending and receiving
*/
- (void) setWaitUdpTotalMillisecond: (int) waitUdpTotalMillisecond;
/**
* get the count of expect task results
* @return the count of expect task results
*/
- (int) getExpectTaskResultCount;
/**
* set the count of expect task results
* @param expectTaskResultCount the count of expect task results
*/
- (void) setExpectTaskResultCount: (int) expectTaskResultCount;
/**
* get whether the router support IPv4
* @return whether the router support IPv4
*/
- (BOOL) isIPv4Supported;
/**
* set whether the router support IPv4
* @param isIPv4Supported whether the router support IPv4
*/
- (void) setIsIPv4Supported:(BOOL) isIPv4Supported;
/**
* get whether the router support IPv6
* @return whether the router support IPv6
*/
- (BOOL) isIPv6Supported;
/**
* set whether the router support IPv6
* @param isIPv6Supported whether the router support IPv6
*/
- (void) setIsIPv6Supported:(BOOL) isIPv6Supported;
/**
* set listening port for IPv6
*/
- (void) setListeningPort6:(int) listeningPort6;
/**
* Set broadcast or multicast
*/
- (void) setBroadcast:(BOOL) broadcast;
@end
+239
View File
@@ -0,0 +1,239 @@
//
// ESPTaskParameter.m
// EspTouchDemo
//
// Created by fby on 5/20/15.
// Copyright (c) 2015 fby. All rights reserved.
//
#import "ESPTouchTaskParameter.h"
@interface ESPTaskParameter()
@property (nonatomic,assign) long intervalGuideCodeMillisecond;
@property (nonatomic,assign) long intervalDataCodeMillisecond;
@property (nonatomic,assign) long timeoutGuideCodeMillisecond;
@property (nonatomic,assign) long timeoutDataCodeMillisecond;
@property (nonatomic,assign) long timeoutTotalCodeMillisecond;
@property (nonatomic,assign) int totalRepeatTime;
@property (nonatomic,assign) int esptouchResultOneLen;
@property (nonatomic,assign) int esptouchResultMacLen;
@property (nonatomic,assign) int esptouchResultIpLen4;
@property (nonatomic,assign) int esptouchResultIpLen6;
@property (nonatomic,assign) int esptouchResultTotalLen4;
@property (nonatomic,assign) int esptouchResultTotalLen6;
@property (nonatomic,assign) int portListening4;
@property (nonatomic,assign) int portListening6;
@property (nonatomic,assign) int targetPort4;
@property (nonatomic,assign) int targetPort6;
@property (nonatomic,assign) int waitUdpReceivingMillisecond;
@property (nonatomic,assign) int waitUdpSendingMillisecond;
@property (nonatomic,assign) int thresholdSucBroadcastCount;
@property (nonatomic,assign) int expectTaskResultCount;
@property (nonatomic,assign) BOOL isIPv4Supported0;
@property (nonatomic,assign) BOOL isIPv6Supported0;
@property (nonatomic,assign) BOOL broadcast;
@end
@implementation ESPTaskParameter
static int _datagramCount = 0;
- (id) init
{
self = [super init];
if (self) {
self.intervalGuideCodeMillisecond = 8;
self.intervalDataCodeMillisecond = 8;
self.timeoutGuideCodeMillisecond = 2000;
self.timeoutDataCodeMillisecond = 4000;
self.timeoutTotalCodeMillisecond = 2000 + 4000;
self.totalRepeatTime = 1;
self.esptouchResultOneLen = 1;
self.esptouchResultMacLen = 6;
self.esptouchResultIpLen4 = 4;
self.esptouchResultIpLen6 = 16;
self.esptouchResultTotalLen4 = 1 + 6 + 4;
self.esptouchResultTotalLen6 = 1 + 6 + 16;
self.portListening4 = 18266;
self.portListening6 = 0;
self.targetPort4 = 7001;
self.targetPort6 = 7001;
self.waitUdpReceivingMillisecond = 15000;
self.waitUdpSendingMillisecond = 45000;
self.thresholdSucBroadcastCount = 1;
self.expectTaskResultCount = 1;
self.isIPv4Supported0 = YES;
self.isIPv6Supported0 = YES;
}
return self;
}
// the range of the result should be 1-100
- (int) __getNextDatagramCount
{
return 1 + (_datagramCount++) % 100;
}
- (long) getIntervalGuideCodeMillisecond
{
return self.intervalGuideCodeMillisecond;
}
- (long) getIntervalDataCodeMillisecond
{
return self.intervalDataCodeMillisecond;
}
- (long) getTimeoutGuideCodeMillisecond
{
return self.timeoutGuideCodeMillisecond;
}
- (long) getTimeoutDataCodeMillisecond
{
return self.timeoutDataCodeMillisecond;
}
- (long) getTimeoutTotalCodeMillisecond
{
return self.timeoutTotalCodeMillisecond;
}
- (int) getTotalRepeatTime
{
return self.totalRepeatTime;
}
- (int) getEsptouchResultOneLen
{
return self.esptouchResultOneLen;
}
- (int) getEsptouchResultMacLen
{
return self.esptouchResultMacLen;
}
- (int) getEsptouchResultIpLen
{
return _isIPv4Supported0 ? _esptouchResultIpLen4 : _esptouchResultIpLen6;
}
- (int) getEsptouchResultTotalLen
{
if (_isIPv4Supported0) {
return _esptouchResultTotalLen4;
} else {
return _esptouchResultTotalLen6;
}
}
- (int) getPortListening
{
if (_isIPv4Supported0) {
return _portListening4;
} else {
return _portListening6;
}
}
// target hostname is : 234.1.1.1, 234.2.2.2, 234.3.3.3 to 234.100.100.100 for IPv4
// target hostname is : ff02::1 for IPv6
- (NSString *) getTargetHostname
{
if (_isIPv4Supported0) {
if (self.broadcast) {
return @"255.255.255.255";
} else {
int count = [self __getNextDatagramCount];
return [NSString stringWithFormat: @"234.%d.%d.%d", count, count, count];
}
} else {
return @"ff02::1%en0";
}
}
- (int) getTargetPort
{
if (_isIPv4Supported0) {
return _targetPort4;
} else {
return _targetPort6;
}
}
- (int) getWaitUdpReceivingMillisecond
{
return self.waitUdpReceivingMillisecond;
}
- (int) getWaitUdpSendingMillisecond
{
return self.waitUdpSendingMillisecond;
}
- (int) getWaitUdpTotalMillisecond
{
return self.waitUdpReceivingMillisecond + self.waitUdpSendingMillisecond;
}
- (int) getThresholdSucBroadcastCount
{
return self.thresholdSucBroadcastCount;
}
- (void) setWaitUdpTotalMillisecond: (int) waitUdpTotalMillisecond
{
if (waitUdpTotalMillisecond < self.waitUdpReceivingMillisecond + [self getTimeoutTotalCodeMillisecond])
{
// if it happen, even one turn about sending udp broadcast can't be completed
NSLog(@"ESPTouchTaskParameter waitUdpTotalMillisecod is invalid, it is less than mWaitUdpReceivingMilliseond + [self getTimeoutTotalCodeMillisecond]");
assert(0);
}
self.waitUdpSendingMillisecond = waitUdpTotalMillisecond - self.waitUdpReceivingMillisecond;
}
- (int) getExpectTaskResultCount
{
return self.expectTaskResultCount;
}
- (void) setExpectTaskResultCount: (int) expectTaskResultCount
{
_expectTaskResultCount = expectTaskResultCount;
}
- (BOOL) isIPv4Supported
{
return _isIPv4Supported0;
}
- (void) setIsIPv4Supported:(BOOL) isIPv4Supported
{
_isIPv4Supported0 = isIPv4Supported;
}
- (BOOL) isIPv6Supported
{
return _isIPv6Supported0;
}
- (void) setIsIPv6Supported:(BOOL) isIPv6Supported
{
_isIPv6Supported0 = isIPv6Supported;
}
- (void) setListeningPort6:(int) listeningPort6
{
_portListening6 = listeningPort6;
}
- (void)setBroadcast:(BOOL)broadcast {
_broadcast = broadcast;
}
@end