Exemplo n.º 1
0
Arquivo: main.c Projeto: agb861/STM32F
int main(void)
{
	
	mxchipInit();
	UART_Init();
  printf("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version());
  
	readConfig(&configParas);
	
#ifdef LowPowerMode
  enable_ps_mode(SLEEP_UNIT_MS, 100, 100);
#endif	
	
  memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));	
	wNetConfig.wifi_mode = Soft_AP;
	strcpy(wNetConfig.wifi_ssid, configParas.wifi_ssid);
	strcpy(wNetConfig.wifi_key, configParas.wifi_key);
	sprintf(wNetConfig.local_ip_addr, "192.168.1.1");
	sprintf(wNetConfig.net_mask, "255.255.255.0");
	sprintf(wNetConfig.gateway_ip_addr, "192.168.1.1");
	sprintf(wNetConfig.dnsServer_ip_addr, "192.168.1.1");
	sprintf(wNetConfig.address_pool_start, "192.168.1.100");
	sprintf(wNetConfig.address_pool_end, "192.168.1.200");
	wNetConfig.dhcpMode = DHCP_Server;
	StartNetwork(&wNetConfig);
  printf("Establish soft AP: %s.....\r\n", wNetConfig.wifi_ssid);
	
  set_tcp_keepalive(3, 60);
	http_init();
	
  while(1) {
    mxchipTick();	
		http_tick();
	}
}
Exemplo n.º 2
0
Arquivo: main.c Projeto: agb861/STM32F
int application_start(void)
{
  mxchipInit();
  Platform_Init();
  UART_Init();
  
#ifdef RFLowPowerMode
  ps_enable();
#endif	
  
#ifdef MCULowPowerMode
  mico_mcu_powersave_config(mxEnable);
#endif	
  
  debug_out("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version());
  
  debug_out (menu);
  debug_out ("\nMXCHIP> ");
  
  while(1) {
      Main_Menu();
    if(configSuccess){
      wNetConfig.wifi_mode = Station;
      wNetConfig.dhcpMode = DHCP_Client;
      wNetConfig.wifi_retry_interval = 100;
      StartNetwork(&wNetConfig);
      debug_out("connect to %s.....\r\n", wNetConfig.wifi_ssid);
      configSuccess = 0;
      debug_out ("\nMXCHIP> ");
    }
  }
}
Exemplo n.º 3
0
Arquivo: main.c Projeto: netstv/STM32F
//Flash orange LED at about 1hz
int main(void)
{
	long cnt = 0;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	//RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOB, ENABLE);
	GPIO_InitTypeDef  gpio_initstructure;
	gpio_initstructure.GPIO_Pin  =  GPIO_Pin_0 | GPIO_Pin_1;
	gpio_initstructure.GPIO_Speed= GPIO_Speed_50MHz;
	gpio_initstructure.GPIO_Mode = GPIO_Mode_OUT;
	gpio_initstructure.GPIO_OType = GPIO_OType_PP;
	gpio_initstructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init( GPIOB, &gpio_initstructure ); 
    USART1_Init();
    
	if (SysTick_Config(SystemCoreClock / 1000))
    	while (1);

     mxchipInit();


    for (;;) {
		cnt++;
		GPIO_SetBits(GPIOB,GPIO_Pin_0);
		GPIO_SetBits(GPIOB,GPIO_Pin_1);
       	Delay(100);
		GPIO_ResetBits(GPIOB,GPIO_Pin_0);
		GPIO_ResetBits(GPIOB,GPIO_Pin_1);
		Delay(100);
	
		printf("Hello World %ld\r\n", cnt);
    }
}
Exemplo n.º 4
0
/* Sysytem initilize */
void mxchipWNet_HA_init(void)
{
  network_InitTypeDef_st wNetConfig;
  network_InitTypeDef_adv_st wNetConfigAdv;
  int err = MXCHIP_FAILED;

  net_para_st para;
	device_info = (mxchipWNet_HA_st *)malloc(sizeof(mxchipWNet_HA_st));
  memset(device_info, 0, sizeof(mxchipWNet_HA_st)); 

  SystemCoreClockUpdate();
	mxchipInit();
  hal_uart_init();
  getNetPara(&para, Station);
  formatMACAddr((void *)device_info->status.mac, &para.mac);
  strcpy((char *)device_info->status.ip, (char *)&para.ip);
  strcpy((char *)device_info->status.mask, (char *)&para.mask);
  strcpy((char *)device_info->status.gw, (char *)&para.gate);
  strcpy((char *)device_info->status.dns, (char *)&para.dns);

  readConfiguration(device_info);

  device_info->tcpServer_fd = -1;
  device_info->tcpClient_fd = (int *)malloc(4 * MAX_CLIENT);
  device_info->cloud_fd = -1;
  device_info->udpSearch_fd = -1;
  memset(device_info->tcpClient_fd, -1, sizeof(4 * MAX_CLIENT));

  if(device_info->conf.fastLinkConf.availableRecord){ //Try fast link
    memcpy(&wNetConfigAdv.ap_info, &device_info->conf.fastLinkConf.ap_info, sizeof(ApList_adv_t));
    memcpy(&wNetConfigAdv.key, &device_info->conf.fastLinkConf.key, device_info->conf.fastLinkConf.key_len);
    wNetConfigAdv.key_len = device_info->conf.fastLinkConf.key_len;
    wNetConfigAdv.dhcpMode = DHCP_Client;
    strcpy(wNetConfigAdv.local_ip_addr, (char*)device_info->conf.ip);
    strcpy(wNetConfigAdv.net_mask, (char*)device_info->conf.mask);
    strcpy(wNetConfigAdv.gateway_ip_addr, (char*)device_info->conf.gw);
    strcpy(wNetConfigAdv.dnsServer_ip_addr, (char*)device_info->conf.dns);
    wNetConfigAdv.wifi_retry_interval = 100;
    err = StartAdvNetwork(&wNetConfigAdv);
  }

  if(err == MXCHIP_FAILED){
    wNetConfig.wifi_mode = Station;
    strcpy(wNetConfig.wifi_ssid, device_info->conf.sta_ssid);
    strcpy(wNetConfig.wifi_key, device_info->conf.sta_key);
    wNetConfig.dhcpMode = DHCP_Client;
    strcpy(wNetConfig.local_ip_addr, (char*)device_info->conf.ip);
    strcpy(wNetConfig.net_mask, (char*)device_info->conf.mask);
    strcpy(wNetConfig.gateway_ip_addr, (char*)device_info->conf.gw);
    strcpy(wNetConfig.dnsServer_ip_addr, (char*)device_info->conf.dns);
	  wNetConfig.wifi_retry_interval = 500;
    StartNetwork(&wNetConfig);
  }
  ps_enable();
}
Exemplo n.º 5
0
int application_start(void)
{
  OSStatus err = kNoErr;
  net_para_st para;

  Platform_Init();
  /*Read current configurations*/
  context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) );
  require_action( context, exit, err = kNoMemoryErr );
  memset(context, 0x0, sizeof(mico_Context_t));
  mico_rtos_init_mutex(&context->flashContentInRam_mutex);
  mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); 

  MICOReadConfiguration( context );

  err = MICOInitNotificationCenter  ( context );

  err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler );
  require_noerr( err, exit );  
  
  /*wlan driver and tcpip init*/
  mxchipInit();
  getNetPara(&para, Station);
  formatMACAddr(context->micoStatus.mac, (char *)&para.mac);
  
  mico_log_trace(); 
  mico_log("%s mxchipWNet library version: %s", APP_INFO, system_lib_version());

  /*Start system monotor thread*/
  err = MICOStartSystemMonitor(context);
  require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") );
  
  err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000);
  require_noerr( err, exit );
  mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000-100, _watchdog_reload_timer_handler, NULL);
  mico_start_timer(&_watchdog_reload_timer);
  
  if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){
    mico_log("Empty configuration. Starting configuration mode...");

#ifdef CONFIG_MODE_EASYLINK
    err = startEasyLink( context );
    require_noerr( err, exit );
#endif

#ifdef CONFIG_MODE_WAC
    err = startMfiWac( context );
    require_noerr( err, exit );
#endif
  }
  else{
    mico_log("Available configuration. Starting Wi-Fi connection...");
    
    /* Regisist notifications */
    err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler );
    require_noerr( err, exit ); 
    
    err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler );
    require_noerr( err, exit ); 

    err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler );
    require_noerr( err, exit );  
   
    if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){
      ps_enable();
    }

    if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){
      mico_mcu_powersave_config(true);
    }

    /*Bonjour service for searching*/
    if(context->flashContentInRam.micoSystemConfig.bonjourEnable == true){
      err = MICOStartBonjourService( Station, context );
      require_noerr( err, exit );
    }

    /*Local configuration server*/
    if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){
      err =  MICOStartConfigServer(context);
      require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") );
    }

    /*Start mico application*/
    err = MICOStartApplication( context );
    require_noerr( err, exit );
    
    _ConnectToAP( context );
  }


  /*System status changed*/
  while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){
    switch(context->micoStatus.sys_state){
      case eState_Normal:
        break;
      case eState_Software_Reset:
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(500);
        PlatformSoftReboot();
        break;
      case eState_Wlan_Powerdown:
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(500);
        wifi_power_down();
        break;
      case eState_Standby:
        mico_log("Enter standby mode");
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(100);
        wifi_power_down();
        Platform_Enter_STANDBY();
        break;
      default:
        break;
    }
  }
    
  require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) );

