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

Reply
  • 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

Children
Related