void create_raw_socket_client_demo(void)
{	
	int idx;
	struct tls_ethif * ethif;
	
	ethif = tls_netif_get_ethif();
	printf("\nip=%d.%d.%d.%d\n",ip4_addr1(&ethif->ip_addr.addr),ip4_addr2(&ethif->ip_addr.addr),
		ip4_addr3(&ethif->ip_addr.addr),ip4_addr4(&ethif->ip_addr.addr));

	DemoRawSockOneshotSendMac();
	
	memset(&raw_socket_c_desc, 0, sizeof(struct tls_socket_desc));
	raw_socket_c_desc.recvf = raw_sk_client_recv;
	raw_socket_c_desc.errf = raw_sk_client_err;
	raw_socket_c_desc.pollf = raw_sk_client_poll;

	raw_socket_c_desc.cs_mode = SOCKET_CS_MODE_CLIENT;
	raw_socket_c_desc.connf = raw_sk_client_connected;

	raw_socket_c_desc.protocol = SOCKET_PROTO_TCP;
	for(idx = 0; idx < 4; idx++){
		raw_socket_c_desc.ip_addr[idx] = RemoteIp[idx];
	}
	raw_socket_c_desc.port = RemotePort;

	printf("\nserver ip=%d.%d.%d.%d,port=%d\n",RemoteIp[0],RemoteIp[1],RemoteIp[2],RemoteIp[3],raw_socket_c_desc.port);
	if(gDemoSys.socket_ok != true)
	{
		tls_socket_create(&raw_socket_c_desc);
	}
}
void create_raw_socket_server_demo(void)
{	
	struct tls_ethif * ethif;

	ethif = tls_netif_get_ethif();
	printf("\nip=%d.%d.%d.%d\n",ip4_addr1(&ethif->ip_addr.addr),ip4_addr2(&ethif->ip_addr.addr),
		ip4_addr3(&ethif->ip_addr.addr),ip4_addr4(&ethif->ip_addr.addr));

	DemoRawSockOneshotSendMac();
	
	memset(&socket_desc, 0, sizeof(struct tls_socket_desc));
	socket_desc.recvf = raw_sk_server_recv;
	socket_desc.errf = raw_sk_server_err;
	socket_desc.pollf = raw_sk_server_poll;

	socket_desc.cs_mode = SOCKET_CS_MODE_SERVER;
	socket_desc.acceptf = raw_sk_server_accept;

	socket_desc.protocol = SOCKET_PROTO_TCP;
	socket_desc.port = LocalPort;
	printf("\nlisten port=%d\n",socket_desc.port);
	if(gDemoSys.socket_ok != true)
	{
		tls_socket_create(&socket_desc);
	}
}
Пример #3
0
static void sys_net_status_changed(u8 status)
{
#if TLS_CONFIG_TLS_DEBUG
	struct tls_ethif * ethif;
#endif

    switch(status)
    {
        case NETIF_WIFI_JOIN_SUCCESS:
            TLS_DBGPRT_INFO("join net success\n");
            tls_sys_net_up();
            break;
	case NETIF_WIFI_JOIN_FAILED:
            TLS_DBGPRT_INFO("join net failed\n");
		tls_sys_connect_failed();
		break;
        case NETIF_WIFI_DISCONNECTED:
            TLS_DBGPRT_INFO("net disconnected\n");
            tls_sys_net_down();
            break;
        case NETIF_IP_NET_UP:
#if TLS_CONFIG_TLS_DEBUG			
			ethif = tls_netif_get_ethif();
            TLS_DBGPRT_INFO("net up ==> ip = %d.%d.%d.%d\n",ip4_addr1(&ethif->ip_addr.addr),ip4_addr2(&ethif->ip_addr.addr),
		ip4_addr3(&ethif->ip_addr.addr),ip4_addr4(&ethif->ip_addr.addr));
#endif			
            break;
        default:
            break;
    }
}
Пример #4
0
int
netannounce(int domain, int proto, char *local, int port)
{
    int s, opt;
    struct addrinfo hints, *res;
    char portstr[6];
	struct tls_ethif *ethif;
	struct sockaddr_in * sin;

		os_snprintf(portstr, 6, "%d", port);
		memset(&hints, 0, sizeof(hints));
		hints.ai_family = domain;
		hints.ai_socktype = proto;
	   // hints.ai_flags = AI_PASSIVE;
		// XXX: Check getaddrinfo for errors!
		if (getaddrinfo(local, portstr, &hints, &res) != 0)
			return (-1); 
	
#if 1
				ethif = tls_netif_get_ethif();
				sin = (struct sockaddr_in *) res->ai_addr ;
				MEMCPY((char *)&sin->sin_addr, (char *)&ethif->ip_addr.addr, 4); 
			
#endif


   // s = socket(domain, proto, 0);
    s = socket(res->ai_family, proto, 0);
    if (s < 0) {
        return (-1);
    }
    opt = 1;
    setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt));

    if (bind(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0) {
        close(s);
        return (-1);
    }

    freeaddrinfo(res);
    
    if (proto == SOCK_STREAM) {
        if (listen(s, 5) < 0) {
			close(s);
            return (-1);
        }
    }

    return (s);
}
Пример #5
0
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;
		}
	}

}
Пример #6
0
int tls_cmd_get_link_status(
        struct tls_cmd_link_status_t *lks)
{
    struct tls_ethif *ni;

