// // SimpleConfig.m // SimpleConfig // // Created by Realsil on 14/11/6. // Copyright (c) 2014年 Realtek. All rights reserved. // #import #import #import "SimpleConfig.h" @implementation SimpleConfig @synthesize config_list, discover_list; int isTriggerSend = 0; int g_device_num = 0; -(id)init { NSLog(@"simple config init"); m_mode = MODE_INIT; m_current_pattern = -1; #if SC_SUPPORT_2X2 m_config_duration = -1; #endif #if SC_CONFIG_QC_TEST m_model = -1; m_mix_model_max_duration = -1; #endif m_shouldStop = false; m_timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerHandler:) userInfo:nil repeats:YES]; config_list = [[NSMutableArray alloc] initWithObjects:nil]; discover_list = [[NSMutableArray alloc] initWithObjects:nil]; m_idev_model = [[NSString alloc] initWithString:[self platformString]]; NSLog(@"iDevice: %@", m_idev_model); return [super init]; } -(void)dealloc { #ifdef ARC [config_list dealloc]; [discover_list dealloc]; #endif for (int i=0; iSC_PATTERN_DEF_SWITCH_THRESHOLD) { [m_pattern[PATTERN_FOUR] rtk_sc_close_sock]; } #endif } -(void)rtk_sc_reopen_sock { NSLog(@"simpleconfig:rtk_sc_reopen_sock"); if (m_timer==nil) { m_timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerHandler:) userInfo:nil repeats:YES]; } [m_pattern[m_current_pattern] rtk_sc_reopen_sock]; #if SC_SUPPORT_2X2 if (m_current_pattern!=PATTERN_FOUR && m_config_duration>SC_PATTERN_DEF_SWITCH_THRESHOLD) { [m_pattern[PATTERN_FOUR] rtk_sc_reopen_sock]; } #endif } -(void)rtk_sc_config_set_devNum: (int)device_num { g_device_num = device_num; [m_pattern[m_current_pattern] rtk_pattern_set_dev_num:device_num]; } -(int)rtk_sc_config_start: (NSString *)ssid psw:(NSString *)password pin:(NSString *)pin { // actually we don't send here. It's timer handler's duty to send int ret; NSLog(@"rtk_sc_start_config, m_idev_model=%@",m_idev_model); // base on the PIN input and device type, decide to use Pattern #if SC_CONFIG_QC_TEST // the following is for testing switch (m_model) { case SC_MODEL_2: if (pin==nil || [pin isEqualToString:@""] || [pin intValue]==0 || [pin isEqualToString:PATTERN_DEF_PIN]){ // no PIN --> P2 m_pattern[PATTERN_TWO] = [[PatternTwo alloc] init:SC_USE_ENCRYPTION]; m_current_pattern = PATTERN_TWO; pin = PATTERN_DEF_PIN; m_pattern[PATTERN_THREE] = nil; }else{ // have PIN --> P3 m_pattern[PATTERN_THREE] = [[PatternThree alloc] init:SC_USE_ENCRYPTION]; m_current_pattern = PATTERN_THREE; m_pattern[PATTERN_TWO] = nil; } break; case SC_MODEL_3: if (pin==nil || [pin isEqualToString:@""] || [pin intValue]==0){ // no PIN --> P2 m_pattern[PATTERN_TWO] = [[PatternTwo alloc] init:SC_USE_ENCRYPTION]; m_current_pattern = PATTERN_TWO; pin = PATTERN_DEF_PIN; m_pattern[PATTERN_THREE] = nil; }else{ // have PIN --> P3 m_pattern[PATTERN_THREE] = [[PatternThree alloc] init:SC_USE_ENCRYPTION]; m_current_pattern = PATTERN_THREE; m_pattern[PATTERN_TWO] = nil; } m_pattern[PATTERN_FOUR] = [[PatternFour alloc] init:SC_USE_ENCRYPTION]; break; case SC_MODEL_1: default: //NSLog(@"SC_MODEL_1"); m_current_pattern = PATTERN_FOUR; m_pattern[m_current_pattern] = [[PatternFour alloc] init:SC_USE_ENCRYPTION]; break; } #else if ([m_idev_model isEqualToString:@"iPad Mini2"]) { m_current_pattern = PATTERN_FOUR; m_pattern[PATTERN_FOUR] = [[PatternFour alloc] init:SC_USE_ENCRYPTION]; }else{ if (pin==nil || [pin isEqualToString:@""] || [pin intValue]==0){ m_pattern[PATTERN_TWO] = [[PatternTwo alloc] init:SC_USE_ENCRYPTION]; m_current_pattern = PATTERN_TWO; pin = PATTERN_DEF_PIN; m_pattern[PATTERN_THREE] = nil; } else{ m_pattern[PATTERN_THREE] = [[PatternThree alloc] init:SC_USE_ENCRYPTION]; m_current_pattern = PATTERN_THREE; m_pattern[PATTERN_TWO] = nil; } // init PATTERN 4, build its profile when needed! m_pattern[PATTERN_FOUR] = [[PatternFour alloc] init:SC_USE_ENCRYPTION]; } #endif // set profile //NSLog(@"simpleconfig: set pattern %d", m_current_pattern+1); ret = [m_pattern[m_current_pattern] rtk_pattern_build_profile:ssid psw:password pin:pin]; if (ret==RTK_FAILED) return ret; [config_list removeAllObjects]; m_shouldStop = false; m_mode = MODE_CONFIG; #if SC_SUPPORT_2X2 m_config_duration = 0; #ifdef ARC if(ssid!=nil) m_ssid = [[[NSString alloc] initWithString:ssid] retain]; else m_ssid = nil; m_password = [[[NSString alloc] initWithString:password] retain]; m_pin = [[[NSString alloc] initWithString:pin] retain]; #endif #endif return ret; } -(void)rtk_sc_config_stop { m_shouldStop = true; NSLog(@"Close socket here!"); //[m_pattern[m_current_pattern] rtk_sc_close_sock]; m_mode = MODE_INIT; } -(int)rtk_sc_discover_start:(unsigned int)scan_time { int ret = RTK_FAILED; // TODO return ret; } -(int) rtk_sc_control_start:(NSString *)client_mac type:(unsigned char)control_type { int ret = RTK_FAILED; // TODO return ret; } -(unsigned int)rtk_sc_get_mode { return m_mode; } #if SC_CONFIG_QC_TEST -(void)rtk_sc_set_sc_model: (unsigned int) model duration: (unsigned int) duration { m_model = model; if (m_model==SC_MODEL_3) // set max duration only under mix model m_mix_model_max_duration = duration; } #endif /*-----------Private Funcs-------------*/ -(BOOL)haveSameMac:(unsigned char *)mac1 mac2:(unsigned char *)mac2 { if(mac1==nil || mac2==nil) return false; for(int i=0; i0) { for (i=0; i<[config_list count]; i++) { config_dev_val = [recv_list objectAtIndex:i]; [config_dev_val getValue:&config_dev]; if (config_dev.ip!=0) { getIP_deviceNum++; } } } } - (NSMutableArray *)getList_forConfirm { return [m_pattern[m_current_pattern] rtk_pattern_get_config_list]; } -(void)timerHandler:(id)sender { if (m_current_pattern<0 || m_current_pattern>=SC_MAX_PATTERN_NUM) { return ; } int status = RTK_FAILED; unsigned int pattern_mode; #if SC_SUPPORT_2X2 #if SC_CONFIG_QC_TEST if (m_model==SC_MODEL_3 && m_config_duration>m_mix_model_max_duration && m_current_pattern!=PATTERN_FOUR) #else if (m_config_duration>SC_PATTERN_DEF_SWITCH_THRESHOLD && m_current_pattern!=PATTERN_FOUR) #endif { // close previous socket [m_pattern[m_current_pattern] rtk_sc_close_sock]; m_current_pattern=PATTERN_FOUR; [m_pattern[m_current_pattern] rtk_pattern_build_profile:m_ssid psw:m_password pin:m_pin]; NSLog(@"m_current_pattern: %d", m_current_pattern); NSLog(@"mode:%d", [m_pattern[m_current_pattern] rtk_sc_get_mode]); } #endif pattern_mode = [m_pattern[m_current_pattern] rtk_sc_get_mode]; //NSLog(@"SimpleConfig(curr_pattern:%d) mode: %d; Pattern mode: %d", m_current_pattern, m_mode, pattern_mode); switch (m_mode) { case MODE_INIT: //NSLog(@"Standing by..."); break; case MODE_CONFIG: if(m_shouldStop==false){ //NSLog(@"SimpleConfig(curr_pattern:%d) mode: %d; Pattern mode: %d", m_current_pattern, m_mode, pattern_mode); // upper layer allows configuring if (pattern_mode==MODE_CONFIG) { // send config profile #if SC_SUPPORT_2X2 // update duration m_config_duration++; { // duration less than SC_PATTERN_SWITCH_THRESHOLD if (m_current_pattern==PATTERN_FOUR) { status = [m_pattern[m_current_pattern] rtk_pattern_send:[NSNumber numberWithInt:1]]; }else{ status = [m_pattern[m_current_pattern] rtk_pattern_send:[NSNumber numberWithInt:SC_SEND_ROUND_PER_SEC]]; } } #endif if (status == RTK_FAILED) { NSLog(@"4rr1"); m_mode = MODE_ALERT; } }else if(pattern_mode==MODE_WAIT_FOR_IP || pattern_mode==MODE_INIT){ m_mode = (pattern_mode==MODE_INIT)? MODE_INIT:MODE_WAIT_FOR_IP; NSLog(@"###1 device num: %lu ###",(unsigned long)[[m_pattern[m_current_pattern] rtk_pattern_get_config_list] count]); // update config_list [self update_config_list]; } else{ NSLog(@"Err2"); m_mode = MODE_ALERT; } }else{ // upper layer orders to stop, reset m_shouldStop flag m_shouldStop = false; m_mode = MODE_INIT; } break; case MODE_WAIT_FOR_IP: if(m_current_pattern==PATTERN_FOUR) { [m_pattern[m_current_pattern] rtk_pattern_send:[NSNumber numberWithInt:1]]; } if(pattern_mode==MODE_WAIT_FOR_IP){ // update config_list [self update_config_list]; NSLog(@"###2 device num: %lu ###",(unsigned long)[config_list count]); }else if(pattern_mode==MODE_INIT){ // update config_list [self update_config_list]; NSLog(@"###3 device num: %lu ### Finish",(unsigned long)[config_list count]); } break; case MODE_DISCOVER: break; case MODE_ALERT: [self showAlert]; m_mode = MODE_INIT; #if SC_SUPPORT_2X2 m_config_duration = -1; #endif break; default: NSLog(@"Error UI mode!"); break; } } -(void)showAlert { if (m_mode == MODE_ALERT) { // garantee only MODE_ALERT can do this UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:m_error delegate:self cancelButtonTitle:@"Stop" otherButtonTitles:nil, nil]; [alert show]; } } @end