Exemplo n.º 1
0
/*
 * Function:    _wilddog_recv_dispatch.
 * Description: recv coap packet and handle it.
 *   
 * Input:       p_recvPdu : point to the recv coap packet.
 *              p_cm_recvArg: recv arg that will transmit to cm port.
 * Output:      N/A.
 * Return:      N/A.
*/
Wilddog_Return_T WD_SYSTEM _wilddog_recv_dispatch
    (
    coap_pdu_t *p_recvPdu,
    Protocol_recvArg_T *p_cm_recvArg
    )
{
    
    size_t playloadLen =0;
    u8 *p_payload = NULL;
    /* get err*/
    wilddog_debug_level(WD_DEBUG_LOG," coap err : %d",p_recvPdu->hdr->code);
    p_cm_recvArg->err = _wilddog_coap_code2Http( \
                            (unsigned int)_GET_COAP_CODE(p_recvPdu->hdr->code));
    
    wilddog_debug_level(WD_DEBUG_LOG,"coap http error %ld",p_cm_recvArg->err);

    /* get token option.*/
    if(p_recvPdu->hdr->token_length != COAP_TOKENLEN)
        return WILDDOG_ERR_INVALID;
    
    memcpy(&p_cm_recvArg->d_token,p_recvPdu->hdr->token,COAP_TOKENLEN);
    /* get observer index.*/
    p_cm_recvArg->d_isObserver = _wilddog_recv_getOptionValue(p_recvPdu,
                                        COAP_OPTION_OBSERVE,
                                        (u8*)&p_cm_recvArg->d_observerIndx,
                                        sizeof(p_cm_recvArg->d_observerIndx));
    
    wilddog_debug_level(WD_DEBUG_LOG, \
                        "coap get observerIndex : %lu ", \
                        p_cm_recvArg->d_observerIndx);
    /* get maxage.*/
    _wilddog_recv_getOptionValue(p_recvPdu,
                                 COAP_OPTION_MAXAGE, \
                                 (u8*)&(p_cm_recvArg->d_maxAge), \
                                 sizeof( p_cm_recvArg->d_maxAge));
    
    wilddog_debug_level(WD_DEBUG_LOG, \
                        "coap get max-age : %lu ", \
                        p_cm_recvArg->d_maxAge);
    /* get blockNum.*/
    /*get payload data */
    coap_get_data(p_recvPdu,&playloadLen,&p_payload);
    /* clean buf .*/
    memset(p_cm_recvArg->p_recvData,0,(size_t)p_cm_recvArg->d_recvDataLen);
    
    if( playloadLen > p_cm_recvArg->d_recvDataLen )
        return WILDDOG_ERR_INVALID;

    memcpy( p_cm_recvArg->p_recvData,p_payload,playloadLen);
    p_cm_recvArg->d_recvDataLen = playloadLen;
    
    wilddog_debug_level(WD_DEBUG_LOG, \
                        "coap recv data :%s", \
                        p_cm_recvArg->p_recvData);
    
    return WILDDOG_ERR_NOERR;
    
}
Exemplo n.º 2
0
/*
 * Function:    wilddog_send
 * Description: wilddog send function, it use the interface in wiced platform.
 * Input:        socketId: The socket id.
 *                  addr_in:  The pointer of Wilddog_Address_T
 *                  tosend: The pointer of the send buffer
 *                  tosendLength: The length of the send buffer.
 * Output:      N/A
 * Return:      If success, return the number of characters sent.; else return -1.
*/
int wilddog_send
    ( 
    int socketId, 
    Wilddog_Address_T* addr_in, 
    void* tosend, 
    s32 tosendLength 
    )
{
    wiced_udp_socket_t* socket = (wiced_udp_socket_t*) socketId;
    wiced_ip_address_t ipaddr;
    ipaddr.version = WICED_IPV4;
    ipaddr.ip.v4 = WILDDOG_MAKE_IPV4(addr_in->ip[0], addr_in->ip[1], \
        addr_in->ip[2], addr_in->ip[3]);

    wiced_packet_t* packet;
    uint8_t* data;
    uint16_t aval;
    if ( wiced_packet_create_udp( socket, 0, &packet, &data, &aval ) != \
        WICED_SUCCESS )
    {
        wilddog_debug_level(WD_DEBUG_ERROR, "error create packet ...\r\n");
        return -1;
    }
    if ( aval < tosendLength )
    {
        /* Delete packet, since the send failed */
        wiced_packet_delete( packet ); 
        wilddog_debug_level(WD_DEBUG_ERROR, \
            "too large length to translate! should be %d, want send %ld\n", \
            aval, tosendLength);
        return -1;
    }

    memcpy( data, tosend, tosendLength );
#if WILDDOG_SELFTEST
    performtest_getDtlsSendTime();
#endif
    wiced_packet_set_data_end( packet, (uint8_t*) ( data + tosendLength ) );
    wilddog_debug_level(WD_DEBUG_LOG, "socketId = %d, port = %d\n", \
        socketId, addr_in->port);

    if ( wiced_udp_send( socket, &ipaddr, addr_in->port, packet ) != \
        WICED_SUCCESS )
    {
        wilddog_debug_level(WD_DEBUG_ERROR, "UDP packet send failed\r\n");
        wiced_packet_delete( packet );
    }
    else
    {
        wilddog_debug_level(WD_DEBUG_LOG, "send packet success!\n");
        return tosendLength;
    }
    return -1;

}
Exemplo n.º 3
0
/*
 * Function:    _wilddog_coap_ackSend.
 * Description: response an ack .
 * Input:       recv_type: recv type conn or non-con .
 *              ack_type : respond an ack or reset.
 *              mid : recv coap's message id.
 *              tokenLen: recv coap's token len.
 *              recv_token: recv coap's token.
 * Output:      N/A.
 * Return:      N/A.
*/
STATIC int WD_SYSTEM _wilddog_coap_ackSend
    (
    u32 recv_type,
    u32 ack_type,
    u32 mid,
    u32 tokenLen,
    u32 recv_token
    ) 
{
    int returnCode= 0;
    unsigned int id = mid;
    size_t tkl = tokenLen;
    unsigned char* p_tk = (unsigned char*)&recv_token;
    coap_pdu_t  *toSend = NULL;
    /* only con request need tobe respond.*/
    if(recv_type != COAP_MESSAGE_CON)
        return WILDDOG_ERR_NOERR;

    toSend = coap_pdu_init(ack_type, 0, id,WILDDOG_PROTO_MAXSIZE);
    coap_add_token(toSend,tkl,p_tk);
    if (toSend == NULL) 
    {
        wilddog_debug_level(WD_DEBUG_ERROR,"coap_addToken error");
        return WILDDOG_ERR_NULL;
    }
    returnCode = _wilddog_sec_send(toSend->hdr,toSend->length);
    
    coap_delete_pdu(toSend);
    return returnCode;

}
Exemplo n.º 4
0
STATIC Wilddog_Return_T WD_SYSTEM _wilddog_coap_destory
    (
    void *p_coap,
    int flag
    )
{
    if(p_coap == 0)
        return WILDDOG_ERR_INVALID;
    
    wilddog_debug_level(WD_DEBUG_LOG,"\tcc\tdestory coap pakge :%p :",p_coap);
    coap_delete_pdu((coap_pdu_t*)p_coap);

    return WILDDOG_ERR_NOERR;
}
/*
 * Function:    _wilddog_url_getChildStr
 * Description: Create the child path of the source path.
 * Input:       p_src_path: The source path.
 *              childName: child's name or path
 * Output:      N/A
 * Return:      Pointer to the child's path.
*/
STATIC Wilddog_Str_T * WD_SYSTEM _wilddog_url_getChildStr
    (
    Wilddog_Str_T*p_srcPath, 
    Wilddog_Str_T* childName
    )
{
    Wilddog_Str_T *p_path = NULL;
    int len = 0;
    int srcLen = 0, childLen = 0;
    int dstPathLen = 0;
    
    if(!childName || !p_srcPath)
        return NULL;
    srcLen = strlen((const char*)p_srcPath);
    childLen = strlen((const char*)childName);

    if(childLen == 0 || \
        (childLen == 1 && childName[0] == '/'))
    {
        return NULL;
    }
    
    if(WILDDOG_ERR_NOERR != _wilddogurl_checkPath(childName))
    {
        wilddog_debug_level(WD_DEBUG_ERROR, "check path error!");
        return NULL;
    }
    len = srcLen + childLen + 3;
    p_path = (Wilddog_Str_T*)wmalloc(len + 1);
    if(NULL == p_path)
    {
        return NULL;
    }

    if(srcLen == 1 && p_srcPath[0] == '/')
    {
        snprintf((char*)p_path, len , "/%s", (char*)childName);
    }
    else
    {
        snprintf((char*)p_path, len , "%s/%s", (char*)p_srcPath, \
            (char*)childName);
    }
    dstPathLen= strlen((const char *)p_path);
    p_path[dstPathLen]= 0;

    return p_path;
}
Exemplo n.º 6
0
/*
 * Function:    _wilddog_recv_getOptionValue.
 * Description: parse  coap option and get it's value.
 *    
 * Input:       p_recvPdu : point to recv coap packet.
 *              optionCode : option code.
 *              p_dst : buffer that will store the  option value.
 *              d_dstSize :  p_dst sizeof.
 * Output:      N/A.
 * Return:      N/A.
*/
BOOL WD_SYSTEM _wilddog_recv_getOptionValue
    (
    coap_pdu_t *p_recvPdu,
    u32 optionCode,
    u8 *p_dst,
    u8 d_dstSize
    )
{
    coap_opt_t *p_op =NULL;
    u8 *p_optionvalue = NULL;
    u8 d_optionlen = 0;
    coap_opt_iterator_t d_oi;

    p_op = coap_check_option(p_recvPdu,optionCode,&d_oi);
    if(p_op)
    {
        d_optionlen = coap_opt_length(p_op);
        if(d_optionlen && d_optionlen <= d_dstSize)
        {
            p_optionvalue = coap_opt_value(p_op);

#if WILDDOG_LITTLE_ENDIAN == 1 
            _sys_coap_ntol((u8*)p_dst,p_optionvalue,d_optionlen); 
#else
            memcpy((u8*)p_dst,p_optionvalue,d_optionlen);
#endif
#if 0
    wilddog_debug_level( WD_DEBUG_WARN, \
                         "option value address = %p ,option len = %d \n",\
                         p_dst,d_optionlen);
#endif
            return TRUE;
        }
 
    }
    
    return FALSE;
}
Exemplo n.º 7
0
/*
 * Function:    _wilddog_coap_creat
 * Description: creat an coap package with no option and payload.
 * Input:       p_arg: cmd/message/token/package len. 
 * Output:      N/A
 * Return:      coap pointer.
*/
STATIC size_t WD_SYSTEM _wilddog_coap_creat
    (
    Protocol_Arg_Creat_T *p_arg,
    int flag
    )
{
    coap_pdu_t *p_pdu = NULL;
    u8 type=0,code = 0;
    if(p_arg == NULL)
        return 0;
    /* get coap type and code.*/
    if(_wilddog_coap_cmd2Typecode(p_arg->cmd,&type,&code) < 0)
        return 0;
    /*creat an coap package.*/
    p_pdu = coap_pdu_init(type,code,p_arg->d_index,p_arg->d_packageLen);

    if(p_pdu == NULL)
        return 0;
    /* add token option.*/
    coap_add_token(p_pdu, COAP_TOKENLEN, (u8*)&(p_arg->d_token));

    wilddog_debug_level(WD_DEBUG_LOG,"\tcc\tcreat coap pakge :%p :",p_pdu);
    return ( size_t )p_pdu;
}
/*
 * Function:    _wilddog_url_parseUrl
 * Description: parse url using wilddog format.
 * Input:       url: Input url.
 * Output:      N/A
 * Return:      Pointer to the url structure.
*/
Wilddog_Url_T * WD_SYSTEM _wilddog_url_parseUrl(Wilddog_Str_T * url)
{
    struct parsed_url * p_paresd_url = NULL;
    Wilddog_Url_T * p_wd_url = NULL;
    int len  = 2;
    
    wilddog_assert(url, NULL);
    
    p_paresd_url = parse_url((char*)url);
    if(NULL == p_paresd_url)
        return NULL;
    
    p_wd_url = (Wilddog_Url_T *)wmalloc(sizeof(Wilddog_Url_T));
    if(NULL == p_wd_url)
    {
        wilddog_debug_level(WD_DEBUG_ERROR, "cannot malloc p_wd_url!\n");
        parsed_url_free(p_paresd_url);
        return NULL;
    }
    
    if(NULL != p_paresd_url->host)
    {
        p_wd_url->p_url_host = (Wilddog_Str_T *)wmalloc( \
            strlen(p_paresd_url->host) + 1);
        if(NULL == p_wd_url->p_url_host)
        {
            wilddog_debug_level(WD_DEBUG_ERROR, "cannot malloc p_url_host!\n");
            _wilddog_url_freeParsedUrl(p_wd_url);
            parsed_url_free(p_paresd_url);
            return NULL;
        }
        strncpy((char*)p_wd_url->p_url_host, (char*)p_paresd_url->host, \
            strlen((const char*)p_paresd_url->host));
    }
    else 
        return NULL;
    
    if(NULL == p_paresd_url->path)
    {
        p_wd_url->p_url_path = (Wilddog_Str_T *)wmalloc(len);
        if(NULL == p_wd_url->p_url_path)
        {
            _wilddog_url_freeParsedUrl(p_wd_url);
            parsed_url_free(p_paresd_url);
            return NULL;
        }
        p_wd_url->p_url_path[0] = '/';
    }
    else
    {
        len += strlen((const char*)p_paresd_url->path);

        if(WILDDOG_ERR_NOERR != \
           _wilddogurl_checkPath((Wilddog_Str_T*)p_paresd_url->path)
           )
        {
            _wilddog_url_freeParsedUrl(p_wd_url);
            parsed_url_free(p_paresd_url);
            return NULL;
        }
        p_wd_url->p_url_path = (Wilddog_Str_T *)wmalloc(len + 1);
        if(NULL == p_wd_url->p_url_path)
        {
            _wilddog_url_freeParsedUrl(p_wd_url);
            parsed_url_free(p_paresd_url);
            return NULL;
        }
        snprintf((char*)p_wd_url->p_url_path, len, "/%s", \
            p_paresd_url->path);
        len = strlen((const char*)p_wd_url->p_url_path);
        if(len > 1 && p_wd_url->p_url_path[len - 1] == '/')
        {
            p_wd_url->p_url_path[len - 1] = 0;
        }
    }
    
    if(NULL != p_paresd_url->query)
    {
        p_wd_url->p_url_query = (Wilddog_Str_T *)wmalloc( \
            strlen((const char*)p_paresd_url->query) + 1);
        if(NULL == p_wd_url->p_url_query)
        {
            _wilddog_url_freeParsedUrl(p_wd_url);
            parsed_url_free(p_paresd_url);
            return NULL;
        }
        strncpy((char*)p_wd_url->p_url_query, (char*)p_paresd_url->query, \
            strlen((const char*)p_paresd_url->query));
    }
    
    parsed_url_free(p_paresd_url);
    return p_wd_url;
}
Exemplo n.º 9
0
/*
 * Function:    wilddog_receive
 * Description: wilddog receive function, it use the interface in wiced platform.
 * Input:        socketId: The socket id.
 *                  addr:  The pointer of Wilddog_Address_T
 *                  buf: The pointer of the send buffer
 *                  bufLen: The length of the send buffer.
 *                  timeout: The max timeout in recv process.
 * Output:      N/A
 * Return:      If success, return the number of bytes received; else return -1.
*/
int wilddog_receive
    ( 
    int socketId, 
    Wilddog_Address_T* addr_in, 
    void* buf, 
    s32 bufLen, 
    s32 timeout
    )
{
    wiced_udp_socket_t* socket = (wiced_udp_socket_t*) socketId;

    wiced_packet_t* receive = NULL;
    u16 totalLeft = 0;
    u16 total = 0;
    uint16_t pos = 0;
    uint8_t* rxData;
    uint16_t fragLeft = 0;
    wiced_ip_address_t recieve_ip_addr;
    uint16_t receive_port;
    wiced_result_t result;
    result = wiced_udp_receive( socket, &receive, timeout );
    if ( result == WICED_SUCCESS )
    {
        if(NULL == receive)
        {
            wilddog_debug_level(WD_DEBUG_ERROR, "receive is NULL!");
            return -1;
        }
        wiced_udp_packet_get_info( receive, &recieve_ip_addr, &receive_port );

        if ( !recieve_ip_addr.ip.v4 == MAKE_IPV4_ADDRESS(addr_in->ip[0], \
            addr_in->ip[1], addr_in->ip[2], addr_in->ip[3]) )
        {
            wilddog_debug_level(WD_DEBUG_ERROR, "addr error!\n" );
            wiced_packet_delete( receive );
            return -1;
        }
        do
        {
            if ( wiced_packet_get_data( receive, 0, (uint8_t**) &rxData, \
                &fragLeft, &totalLeft ) != WICED_SUCCESS )
            {
                wilddog_debug_level(WD_DEBUG_ERROR, \
                    "get data from packet error \r\n" );
                wiced_packet_delete( receive );
                return -1;
            }
            else
            {
                if ( pos + fragLeft > bufLen )
                {
                    /* too large, drop*/
                    wiced_packet_delete( receive );
                    wilddog_debug_level(WD_DEBUG_ERROR, \
                        "too large receive end , get %d bytes\n", pos );
                    return 0;
                }
                if ( total == 0 )
                {
                    /*only get first data, next fragment's total is wrong*/
                    total = totalLeft;
                }

                memcpy( (uint8_t *) ( buf + pos ), rxData, fragLeft );
                pos += fragLeft;

                fragLeft = 0;
            }
            if ( pos == total )
            {
                /*end*/
#if WILDDOG_SELFTEST
                {
                    performtest_getWaitSessionQueryTime();
                    performtest_getWaitRecvTime();
                }
#endif
                wilddog_debug_level(WD_DEBUG_LOG, "received %d data!", pos);
                wiced_packet_delete( receive );
                return pos;
            }
            else if ( pos < total )
            {
                /*get next fragment*/
                wilddog_debug_level(WD_DEBUG_LOG, "more than one packet!");
                if ( WICED_SUCCESS != _packet_get_next_fragment( receive, \
                    &receive ) )
                {
                    wilddog_debug_level(WD_DEBUG_ERROR, \
                        "get next fragment err! %d lost!", total);
					if(receive)
	                    wiced_packet_delete( receive );
                    return -1;
                }
                if ( NULL == receive )
                {
                    wilddog_debug_level(WD_DEBUG_ERROR, \
                        "get next fragment err! %d lost!", total);
                    return -1;
                }

            }
            else
            {
                wiced_packet_delete( receive );
                return -1;
            }
        } while ( pos < total );
    }
    else
    {
        wilddog_debug_level(WD_DEBUG_LOG, "result = %d",result);
    }
    /*if(receive)
        wiced_packet_delete( receive );*/
    return 0;
}
Exemplo n.º 10
0
int test(char *uid)
{
	char url[1024];

	char en_key = 0;
	BOOL isFinished = FALSE;
	Wilddog_T wilddog;
	Wilddog_Handle_T s_handle;
	Wilddog_Node_T *p_head = NULL, *p_node = NULL/*, *p_snapshot = NULL*/;
	u8 value1[5] = {246,12,0,0,6};
	/*u8 value2[4] = {23,67,98,1};*/
	wFloat f = 2.3;

	Wilddog_Node_T *root;
	Wilddog_Node_T *L1c1,*L1c2;
	Wilddog_Node_T *L2c1,*L2c2,*L2c3;
	Wilddog_Node_T *L3c1,*L3c2,*L3c3,*L3c4,*L3c5;
	Wilddog_Node_T *L4c1,*L4c2,*L4c3,*L4c4,*L4c5;

	int count = 0;	
	sprintf(url,"%s%s%s",TEST_URL_HEAD,uid,TEST_URL_END);

	memset(&s_handle, 0, sizeof(s_handle));
	s_handle.isFinished = FALSE;

	printf("*******************************************\n");
	printf("First Step: Build the data tree \n");
	printf("Please press enter key to continue!\n");
	printf("*******************************************\n");
	printf("*******************************************\n");
	printf("\n\n");
	
#ifndef WILDDOG_PORT_TYPE_WICED
	while(1)
	{
		en_key= getchar();
		if(en_key == '\n')
			break;
	}
	en_key = 0;
#endif

	root = wilddog_node_createNum((Wilddog_Str_T *)"root",9999);
	L1c1 = wilddog_node_createFalse((Wilddog_Str_T *)"L1c1");
	L1c2 = wilddog_node_createTrue((Wilddog_Str_T *)"L1c2");
	L2c1 = wilddog_node_createNum((Wilddog_Str_T *)"L2c1",-10000);
	L2c2 = wilddog_node_createFloat((Wilddog_Str_T *)"L2c2",f);
	L2c3 = wilddog_node_createTrue((Wilddog_Str_T *)"L2c3");          //true
	L3c1 = wilddog_node_createBString((Wilddog_Str_T *)"L3c1",value1,sizeof(value1)/sizeof(u8));    //BString
	L3c2 = wilddog_node_createTrue((Wilddog_Str_T *)"L3c2");
	L3c3 = wilddog_node_createTrue((Wilddog_Str_T *)"L3c3");
	L3c4 = wilddog_node_createNull((Wilddog_Str_T *)"L3c4");   //NULL
	L3c5 = wilddog_node_createTrue((Wilddog_Str_T *)"L3c5");
	L4c1 = wilddog_node_createNum((Wilddog_Str_T *)"L4c1",875);//    +Num
	L4c2 = wilddog_node_createNum((Wilddog_Str_T *)"L4c2",-5693);    //   -Num
	L4c3 = wilddog_node_createFloat((Wilddog_Str_T *)"L4c3",f);     //float
	L4c4 = wilddog_node_createFalse((Wilddog_Str_T *)"L4c4");        //false
	L4c5 = wilddog_node_createUString((Wilddog_Str_T *)"L4c5",(Wilddog_Str_T *)"string");      //UString
	
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(root,L1c1))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(root,L1c2))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L1c1,L2c1))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L1c1,L2c2))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L1c2,L2c3))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L2c1,L3c1))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L2c1,L3c2))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L2c2,L3c3))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L2c2,L3c4))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L2c2,L3c5))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L3c2,L4c1))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L3c3,L4c2))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L3c3,L4c3))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L3c5,L4c4))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	if(WILDDOG_ERR_NOERR != wilddog_node_addChild(L3c5,L4c5))
	{
		wilddog_debug_level(WD_DEBUG_ERROR,"node add error");
	}
	

	printf("*******************************************\n");
	printf("The data is built as below:\n");
	wilddog_debug_printnode(root);
	printf("\n");
	printf("*******************************************\n");
	printf("\n\n");


