示例#1
0
文件: fapp_dhcp.c 项目: 8bitgeek/fnet
/************************************************************************
* NAME: fapp_dhcp_cmd
*
* DESCRIPTION: Enable DHCP client. 
************************************************************************/
void fapp_dhcp_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t ** argv )
{
    struct              fnet_dhcp_params dhcp_params;
    fnet_dhcp_desc_t    dhcp_desc;
    fnet_netif_desc_t   netif = fnet_netif_get_default();

    if(argc == 1u    /* By default is "init".*/
#if 0 /* DHCP reboot feature not used too much. */
    || fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0
#endif    
    ) /* [reboot] */
    {
               
        fnet_memset_zero(&dhcp_params, sizeof(struct fnet_dhcp_params));

        fapp_dhcp_discover_counter = FAPP_CFG_DHCP_CMD_DISCOVER_MAX; /* reset counter.*/
        
#if 0 /* DHCP reboot feature not used too much. */
        if(fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0) /* [reboot] */
            dhcp_params.requested_ip_address.s_addr = fnet_netif_get_ip4_addr(netif);
#endif            

        fapp_dhcp_ip_old = fnet_netif_get_ip4_addr(netif); /* Save ip to restore if cancelled. */
        
        /* Enable DHCP client */
        dhcp_desc = fnet_dhcp_init(netif, &dhcp_params);
        if(dhcp_desc != FNET_ERR)
        {
            fapp_dhcp_desc = dhcp_desc;

            /* Register DHCP event handlers. */
            fnet_dhcp_handler_updated_set(fapp_dhcp_desc, fapp_dhcp_handler_updated, (void *)desc);
            fnet_dhcp_handler_discover_set(fapp_dhcp_desc, fapp_dhcp_handler_discover, (void *)desc);
            
            fnet_shell_println(desc, FAPP_TOCANCEL_STR);
            fnet_shell_block(desc, fapp_dhcp_on_ctrlc); /* Block shell. */
        }
        else
        {
            fnet_shell_println(desc, FAPP_INIT_ERR, "DHCP");
        }
    }
    else if((argc == 2u) && (fnet_strcasecmp(&FAPP_COMMAND_RELEASE[0], argv[1]) == 0)) /* [release] */
    {
        fapp_dhcp_release();
    }
    else
    {
        fnet_shell_println(desc, FAPP_PARAM_ERR, argv[1]);
    }
}
示例#2
0
文件: fapp_ping.c 项目: rschuck/K64f
/************************************************************************
* NAME: fapp_ping_cmd
*
* DESCRIPTION: Ping command. 
************************************************************************/
void fapp_ping_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t ** argv )
{
    struct fnet_ping_params ping_params;
    fnet_index_t            i;
    fnet_char_t             *p;
    fnet_uint32_t           value;
    fnet_char_t             ip_str[FNET_IP_ADDR_STR_SIZE];
    
    
    fnet_memset_zero(&ping_params, sizeof(ping_params));
    
    ping_params.cookie = (fnet_uint32_t)desc;
    ping_params.handler = fapp_ping_handler;
    ping_params.packet_size = FAPP_PING_DEFAULT_SIZE;
    ping_params.timeout = FAPP_PING_DEFAULT_TIMEOUT;
    ping_params.pattern = FAPP_PING_DEFAULT_PATTERN;
    ping_params.ttl = FAPP_PING_DEFAULT_HOP_LIMIT;
    ping_params.packet_count = FAPP_PING_DEFAULT_NUMBER;
    
    /* Last parameter must be ip address.*/
    i = (argc-1u);
    if(fnet_inet_ptos(argv[i], &ping_params.target_addr) == FNET_ERR)
    {
        goto ERROR_PARAMETER;
    }
    else
    {
        /* TBD Optimise parameters parsing.*/
        if(argc > 2u) /* There are additional parameters */
        { 
            /* [-c <count>][-i <seconds>]\n\t[-p <pattern>][-s <size>][-h <hoplimit/ttl>] */
            for(i=1u; i<(fnet_index_t)(argc-1u); i++)
            {
                if (!fnet_strcmp(argv[i], "-c"))
                {
                    i++;
                    value = fnet_strtoul(argv[i], &p, 10u);
		            if ((value == 0U) && (p == argv[i]))
		            {
		                goto ERROR_PARAMETER;
		            }
		            else
                    {
		                ping_params.packet_count = value;
                    }
                }
                else if (!fnet_strcmp(argv[i], "-i"))
                {
                    i++;
                    value = fnet_strtoul(argv[i], &p, 10u);
		            if ((value == 0U) && (p == argv[i]))
		            {
		                goto ERROR_PARAMETER;
		            }
		            else
                    {
		                ping_params.timeout = value*1000U;
                    }
                }
                else if (!fnet_strcmp(argv[i], "-p"))
                {
                    i++;
                    value = fnet_strtoul(argv[i], &p, 10u);
		            if ((value == 0U) && (p == argv[i]))
		            {
		                goto ERROR_PARAMETER;
		            }
		            else
                    {
		                ping_params.pattern = (fnet_uint8_t)value;
                    }
                }
                else if (!fnet_strcmp(argv[i], "-s"))
                {
                    i++;
                    value = fnet_strtoul(argv[i], &p, 10u);
		            if ((value == 0U) && (p == argv[i]))
		            {
		                goto ERROR_PARAMETER;
		            }
		            else
                    {
		                ping_params.packet_size = (fnet_size_t)value;
                    }
                }
                else if (!fnet_strcmp(argv[i], "-h"))
                {
                    i++;
                    value = fnet_strtoul(argv[i], &p, 10u);
		            if ((value == 0U) && (p == argv[i]))
		            {
		                goto ERROR_PARAMETER;
		            }
		            else
                    {
		                ping_params.ttl = (fnet_uint8_t)value;
                    }
                }
                else if (!fnet_strcmp(argv[i], "-n"))
                {
                    /* Just ignore  the -n parameter.*/
                }
                else if (!fnet_strcmp(argv[i], "-I"))
                {
                    i++;
                    /* Just ignore  the -I parameter and its value.*/
                }                
                else /* Wrong parameter.*/
                {
                    goto ERROR_PARAMETER;
                }
            }
        }
       
        if(fnet_ping_request(&ping_params) == FNET_OK)
        {
            fnet_shell_println(desc, FAPP_DELIMITER_STR);
            fnet_shell_println(desc, " PING" );
            fnet_shell_println(desc, FAPP_DELIMITER_STR);
            fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "Remote IP addr", fnet_inet_ntop(ping_params.target_addr.sa_family, ping_params.target_addr.sa_data, ip_str, sizeof(ip_str)));
            fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Message Size", ping_params.packet_size>FNET_CFG_PING_PACKET_MAX?FNET_CFG_PING_PACKET_MAX:ping_params.packet_size);
            fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Num. of messages", ping_params.packet_count);
            fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Pattern", ping_params.pattern);    
            fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Hoplimit (TTL)", ping_params.ttl);      
            fnet_shell_println(desc, FAPP_TOCANCEL_STR);
            fnet_shell_println(desc, FAPP_DELIMITER_STR);

            fnet_shell_block(desc, fapp_ping_on_ctrlc); /* Block shell. */
        }
        else
        {
            fnet_shell_println(desc, FAPP_INIT_ERR, "PING");
        }
    }

    return;
    