exit:
  mico_rtos_delete_thread(NULL);
  return kNoErr;
}
Exemplo n.º 6
0
int application_start(void)
{
  OSStatus err = kNoErr;
  net_para_st para;

  Platform_Init();
  /*Read current configurations*/
  context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) );
  require_action( context, exit, err = kNoMemoryErr );
  memset(context, 0x0, sizeof(mico_Context_t));
  mico_rtos_init_mutex(&context->flashContentInRam_mutex);
  mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); 

  MICOReadConfiguration( context );

  err = MICOInitNotificationCenter  ( context );

  err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler );
  require_noerr( err, exit );  
  
  /*wlan driver and tcpip init*/
  mxchipInit();
  getNetPara(&para, Station);
  formatMACAddr(context->micoStatus.mac, (char *)&para.mac);
  
  mico_log_trace(); 
  mico_log("%s mxchipWNet library version: %s", APP_INFO, system_lib_version());

  /*Start system monotor thread*/
  err = MICOStartSystemMonitor(context);
  require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") );
  
  err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000);
  require_noerr( err, exit );
  mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000-100, _watchdog_reload_timer_handler, NULL);
  mico_start_timer(&_watchdog_reload_timer);
  
  if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){
    mico_log("Empty configuration. Starting configuration mode...");

#ifdef CONFIG_MODE_EASYLINK
  err = startEasyLink( context );
  require_noerr( err, exit );
#endif

#ifdef CONFIG_MODE_WAC
  WACPlatformParameters_t* WAC_Params = NULL;
  WAC_Params = calloc(1, sizeof(WACPlatformParameters_t));
  require(WAC_Params, exit);

  str2hex((unsigned char *)para.mac, WAC_Params->macAddress, 6);
  WAC_Params->isUnconfigured          = 1;
  WAC_Params->supportsAirPlay         = 0;
  WAC_Params->supportsAirPrint        = 0;
  WAC_Params->supports2_4GHzWiFi      = 1;
  WAC_Params->supports5GHzWiFi        = 0;
  WAC_Params->supportsWakeOnWireless  = 0;

  WAC_Params->firmwareRevision =  FIRMWARE_REVISION;
  WAC_Params->hardwareRevision =  HARDWARE_REVISION;
  WAC_Params->serialNumber =      SERIAL_NUMBER;
  WAC_Params->name =              context->flashContentInRam.micoSystemConfig.name;
  WAC_Params->model =             MODEL;
  WAC_Params->manufacturer =      MANUFACTURER;

  WAC_Params->numEAProtocols =    1;
  WAC_Params->eaBundleSeedID =    BUNDLE_SEED_ID;
  WAC_Params->eaProtocols =       (char **)eaProtocols;;

  err = startMFiWAC( context, WAC_Params, 1200);
  free(WAC_Params);
  require_noerr( err, exit );
#endif
  }
  else{
    mico_log("Available configuration. Starting Wi-Fi connection...");
    
    /* Regisist notifications */
    err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler );
    require_noerr( err, exit ); 
    
    err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler );
    require_noerr( err, exit ); 

    err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler );
    require_noerr( err, exit );  
   
    if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){
      ps_enable();
    }

    if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){
      mico_mcu_powersave_config(true);
    }

    /*Bonjour service for searching*/
    // if(context->flashContentInRam.micoSystemConfig.bonjourEnable == true){
    //   err = MICOStartBonjourService( Station, context );
    //   require_noerr( err, exit );
    // }

    err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mDNSResponder", mDNSResponder_thread, 0x1000, (void*)context );
    require_noerr_action( err, exit, mico_log("ERROR: Unable to start mDNSResponder thread.") );

    /*Local configuration server*/
    if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){
      err =  MICOStartConfigServer(context);
      require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") );
    }

    /*Start mico application*/
    err = MICOStartApplication( context );
    require_noerr( err, exit );
    
    _ConnectToAP( context );
  }


  /*System status changed*/
  while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){
    switch(context->micoStatus.sys_state){
      case eState_Normal:
        break;
      case eState_Software_Reset:
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(500);
        PlatformSoftReboot();
        break;
      case eState_Wlan_Powerdown:
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(500);
        wifi_power_down();
        break;
      case eState_Standby:
        mico_log("Enter standby mode");
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(200);
        wifi_power_down();
        Platform_Enter_STANDBY();
        break;
      default:
        break;
    }
  }
    
  require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) );

