how to make example gatt_security_server notify?

datluong
Posts: 10
Joined: Thu Jun 13, 2019 10:34 am

how to make example gatt_security_server notify?

Postby datluong » Fri Jun 28, 2019 8:33 am

Dear All,
I am working with example gatt_security_server in esp-idf and when I tested it with nordic mobile application, it just did not return any value to the nordic.
So I check the example code and found out that they just leave it blank like this :

Code: Select all

case ESP_GATTS_WRITE_EVT:
            ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_WRITE_EVT, write value:");
            esp_log_buffer_hex(GATTS_TABLE_TAG, param->write.value, param->write.len);
            
So, I put some code in like this:

Code: Select all

case ESP_GATTS_WRITE_EVT:
            ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_WRITE_EVT, write value:");
            esp_log_buffer_hex(GATTS_TABLE_TAG, param->write.value, param->write.len);
            uint16_t descr_value = param->write.value[1]<<8 | param->write.value[0];
                if (descr_value == 0x0001){
                        ESP_LOGI(GATTS_TABLE_TAG, "notify enable");
                        uint8_t notify_data[1] = {2};
                        esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, heart_rate_profile_tab[HEART_PROFILE_APP_IDX].char_handle,
                                                sizeof(notify_data), notify_data, false);
                }
            break;
So when I set notify enable in the nordic application, I expected to get value of 2.
But turn out, I did not get any notify from the esp32.
Here is what I got from logging via UART:

Code: Select all

 (500) SEC_GATTS_DEMO: app_main init bluetooth
I (540) SEC_GATTS_DEMO: The number handle = 8
I (550) SEC_GATTS_DEMO: advertising start success
I (4810) SEC_GATTS_DEMO: ESP_GATTS_CONNECT_EVT
I (5150) SEC_GATTS_DEMO: remote BD_ADDR: 461b6d87b0ef
I (5150) SEC_GATTS_DEMO: address type = 1
I (5150) SEC_GATTS_DEMO: pair status = success
I (5160) SEC_GATTS_DEMO: Bonded devices number : 1

I (5160) SEC_GATTS_DEMO: Bonded devices list : 1

I (5170) SEC_GATTS_DEMO: 46 1b 6d 87 b0 ef 
I (8950) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (8950) SEC_GATTS_DEMO: 00 00 
I (10950) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (10950) SEC_GATTS_DEMO: 01 00 
I (10950) SEC_GATTS_DEMO: notify enable
I (12850) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (12850) SEC_GATTS_DEMO: 00 00 
I (13530) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (13540) SEC_GATTS_DEMO: 01 00 
I (13540) SEC_GATTS_DEMO: notify enable
I (14610) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (14610) SEC_GATTS_DEMO: 00 00 
I (16460) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (16460) SEC_GATTS_DEMO: 01 00 
I (16460) SEC_GATTS_DEMO: notify enable
I (17480) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (17480) SEC_GATTS_DEMO: 00 00 
I (18990) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (19000) SEC_GATTS_DEMO: 01 00 
I (19000) SEC_GATTS_DEMO: notify enable
I (22260) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (22260) SEC_GATTS_DEMO: 00 00 
it showed that the code run through this part already. Is there any idea how to make notify in this example?
May thanks.

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: how to make example gatt_security_server notify?

Postby chegewara » Fri Jun 28, 2019 11:27 pm

Move this code outside of callback function or BLE driver will lock:

Code: Select all

uint8_t notify_data[1] = {2};
esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, heart_rate_profile_tab[HEART_PROFILE_APP_IDX].char_handle,
                                                sizeof(notify_data), notify_data, 

datluong
Posts: 10
Joined: Thu Jun 13, 2019 10:34 am

Re: how to make example gatt_security_server notify?

Postby datluong » Mon Jul 01, 2019 2:46 am

Thank you for your response,
I took your advice and then, I tried to do something like this:
Instead of directly used function esp_ble_gatts_send_indicate inside call back function. I used a task insteads.

Code: Select all

case ESP_GATTS_WRITE_EVT:
            ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_WRITE_EVT, write value:");
            esp_log_buffer_hex(GATTS_TABLE_TAG, param->write.value, param->write.len);
            uint16_t descr_value = param->write.value[1]<<8 | param->write.value[0];
                if (descr_value == 0x0001){
                    if (testNotifyTask == NULL)
                    {
                        ESP_LOGI(GATTS_TABLE_TAG, "notify enable");
                        globalIf = gatts_if;
                        globalConn = param->write.conn_id;
                        xTaskCreate(
                            (TaskFunction_t)&testNotify,
                            "testNotify",
                            2048,
                            NULL,
                            5,
                            &testNotifyTask);
                    }
                    else
                    {
                        vTaskResume(testNotifyTask);
                    }
                       
                }
                else if(descr_value == 0x0000)
                {
                     if (testNotifyTask == NULL)
                    {
                        
                    }
                    else
                    {
                        vTaskSuspend(testNotifyTask);
                    }
                }
            break;
