//联网demo,根据参数判断是一键配置联网还是主动联网 //一键配置命令示例:t-oneshot //主动联网命令示例:t-connet("ssid","pwd"); int DemoConnectNet(char *buf) { if(strstr(buf,DEMO_ONESHOT) != NULL) //一键配置 { ifoneshot = 1; CreateOneshotDemoTask(); } else if(strstr(buf, DEMO_CONNET) != NULL) { char *p1 = NULL,*p2 = NULL; char ssid[64]; char pwd[70]; struct tls_param_ip ip_param; if(strchr(buf, ';') != NULL || strchr(buf,')') != NULL) //收到了命令结束符 { printf("\ninput:%s\n",buf); memset(ssid,0,sizeof(ssid)); memset(pwd,0,sizeof(pwd)); p1 = strchr(buf,'"'); if(NULL == p1) return WM_FAILED; p2 = p1 +1; //ssid的起始位置 p1 = strchr(p2,'"'); //ssid的结束位置 if(NULL == p1) return WM_FAILED; MEMCPY(ssid, p2, p1 - p2); printf("\nssid=%s\n",ssid); p2 = p1 + 1; p1 = strchr(p2,'"'); if(NULL == p1) return WM_FAILED; p2 = p1 + 1; //pwd 的起始位置 p1 = strchr(p2, '"'); //pwd的结束位置 if(NULL == p1) return WM_FAILED; if(p1 - p2 > 64) { printf("\npassword too long,error!\n"); return WM_FAILED; } MEMCPY(pwd, p2, p1 - p2); printf("\npassword=%s\n",pwd); tls_wifi_set_oneshot_flag(0); ifoneshot = 0; tls_param_get(TLS_PARAM_ID_IP, &ip_param, FALSE); ip_param.dhcp_enable = true; tls_param_set(TLS_PARAM_ID_IP, &ip_param, FALSE); tls_wifi_connect((u8 *)ssid, strlen(ssid), (u8 *)pwd, strlen(pwd)); tls_netif_add_status_event(con_net_status_changed_event); printf("\nplease wait connect net......\n"); } else return DEMO_CONSOLE_SHORT_CMD; } return WM_SUCCESS; }
static void demo_sock_fwup_task(void *sdata) { // ST_Demo_Sys *sys = (ST_Demo_Sys *)sdata; void *msg; struct tls_ethif * ethif = tls_netif_get_ethif(); printf("\nsock fwup task\n"); if(ethif->status) //已经在网 { tls_os_queue_send(demo_sock_fwup_q, (void *)DEMO_MSG_SOCKET_CREATE, 0); } else { struct tls_param_ip ip_param; tls_param_get(TLS_PARAM_ID_IP, &ip_param, TRUE); ip_param.dhcp_enable = true; tls_param_set(TLS_PARAM_ID_IP, &ip_param, TRUE); tls_wifi_set_oneshot_flag(1); /*一键配置使能*/ printf("\nwait one shot......\n"); } tls_netif_add_status_event(sock_fwup_net_status_changed_event); for(;;) { tls_os_queue_receive(demo_sock_fwup_q, (void **)&msg, 0, 0); //printf("\n msg =%d\n",msg); switch((u32)msg) { case DEMO_MSG_WJOIN_SUCCESS: break; case DEMO_MSG_SOCKET_CREATE: create_socket_fwup_demo(); break; case DEMO_MSG_WJOIN_FAILD: break; case DEMO_MSG_SOCKET_ERR: printf("\nsocket err\n"); break; default: break; } } }
int tls_sys_init() { int err; void *msg; #if TLS_CONFIG_WEB_SERVER struct tls_webs_cfg webcfg; #endif /* create messge queue */ #define SYS_MSG_SIZE 20 msg = tls_mem_alloc(SYS_MSG_SIZE * sizeof(void *)); if (!msg) return -1; err = tls_os_queue_create(&msg_queue, msg, SYS_MSG_SIZE, 0); if (err) return -1; /* create task */ tls_os_task_create(NULL, NULL, tls_sys_task, (void *)0, (void *)&sys_task_stk, /* 任务栈的起始地址 */ SYS_TASK_STK_SIZE * sizeof(u32), /* 任务栈的大小 */ TLS_SYS_TASK_PRIO, 0); tls_netif_add_status_event(sys_net_status_changed); #if TLS_CONFIG_WEB_SERVER tls_param_get(TLS_PARAM_ID_WEBS_CONFIG, (void *)&webcfg, FALSE); if ((webcfg.AutoRun)){ httpd_init(webcfg.PortNum); //printf("Web manager server start (tcp port:%d)\n\r",webcfg.PortNum); } #endif return 0; }
static void demo_oneshot_task(void *sdata) { //ST_Demo_Sys *sys = (ST_Demo_Sys *)sdata; void *msg; struct tls_param_ip ip_param; tls_param_get(TLS_PARAM_ID_IP, &ip_param, TRUE); ip_param.dhcp_enable = true; tls_param_set(TLS_PARAM_ID_IP, &ip_param, TRUE); tls_wifi_set_oneshot_flag(1); /*一键配置使能*/ tls_netif_add_status_event(con_net_status_changed_event); printf("\nwait one shot......\n"); for(;;) { tls_os_queue_receive(demo_oneshot_q, (void **)&msg, 0, 0); //printf("\n msg =%d\n",msg); switch((u32)msg) { case DEMO_MSG_WJOIN_SUCCESS: break; case DEMO_MSG_SOCKET_CREATE: #if TLS_CONFIG_SOCKET_STD DemoStdSockOneshotSendMac(); //任选一个 #endif #if TLS_CONFIG_SOCKET_RAW //DemoRawSockOneshotSendMac(); #endif //TLS_CONFIG_SOCKET_RAW break; case DEMO_MSG_WJOIN_FAILD: break; default: break; } } }
static void demo_raw_sock_s_task(void *sdata) { ST_Demo_Sys *sys = (ST_Demo_Sys *)sdata; void *msg; struct tls_ethif * ethif = tls_netif_get_ethif(); printf("\nraw sock s task\n"); //用于socket接收数据使用 sys->sock_rx = tls_mem_alloc(DEMO_BUF_SIZE); if(NULL == sys->sock_rx) { printf("\nmalloc socket rx fail\n"); return; } memset(sys->sock_rx, 0, DEMO_BUF_SIZE); ////// if(ethif->status) //已经在网 { tls_os_queue_send(demo_raw_sock_s_q, (void *)DEMO_MSG_SOCKET_CREATE, 0); } else { struct tls_param_ip ip_param; tls_param_get(TLS_PARAM_ID_IP, &ip_param, TRUE); ip_param.dhcp_enable = true; tls_param_set(TLS_PARAM_ID_IP, &ip_param, TRUE); tls_wifi_set_oneshot_flag(1); /*一键配置使能*/ printf("\nwait one shot......\n"); } tls_netif_add_status_event(raw_sock_s_net_status_changed_event); for(;;) { tls_os_queue_receive(demo_raw_sock_s_q, (void **)&msg, 0, 0); //printf("\n msg =%d\n",msg); switch((u32)msg) { case DEMO_MSG_WJOIN_SUCCESS: break; case DEMO_MSG_SOCKET_CREATE: create_raw_socket_server_demo(); break; case DEMO_MSG_WJOIN_FAILD: if(sys->socket_num > 0) { sys->socket_num = 0; sys->socket_ok = FALSE; } break; case DEMO_MSG_SOCKET_RECEIVE_DATA: /*收到数据,自行处理*/ #if (TLS_CONFIG_UART) tls_uart_tx(sys->sock_rx,sys->sock_data_len); /*发到串口上显示*/ #endif break; case DEMO_MSG_SOCKET_ERR: tls_os_time_delay(200); printf("\nsocket err\n"); tls_socket_create(&socket_desc); break; default: break; } } }
/***************************************************************************** * Function Name // task_start * Descriptor // before create multi_task, we create a task_start task * // in this example, this task display the cpu usage * Input * Output * Return ****************************************************************************/ void task_start (void *data) { extern void CreateUserTask(void); extern void RestoreParamToDefault(void); int err; u8 mac_addr[6]; u8 tx_gain[12]; u8 errstatus; tls_irq_init(); /* initial os ticker */ tls_os_timer_init(); #if OS_TASK_STAT_EN > 0 /* Initialize uC/OS-II's statistics */ OSStatInit(); #endif tls_spi_slave_sel(SPI_SLAVE_FLASH); tls_spi_init(); tls_fls_init(); tls_fls_gd25qxx_drv_install(); tls_restore_param_from_backup(); RestoreParamToDefault(); //tls_param_to_default(); err = tls_param_init(); if (err) { TLS_DBGPRT_INFO("read spi param err\n"); } tls_fwup_init(); /* ¶ÁÈ¡efuseÖеÄmacµØÖ· */ tls_get_mac_addr(mac_addr); tls_get_tx_gain(tx_gain); TLS_DBGPRT_INFO("tx gain "); TLS_DBGPRT_DUMP(&tx_gain[0], 12); TLS_DBGPRT_INFO("mac addr "); TLS_DBGPRT_DUMP(&mac_addr[0], 6); if(tls_wl_init(tx_gain, &mac_addr[0]) == NULL){ TLS_DBGPRT_INFO("wl driver initial failured\n"); } if (wpa_supplicant_init(mac_addr)) { TLS_DBGPRT_INFO("supplicant initial failured\n"); } tls_ethernet_init(); tls_sys_init(); #if TLS_CONFIG_HOSTIF tls_hostif_init(); #endif #if (TLS_CONFIG_HOSTIF && TLS_CONFIG_HS_SPI) tls_hspi_init(); #endif #if (TLS_CONFIG_HOSTIF &&TLS_CONFIG_UART) tls_uart_init(); #endif tls_sys_auto_mode_run(); tls_netif_add_status_event(tls_main_net_status_changed); BLINKTIMER = OSTmrCreate(0, 10, 2, BlinkTimerProc, NULL, NULL, &errstatus); if (BLINKTIMER){ OSTmrStart(BLINKTIMER, &errstatus); } tls_wifi_one_shot_init(); disp_version_info(); /* User start here */ CreateUserTask(); for (;;) { tls_os_time_delay(0x10000000); } }