Page 1 of 1

esp_ble_gatt_set_local_mtu not work on Gatts Server code

Posted: Sun Jun 10, 2018 11:28 am
by avdalimov14
Hi folks,

While I was implementing Gatt Server and Client on the same esp32 component, I've figured that after registering the Gatt server handler the command "esp_ble_gatt_set_local_mtu" is doing nothing.
After I've found it, I've tested this command in the gatt_server example and I've encountered the same problem.
Here is my main code from gatt_server example (with little changes):

Code: Select all

void app_main()
{
    esp_err_t ret;

    // Initialize NVS.
    ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK( ret );

    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));

    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    ret = esp_bt_controller_init(&bt_cfg);
    if (ret) {
        ESP_LOGE(GATTS_TAG, "%s initialize controller failed\n", __func__);
        return;
    }

    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
    if (ret) {
        ESP_LOGE(GATTS_TAG, "%s enable controller failed\n", __func__);
        return;
    }
    ret = esp_bluedroid_init();
    if (ret) {
        ESP_LOGE(GATTS_TAG, "%s init bluetooth failed\n", __func__);
        return;
    }
    ret = esp_bluedroid_enable();
    if (ret) {
        ESP_LOGE(GATTS_TAG, "%s enable bluetooth failed\n", __func__);
        return;
    }

    ret = esp_ble_gatts_register_callback(gatts_event_handler);
    if (ret){
        ESP_LOGE(GATTS_TAG, "gatts register error, error code = %x", ret);
        return;
    }
    ret = esp_ble_gap_register_callback(gap_event_handler);
    if (ret){
        ESP_LOGE(GATTS_TAG, "gap register error, error code = %x", ret);
        return;
    }
    ret = esp_ble_gatts_app_register(PROFILE_A_APP_ID);
    if (ret){
        ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
        return;
    }
    ret = esp_ble_gatts_app_register(PROFILE_B_APP_ID);
    if (ret){
        ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
        return;
    }
    esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(50);
    if (local_mtu_ret){
        ESP_LOGE(GATTS_TAG, "set local  MTU failed, error code = %x", local_mtu_ret);
    }

    // init gpio2 as output
	gpio_config_t io_conf;
	//disable interrupt
	io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
	//set as output mode
	io_conf.mode = GPIO_MODE_OUTPUT;
	//bit mask of the pins that you want to set,e.g.GPIO18/19
	io_conf.pin_bit_mask = (1ULL<<2);
	//disable pull-down mode
	io_conf.pull_down_en = 0;
	//disable pull-up mode
	io_conf.pull_up_en = 0;
	//configure GPIO with the given settings
	gpio_config(&io_conf);

	//Init ADC and Characteristics
//	esp_adc_cal_characteristics_t characteristics;
//	adc1_config_width(ADC_WIDTH_BIT_12);
//	adc1_config_channel_atten(ADC1_TEST_CHANNEL, ADC_ATTEN_DB_11);
//	esp_adc_cal_get_characteristics(V_REF, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, &characteristics);

	uint8_t currentGpioState = 0;
	gpio_set_level(2, currentGpioState);
	while(1){
//		if (currentGpioState)
//			currentGpioState = 0;
//		else
//			currentGpioState = 1;
//		gpio_set_level(2, currentGpioState);
//		gatts_bb_measurementsCharValue.attr_value[0] = currentGpioState;
//		vTaskDelay(pdMS_TO_TICKS(10000));
//		pulseVoltage = adc1_to_voltage(ADC1_TEST_CHANNEL, &characteristics);
//		printf("%d mV\n",pulseVoltage);
//		if (threshold < pulseVoltage) {
//			char_pulseVal[0] = 1ULL;
//			//gatts_demo_pulseVoltage_val.attr_value[0] = 1ULL;
//
//		}
//		else {
//			char_pulseVal[0] = NULL;
//			//gatts_demo_pulseVoltage_val.attr_value[0] = NULL;
//
//		}
		if (gl_profile_tab[PROFILE_A_APP_ID].connected) {
			esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(155);
			        if (local_mtu_ret){
			            ESP_LOGE(GATTS_TAG, "set local  MTU failed, error code = %x", local_mtu_ret);
			        }

			static int j = 0;
			gpio_set_level(2, 1);
			esp_ble_gatts_send_indicate(gl_profile_tab[PROFILE_A_APP_ID].gatts_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, gl_profile_tab[PROFILE_A_APP_ID].char_handle,
					gatts_bb_measurementsCharValue.attr_len, gatts_bb_measurementsCharValue.attr_value, false);
//			currentGpioState = char_pulseVal[0];
//			gpio_set_level(2, gatts_demo_pulseVoltage_val.attr_value[0]);
			for (int i = 6; i <= 9; i++)
				bb_measurementsCharValue[i]++;
			bb_measurementsCharValue[16]++;
			j++;
			if (j > 8) {
				bb_measurementsCharValue[6] = 91;
				bb_measurementsCharValue[7] = 85;
				bb_measurementsCharValue[8] = 100;
				bb_measurementsCharValue[9] = 90;
				bb_measurementsCharValue[16] = 11;
				j = 0;
			}
		}
		vTaskDelay(pdMS_TO_TICKS(200));
		gpio_set_level(2, 0);
		vTaskDelay(pdMS_TO_TICKS(19800));
	}

    return;
}
and here is the output:

Code: Select all

I (433) cpu_start: Pro cpu start user code
I (115) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (204) BTDM_INIT: BT controller compile version [8c18895]

I (204) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (474) phy: phy_version: 366.0, ba9923d, Dec 29 2017, 14:25:06, 0, 0
I (504) GATTS_DEMO: REGISTER_APP_EVT, status 0, app_id 0

