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

How to run proxy and Nus at the same time

Hi all, 

when I insert ble_nus into mesh_gatt . I realized there are two pulse functions with the same function: 

void ble_nus_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
    if ((p_context == NULL) || (p_ble_evt == NULL))
    {
        return;
    }

    ble_nus_t * p_nus = (ble_nus_t *)p_context;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            on_connect(p_nus, p_ble_evt);
            break;

        case BLE_GATTS_EVT_WRITE:
            on_write(p_nus, p_ble_evt);
            break;

        case BLE_GATTS_EVT_HVN_TX_COMPLETE:
            on_hvx_tx_complete(p_nus, p_ble_evt);
            break;

        default:
            // No implementation needed.
            break;
    }
}

and 

void mesh_gatt_on_ble_evt(const ble_evt_t * p_ble_evt, void * p_context)
{
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            connect_evt_handle(p_ble_evt);
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            disconnect_evt_handle(p_ble_evt);
            break;

        case BLE_GATTS_EVT_WRITE:
            write_evt_handle(p_ble_evt);
            break;

        case BLE_GATTS_EVT_HVN_TX_COMPLETE:
            tx_complete_handle(p_ble_evt->evt.gatts_evt.conn_handle);
            break;


        /* TODO: The following events should be handled by an SDK module/the application. */
        case BLE_GATTS_EVT_SYS_ATTR_MISSING:
        {
            uint32_t err_code = sd_ble_gatts_service_changed(p_ble_evt->evt.gatts_evt.conn_handle,
                                                             m_gatt.handles.service,
                                                             m_gatt.handles.service);

            /* Those errors can be expected when sending trying to send Service Changed indication
             * if the CCCD is not set to indicate. Thus set the returning error code to success. */
            NRF_MESH_ASSERT((err_code == NRF_SUCCESS) ||
                            (err_code == BLE_ERROR_INVALID_CONN_HANDLE) ||
                            (err_code == NRF_ERROR_INVALID_STATE) ||
                            (err_code == NRF_ERROR_BUSY));
            NRF_MESH_ERROR_CHECK(sd_ble_gatts_sys_attr_set(p_ble_evt->evt.gatts_evt.conn_handle, NULL, 0, 0));
            break;
        }

        case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
        {
            ble_gap_data_length_params_t dl_params;
            memset(&dl_params, BLE_GAP_DATA_LENGTH_AUTO, sizeof(dl_params));
            NRF_MESH_ERROR_CHECK(sd_ble_gap_data_length_update(p_ble_evt->evt.gap_evt.conn_handle,
                                                               &dl_params,
                                                               NULL));
            break;
        }

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            NRF_MESH_ERROR_CHECK(sd_ble_gap_sec_params_reply(p_ble_evt->evt.gap_evt.conn_handle,
                                                             BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP,
                                                             NULL,
                                                             NULL));
            break;

        case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
        {
            ble_gap_phys_t const phys =
            {
                .rx_phys = BLE_GAP_PHY_AUTO,
                .tx_phys = BLE_GAP_PHY_AUTO,
            };
            NRF_MESH_ERROR_CHECK(sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys));
            break;
        }

        case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
            exchange_mtu_req_handle(p_ble_evt);
            break;

        case BLE_GATTS_EVT_SC_CONFIRM:
            break;

        default:
            break;
    }
}

