mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-11 20:48:16 +08:00
149 lines
4.6 KiB
C
149 lines
4.6 KiB
C
#include "user_wifi.h"
|
||
|
||
#include "main.h"
|
||
#include "mico_socket.h"
|
||
#include "user_sntp.h"
|
||
#include "user_key.h"
|
||
|
||
#define os_log(format, ...) custom_log("WIFI", format, ##__VA_ARGS__)
|
||
|
||
char wifi_status = WIFI_STATE_NOCONNECT;
|
||
|
||
mico_timer_t wifi_led_timer;
|
||
|
||
static void wifi_connect_sys_config( void )
|
||
{
|
||
if ( strlen( sys_config->micoSystemConfig.ssid ) > 0 )
|
||
{
|
||
os_log("connect ssid:%s key:%s",sys_config->micoSystemConfig.ssid,sys_config->micoSystemConfig.user_key);
|
||
network_InitTypeDef_st wNetConfig;
|
||
memset( &wNetConfig, 0, sizeof(network_InitTypeDef_st) );
|
||
strcpy( wNetConfig.wifi_ssid, sys_config->micoSystemConfig.ssid );
|
||
strcpy( wNetConfig.wifi_key, sys_config->micoSystemConfig.user_key );
|
||
wNetConfig.wifi_mode = Station;
|
||
wNetConfig.dhcpMode = DHCP_Client;
|
||
wNetConfig.wifi_retry_interval = 6000;
|
||
micoWlanStart( &wNetConfig );
|
||
wifi_status = WIFI_STATE_CONNECTING;
|
||
} else
|
||
wifi_status = WIFI_STATE_FAIL;
|
||
}
|
||
void wifi_start_easylink( )
|
||
{
|
||
wifi_status = WIFI_STATE_EASYLINK;
|
||
micoWlanStartEasyLink( 20000 );
|
||
led( 1 );
|
||
}
|
||
uint32_t ip=0xd248912c;
|
||
//easylink Íê³É»Øµ÷
|
||
void wifi_easylink_completed_handle( network_InitTypeDef_st *nwkpara, void * arg )
|
||
{
|
||
os_log("wifi_easylink_wps_completed_handle:");
|
||
if ( nwkpara == NULL )
|
||
{
|
||
os_log("EasyLink fail");
|
||
micoWlanStopEasyLink( );
|
||
return;
|
||
}
|
||
|
||
os_log("ssid:%s",nwkpara->wifi_ssid);
|
||
os_log("key:%s",nwkpara->wifi_key);
|
||
os_log("local_ip_addr:%s",nwkpara->local_ip_addr);
|
||
os_log("net_mask:%s",nwkpara->net_mask);
|
||
os_log("gateway_ip_addr:%s",nwkpara->gateway_ip_addr);
|
||
os_log("dnsServer_ip_addr:%s",nwkpara->dnsServer_ip_addr);
|
||
|
||
//±£´æwifi¼°ÃÜÂë
|
||
strcpy( sys_config->micoSystemConfig.ssid, nwkpara->wifi_ssid );
|
||
strcpy( sys_config->micoSystemConfig.user_key, nwkpara->wifi_key );
|
||
sys_config->micoSystemConfig.user_keyLength = strlen( nwkpara->wifi_key );
|
||
mico_system_context_update( sys_config );
|
||
|
||
wifi_status = WIFI_STATE_NOCONNECT;
|
||
os_log("EasyLink stop");
|
||
micoWlanStopEasyLink( );
|
||
}
|
||
|
||
//wifiÒÑÁ¬½Ó»ñÈ¡µ½IPµØÖ· »Øµ÷
|
||
static void wifi_get_ip_callback( IPStatusTypedef *pnet, void * arg )
|
||
{
|
||
os_log("got IP:%s", pnet->ip);
|
||
wifi_status = WIFI_STATE_CONNECTED;
|
||
|
||
}
|
||
//wifiÁ¬½Ó״̬¸Ä±ä»Øµ÷
|
||
static void wifi_status_callback( WiFiEvent status, void *arg )
|
||
{
|
||
if ( status == NOTIFY_STATION_UP ) //wifiÁ¬½Ó³É¹¦
|
||
{
|
||
//wifi_status = WIFI_STATE_CONNECTED;
|
||
} else if ( status == NOTIFY_STATION_DOWN ) //wifi¶Ï¿ª
|
||
{
|
||
wifi_status = WIFI_STATE_NOCONNECT;
|
||
if ( !mico_rtos_is_timer_running( &wifi_led_timer ) ) mico_rtos_start_timer( &wifi_led_timer );
|
||
}
|
||
}
|
||
//100ms¶¨Ê±Æ÷»Øµ÷
|
||
static void wifi_led_timer_callback( void* arg )
|
||
{
|
||
static unsigned int num = 0;
|
||
num++;
|
||
switch ( wifi_status )
|
||
{
|
||
case WIFI_STATE_FAIL:
|
||
os_log("wifi connect fail");
|
||
led( 0 );
|
||
mico_rtos_stop_timer( &wifi_led_timer );
|
||
break;
|
||
case WIFI_STATE_NOCONNECT:
|
||
wifi_connect_sys_config( );
|
||
break;
|
||
|
||
case WIFI_STATE_CONNECTING:
|
||
//if ( num > 1 )
|
||
{
|
||
num = 0;
|
||
led( -1 );
|
||
}
|
||
break;
|
||
case WIFI_STATE_NOEASYLINK:
|
||
wifi_start_easylink( );
|
||
break;
|
||
case WIFI_STATE_EASYLINK:
|
||
led( 1 );
|
||
break;
|
||
case WIFI_STATE_CONNECTED:
|
||
led( 0 );
|
||
mico_rtos_stop_timer( &wifi_led_timer );
|
||
break;
|
||
}
|
||
}
|
||
|
||
void wifi_init( void )
|
||
{
|
||
//wifiÅäÖóõʼ»¯
|
||
// network_InitTypeDef_st wNetConfig;
|
||
|
||
// memset(&wNetConfig, 0, sizeof(network_InitTypeDef_st));
|
||
// wNetConfig.wifi_mode = Station;
|
||
// snprintf(wNetConfig.wifi_ssid, 32, "Honor 9" );
|
||
// strcpy((char*)wNetConfig.wifi_key, "19910911");
|
||
// wNetConfig.dhcpMode = DHCP_Client;
|
||
// wNetConfig.wifi_retry_interval=6000;
|
||
// micoWlanStart(&wNetConfig);
|
||
|
||
//wifi״̬ÏÂledÉÁ˸¶¨Ê±Æ÷³õʼ»¯
|
||
mico_rtos_init_timer( &wifi_led_timer, 100, (void *) wifi_led_timer_callback, NULL );
|
||
//easylink Íê³É»Øµ÷
|
||
mico_system_notify_register( mico_notify_EASYLINK_WPS_COMPLETED, (void *) wifi_easylink_completed_handle, NULL );
|
||
//wifiÒÑÁ¬½Ó»ñÈ¡µ½IPµØÖ· »Øµ÷
|
||
mico_system_notify_register( mico_notify_DHCP_COMPLETED, (void *) wifi_get_ip_callback, NULL );
|
||
//wifiÁ¬½Ó״̬¸Ä±ä»Øµ÷
|
||
mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void*) wifi_status_callback, NULL );
|
||
//sntp_init();
|
||
//Æô¶¯¶¨Ê±Æ÷¿ªÊ¼½øÐÐwifiÁ¬½Ó
|
||
if ( !mico_rtos_is_timer_running( &wifi_led_timer ) ) mico_rtos_start_timer( &wifi_led_timer );
|
||
|
||
}
|
||
|