    ni=tls_netif_get_ethif();
    if (tls_cmd_get_net_up())
        lks->status = 1;
    else
        lks->status = 0;
    MEMCPY(lks->ip, (char *)&ni->ip_addr.addr, 4);
    MEMCPY(lks->netmask, (char *)&ni->netmask.addr, 4);
    MEMCPY(lks->gw, (char *)&ni->gw.addr, 4);
    MEMCPY(lks->dns1, (char *)&ni->dns1.addr, 4);
    MEMCPY(lks->dns2, (char *)&ni->dns2.addr, 4);

    return 0;
}
Пример #7
0
int tls_cmd_set_ip_info(
        struct tls_cmd_ip_params_t *params, u8 update_flash)
{
    struct tls_ethif *ethif;
    struct tls_param_ip param_ip;

    ethif=tls_netif_get_ethif();
	if(tls_param_get_updp_mode() == 0)
	{
	    if (params->type == 0) {
	        /* enable dhcp */
			u8 mode;
			tls_param_get(TLS_PARAM_ID_WPROTOCOL, (void* )&mode, false);
	        if ((mode == IEEE80211_MODE_INFRA)
#if TLS_CONFIG_APSTA
	            || (mode == IEEE80211_MODE_APSTA)
#endif
	           ) {
	            tls_dhcp_start(); 
	        }
	        else
	            return -1;
	    } else {
	        tls_dhcp_stop();
	        MEMCPY((char *)&ethif->ip_addr.addr, &params->ip_addr, 4);
	        MEMCPY((char *)&ethif->dns1.addr, &params->dns, 4);
	        MEMCPY((char *)&ethif->netmask.addr, &params->netmask, 4);
	        MEMCPY((char *)&ethif->gw.addr, &params->gateway, 4); 
	        tls_netif_set_addr(
	                &ethif->ip_addr, &ethif->netmask, &ethif->gw);
	    }
	}

    /* update flash params */
    param_ip.dhcp_enable = params->type ? 0 : 1;
    MEMCPY((char *)param_ip.dns1, &params->dns, 4);
    MEMCPY((char *)param_ip.dns2, param_ip.dns2, 4);
    MEMCPY((char *)param_ip.gateway, &params->gateway, 4);
    MEMCPY((char *)param_ip.ip, &params->ip_addr, 4);
    MEMCPY((char *)param_ip.netmask, &params->netmask, 4);
    tls_param_set(TLS_PARAM_ID_IP, (void *)&param_ip, (bool)update_flash);

    return 0;
}
Пример #8
0
void create_socket_fwup_demo(void)
{
	struct tls_ethif * ethif;
	ethif = tls_netif_get_ethif();
	printf("\nip=%d.%d.%d.%d\n",ip4_addr1(&ethif->ip_addr.addr),ip4_addr2(&ethif->ip_addr.addr),
		ip4_addr3(&ethif->ip_addr.addr),ip4_addr4(&ethif->ip_addr.addr));

	/*oneshot config broadcast mac addr*/
	DemoRawSockOneshotSendMac();
	
	if(fwup_skt_num<0)
	{
		memset(&sock_desc, 0, sizeof(struct tls_socket_desc));
		sock_desc.cs_mode = SOCKET_CS_MODE_SERVER;
		sock_desc.acceptf = socket_fwup_accept;
		sock_desc.recvf = socket_fwup_recv;
		sock_desc.errf = socket_fwup_err;
		sock_desc.pollf = socket_fwup_poll;
		sock_desc.protocol = SOCKET_PROTO_TCP;
		sock_desc.port = SOCKET_FWUP_PORT;
		fwup_skt_num = tls_socket_create(&sock_desc);
	}
}
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;
		}
	}

}