/************************************************************************
* NAME: fnet_http_post_handle
*
* DESCRIPTION: 
************************************************************************/
static int fnet_http_post_handle(struct fnet_http_if * http, struct fnet_http_uri * uri)
{
    int result = FNET_ERR;
    
    const struct fnet_http_post *post_ptr;
    
    if(http->post_table)
    /* POST table is initialized.*/
    {
        /* Skip first '/' and ' ' */
        while(*uri->path == '/' || *uri->path == ' ')
            uri->path++;
        
        http->send_param.data_ptr = 0; /* Clear. */    
        
        /* Find POST function */
        for(post_ptr = http->post_table; post_ptr->name; post_ptr++)
    	{
    	    if (!fnet_strcmp(uri->path, 
    		                   post_ptr->name)) 
    		{				 
    		    http->send_param.data_ptr = (void*)post_ptr;
    		    if(post_ptr->handle)
    		        result = post_ptr->handle(uri->query, &http->response.cookie);
    		    else
    		        result = FNET_OK;
    		        
    	        break;
    	    }
    	}
	}
    return result;
}
Exemple #2
0
/************************************************************************
* NAME: fapp_boot_mode_by_name
*
* DESCRIPTION:
************************************************************************/
const struct boot_mode *fapp_boot_mode_by_name (fnet_char_t *name)
{
    const struct boot_mode *result = 0;
    fnet_index_t            i;

    for(i=0u; i< BOOT_MODE_COUNT; i++)
    {
        if(fnet_strcmp(boot_modes[i].name, name ) == 0)
        {
            result = &boot_modes[i];
            break;
        } 
    }
    
    return result;   
}
Exemple #3
0
/************************************************************************
* NAME: fnet_fs_rom_mount
*
* DESCRIPTION:
*************************************************************************/
static fnet_return_t fnet_fs_rom_mount( const void *arg )
{
    fnet_return_t                   result = FNET_ERR;
    const struct fnet_fs_rom_image  *image;

    if(arg)
    {
        /* Check if the image is ROM FS image and version number*/
        image = ((const struct fnet_fs_rom_image * )arg);
        if( (fnet_strcmp( FNET_FS_ROM_NAME, image->name ) == 0)
            && (image->version == FNET_FS_ROM_VERSION))
        {
            result = FNET_OK;
        }
    }

    return result;
}
Exemple #4
0
/************************************************************************
* DESCRIPTION: Returns a FS given its name.
*************************************************************************/
struct fnet_fs *fnet_fs_find_name( fnet_char_t *name )
{
    struct fnet_fs *fs;
    struct fnet_fs *result = 0;

    if(name)
    {
        for (fs = fnet_fs_list; fs != 0; fs = fs->_next)
        {
            if(fnet_strcmp(name, fs->name) == 0)
            {
                result = fs;
                break;
            }
        }
    }
    return result;
}
/************************************************************************
* NAME: fnet_fs_find_name
*
* DESCRIPTION: Returns a FS given its name.
*************************************************************************/
struct fnet_fs * fnet_fs_find_name( char *name )
{
    struct fnet_fs *fs;
    struct fnet_fs *result = 0;

    fnet_os_mutex_lock();

    if(name)
        for (fs = fnet_fs_list; fs != 0; fs = fs->_next)
        {
            if(fnet_strcmp(name, fs->name) == 0)
            {
                result = fs;
                break;
            }
        }

