This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Coap response error

Hi, 

I have a problem with coap response. I use iot coap library.

Client send nonconfirmable coap message with payload to broadcast addr

Server get payload and send nonconfirmable response without payload to p_message.remote address.

Server use message id and token from request but client dont get response.

I was seen the request and response from server on nrf sniffer but client dont get the response, but cames response error.

Can you help me find problem?

Thank you.

Frantisek

Parents
  • void coap_pir_send_request(uint8_t state)
    {
        coap_message_t       * p_request;
        coap_message_conf_t   message_conf;
        coap_remote_t         remote_device;
        uint32_t              err_code;
        
        
        memset(&message_conf, 0, sizeof(message_conf));
        message_conf.type             = COAP_TYPE_NON;
        message_conf.code             = COAP_CODE_GET;
        message_conf.port.port_number = COAP_PORT;
        message_conf.id               = coap_header_message_id++; 
    
        (void)uint16_encode(HTONS(coap_header_token), message_conf.token);
        coap_header_token++;
        
        message_conf.token_len = sizeof(coap_header_token);
        message_conf.response_callback = coap_pir_response_callback;
        
        
        err_code = coap_message_new(&p_request, &message_conf);
        APP_ERROR_CHECK(err_code);
        
        //TODO leader address
        //fde5:8dba:82e1:1::ff:fe00:fc00
        err_code = otIp6AddressFromString((char *)"ff03::1", (otIp6Address *)&remote_device.addr[0]);
        ASSERT(err_code == OT_ERROR_NONE);
        remote_device.port_number = COAP_PORT;
        
        err_code = coap_message_remote_addr_set(p_request, &remote_device);
        APP_ERROR_CHECK(err_code);
        
        err_code = coap_message_opt_str_add(p_request, COAP_OPT_URI_PATH, (uint8_t *)"pir", 3);
        APP_ERROR_CHECK(err_code);
    
        err_code = coap_message_payload_set(p_request, &state, sizeof(state));
        APP_ERROR_CHECK(err_code);
    
        uint32_t handle;
        NRF_LOG_INFO("send coap message pir state");
        err_code = coap_message_send(&handle, p_request);
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_ERROR("CoAP Message skipped, error code = 0x%08lX.", err_code);
        }
        err_code = coap_message_delete(p_request);
        APP_ERROR_CHECK(err_code);
        //TODO led
        //nrf_gpio_pin_toggle(12);
    }
    
    
    void coap_pir_response_callback(uint32_t status, void * p_arg, coap_message_t * p_message)
    {   
        NRF_LOG_INFO("coap pi response callback ---------------");
        if(status == NRF_SUCCESS)
        {
            NRF_LOG_INFO("pir response");
            coap_pir_msg_resend = 0;
        }
        else
        {
            NRF_LOG_INFO("response error");
            if(coap_pir_msg_resend++ < COAP_PIR_MSG_MAX_RESEND)
            {
                coap_pir_send_request(coap_pir_last_state);
            }
            else
            {
                coap_pir_msg_resend = 0;
                NRF_LOG_INFO("error: no response from lamp");
            }
        }
    }

    client code

    void coap_pir_request_callback(coap_resource_t * p_resource, coap_message_t * p_request)
    {
        coap_message_conf_t pir_response_config;
        NRF_LOG_INFO("coap pir rq callback");
        //evaluate pir request
        //------------------------------------------------
        if(use_pir_messages)
        {
            if(p_request->p_payload[0] == 1)
            {
                //movement stop
                pir_set_lc_pin(1);
            }
            else
            {
                //movement start
                pir_set_lc_pin(0);
            }
        }    
        //-------------------------------------------------
        
        if (p_request->header.type == COAP_TYPE_NON)
        {
            pir_response_config.type = COAP_TYPE_NON;
        }
        else if (p_request->header.type == COAP_TYPE_CON)
        {
            pir_response_config.type = COAP_TYPE_ACK;
        }
        pir_response_config.id = p_request->header.id;
        pir_response_config.code = COAP_CODE_204_CHANGED;
        pir_response_config.port.port_number = COAP_PORT;
        pir_response_config.token_len = p_request->header.token_len;
        memcpy(&pir_response_config.token[0], &p_request->token[0], p_request->header.token_len);
        
        coap_pir_response_send(pir_response_config, p_request->remote);
    }
    
    void coap_pir_response_send(coap_message_conf_t message_conf, coap_remote_t remote_address)
    {
        coap_message_t * p_response;
        uint32_t msg_handle;
    
        uint32_t err_code = coap_message_new(&p_response, &message_conf);
        APP_ERROR_CHECK(err_code);
        
        err_code = coap_message_remote_addr_set(p_response, &remote_address);
        APP_ERROR_CHECK(err_code);
            
        err_code = coap_message_send(&msg_handle, p_response);
        NRF_LOG_INFO("sen pir resp %d", err_code);
        APP_ERROR_CHECK(err_code);
    	
        NRF_LOG_INFO("sended coap message pir response");
    	
        err_code = coap_message_delete(p_response);
        APP_ERROR_CHECK(err_code);
    }
    
    
    //------------------------------------------------------------------------------
    //---------------------------- coap mod_mes messages ---------------------------
    //------------------------------------------------------------------------------
    
    void coap_mod_msg_request_callback(coap_resource_t * p_resource, coap_message_t * p_request)
    {
    //TODO
        nrf_gpio_pin_toggle(12);
        
        //save info about peer
        if (p_request->header.type == COAP_TYPE_NON)
        {
            response_config.type = COAP_TYPE_NON;
        }
        else if (p_request->header.type == COAP_TYPE_CON)
        {
            response_config.type = COAP_TYPE_ACK;
        }
        response_config.code = COAP_CODE_205_CONTENT;
        response_config.id = p_request->header.id;
        response_config.port.port_number = COAP_PORT;
        response_config.token_len = p_request->header.token_len;
        memcpy(&response_config.token[0], &p_request->token[0], p_request->header.token_len);
        remote = p_request->remote;
        
        if(memcmp(p_request->p_payload, modbus_cmd_hello_get_lcid, p_request->payload_len) == 0)
        {
            address_state = ADDRESS_STATE_LCID_NOT_SEND;
            coap_mod_msg_response_send(NULL, 0);        
        }
        else
        {
            modbus_lc_request(p_request->p_payload, p_request->payload_len);
        }
        
        NRF_LOG_INFO("received data, len: %d",p_request->payload_len);
    }
    
    
    
    void coap_mod_msg_response_send(uint8_t* data, uint16_t size)
    {
        coap_message_t * p_response;
        uint32_t msg_handle;
    	
        uint32_t err_code = coap_message_new(&p_response, &response_config);
        APP_ERROR_CHECK(err_code);
        
        err_code = coap_message_remote_addr_set(p_response, &remote);
        APP_ERROR_CHECK(err_code);
        
    	if(size < 0 || data != NULL)
        {
            err_code = coap_message_payload_set(p_response, data, size);
            APP_ERROR_CHECK(err_code);
        }
        
        NRF_LOG_INFO("response send, size of send %d", size);
        
        err_code = coap_message_send(&msg_handle, p_response);
        NRF_LOG_INFO("error %d: ", err_code);
        //APP_ERROR_CHECK(err_code);
    	
        NRF_LOG_INFO("sended coap message mod_msg response");
    	
        err_code = coap_message_delete(p_response);
        APP_ERROR_CHECK(err_code);   
    }
    
    //------------------------------------------------------------------------------
    //---------------------------- coap address messages ---------------------------
    //------------------------------------------------------------------------------
    void coap_address_send_request(uint16_t lcid)
    {
        coap_message_t       * p_request;
        coap_message_conf_t   message_conf;
        coap_remote_t         remote_device;
        uint32_t              err_code;
        
        
        memset(&message_conf, 0, sizeof(message_conf));
        message_conf.type             = COAP_TYPE_NON;
        message_conf.code             = COAP_CODE_GET;
        message_conf.port.port_number = COAP_PORT;
        message_conf.id               = coap_header_message_id++; 
    
        (void)uint16_encode(HTONS(coap_header_token), message_conf.token);
        coap_header_token++;
        
        message_conf.token_len = sizeof(coap_header_token);
        message_conf.response_callback = coap_address_response_callback;
        
        
        err_code = coap_message_new(&p_request, &message_conf);
        APP_ERROR_CHECK(err_code);
        
        //TODO leader address
        err_code = otIp6AddressFromString((char *)"ff03::1", (otIp6Address *)&remote_device.addr[0]);
        ASSERT(err_code == OT_ERROR_NONE);
        remote_device.port_number = COAP_PORT;
       
        err_code = coap_message_remote_addr_set(p_request, &remote_device);
        APP_ERROR_CHECK(err_code);
        
        err_code = coap_message_opt_str_add(p_request, COAP_OPT_URI_PATH, (uint8_t *)"adr", 3);
        APP_ERROR_CHECK(err_code);
    
        err_code = coap_message_payload_set(p_request, &lcid, sizeof(uint16_t));
        APP_ERROR_CHECK(err_code);
    
        uint32_t handle;
        NRF_LOG_INFO("send coap message lcid");
        err_code = coap_message_send(&handle, p_request);
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_ERROR("CoAP Message skipped, error code = 0x%08lX.", err_code);
        }
        err_code = coap_message_delete(p_request);
        APP_ERROR_CHECK(err_code);
        //TODO led
        //nrf_gpio_pin_toggle(12);
    }
    
    
    void coap_address_response_callback(uint32_t status, void * p_arg, coap_message_t * p_message)
    {   
        if(status == NRF_SUCCESS)
        {
            address_state = ADDRESS_STATE_UP_TO_DATE;
            addresses.ipv6 = get_my_actual_ip6_addr();
            NRF_LOG_INFO("addr response------PAUSE----");
        }
        else
        {
            NRF_LOG_INFO("response error");
        }
        
    }
    

    server code

  • Maybe you can share the sniffer log? Do the issue only occur with empty payload in the response, or does it also happen if you piggy back the response? Try to memset(&pir_response_config, 0x00sizeof(coap_message_conf_t));

    Is the status code COAP_TRANSMISSION_RESET_BY_PEER or COAP_TRANSMISSION_TIMEOUT?

  • Hi, based on the sniffer log you sent I thought you were developing with Thread, since it says 802.15.4, but now I'm not so sure. The functions you are using in your code come from the BLE IoT library. So are you using the BLE based IoT library?

    If so, can you post the output of "ifconfig" and specifically check if the bt0 interface has gotten a global IP? Also, have you installed radvd so the global IP is given to the client?

    Also please describe the whole setup. Which protocol you're developing on, which libraries, what type of hardware, DKs, computers, servers, etc... Thanks a lot!

  • I use library from: nRF5_SDK_for_Thread_and_Zigbee_v4.1.0_32ce5f8\components\iot\coap

    I use Thread but I communicate point to point without border router

    I use nRF52840 specifically Fanstel BT840F

  • I try to send multicast message for link-local scope and I need to know that at least 1 node get the message.

    client                              ->                              server

    multicast NONconfirmable request                                     

    server                             ->                              client

    unicast NONconfirmable response, the same token, different message ID 

    is it the coorect design? 

    This is pattern for message exchange. 

    Message ID is different but token is the same. 



    If a request is sent in a Non-confirmable message, then the response is sent using a new Non-confirmable message, although the server may instead send a Confirmable message. This type of exchange is illustrated in Figure 6. Client Server | | | NON [0x7a11] | | GET /temperature | | (Token 0x74) | +----------------->| | | | NON [0x23bc] | | 2.05 Content | | (Token 0x74) | | "22.5 C" | |<-----------------+ | | Figure 6: A Request and a Response Carried in Non-confirmable Messages



    https://tools.ietf.org/html/rfc7252#section-2.2
  • Hello,

    Sorry for the delay. 

    So you are not using Thread, then? The coap module from components\iot\coap is not part of Thread nor Zigbee (and it is actually removed/deprecated in the latest SDK, SDK17). The 802.15.4 drivers that are used in Thread in the Thread and Zigbee SDK is found in SDK\external\nRF-IEEE-802.15.4-radio-driver, but the coap used in this SDK is part of the openthread library. 

    I am struggling to understand your actual issue. Is it that the message ID is different from the request to the response? (the "get temperature" message and reply message with the temperature?)

    If so, I can try to ask our Thread and Zigbee team, but they are typically not that familiar with the "iot" module, as this is old and was never actually finished. 

    Best regards,

    Edvin

  • I must use IOT module because I use DFU OTA modul and it has problem with default coap library

Reply Children
No Data
Related