ERROR_PARAMETER:
    fnet_shell_println(desc, FAPP_PARAM_ERR, argv[i]);
    return;    
}
示例#3
0
/************************************************************************
* NAME: fapp_dhcp_cmd
*
* DESCRIPTION: Enable DHCP client.
************************************************************************/
void fapp_dhcp_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t **argv )
{
    struct              fnet_dhcp_params dhcp_params;
    fnet_dhcp_desc_t    dhcp_desc;
    fnet_netif_desc_t   netif = fnet_netif_get_default();

    if(argc == 1u    /* By default is "init".*/
#if 0 /* DHCP reboot feature not used too much. */
        ||((argc == 2u) && (fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0))  /* [reboot] */
#endif
        ||((argc == 2u) && (fnet_strcasecmp(&FAPP_DHCP_COMMAND_AUTOIP[0], argv[1]) == 0)) /* [autoip] */
      ) 
    {

        fnet_memset_zero(&dhcp_params, sizeof(struct fnet_dhcp_params));

        fapp_dhcp_discover_counter = FAPP_CFG_DHCP_CMD_DISCOVER_MAX; /* reset counter.*/

#if 0 /* DHCP reboot feature not used too much. */
        if(fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0) /* [reboot] */
            dhcp_params.requested_ip_address.s_addr = fnet_netif_get_ip4_addr(netif);
#endif
        /* Check Auto-IP flag.*/
        if((argc == 2u) && (fnet_strcasecmp(&FAPP_DHCP_COMMAND_AUTOIP[0], argv[1]) == 0)) 
        {
            fapp_dhcp_autoip = FNET_TRUE;
        }
        else
        {
            fapp_dhcp_autoip = FNET_FALSE;
        }

        dhcp_params.probe_addr = FNET_TRUE; /* Enable probing of the newly received address.*/

        /* Save current IP address only if it was allocated manually/statically. */
        if(fnet_netif_get_ip4_addr_type(netif) == FNET_NETIF_IP_ADDR_TYPE_MANUAL)
        {
            fapp_dhcp_ip_old = fnet_netif_get_ip4_addr(netif); /* Save ip to restore if cancelled. */
        }
        else
        {
            fapp_dhcp_ip_old = 0;
        }

        /* Start DHCP client */
        dhcp_desc = fnet_dhcp_init(netif, &dhcp_params);
        if(dhcp_desc)
        {
            fapp_dhcp_desc = dhcp_desc;

            /* Register DHCP event handler callbacks. */
            fnet_dhcp_set_callback_updated(fapp_dhcp_desc, fapp_dhcp_callback_updated, (void *)desc);
            fnet_dhcp_set_callback_discover(fapp_dhcp_desc, fapp_dhcp_callback_discover, (void *)desc);

            fnet_shell_println(desc, FAPP_DELIMITER_STR);
            fnet_shell_println(desc, " DHCPv4 Client started.");
            fnet_shell_println(desc, FAPP_DELIMITER_STR);

            fnet_shell_println(desc, FAPP_TOCANCEL_STR);
            fnet_shell_block(desc, fapp_dhcp_on_ctrlc); /* Block shell. */
        }
        else
        {
            fnet_shell_println(desc, FAPP_INIT_ERR, "DHCP");
        }
    }
    else if((argc == 2u) && (fnet_strcasecmp(&FAPP_COMMAND_RELEASE[0], argv[1]) == 0)) /* [release] */
    {
        fapp_dhcp_release();
    }
    else
    {
        fnet_shell_println(desc, FAPP_PARAM_ERR, argv[1]);
    }
}