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

cable replacement service

Hi everyone for my application I must use the Bluegiga dongle like central (PC side) and nRF51822 for peripheral (hardware side). So I implemented cable replacement app and I created a new service starting from ble_nus.c (see ble_app_uart example) and the suggestions that I found here devzone.nordicsemi.com/.../. This service use only one custom characteristic for matching with the dongle, in indicate mode. I put the snippet code below:

static uint32_t tx_char_add(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init)
{
    ble_gatts_char_md_t char_md;
	ble_gatts_attr_md_t cccd_md;
    ble_gatts_attr_t    attr_char_value;
    ble_uuid_t          ble_tx_uuid;
    ble_gatts_attr_md_t attr_md;
	uint32_t			err_code;
    
	memset(&cccd_md, 0, sizeof(cccd_md));

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
	BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
    cccd_md.vloc = BLE_GATTS_VLOC_STACK;

    memset(&char_md, 0, sizeof(char_md));
    
    char_md.char_props.write		    = 1;
	char_md.char_props.indicate         = 1;
    char_md.p_char_user_desc            = NULL;
    char_md.p_char_pf                   = NULL;
    char_md.p_user_desc_md              = NULL;
    char_md.p_cccd_md                   = &cccd_md;
    char_md.p_sccd_md                   = NULL;
    
	 ble_uuid128_t nus_tx_base_uuid = NUS_TX_BASE_UUID;  
	 err_code = sd_ble_uuid_vs_add(&nus_tx_base_uuid, &ble_tx_uuid.type);
	 if (err_code != NRF_SUCCESS)
		{
			return err_code;
		}
	
    ble_tx_uuid.uuid = BLE_UUID_NUS_TX_CHARACTERISTIC;
    
    memset(&attr_md, 0, sizeof(attr_md));

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
    
    attr_md.vloc                        = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth                     = 0;
    attr_md.wr_auth                     = 0;
    attr_md.vlen                        = 1;
    
    memset(&attr_char_value, 0, sizeof(attr_char_value));

    attr_char_value.p_uuid              = &ble_tx_uuid;
    attr_char_value.p_attr_md           = &attr_md;
    attr_char_value.init_len            = 1;
    attr_char_value.init_offs           = 0;
    attr_char_value.max_len             = BLE_NUS_MAX_TX_CHAR_LEN;
    
    return sd_ble_gatts_characteristic_add(p_nus->service_handle,
                                           &char_md,
                                           &attr_char_value,
                                           &p_nus->tx_handles);
}

...

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

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

        case BLE_GAP_EVT_DISCONNECTED:
            on_disconnect(p_nus, p_ble_evt);
            break;

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

	    case BLE_GATTS_EVT_HVC:
            on_hvc(p_nus, p_ble_evt);
            break;
				
        default:
            // No implementation needed.
            break;
    }
}

...

uint32_t ble_nus_send_string(ble_nus_t * p_nus, uint8_t * string, uint16_t length)
{
    ble_gatts_hvx_params_t hvx_params;

    if (p_nus == NULL)
    {
        return NRF_ERROR_NULL;
    }
    
    if ((p_nus->conn_handle == BLE_CONN_HANDLE_INVALID))
    {
        return NRF_ERROR_INVALID_STATE;
    }
    
    if (length > BLE_NUS_MAX_DATA_LEN)
    {
        return NRF_ERROR_INVALID_PARAM;
    }
    
    memset(&hvx_params, 0, sizeof(hvx_params));

    hvx_params.handle = p_nus->rx_handles.value_handle;
    hvx_params.p_data = string;
    hvx_params.p_len  = &length;
    hvx_params.type   = BLE_GATT_HVX_INDICATION;
    
    return sd_ble_gatts_hvx(p_nus->conn_handle, &hvx_params);
}

It works fine, when I transmit some data from PC I receive the data in Uart line; but if I transmit data from hardware there is some issues. If I transmit data <20 byte, PC receive it but not always and if I transmit data >20 byte the NRF51822 disconnect. I think that my modify is not enough... I have some problem with indicate service, I think... Some suggestion? If anyone are interesting about this application I'm available to discuss it.

Thanks a lot. Paolo A.

Parents Reply Children
No Data
Related