/*
	printf("*******************************************\n");
	printf("Second Step: Use the Node API to change the data tree\n");
	printf("Change the L1c1 node's key from \"L1c1\" to \"L1c1New\" \n");
	wilddog_node_setKey(L1c1,(Wilddog_Str_T *)"L1c1New");
	printf("Change the L3c1 node's value from Bytestring \"{0xf6,0x0c,0x00,0x00,0x06}\" to Bytestring \"{0x17,0x43,0x62,0x01}\"\n ");
	wilddog_node_setValue(L3c1,value2,sizeof(value2)/sizeof(u8));
	printf("Please press enter key to continue!\n");
	printf("*******************************************\n");
	printf("\n\n");

#ifndef WILDDOG_PORT_TYPE_WICED

	while(1)
	{
		en_key= getchar();
		if(en_key == '\n')
			break;
	}
	en_key = 0;
#endif

	printf("*******************************************\n");
	printf("The data tree had changed as below:\n");
	wilddog_debug_printnode(root);
	printf("\n");
	printf("*******************************************\n");
	printf("\n\n");
*/
/*
	printf("*******************************************\n");
	printf("Clone the data tree from L2c2 node\n");
	printf("Print the L2c2 data tree\n");
	p_snapshot = wilddog_node_clone(L2c2);
	wilddog_debug_printnode(p_snapshot);
	wilddog_node_delete(p_snapshot);
	printf("\n");
	printf("*******************************************\n");
	printf("\n\n");
*/

	
	/*************************************************************/
	
	wilddog = wilddog_initWithUrl((Wilddog_Str_T *)url);

	printf("*******************************************\n");
	printf("Third step: Remove the %s's data\n",uid);
	printf("Please press enter key to continue!\n");
	printf("*******************************************\n");
	printf("\n\n");
	
