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

Sending message from LPN to friend node

Hi everyone
Currently, I am working on a project related to the mesh network friendship feature on nrf52840. I am having the following problem: I have established a friendship between my model and example server light_switch I have written a function to send a message there but at the moment I don't see the server-side telling me if I receive the message. . So what can I do?

Parents
  • Hi. 

    Are you sure that the client did send the message?

    Are the message received on the server, but the problem is that it is not ACK'ed to the client?

    Can you please add some more information on how you are sending the message from the client. Preferably adding come snippet of your code. Also, SDK version etc. could be valuable information. 

    Br, 
    Joakim

  • #include "ocsensor_fr.h"
    #include "mesh_app_utils.h"
    #include "nrf_strerror.h"
    #include "node_ocsensor_model.h"
    
    void initiate_friendship()
    {
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Initiating the friendship establishment procedure.\n");
    
        mesh_lpn_friend_request_t freq;
        freq.friend_criteria.friend_queue_size_min_log = MESH_FRIENDSHIP_MIN_FRIEND_QUEUE_SIZE_16;
        freq.friend_criteria.receive_window_factor = MESH_FRIENDSHIP_RECEIVE_WINDOW_FACTOR_1_0;
        freq.friend_criteria.rssi_factor = MESH_FRIENDSHIP_RSSI_FACTOR_2_0;
        freq.poll_timeout_ms = POLL_TIMEOUT_MS;
        freq.receive_delay_ms = RECEIVE_DELAY_MS;
    
        uint32_t status = mesh_lpn_friend_request(freq, FRIEND_REQUEST_TIMEOUT_MS);
    
        switch (status)
        {
            case NRF_SUCCESS:
                break;
    
            case NRF_ERROR_INVALID_STATE:
                __LOG(LOG_SRC_APP, LOG_LEVEL_ERROR, "Already in an active friendship\n");
                break;
    
            case NRF_ERROR_INVALID_PARAM:
                __LOG(LOG_SRC_APP, LOG_LEVEL_ERROR, "Friend request parameters outside of valid ranges.\n");
                break;
    
            default:
                ERROR_CHECK(status);
                break;
        }
    }
    
    void terminate_friendship()
    {
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Terminating the active friendship\n");
    
        uint32_t status = mesh_lpn_friendship_terminate();
        switch (status)
        {
            case NRF_SUCCESS:
                break;
    
            case NRF_ERROR_INVALID_STATE:
                __LOG(LOG_SRC_APP, LOG_LEVEL_ERROR, "Not in an active friendship\n");
                break;
    
            default:
                ERROR_CHECK(status);
                break;
        }
    }
    void mesh_core_event_cb(const nrf_mesh_evt_t * p_evt)
    {
        switch (p_evt->type)
        {
            case NRF_MESH_EVT_LPN_FRIEND_OFFER:
            {
                const nrf_mesh_evt_lpn_friend_offer_t * p_offer = &p_evt->params.friend_offer;
    
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Received friend offer from 0x%04X\n", p_offer->src);
    
                uint32_t status = mesh_lpn_friend_accept(p_offer);
                switch (status)
                {
                    case NRF_SUCCESS:
                        break;
    
                    case NRF_ERROR_INVALID_STATE:
                    case NRF_ERROR_INVALID_PARAM:
                    case NRF_ERROR_NULL:
                        __LOG(LOG_SRC_APP, LOG_LEVEL_ERROR, "Cannot accept friendship: %d\n", status);
                        break;
    
                    default:
                        ERROR_CHECK(status);
                        break;
                }
                if (status != NRF_SUCCESS)
                {
                    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Check error [%s] \n", nrf_strerror_get(status));
                    break;
                }
                break;
            }
    
            case NRF_MESH_EVT_LPN_FRIEND_POLL_COMPLETE:
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Friend poll procedure complete\n");
                break;
    
            case NRF_MESH_EVT_LPN_FRIEND_REQUEST_TIMEOUT:
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Friend Request timed out\n");
                break;
    
            case NRF_MESH_EVT_FRIENDSHIP_ESTABLISHED:
            {
                const nrf_mesh_evt_friendship_established_t * p_est = &p_evt->params.friendship_established;
                (void)p_est;
    
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Friendship established with: 0x%04X\n", p_est->friend_src);
    
                break;
            }
    
            case NRF_MESH_EVT_FRIENDSHIP_TERMINATED:
            {
                const nrf_mesh_evt_friendship_terminated_t * p_term = &p_evt->params.friendship_terminated;
                UNUSED_VARIABLE(p_term);
    
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Friendship with 0x%04X terminated. Reason: %d\n", p_term->friend_src, p_term->reason);
                break;
            }
    
            default:
                break;
        }
    }
    static size_t length_message(uint8_t type)
    {
        switch (type)
        {
            case OCSENSOR_MESSAGE_REP_TYPE_REP_STATE:
                return (OCSENSOR_MESSAGE_REP_HEADER + sizeof(ocsensor_message_state_t));
    
            case OCSENSOR_MESSAGE_REP_TYPE_REP_GET_INFO:
            case OCSENSOR_MESSAGE_REP_TYPE_REP_GET_INFO_HAVE_INTERNET:
                return (OCSENSOR_MESSAGE_REP_HEADER + sizeof(ocsensor_message_info_t));
        }
    
        return 0;
    }
    static void message_create(node_ocsensor_model_init_params_t * p_model, uint16_t tx_opcode, const uint8_t * p_buffer, uint16_t length, access_message_tx_t * p_message)
    {
        p_message->opcode.opcode = tx_opcode;
        p_message->opcode.company_id = HUNONIC_COMPANY_ID;
        p_message->p_buffer = p_buffer;
        p_message->length = length;
        p_message->force_segmented = false;
        p_message->transmic_size = NRF_MESH_TRANSMIC_SIZE_DEFAULT;
        p_message->access_token = nrf_mesh_unique_token_get();
    }
    //
    uint32_t message_unack(node_ocsensor_model_init_params_t * p_model, generic_onoff_status_params_t * params, const oc_sensor_transition_t * p_transition, uint8_t repeats)
    {
        if (p_model == NULL || params == NULL)
        {
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "NRF_ERROR_NULL \n");
            return NRF_ERROR_NULL;
        }
    
        if (p_transition != NULL && (p_transition->transition_time_ms > TRANSITION_TIME_MAX_MS || p_transition->delay_ms > DELAY_TIME_MAX_MS))
        {
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "NRF_ERROR_INVALID_PARAM \n");
            return NRF_ERROR_INVALID_PARAM;
        }
        oc_set_msg_pkt_t msg;
    
        //uint8_t msg_length = message_set_packet_create(&msg, p_rep, p_transition);
    
        message_create(p_model, HUNONIC_OPCODE_GATEWAY_OCSENSOR_TX, (const uint8_t *)&msg, 8, &p_model->access_message.message);
        
        uint32_t status = NRF_SUCCESS;
    
        for (uint32_t i = 0; i <= repeats; ++i)
        {
            status = access_model_publish(p_model->model_handle, &p_model->access_message.message);
            if (status != NRF_SUCCESS)
            {
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "access_model_publish [%s] \n", nrf_strerror_get(status));
                break;
            }
        }
        return status;
    }
    
    void send_msg_to_fr(bool state, node_ocsensor_model_init_params_t * p_model)
    {
        uint32_t status = NRF_SUCCESS;
    
        static uint8_t tid = 0;
        generic_onoff_status_params_t params;
        oc_sensor_transition_t transition_params;
    
        params.present_on_off = state;
    
        transition_params.delay_ms = 50;
        transition_params.transition_time_ms = 100;
    
        DEBUG_LOG("Sending msg: %d\n", params.present_on_off);
    
        status = message_unack(p_model, &params, &transition_params, 2);
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "test [%s] \n", nrf_strerror_get(status));
        DEBUG_LOG("Send Ret[%u]\n", status);
    
        switch (status)
        {
            case NRF_SUCCESS:
                break;
    
            case NRF_ERROR_NO_MEM:
            break;
            case NRF_ERROR_BUSY:
            break;
            case NRF_ERROR_INVALID_STATE:
                __LOG(LOG_SRC_APP, LOG_LEVEL_ERROR, "Cannot send the message\n");
                break;
    
            case NRF_ERROR_INVALID_PARAM:
                __LOG(LOG_SRC_APP, LOG_LEVEL_ERROR, "Publication not configured\n");
                break;
    
            default:
                break;
        }
    }
    

Reply Children
No Data
Related