    fnet_os_mutex_unlock();
    return result;
}
Exemple #6
0
/************************************************************************
* 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;    
}
Exemple #7
0
/************************************************************************
* NAME: fnet_http_ssi_send
*
* DESCRIPTION: 
************************************************************************/
static fnet_size_t fnet_http_ssi_send (struct fnet_http_if * http)
{
    fnet_size_t                 result = 0u;
    fnet_size_t                 read_result = 0u;
    fnet_index_t                ssi_head_index = 0u;
    fnet_index_t                ssi_tail_index = 0u;
    struct fnet_http_session_if *session =  http->session_active; 
    fnet_uint8_t                *buffer = session->buffer;
    fnet_bool_t                 next = FNET_FALSE;
    
    while ((result < sizeof(session->buffer)) && (next == FNET_FALSE))
    {
        if((http->ssi.state != FNET_HTTP_SSI_INCLUDING) /* Read from file if not in including. */
           &&((read_result = fnet_fs_fread(buffer, 1u, session->send_param.file_desc)) == 0u) )
        {
            break; /*EOF*/
        }
        
        switch (http->ssi.state)
        {
            case FNET_HTTP_SSI_WAIT_HEAD:
                if(*buffer == fnet_http_ssi_head[ssi_head_index])
                {
                    ssi_head_index++;
                    if(ssi_head_index == sizeof(fnet_http_ssi_head))
                    { /* Head is found */
                        
                        if(result >= sizeof(fnet_http_ssi_head)) 
                        { /* Found in the middle */
                            fnet_fs_fseek (session->send_param.file_desc, -((fnet_int32_t)sizeof(fnet_http_ssi_head)), FNET_FS_SEEK_CUR);
                            next = FNET_TRUE; /* break */
                            result -= sizeof(fnet_http_ssi_head); /* Correct result */
                        }
                        else
                        {
                            http->ssi.state = FNET_HTTP_SSI_WAIT_TAIL;
                        }
                    }
                }
                else
                {
                    ssi_head_index = 0u;
                }
                break;
                
            case FNET_HTTP_SSI_WAIT_TAIL:
                if(*buffer == fnet_http_ssi_tail[ssi_tail_index])
                {
                    ssi_tail_index++;
                    if(ssi_tail_index == sizeof(fnet_http_ssi_tail))
                    { /* Tail is found */
                        const struct fnet_http_ssi  *ssi_ptr = 0;
                        fnet_char_t                *ssi_name = (fnet_char_t*)&session->buffer[sizeof(fnet_http_ssi_head)];
                        fnet_char_t                *ssi_param;

                        http->ssi.send = 0;
                        
                        session->buffer[buffer + 1 - session->buffer - sizeof(fnet_http_ssi_tail)] = '\0'; /* Mark end of the SSI. */

                        /* Find SSI parameters. */
                        if((ssi_param = fnet_strchr( (fnet_char_t*)session->buffer, ' ' )) !=0)
                        {
                            *ssi_param = '\0';  /* Mark end of the SSI name. */
                            ssi_param ++;       /* Point to the begining of params. */
                        }
                        
                        if(http->ssi.ssi_table)
                        /* SSI table is initialized.*/
                        {
                            /* Find SSI handler */
    	                    for(ssi_ptr = http->ssi.ssi_table; (ssi_ptr->name) && (ssi_ptr->send); ssi_ptr++)
    	                    {
    		                    if (!fnet_strcmp( ssi_name, 
    		                                        ssi_ptr->name))                    
    		                    {				 
    		                        http->ssi.send = ssi_ptr->send;
    		                        break;
    	                        }
    	                    }
	                    }
                       
                        read_result = 0u; /* Eliminate the include. */
                        if(http->ssi.send)
                        { /* SSI Handler is found. */
                            if((ssi_ptr->handle == 0) || (ssi_ptr->handle(ssi_param, &session->response.cookie) == FNET_OK))
                            {
                                buffer = session->buffer; /* Reset */
                                result = 0u;
                                
                                http->ssi.state = FNET_HTTP_SSI_INCLUDING;
                            }
                            else
                            {
                                http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                            }
                        }
                        else
                        {
                            http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                        }
                    }
                }
                else
                {
                    ssi_tail_index = 0u;
                }
                break;
            case FNET_HTTP_SSI_INCLUDING:
                {
                    fnet_bool_t eof;
                    read_result = http->ssi.send(session->buffer, sizeof(session->buffer), &eof, &session->response.cookie);
                    if((read_result == 0u) || (eof == FNET_TRUE))
                    {
                        http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                    }
                    
                    next = FNET_TRUE; /* break */
                }
                break;
            default:
                break;
        }
        buffer+=read_result;
        result+=read_result;
    }
    
    if(read_result && (next == FNET_FALSE) && ssi_head_index && (http->ssi.state == FNET_HTTP_SSI_WAIT_HEAD) )
    { /* Potential SSI is splitted => parse in the next itteration */
        result-=ssi_head_index; /* adjust result */
        fnet_fs_fseek(session->send_param.file_desc, -(fnet_int32_t)ssi_head_index, FNET_FS_SEEK_CUR);
    }
    
    return result;
}
/************************************************************************
* NAME: fnet_http_ssi_send
*
* DESCRIPTION: 
************************************************************************/
static unsigned long fnet_http_ssi_send (struct fnet_http_if * http)
{
    unsigned long result = 0;
    unsigned long read_result = 0;
    int ssi_head_index = 0;
    int ssi_tail_index = 0;
    char * buffer = http->buffer;
    int next = 0;
    
    while (result<sizeof(http->buffer) && (next == 0))
    {
        if(http->ssi.state != FNET_HTTP_SSI_INCLUDING) /* Read from file if not in including. */
            if((read_result = fnet_fs_fread(buffer, 1, http->send_param.file_desc)) == 0)
                break; /*EOF*/
        
        switch (http->ssi.state)
        {
            case FNET_HTTP_SSI_WAIT_HEAD:
                if(*buffer == fnet_http_ssi_head[ssi_head_index])
                {
                    ssi_head_index++;
                    if(ssi_head_index == sizeof(fnet_http_ssi_head))
                    { /* Head is found */
                        
                        if(result >= sizeof(fnet_http_ssi_head)) 
                        { /* Found in the middle */
                            fnet_fs_fseek (http->send_param.file_desc, -sizeof(fnet_http_ssi_head), FNET_FS_SEEK_CUR);
                            next = 1; /* break */
                            result -= sizeof(fnet_http_ssi_head); /* Correct result */
                        }
                        else
                            http->ssi.state = FNET_HTTP_SSI_WAIT_TAIL;
                        
                    }
                }
                else
                    ssi_head_index = 0;
                break;
                
            case FNET_HTTP_SSI_WAIT_TAIL:
                if(*buffer == fnet_http_ssi_tail[ssi_tail_index])
                {
                    ssi_tail_index++;
                    if(ssi_tail_index == sizeof(fnet_http_ssi_tail))
                    { /* Tail is found */
                        const struct fnet_http_ssi *ssi_ptr = 0;
                        char * ssi_name = &http->buffer[sizeof(fnet_http_ssi_head)];
                        char * ssi_param;

                        http->ssi.send = 0;
                        
                        http->buffer[buffer + 1 - http->buffer - sizeof(fnet_http_ssi_tail)] = '\0'; /* Mark end of the SSI. */

                        /* Find SSI parameters. */
                        if((ssi_param = fnet_strchr( http->buffer, ' ' )) !=0)
                        {
                            *ssi_param = '\0';  /* Mark end of the SSI name. */
                            ssi_param ++;       /* Point to the begining of params. */
                        }
                        
                        if(http->ssi.ssi_table)
                        /* SSI table is initialized.*/
                        {
                            /* Find SSI handler */
    	                    for(ssi_ptr = http->ssi.ssi_table; ssi_ptr->name && ssi_ptr->send; ssi_ptr++)
    	                    {
    		                    if (!fnet_strcmp( ssi_name, 
    		                                        ssi_ptr->name))                    
    		                    {				 
    		                        http->ssi.send = ssi_ptr->send;
    		                        break;
    	                        }
    	                    }
	                    }
                       
                        read_result = 0; /* Eliminate the include. */
                        if(http->ssi.send)
                        { /* SSI Handler is found. */
                            if((ssi_ptr->handle == 0) || (ssi_ptr->handle(ssi_param, &http->response.cookie) == FNET_OK))
                            {
                                buffer = http->buffer; /* Reset */
                                result = 0;
                                
                                http->ssi.state = FNET_HTTP_SSI_INCLUDING;
                            }
                            else
                                http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                        }
                        else
                            http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                    }

                }
                else
                    ssi_tail_index = 0;
                break;
            case FNET_HTTP_SSI_INCLUDING:
                {
                    char eof;
                    read_result = (unsigned long) http->ssi.send(http->buffer, sizeof(http->buffer), &eof, &http->response.cookie);
                    if((read_result == 0) || (eof == 1))
                        http->ssi.state = FNET_HTTP_SSI_WAIT_HEAD;
                    
                    next = 1; /* break */
                }
                break;
        
        }
        buffer+=read_result;
        result+=read_result;
    }
    
    if(read_result && (next == 0) && ssi_head_index && (http->ssi.state == FNET_HTTP_SSI_WAIT_HEAD) )
    { /* Potential SSI is splitted => parse in the next itteration */
        result-=ssi_head_index; /* adjust result */
        fnet_fs_fseek (http->send_param.file_desc, -ssi_head_index, FNET_FS_SEEK_CUR);
    }
    
    return result;
}
Exemple #9
0
static int fapp_http_ssi_echo_handle(char * query, long *cookie)
{
    int result = FNET_OK;
    const struct fapp_http_echo_variable *  echo_var_ptr;
    fnet_netif_desc_t netif = fapp_default_netif;
    
    const char *ssi_buffer_ptr = 0;
    
    /* Find static echo value. */
	for(echo_var_ptr = fapp_http_echo_variables; echo_var_ptr->variable && echo_var_ptr->value; echo_var_ptr++)
	{
        if (!fnet_strcmp( query, echo_var_ptr->variable))                    
        {				 
            ssi_buffer_ptr = echo_var_ptr->value;
            break;
        }
    }
   
    /* Find run-time echo values. */
    if(ssi_buffer_ptr == 0)
    {
    #if FNET_CFG_IP4
        char ip_str[FNET_IP4_ADDR_STR_SIZE];
    #endif

        ssi_buffer_ptr = fapp_http_ssi_buffer; 
        if (!fnet_strcmp( query, "IP_ADDRESS"))
        {
        #if FNET_CFG_IP4
            fnet_ip4_addr_t ip_adr = fnet_netif_get_ip4_addr(netif);
            fnet_inet_ntoa(*(struct in_addr *)( &ip_adr), ip_str);
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "%s", ip_str);
        #else
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "...");            
        #endif /* FNET_CFG_IP4 */
        }
        else if (!fnet_strcmp( query, "SUBNET_MASK"))
        {
        #if FNET_CFG_IP4
            fnet_ip4_addr_t ip_adr = fnet_netif_get_ip4_subnet_mask(netif);
            fnet_inet_ntoa(*(struct in_addr *)( &ip_adr), ip_str);
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "%s", ip_str);
        #else
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "...");            
        #endif /* FNET_CFG_IP4 */            
        }
        else if (!fnet_strcmp( query, "GATEWAY"))
        {
        #if FNET_CFG_IP4
            fnet_ip4_addr_t ip_adr = fnet_netif_get_ip4_gateway(netif);
            fnet_inet_ntoa(*(struct in_addr *)( &ip_adr), ip_str);
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "%s", ip_str);
        #else
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "...");            
        #endif /* FNET_CFG_IP4 */            
        }
        else if (!fnet_strcmp( query, "MAC"))
        {
            fnet_mac_addr_t macaddr;
            char mac_str[FNET_MAC_ADDR_STR_SIZE];
            fnet_netif_get_hw_addr(netif, macaddr, sizeof(fnet_mac_addr_t));
            fnet_mac_to_str(macaddr, mac_str);
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "%s", mac_str);
        }
        else
        {
            result = FNET_ERR;
        }
    }
    
    *cookie = (long)ssi_buffer_ptr; /* Save ssi_buffer_ptr as cookie.*/
    
    return result;
}