exit:
  mico_rtos_delete_thread(NULL);
  return kNoErr;
}
Exemplo n.º 7
0
Arquivo: main.c Projeto: agb861/STM32F
int main(void)
{
  int i, j, fd_listen = -1, fd_udp = -1, fd_client = -1;
  char *buf, ip_address[16],ipstr[32];
  int len;
  int con = -1;
	int opt = 0;
  int clientfd[8];
  fd_set readfds, exceptfds;
  struct timeval_t t;
  struct sockaddr_t addr;
  socklen_t addrLen;
  struct timeval_t timeout;
 
	
#ifdef	DynamicMemAlloc
	int bufferSize;
  libConfig.tcp_buf_dynamic = mxEnable;
  libConfig.tcp_max_connection_num = 12;
  libConfig.tcp_rx_size = 2048;
  libConfig.tcp_tx_size = 2048;
  libConfig.hw_watchdog = 0;
  libConfig.wifi_channel = WIFI_CHANNEL_1_13;
	lib_config(&libConfig);
#endif

  for(i=0;i<8;i++) 
    clientfd[i] = -1;

  buf = (char*)malloc(3*1024);
  
	mxchipInit();
	UART_Init();
  printf("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version());
	
#ifdef LowPowerMode
  ps_enable();
#endif	
	
  memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
	wNetConfig.wifi_mode = Station;
	strcpy((char*)wNetConfig.wifi_ssid, AP_NAME);
	strcpy((char*)wNetConfig.wifi_key, AP_PASSWORD);
	wNetConfig.dhcpMode = DHCP_Client;
	StartNetwork(&wNetConfig);
  printf("Connect to %s.....\r\n", wNetConfig.wifi_ssid);
	
  t.tv_sec = 0;
  t.tv_usec = 100;
	
  set_tcp_keepalive(3, 60);
	
  while(1) {
    mxchipTick();	
		
		/*If wifi is established, connect to www.baidu.com, and send a http request*/
#ifdef BlockMode
		if(wifi_up&&webserverTest){
			webserverTest = 0;
			if(gethostbyname(WEB_SERVER, (u8 *)ipstr, 32)!=0){
        printf("DNS test: %s failed \r\n", WEB_SERVER);	
				webserverTest = 1;
				continue;
			}
			printf("DNS test: %s address is %s \r\n", WEB_SERVER, ipstr);
			
			fd_client	= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
			addr.s_ip = inet_addr(ipstr); 
      addr.s_port = 80;
      timeout.tv_sec = 3;
      timeout.tv_usec = 0;
      setsockopt(fd_client,0,SO_CONTIMEO,&timeout,sizeof(struct timeval_t));
			if (connect(fd_client, &addr, sizeof(addr))!=0) {
        close(fd_client);
        fd_client = -1;
        printf("Connect to web server failed! \r\n");
        webserverTest = 1;
      }
			else{
				printf("Connect to web server success! Reading web pages...\r\n");
				send(fd_client, httpRequest, sizeof(httpRequest), 0);
			}
		}
#else
		if(cloud_ip_addr == -1 && dns_pending == 0 && wifi_up){  //DNS function
			cloud_ip_addr = dns_request(WEB_SERVER);
			if(cloud_ip_addr == -1)
				printf("DNS test: %s failed. \r\n", WEB_SERVER); 
			else if (cloud_ip_addr == 0) //DNS pending, waiting for callback
				dns_pending = 1;
		}

    if( fd_client == -1 && cloud_ip_addr!=-1 && !dns_pending){
			fd_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
			setsockopt(fd_client,0,SO_BLOCKMODE,&opt,4);
			addr.s_ip = cloud_ip_addr; 
			addr.s_port = 80;
      printf("Connecting to %s..., at port %d\r\n", inet_ntoa(ipstr, cloud_ip_addr), addr.s_port); 
			if (connect(fd_client, &addr, sizeof(addr))!=0) {
				printf("Connect to %s failed.\r\n", WEB_SERVER); 
        printf("Free: %d, max length: %d.\r\n", total_free, max_len);
      }
		}
    if(cloud_connected == 1){
      send(fd_client, httpRequest, sizeof(httpRequest), 0);
      cloud_connected = 0;     
    }   
#endif
			
		/*Establish a TCP server that accept the tcp clients connections*/
		if (fd_listen==-1) {
      fd_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
#ifdef	DynamicMemAlloc				
			  bufferSize = 5*1024;
				setsockopt(fd_listen,0,SO_RDBUFLEN,&bufferSize,4);
				bufferSize = 5*1024;
				setsockopt(fd_listen,0,SO_WRBUFLEN,&bufferSize,4);
#endif	
      addr.s_port = 8080;
      bind(fd_listen, &addr, sizeof(addr));
      listen(fd_listen, 0);
      printf("TCP server established at port: %d \r\n", addr.s_port);
    }
		
		/*Establish a UDP port to receive any data sent to this port*/
		if (fd_udp==-1) {
      fd_udp = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP);
      addr.s_port = 8090;
      bind(fd_udp, &addr, sizeof(addr));
      printf("Open UDP port %d\r\n", addr.s_port);
    }
		
		/*Check status on erery sockets */
		FD_ZERO(&readfds);
		FD_SET(fd_listen, &readfds);	
		FD_SET(fd_udp, &readfds);	
		if(fd_client!=-1)
		  FD_SET(fd_client, &readfds);
		for(i=0;i<8;i++) {
			if (clientfd[i] != -1)
				FD_SET(clientfd[i], &readfds);
		}
		
		select(1, &readfds, NULL, &exceptfds, &t);
    
    /*Check tcp connection requests */
		if(FD_ISSET(fd_listen, &readfds))
		{
			j = accept(fd_listen, &addr, &len);
			if (j > 0) {
			  inet_ntoa(ip_address, addr.s_ip );
			  printf("Client %s:%d connected\r\n", ip_address, addr.s_port);
			  for(i=0;i<8;i++) {
				  if (clientfd[i] == -1) {
					  clientfd[i] = j;
					  break;
				  }
			  }
			}
		}
		
		/*Read html data from www.baidu.com */
		if(fd_client != -1){
			if(FD_ISSET(fd_client, &readfds))
			{
				con = recv(fd_client, buf, 2*1024, 0);
				if(con > 0)
					printf("Get %s data successful! data length: %d bytes\r\n", WEB_SERVER, con);
				else{
					close(fd_client);
					fd_client = -1;
          cloud_ip_addr = -1;
          webserverTest = 1;
					printf("Web connection closed.\r\n");
				}
			}
	  }
    
   /*Read data from tcp clients and send data back */ 
	 for(i=0;i<8;i++) {
      if (clientfd[i] != -1) {
        if (FD_ISSET(clientfd[i], &readfds)) {
          con = recv(clientfd[i], buf, 1*1024, 0);
          if (con > 0) 
            send(clientfd[i], buf, con, 0);
          else {
            close(clientfd[i]);
            clientfd[i] = -1;
          }
        }
        else if (FD_ISSET(clientfd[i], &exceptfds))
          clientfd[i] = -1;
      }
    }
		
		/*Read data from udp and send data back */ 
		if (FD_ISSET(fd_udp, &readfds)) {
      con = recvfrom(fd_udp, buf, 3*1024, 0, &addr, &addrLen);
      sendto(fd_udp, buf, con, 0, &addr, sizeof(struct sockaddr_t));
    }
	}
}
Exemplo n.º 8
0
Arquivo: main.c Projeto: perrylau/web
int main(void)
{
	u32 endtime;
    
	system_state = SYS_STATE_INIT;
	wifi_state = 0;
	reset_uart_init();
	default_gpio_init();
	check_gotostandby();
	int2host_init();
	int2host(1);
	NFC_TAG_INIT();
	mxchipInit();
	userResetConfig();
	reset_uart_deinit();
	get_config();
	tcpip_init();
	emsp_init();
	uart_init();
	http_init();
#ifndef USE_0302
	APP_Verify_Program_Code();
#endif

	dual_uap_start();
	SetTimer(1, main_function_tick, 1);
	wifi_option_config();
	int2host(0);
	system_is_bootup(); //EMSP_CMD_SYSTEM_BOOTUP
	while(1) {
		if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) != Bit_RESET) 
			IWDG_ReloadCounter();	// Reload IWDG counter
		else
			reload();
		
		if(NFC_STARTED){
			SetTimer(3000,saveNFCConfig,0);
			NFC_STARTED = 0;     //NFC task started
			NFC_STARTED2 = 1;    //NFC task pending
		}
		
		http_tick(); // HTTP Server
		mxchipTick(); // wifi, network function
		switch (system_state) {
			case SYS_STATE_RESET:
        wait_uart_dma_clean();
				NVIC_SystemReset();
				break;
			case SYS_STATE_GOTO_DEFAULT:
				default_config();
				save_config();
				NVIC_SystemReset();
				break;
			case SYS_STATE_DELAY_RELOAD:
				endtime = MS_TIMER+500;
				while(endtime>MS_TIMER)
					mxchipTick();
				
				NVIC_SystemReset();
				break;
				
			case SYS_STATE_WPS: // User pushed WPS PBC, try wps.
				user_wifi_stop();
				wps_pbc_start();
				set_sys_state(last_work_state);
				break;
			case SYS_STATE_WPS_FAIL:
                wps_ing = 0;
				user_wifi_start();
				set_sys_state(last_work_state);
                set_conncetion_status(0);
				break;
			case SYS_STATE_WPS_SUCCESS:
                wps_ing = 0;
				set_sys_state(last_work_state);
				save_config();
                set_conncetion_status(0);
				break;
			default:
				break;
		}
	}
}
Exemplo n.º 9
0
int main(void)
{
	int 			fd_client = -1;
	char 			*buf;
	int 			con = -1;
	int 			opt = 0;
	fd_set 			readfds, exceptfds;
	struct 			timeval_t t;
	struct 			sockaddr_t addr;
	lib_config_t 	libConfig;
	volatile int	setval;
	volatile int	readval;

	buf = (char*)malloc(3*1024);

	libConfig.tcp_buf_dynamic = mxEnable;
	libConfig.tcp_max_connection_num = 12;
	libConfig.tcp_rx_size = 2048;
	libConfig.tcp_tx_size = 2048;
	libConfig.hw_watchdog = 0;
	libConfig.wifi_channel = WIFI_CHANNEL_1_13;
	lib_config(&libConfig);
  
	mxchipInit();
	UART_Init();


	printf("\r\n%s\r\n mxchipWNet library version: %s\r\n", APP_INFO, system_lib_version());
	printf(menu);
	printf ("\nMXCHIP> ");
	
	//init http
	set_tcp_keepalive(3, 60);
	setSslMaxlen(6*1024);
	t.tv_sec = 0;
  	t.tv_usec = 100;
	
//	void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

	while(1)
	{
		mxchipTick();	
		if(menu_enable)
			Main_Menu();
		if(configSuccess)
		{
			wNetConfig.wifi_mode = Station;
			wNetConfig.dhcpMode = DHCP_Client;
			StartNetwork(&wNetConfig);
			printf("connect to %s.....\r\n", wNetConfig.wifi_ssid);
			configSuccess = 0;
			menu_enable = 1;

			sysTick_configuration();
			config_gpio_pc6();
			config_gpio_pb8();

			while(1) 
			{
				readval= GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_6);
				mxchipTick();
				//If wifi is established, connect to WEBSERVER, and send a http request
				if(https_server_addr == 0 && dns_pending == 0)
				{  
					//DNS function
					https_server_addr = dns_request(WEB_SERVER);
					if(https_server_addr == -1)
						printf("DNS test: %s failed. \r\n", WEB_SERVER); 
					else if (https_server_addr == 0) //DNS pending, waiting for callback
					{
						dns_pending = 1;
						printf("2DNS test: %s success. \r\n", WEB_SERVER); 
						printf("21http server addr=%x\n",https_server_addr);
					}
				}
				
				if( fd_client == -1 && (u32)https_server_addr>0)
				{
					fd_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
					setsockopt(fd_client,0,SO_BLOCKMODE,&opt,4);
					addr.s_ip = https_server_addr; 
					addr.s_port = 443;
					if (connect(fd_client, &addr, sizeof(addr))!=0) 
						printf("Connect to %s failed.\r\n", WEB_SERVER); 
					else   
						printf("Connect to %s success.\r\n", WEB_SERVER); 	
				}

				if(serverConnectted&&sslConnectted==0)
				{
					printf("Connect to web server success! Setup SSL ecryption...\r\n");
					if (setSSLmode(1, fd_client)!= MXCHIP_SUCCESS)
					{
						printf("SSL connect fail\r\n");
						close(fd_client);
						fd_client = -1;
						serverConnectted = 0;
					} 
					else 
					{
						printf("SSL connect\r\n");
						sslConnectted = 1;
					}
				}	
		
				if(readval==1&&sslConnectted)
				{
					printf("read value is 1\n");
					send(fd_client, sendhttpRequest, strlen(sendhttpRequest), 0);
				//	return 0;
				}
				if(check_responce&&sslConnectted)
				{
					printf("readval=%d\n",readval);
					check_responce=0;
					send(fd_client, responcehttpRequest, strlen(responcehttpRequest), 0);
					//Check status on erery sockets 
					FD_ZERO(&readfds);

					if(sslConnectted)
						FD_SET(fd_client, &readfds);
		
					select(1, &readfds, NULL, &exceptfds, &t);
		
					//Read html data from WEBSERVER
					if(sslConnectted)
					{
						if(FD_ISSET(fd_client, &readfds))
						{
							con = recv(fd_client, buf, 2*1024, 0);
							if(con > 0)
							{
								printf("Get %s data successful! data length: %d bytes data\r\n", WEB_SERVER, con);
								setval=r_http(buf);
								printf("read from http  is %d\n",setval);
					
								if(setval==1)
									GPIO_SetBits(GPIOB,GPIO_Pin_8);
								 else
								  	GPIO_ResetBits(GPIOB,GPIO_Pin_8);
							}
							else
							{
								close(fd_client);
								serverConnectted = 0;
								sslConnectted = 0;
								fd_client = -1;
								printf("Web connection closed.\r\n");
							}
						}
					}
				}
			}
		}
		
		if(easylink)
		{
			OpenEasylink2(60);	
			easylink = 0;
		}
	}
}