#ifndef WILDDOG_PORT_TYPE_WICED

	while(1)
	{
		en_key= getchar();
		if(en_key == '\n')
			break;
	}
	en_key = 0;
#endif

	wilddog_removeValue(wilddog, test_onDeleteFunc, (void*)&isFinished);
	while(1)
	{
		if(TRUE == isFinished)
		{
			wilddog_debug("remove success!");
			break;
		}
		wilddog_trySync();
	}
	isFinished = FALSE;
	/*************************************************************/

	printf("*******************************************\n");
	printf("It has removed all data in %s!\n",uid);
	printf("Please check the data on %s page\n",url);
	printf("*******************************************\n");
	printf("\n\n");


	printf("*******************************************\n");
	printf("Fourth step: Set the root node to the %s's data tree\n",uid);
	printf("Please press enter key to continue!\n");
	printf("*******************************************\n");
	printf("\n\n");

#ifndef WILDDOG_PORT_TYPE_WICED
	
	while(1)
	{
		en_key= getchar();
		if(en_key == '\n')
			break;
	}
	en_key = 0;
#endif

	wilddog_setValue(wilddog,root,test_onSetFunc,(void*)&isFinished);
	wilddog_node_delete(root);
	
	while(1)
	{
		if(TRUE == isFinished)
		{
			wilddog_debug("set success!");
			break;
		}
		wilddog_trySync();
	}

	isFinished = FALSE;
	
	printf("*******************************************\n");
	printf("It has set the data in %s!\n",uid);
	printf("Please check the data tree on %s page\n",url);
	printf("*******************************************\n");
	printf("\n\n");