I (514) GATTS_DEMO: CREATE_SERVICE_EVT, status 0,  service_handle 40

I (514) GATTS_DEMO: SERVICE_START_EVT, status 0, service_handle 40

I (514) GATTS_DEMO: ADD_CHAR_EVT, status 0,  attr_handle 42, service_handle 40

I (524) GATTS_DEMO: the gatts demo char length = 1b

I (524) GATTS_DEMO: prf_char[0] =0

I (534) GATTS_DEMO: prf_char[1] =11

I (534) GATTS_DEMO: prf_char[2] =22

I (544) GATTS_DEMO: prf_char[3] =33

I (544) GATTS_DEMO: prf_char[4] =44

I (554) GATTS_DEMO: prf_char[5] =55

I (554) GATTS_DEMO: prf_char[6] =5b

I (554) GATTS_DEMO: prf_char[7] =5f

I (564) GATTS_DEMO: prf_char[8] =64

I (564) GATTS_DEMO: prf_char[9] =50

I (574) GATTS_DEMO: prf_char[a] =aa

I (574) GATTS_DEMO: prf_char[b] =bb

I (584) GATTS_DEMO: prf_char[c] =cc

I (584) GATTS_DEMO: prf_char[d] =dd

I (584) GATTS_DEMO: prf_char[e] =ee

I (594) GATTS_DEMO: prf_char[f] =ff

I (594) GATTS_DEMO: prf_char[10] =b

I (604) GATTS_DEMO: prf_char[11] =0

I (604) GATTS_DEMO: prf_char[12] =0

I (614) GATTS_DEMO: prf_char[13] =0

I (614) GATTS_DEMO: prf_char[14] =0

I (624) GATTS_DEMO: prf_char[15] =0

I (624) GATTS_DEMO: prf_char[16] =0

I (624) GATTS_DEMO: prf_char[17] =0

I (634) GATTS_DEMO: prf_char[18] =0

I (634) GATTS_DEMO: prf_char[19] =0

I (644) GATTS_DEMO: prf_char[1a] =0

I (644) GATTS_DEMO: ADD_DESCR_EVT, status 0, attr_handle 43, service_handle 40

I (654) GATTS_DEMO: REGISTER_APP_EVT, status 0, app_id 1

I (664) GATTS_DEMO: CREATE_SERVICE_EVT, status 0,  service_handle 44

I (664) GATTS_DEMO: SERVICE_START_EVT, status 0, service_handle 44

I (674) GATTS_DEMO: ADD_CHAR_EVT, status 0,  attr_handle 46, service_handle 44

I (684) GATTS_DEMO: ADD_DESCR_EVT, status 0, attr_handle 47, service_handle 44

I (694) gpio: GPIO[2]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (13244) GATTS_DEMO: ESP_GATTS_CONNECT_EVT, conn_id 0, remote 65:b3:28:22:85:ef:
I (13254) GATTS_DEMO: CONNECT_EVT, conn_id 0, remote 65:b3:28:22:85:ef:
I (13954) GATTS_DEMO: update connetion params status = 0, min_int = 16, max_int = 32,conn_int = 24,latency = 0, timeout = 400
I (14294) GATTS_DEMO: update connetion params status = 0, min_int = 0, max_int = 0,conn_int = 6,latency = 0, timeout = 2000
I (14554) GATTS_DEMO: update connetion params status = 0, min_int = 0, max_int = 0,conn_int = 24,latency = 0, timeout = 400
E (20704) BT: attribute value too long, to be truncated to 20
I (20704) GATTS_DEMO: ESP_GATTS_CONF_EVT, status 0
I (21484) GATTS_DEMO: GATT_WRITE_EVT, conn_id 0, trans_id 1, handle 47

I (21484) GATTS_DEMO: GATT_WRITE_EVT, value len 2, value :
I (21484) GATTS_DEMO: 01 00 
I (21494) GATTS_DEMO: notify enable
I (21494) GATTS_DEMO: ESP_GATTS_CONF_EVT status 0
E (40704) BT: attribute value too long, to be truncated to 20
I (40704) GATTS_DEMO: ESP_GATTS_CONF_EVT, status 0
E (60704) BT: attribute value too long, to be truncated to 20
I (60704) GATTS_DEMO: ESP_GATTS_CONF_EVT, status 0
E (80704) BT: attribute value too long, to be truncated to 20
I (80704) GATTS_DEMO: ESP_GATTS_CONF_EVT, status 0
E (100704) BT: attribute value too long, to be truncated to 20
I (100704) GATTS_DEMO: ESP_GATTS_CONF_EVT, status 0
E (120704) BT: attribute value too long, to be truncated to 20
I (120704) GATTS_DEMO: ESP_GATTS_CONF_EVT, status 0
E (140704) BT: attribute value too long, to be truncated to 20
The client I've connected to is nRF connect (Nordic's BLE Application).

thanks for helping,
Albert

Re: esp_ble_gatt_set_local_mtu not work on Gatts Server code

Posted: Mon Jun 11, 2018 4:25 am
by chegewara
esp_ble_gatt_set_local_mtu is to setup local value that is then used during request from client. Setting local mtu does nothing if you dont have request from client. Try to setup your server with esp_ble_gatt_set_local_mtu(50) and when you connect with nRF connect request MTU 200. Its in the upper right corner above list of services.

Re: esp_ble_gatt_set_local_mtu not work on Gatts Server code

Posted: Wed Jun 13, 2018 1:41 pm
by avdalimov14
Thank you for your answer.

Yes, I could make the request from nRF but I thought that it should do something also when I'm the server because of gatt_server example from the esp-idf repo.

Thanks again for your explanation :)