if they let them coexist , when I use nrf_connect connect and use mesh_app connect , It jump to 2 function , and I only want , if I connect to nus , it jump to ble_nus_on_ble_evt , If I connect to proxy , It jump to mesh_gatt_on_ble_evt . Is this feasible? Can you give me advice ? Thank !!!

  • In the regular ble examples there is a dispatch function that calls all *_ble_evt functions for the project. I suggest to take the same approach. All the individual handlers ( on_disconnect/ disconnect_evt_handle etc.) should check if the message is for them, and if not, ignore it. Therefore, writing a dispatch function and calling both should work. 

  • In BLE_GAP_EVT_CONNECTED , how to know the connection of nus or proxy ? 

  • At the point of the BLE_GAP_EVT_CONNECTED, there is no distinction yet. The differences are only on events like BLE_GATTS_EVT_WRITE and BLE_GATTS_EVT_HVN_TX_COMPLETE. It's the same connection, but one reads/writes to different characteristics. Ones accessing a characteristic, one compares the handle in the p_ble_evt with the handle of the characteristic of the NUS or Proxy to determine if it is being addressed.

  • I have this example, which I am still working on. It's not "clean" as I cann the nus event handler from the mesh gatt event handler in stead of making a dispatcher. But it's a part of a project I've been working on myself.

    At least this code gets into the nus data handler with the data I sent to it. That's as far as I have tested this code.

    void mesh_gatt_on_ble_evt(const ble_evt_t * p_ble_evt, void * p_context)
    {
    
    	/// we need to put in this NUS this somewhere... this not not neat
    	// to do it here... but until I find a better structure to support this
    	// within the mesh sdk.
        ble_nus_on_ble_evt(p_ble_evt, &g_nus); // curious, did the parameters swap on this SDK version?
    	///!!!
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
    
            	// added for nus support
    
    
            	//
    
                connect_evt_handle(p_ble_evt);
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                disconnect_evt_handle(p_ble_evt);
                break;
    
            case BLE_GATTS_EVT_WRITE:
                write_evt_handle(p_ble_evt);
                break;
    
            case BLE_GATTS_EVT_HVN_TX_COMPLETE:
                tx_complete_handle(p_ble_evt->evt.gatts_evt.conn_handle);
                break;
    
    
            /* TODO: The following events should be handled by an SDK module/the application. */
            case BLE_GATTS_EVT_SYS_ATTR_MISSING:
            {
            	/*
                uint32_t err_code = sd_ble_gatts_service_changed(p_ble_evt->evt.gatts_evt.conn_handle,
                                                                 m_gatt.handles.service,
                                                                 m_gatt.handles.service);
    
                // Those errors can be expected when sending trying to send Service Changed indication
                // if the CCCD is not set to indicate. Thus set the returning error code to success.
                NRF_MESH_ASSERT((err_code == NRF_SUCCESS) ||
                                (err_code == BLE_ERROR_INVALID_CONN_HANDLE) ||
                                (err_code == NRF_ERROR_INVALID_STATE) ||
                                (err_code == NRF_ERROR_BUSY));
                NRF_MESH_ERROR_CHECK(sd_ble_gatts_sys_attr_set(p_ble_evt->evt.gatts_evt.conn_handle, NULL, 0, 0));
                break;
                */
    
            	// Well... the original code above crashes when adding the NUS. I've used the following
            	// code to handle BLE_GATTS_EVT_SYS_ATTR_MISSING in another NUS based project.
            	// I'd say gatts_service_changes ain't a mesh thing thus this should pose no problem.
            	uint32_t err_code = sd_ble_gatts_sys_attr_set(p_ble_evt->evt.gatts_evt.conn_handle, NULL, 0, 0);
            	break;
            }
    
            case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
            {
                ble_gap_data_length_params_t dl_params;
                memset(&dl_params, BLE_GAP_DATA_LENGTH_AUTO, sizeof(dl_params));
                NRF_MESH_ERROR_CHECK(sd_ble_gap_data_length_update(p_ble_evt->evt.gap_evt.conn_handle,
                                                                   &dl_params,
                                                                   NULL));
                break;
            }
    
            case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
                NRF_MESH_ERROR_CHECK(sd_ble_gap_sec_params_reply(p_ble_evt->evt.gap_evt.conn_handle,
                                                                 BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP,
                                                                 NULL,
                                                                 NULL));
                break;
    
            case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
            {
                ble_gap_phys_t const phys =
                {
                    .rx_phys = BLE_GAP_PHY_AUTO,
                    .tx_phys = BLE_GAP_PHY_AUTO,
                };
                NRF_MESH_ERROR_CHECK(sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys));
                break;
            }
    
            case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
                exchange_mtu_req_handle(p_ble_evt);
                break;
    
            case BLE_GATTS_EVT_SC_CONFIRM:
                break;
    
            default:
                break;
        }
    }

Related