/*****************************************************************/
	printf("*******************************************\n");
	printf("Fifth step: Get the %s's data\n",uid);
	printf("Please press enter key to continue!\n");
	printf("*******************************************\n");
	printf("\n\n");

#ifndef WILDDOG_PORT_TYPE_WICED

	while(1)
	{
		en_key= getchar();
		if(en_key == '\n')
			break;
	}
	en_key = 0;
#endif

	s_handle.p_node = NULL;	
	wilddog_getValue(wilddog, test_onQueryFunc, (void*)(&s_handle));
	
	while(1)
	{
		if(s_handle.isFinished)
		{
			if(s_handle.p_node)
			{
				wilddog_debug("print node:");
				wilddog_debug_printnode(s_handle.p_node);
				wilddog_node_delete(s_handle.p_node);
				s_handle.p_node= NULL;
				printf("\n");
				break;
			}
		}
		wilddog_trySync();
	}
	
	isFinished = FALSE;
	printf("*******************************************\n");
	printf("It has get the data in %s!\n",uid);
	printf("Please check the data tree!\n");
	printf("*******************************************\n");
	printf("\n\n");




/*****************************************************************/
	p_head = wilddog_node_createObject((Wilddog_Str_T *)"3");
	p_node = wilddog_node_createNum((Wilddog_Str_T *)"2",1234);
	wilddog_node_addChild(p_head, p_node);
	
	printf("*******************************************\n");
	printf("Sixth step: Build a node as \"{\"2\":1234}\"\n");
	printf("Push the new node to the %s's data tree's root node\n",uid);
	printf("Please press enter key to continue!\n");
	printf("*******************************************\n");
	printf("\n\n");