So when the client send a notify signal to server, I will create or resume the task if it is already exist.
In the task function, I used function:

Code: Select all

esp_ble_gatts_send_indicate(globalIf, 
                                    globalConn, 
                                    heart_rate_profile_tab[HEART_PROFILE_APP_IDX].char_handle,
                                    sizeof(notify_data), 
                                    notify_data, 
                                    false);
to notify every 1 second:

Code: Select all

void testNotify()
{
    // uint8_t indicate_data[12] = {0};
    while (1)
    {
        ESP_LOGI(GATTS_TABLE_TAG, "testNotify");
        // for (int x = 0; x < 6; x++)
        // {
        //     indicate_data[x * 2] = (rightSide.motor[x].current >> 8);
        //     indicate_data[x * 2 + 1] = rightSide.motor[x].current;
        // }
        uint8_t notify_data[1] = {2};
        ESP_LOGI("data send", "%d", notify_data[0]);
        //the size of indicate_data[] need less than MTU size
         esp_ble_gatts_send_indicate(globalIf, 
                                    globalConn, 
                                    heart_rate_profile_tab[HEART_PROFILE_APP_IDX].char_handle,
                                    sizeof(notify_data), 
                                    notify_data, 
                                    false);

        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
But somehow it still did not work. Here is the logging info I got:

Code: Select all

I (6390) SEC_GATTS_DEMO: 46 1b 6d 87 b0 ef 
I (9740) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (9740) SEC_GATTS_DEMO: 01 00 
I (9740) SEC_GATTS_DEMO: notify enable
I (9740) SEC_GATTS_DEMO: testNotify
I (9740) data send: 2
I (9850) SEC_GATTS_DEMO: testNotify
I (9850) data send: 2
I (9950) SEC_GATTS_DEMO: testNotify
I (9950) data send: 2
I (10050) SEC_GATTS_DEMO: testNotify
I (10050) data send: 2
I (10150) SEC_GATTS_DEMO: testNotify
I (10150) data send: 2
I (10250) SEC_GATTS_DEMO: testNotify
I (10250) data send: 2
I (10350) SEC_GATTS_DEMO: testNotify
I (10350) data send: 2
I (10450) SEC_GATTS_DEMO: testNotify
I (10450) data send: 2
I (10550) SEC_GATTS_DEMO: testNotify
I (10550) data send: 2
I (10650) SEC_GATTS_DEMO: testNotify
I (10650) data send: 2
I (10750) SEC_GATTS_DEMO: testNotify
I (10750) data send: 2
I (10850) SEC_GATTS_DEMO: testNotify
I (10850) data send: 2
I (10950) SEC_GATTS_DEMO: testNotify
I (10950) data send: 2
I (11050) SEC_GATTS_DEMO: testNotify
I (11050) data send: 2
I (11150) SEC_GATTS_DEMO: testNotify
I (11150) data send: 2
I (11250) SEC_GATTS_DEMO: testNotify
I (11250) data send: 2
I (11350) SEC_GATTS_DEMO: testNotify
I (11350) data send: 2
I (11450) SEC_GATTS_DEMO: testNotify
I (11450) data send: 2
I (11550) SEC_GATTS_DEMO: testNotify
I (11550) data send: 2
I (11650) SEC_GATTS_DEMO: testNotify
I (11650) data send: 2
I (11750) SEC_GATTS_DEMO: testNotify
I (11750) data send: 2
I (11850) SEC_GATTS_DEMO: testNotify
I (11850) data send: 2
I (11950) SEC_GATTS_DEMO: testNotify
I (11950) data send: 2
I (12050) SEC_GATTS_DEMO: testNotify
I (12050) data send: 2
I (12150) SEC_GATTS_DEMO: testNotify
I (12150) data send: 2
I (12250) SEC_GATTS_DEMO: testNotify
I (12250) data send: 2
I (12350) SEC_GATTS_DEMO: testNotify
I (12350) data send: 2
I (12370) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (12370) SEC_GATTS_DEMO: 00 00 
I (15050) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (15050) SEC_GATTS_DEMO: 01 00 
I (15050) SEC_GATTS_DEMO: testNotify
I (15050) data send: 2
In my nrf application, I still do not get anything.
Do you have any suggestion on how to solve it now?

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: how to make example gatt_security_server notify?

Postby chegewara » Mon Jul 01, 2019 3:11 am

It seems that code works, because logs shows notify that task is working. Now you need to check 2 things, if this function returns ESP_OK and all parameters passed to it are correct:

Code: Select all

esp_ble_gatts_send_indicate(globalIf, 
                                    globalConn, 
                                    heart_rate_profile_tab[HEART_PROFILE_APP_IDX].char_handle,
                                    sizeof(notify_data), 
                                    notify_data, 
                                    false);

datluong
Posts: 10
Joined: Thu Jun 13, 2019 10:34 am

Re: how to make example gatt_security_server notify?

Postby datluong » Mon Jul 01, 2019 7:32 am

Hi,
Thank you for your response,
The thing is when I try to log the error of the function:

Code: Select all

void testNotify()
{
    // uint8_t indicate_data[12] = {0};
    uint8_t notify_data[1] = {48};
    int error = 0;
    while (1)
    {
        ESP_LOGI(GATTS_TABLE_TAG, "testNotify");
        // for (int x = 0; x < 6; x++)
        // {
        //     indicate_data[x * 2] = (rightSide.motor[x].current >> 8);
        //     indicate_data[x * 2 + 1] = rightSide.motor[x].current;
        // }
        
        ESP_LOGI("data send", "%d", notify_data[0]);
        //the size of indicate_data[] need less than MTU size
        error = esp_ble_gatts_send_indicate(globalIf, 
                                    globalConn, 
                                    heart_rate_profile_tab[HEART_PROFILE_APP_IDX].char_handle,
                                    sizeof(notify_data), 
                                    notify_data, 
                                    false);
        ESP_LOGI("error", "%d", error);   
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
And here is the logging file:

Code: Select all

I (2488) SEC_GATTS_DEMO: 46 1b 6d 87 b0 ef 
I (23328) SEC_GATTS_DEMO: ESP_GATTS_DISCONNECT_EVT
I (23328) SEC_GATTS_DEMO: advertising start success
I (27758) SEC_GATTS_DEMO: ESP_GATTS_CONNECT_EVT
I (31468) SEC_GATTS_DEMO: key type = ESP_LE_KEY_LENC
I (31468) SEC_GATTS_DEMO: key type = ESP_LE_KEY_LID
I (31718) SEC_GATTS_DEMO: key type = ESP_LE_KEY_PENC
I (31718) SEC_GATTS_DEMO: key type = ESP_LE_KEY_PID
I (31718) SEC_GATTS_DEMO: remote BD_ADDR: 461b6d87b0ef
I (31728) SEC_GATTS_DEMO: address type = 1
I (31728) SEC_GATTS_DEMO: pair status = success
I (31738) SEC_GATTS_DEMO: Bonded devices number : 1

I (31738) SEC_GATTS_DEMO: Bonded devices list : 1

I (31748) SEC_GATTS_DEMO: 46 1b 6d 87 b0 ef 
I (33858) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (33858) SEC_GATTS_DEMO: 01 00 
I (33858) SEC_GATTS_DEMO: notify enable
I (33858) SEC_GATTS_DEMO: testNotify
I (33868) data send: 48
I (33868) error: 0
I (34868) SEC_GATTS_DEMO: testNotify
I (34868) data send: 48
I (34868) error: 0
I (35868) SEC_GATTS_DEMO: testNotify
I (35868) data send: 48
I (35868) error: 0
I (36868) SEC_GATTS_DEMO: testNotify
I (36868) data send: 48
I (36868) error: 0
I (37868) SEC_GATTS_DEMO: testNotify
I (37868) data send: 48
I (37868) error: 0
I (38868) SEC_GATTS_DEMO: testNotify
I (38868) data send: 48
I (38868) error: 0
I (39868) SEC_GATTS_DEMO: testNotify
I (39868) data send: 48
I (39868) error: 0
I (40868) SEC_GATTS_DEMO: testNotify
I (40868) data send: 48
I (40868) error: 0
I (41868) SEC_GATTS_DEMO: testNotify
I (41868) data send: 48
I (41868) error: 0
I (42868) SEC_GATTS_DEMO: testNotify
I (42868) data send: 48
I (42868) error: 0
I (43868) SEC_GATTS_DEMO: testNotify
I (43868) data send: 48
I (43868) error: 0
I (44868) SEC_GATTS_DEMO: testNotify
I (44868) data send: 48
I (44868) error: 0
I (45868) SEC_GATTS_DEMO: testNotify
I (45868) data send: 48
I (45868) error: 0
I (46868) SEC_GATTS_DEMO: testNotify
I (46868) data send: 48
I (46868) error: 0
I (47868) SEC_GATTS_DEMO: testNotify
I (47868) data send: 48
I (47868) error: 0
I (48868) SEC_GATTS_DEMO: testNotify
I (48868) data send: 48
I (48868) error: 0
I (49868) SEC_GATTS_DEMO: testNotify
I (49868) data send: 48
I (49868) error: 0
I (50868) SEC_GATTS_DEMO: testNotify
I (50868) data send: 48
I (50868) error: 0
I (51868) SEC_GATTS_DEMO: testNotify
I (51868) data send: 48
I (51868) error: 0
I (52678) SEC_GATTS_DEMO: ESP_GATTS_WRITE_EVT, write value:
I (52678) SEC_GATTS_DEMO: 00 00 
I (54478) SEC_GATTS_DEMO: ESP_GATTS_DISCONNECT_EVT
I (54478) SEC_GATTS_DEMO: advertising start success
seen likes everything works fine but I did not receive any thing on nrf connect application on android phone :(
Here is my phone screen:
Image
Attachments
65631537_856740188030819_8679971864031789056_n.png
65631537_856740188030819_8679971864031789056_n.png (201.76 KiB) Viewed 7587 times

datluong
Posts: 10
Joined: Thu Jun 13, 2019 10:34 am

Re: how to make example gatt_security_server notify?

Postby datluong » Mon Jul 01, 2019 7:42 am

Funny thing is that, when I tried gatt_server example, the function worked. Even that I put it in call back function. Actually in the example, they put it in the call back function.
It like this:

Code: Select all

case ESP_GATTS_WRITE_EVT: {
        ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d", param->write.conn_id, param->write.trans_id, param->write.handle);
        if (!param->write.is_prep){
            ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len);
            esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
            if (gl_profile_tab[PROFILE_A_APP_ID].descr_handle == param->write.handle && param->write.len == 2){
                uint16_t descr_value = param->write.value[1]<<8 | param->write.value[0];
                if (descr_value == 0x0001){
                    if (a_property & ESP_GATT_CHAR_PROP_BIT_NOTIFY){
                        ESP_LOGI(GATTS_TAG, "notify enable");
                        uint8_t notify_data[15];
                        for (int i = 0; i < sizeof(notify_data); ++i)
                        {
                            notify_data[i] = i%0xff;
                        }
                        //the size of notify_data[] need less than MTU size
                        esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, gl_profile_tab[PROFILE_A_APP_ID].char_handle,
                                                sizeof(notify_data), notify_data, false);
                    }
                }else if (descr_value == 0x0002){
                    if (a_property & ESP_GATT_CHAR_PROP_BIT_INDICATE){
                        ESP_LOGI(GATTS_TAG, "indicate enable");
                        uint8_t indicate_data[15];
                        for (int i = 0; i < sizeof(indicate_data); ++i)
                        {
                            indicate_data[i] = i%0xff;
                        }
                        //the size of indicate_data[] need less than MTU size
                        esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, gl_profile_tab[PROFILE_A_APP_ID].char_handle,
                                                sizeof(indicate_data), indicate_data, true);
                    }
                }
                else if (descr_value == 0x0000){
                    ESP_LOGI(GATTS_TAG, "notify/indicate disable ");
                }else{
                    ESP_LOGE(GATTS_TAG, "unknown descr value");
                    esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
                }

            }
        }
        example_write_event_env(gatts_if, &a_prepare_write_env, param);
        break;
    }
And everything worked fine, I got the notify information in the nrf phone application:
Attachments
65854958_410555426258715_6996143490551775232_n.png
65854958_410555426258715_6996143490551775232_n.png (201.64 KiB) Viewed 7585 times

datluong
Posts: 10
Joined: Thu Jun 13, 2019 10:34 am

Re: how to make example gatt_security_server notify?

Postby datluong » Mon Jul 01, 2019 9:03 am

I have fixed it, due to the different in char_handle. It should be

Code: Select all

heart_rate_handle_table[HRS_IDX_HR_MEAS_NTF_CFG]
instead of

Code: Select all

heart_rate_profile_tab[HEART_PROFILE_NUM].char_handle
.
Thank you very much.

Who is online

Users browsing this forum: Nespressif and 68 guests