#ifndef WILDDOG_PORT_TYPE_WICED
	
	while(1)
	{
		en_key= getchar();
		if(en_key == '\n')
			break;
	}
	en_key = 0;
#endif
	
	wilddog_push(wilddog, p_head, test_onPushFunc, (void *)&isFinished);	
	wilddog_node_delete(p_head);
	
	while(1)
	{
		if(isFinished)
		{
			wilddog_debug("push success!");
			break;
		}
		wilddog_trySync();
	}

	printf("*******************************************\n");
	printf("It has pushed the data in %s!\n",uid);
	printf("Please check the data tree on %s page\n",url);
	printf("\{\"2\":1234}\" will have a parent which have a long number\n");
Exemplo n.º 11
0
int main(void)
{
	int i, j;
 
	Wilddog_T p_repo1_ref1 = 0, p_repo1_ref2 =0;
	Wilddog_T p_repo2_ref1 = 0, p_repo2_ref2 = 0,p_repo2_ref1_cpy = 0;
	Wilddog_T p_ref[6];
	Wilddog_T p_parent = 0, p_root = 0, p_child  = 0;

	for(j = 0; j < FUNC_NUM; j++)
	{
		memset(&test_results[j], 0, sizeof(struct test_reult_t));
	}
	test_results[0].name = "wilddog_getParent";
	test_results[1].name = "wilddog_getRoot";
	test_results[2].name = "wilddog_getChild";

	/*
	 *  test the url format
	 */
	for( i = 0; i < sizeof(test_url)/sizeof(char *); i++)
	{
		p_ref[i] = wilddog_initWithUrl((Wilddog_Str_T *)test_url[i]);	

		if(p_ref[i])
		{
			wilddog_debug_level(WD_DEBUG_LOG, "p_ref--  [%d] \n\n", i);
			wilddog_debug_printUrl(p_ref[i]);
			wilddog_debug_level(WD_DEBUG_LOG, "----------------------\n\n\n");
		}
		else
		{
			wilddog_debug_level(WD_DEBUG_LOG, "------------------------\n");
			wilddog_debug_level(WD_DEBUG_LOG, "%s could not init\n\n", test_url[i]);
			wilddog_debug_level(WD_DEBUG_LOG, "------------------------\n");
			
			TEST_RESULT_PRINTF("test_all:wilddog_initWithUrl error",TESTFUNCNAME_TABLECASE,TEST_ERR,ABORT_ERR);
			return ABORT_ERR;
		}
	}

	wilddog_debug_level(WD_DEBUG_LOG, "\n\ndestroy the test url before\n\n");
	for( i = 0; i < sizeof(test_url)/sizeof(char *); i++)
	{
		wilddog_debug_level(WD_DEBUG_LOG, "destroy %d test url %lu\n", i, (long unsigned int)p_ref[i]);
		if(p_ref[i])
			wilddog_destroy(&(p_ref[i]));
	}
	wilddog_debug_level(WD_DEBUG_LOG, "\n\ndestroy the test url after\n\n");

	/*
	 *  test the multi repo and multi ref
	 *  test getparent getchild getroot
	 */
	p_repo1_ref1 = wilddog_initWithUrl((Wilddog_Str_T *)"coaps://appid1.wilddogio.com/a1");
	p_repo1_ref2 = wilddog_initWithUrl((Wilddog_Str_T *)"coaps://appid1.wilddogio.com/a1/b2");

	p_repo2_ref1 = wilddog_initWithUrl((Wilddog_Str_T *)"coaps://appid2.wilddogio.com/c3");
	p_repo2_ref1_cpy = wilddog_initWithUrl((Wilddog_Str_T *)"coaps://appid2.wilddogio.com/c3");
	p_repo2_ref2 = wilddog_initWithUrl((Wilddog_Str_T *)"coaps://appid2.wilddogio.com/c3/d4/e5");
	
	if(p_repo1_ref1)
	{
		wilddog_debug_level(WD_DEBUG_LOG, "repo1--ref1\n\n");
		wilddog_debug_printUrl(p_repo1_ref1);
		wilddog_debug_level(WD_DEBUG_LOG, "----------------------\n\n\n");
	}

	if(p_repo1_ref2)
	{
		wilddog_debug_level(WD_DEBUG_LOG, "repo1--ref2\n\n");
		wilddog_debug_printUrl(p_repo1_ref2);
		wilddog_debug_level(WD_DEBUG_LOG, "----------------------\n\n\n");
	}

	if(p_repo2_ref1)
	{
		wilddog_debug_level(WD_DEBUG_LOG, "repo2--ref1\n\n");
		wilddog_debug_printUrl(p_repo2_ref1);
		wilddog_debug_level(WD_DEBUG_LOG, "----------------------\n\n\n");
	}
	if(p_repo2_ref1 != p_repo2_ref1_cpy)
	{
		TEST_RESULT_PRINTF( "test_all: new same url error ",TESTFUNCNAME_TABLECASE,TEST_ERR,ABORT_ERR);
		return ABORT_ERR;
	}
	if(p_repo2_ref2)
	{
		wilddog_debug_level(WD_DEBUG_LOG, "repo2--ref2\n\n");
		wilddog_debug_printUrl(p_repo2_ref2);
		wilddog_debug_level(WD_DEBUG_LOG, "----------------------\n\n\n");
	}

	p_parent = wilddog_getParent(p_repo1_ref2);
	
	if(p_parent == p_repo1_ref1)
	{
		wilddog_debug_level(WD_DEBUG_LOG, "repo1--ref2--parent\n\n");
		wilddog_debug_printUrl(p_parent);
		test_results[0].result = TRUE;
		wilddog_debug_level(WD_DEBUG_LOG, "----------------------\n\n\n");

	}
	else
	{
		wilddog_debug_level(WD_DEBUG_LOG, "could not get repo1_ref2's parent\n\n");
		TEST_RESULT_PRINTF("test_all:get parent error",TESTFUNCNAME_TABLECASE,TEST_ERR,ABORT_ERR);
		return ABORT_ERR;
	}

	p_root = wilddog_getRoot(p_repo1_ref2);
	if(p_root)
	{
		wilddog_debug_level(WD_DEBUG_LOG, "repo1--ref2--root\n\n");
		wilddog_debug_printUrl(p_root);
		test_results[1].result = TRUE;
		wilddog_debug_level(WD_DEBUG_LOG, "----------------------\n\n\n");
		wilddog_destroy(&p_root);
	}
	else
	{
		wilddog_debug_level(WD_DEBUG_LOG, "could not get repo1_ref2's root\n\n");
		TEST_RESULT_PRINTF("test_all:get root error",TESTFUNCNAME_TABLECASE,TEST_ERR,ABORT_ERR);
		return ABORT_ERR;
	}
	wilddog_debug_level(WD_DEBUG_LOG, "p_repo1_ref1 = %u",( unsigned int)p_repo1_ref1);
	wilddog_debug_printUrl(p_repo1_ref1);
	p_child = wilddog_getChild(p_repo2_ref2, (Wilddog_Str_T *)"c3/d4");
	wilddog_debug_level(WD_DEBUG_LOG, "p_child = %u", ( unsigned int)p_child);
	if(p_child)
	{
		wilddog_debug_level(WD_DEBUG_LOG, "p_repo2_ref2--child\n\n");
		wilddog_debug_printUrl(p_child);
		test_results[2].result = TRUE;
		wilddog_debug_level(WD_DEBUG_LOG, "----------------------\n\n\n");
		wilddog_destroy(&p_child);
	}
	else
	{
		wilddog_debug_level(WD_DEBUG_LOG, "could not get p_repo2_ref2's child /c3/d4\n\n");
		TEST_RESULT_PRINTF("test_all:get chile error",TESTFUNCNAME_TABLECASE,TEST_ERR,ABORT_ERR);
		return ABORT_ERR;
	}
	p_parent = wilddog_getParent(p_repo2_ref2);
	if(p_parent)
	{
		wilddog_debug_level(WD_DEBUG_LOG, "repo2--ref2--parent\n\n");
		wilddog_debug_printUrl(p_parent);
		wilddog_debug_level(WD_DEBUG_LOG, "----------------------\n\n\n");
		wilddog_destroy(&p_parent);
	}
	else
	{
		wilddog_debug_level(WD_DEBUG_LOG, "could not get repo2_ref2's parent\n\n");
	}

	wilddog_debug_level(WD_DEBUG_LOG, "\n\ndestroy all ref\n\n");
		
	wilddog_destroy(&p_repo1_ref1);
	wilddog_destroy(&p_repo1_ref2);
	wilddog_destroy(&p_repo2_ref1);
	wilddog_destroy(&p_repo2_ref2);
	
	for(j = 0 ; j < FUNC_NUM; j++)
	{
		if(test_results[j].name)
		{
			printf("%-32s\t%s\n", test_results[j].name, (test_results[j].result == TRUE?("PASS"):("FAIL")));
			if(test_results[j].result != TRUE)
				return -1;
		}
	}
	return 0;
}
Exemplo n.º 12
0
/*  to do 
 * Function:    _wilddog_coap_receive.
 * Description: recv coap packet and handle it.
 *   
 * Input:       N/A.
 * Output:      N/A.
 * Return:      Wilddog_Return_T type
*/
Wilddog_Return_T WD_SYSTEM _wilddog_coap_receive(void *p_arg,int flag)
{
    int res =0;
    u32 recv_type = 0;
    u32 ack_type = COAP_MESSAGE_ACK;
    u32 tmp_tokenLen = 0;
    u32 tmp_token = 0;
    u32 tmp_mid = 0;
    Protocol_recvArg_T recvArg;
    coap_pdu_t *p_pdu = NULL;

    memset(&recvArg,0,sizeof(Protocol_recvArg_T));

    recvArg.p_recvData = _wilddog_coap_mallocRecvBuffer();
    if( recvArg.p_recvData  == NULL)
    {
        wilddog_debug_level(WD_DEBUG_ERROR, "malloc failed!");
        return WILDDOG_ERR_NULL;
    }
    
    res = _wilddog_sec_recv((void*)recvArg.p_recvData,(s32)WILDDOG_PROTO_MAXSIZE);
    /*@ NO enougth space */
    if( res <= 0 || res  > WILDDOG_PROTO_MAXSIZE ) 
        goto _COAPRECV_ERR;

    recvArg.d_recvDataLen = res;

    /* distinguish recv an coap packet. */
    p_pdu = _wilddog_recvCoap(recvArg.p_recvData,recvArg.d_recvDataLen);
    if(p_pdu == NULL)
        goto _COAPRECV_ERR; 
    
#ifdef WILDDOG_SELFTEST                        
    ramtest_caculate_peakRam();             
    performtest_getHandleRecvDtlsTime();
#endif  

#ifdef WILDDOG_DEBUG
#if DEBUG_LEVEL <= WD_DEBUG_LOG
    printf("recv:\n");
    coap_show_pdu(p_pdu);
#endif
#endif
    /* dispatch .*/
    recvArg.d_recvDataLen = WILDDOG_PROTO_MAXSIZE;
    if( _wilddog_recv_dispatch(p_pdu,&recvArg) < 0)
    {
        coap_delete_pdu(p_pdu);
        goto _COAPRECV_ERR;
            
    }
    tmp_mid = p_pdu->hdr->id;
    recv_type = p_pdu->hdr->type;
    tmp_tokenLen = p_pdu->hdr->token_length;
    memcpy(&tmp_token,p_pdu->hdr->token,p_pdu->hdr->token_length);

    coap_delete_pdu(p_pdu);
    /* call back.*/
    if( _wilddog_coap_findRespondNode(&recvArg) != TRUE)
        ack_type = COAP_MESSAGE_RST;
    /* ack */
    _wilddog_coap_ackSend(recv_type,ack_type,tmp_mid,tmp_tokenLen,tmp_token);
    _wilddog_coap_freeRecvBuffer( recvArg.p_recvData );
   
    return  WILDDOG_ERR_NOERR;
   
_COAPRECV_ERR:
    _wilddog_coap_freeRecvBuffer( recvArg.p_recvData );
    return WILDDOG_ERR_NULL;
}