Quesetion to understand a call back function in a sample

this may be a very simple question, but I just don't it.

I am able to run the sample Exercise 1 – Nordic Developer Academy (nordicsemi.com) on the nRF52832 eval board, but I don't understand the following BLE call back function:

static ssize_t read_button(struct bt_conn *conn,
			  const struct bt_gatt_attr *attr,
			  void *buf,
			  uint16_t len,
			  uint16_t offset)
{
	//get a pointer to button_state which is passed in the BT_GATT_CHARACTERISTIC() and stored in attr->user_data
	const char *value = attr->user_data;
	LOG_DBG("Attribute read, handle: %u, conn: %p", attr->handle,
		(void *)conn);
	if (lbs_cb.button_cb) {
		// Call the application callback function to update the get the current value of the button
		button_state = lbs_cb.button_cb();
		return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
					 sizeof(*value));
	}
	return 0;
}

This line 
button_state = lbs_cb.button_cb();
calls the application callback to get the current button state        

but button_state is not used in the following function 
bt_gatt_attr_read. 

I guess the variable "value" of  bt_gatt_attr_read should carry the current button state, then how is the input variable "value" got updated?

Thanks!
Parents
  • Hello,


    The button_state is a global variable which is used with BT_GATT_CHARACTERISTIC within the service definition. So here (in the line you have mentioned), this variable is updated.

    This variable kind of holds the current button state as you press the up-arrow button on the application. 

    However, the variable value is a pointer to user_data in the passed attributes. The bt_gatt_attr_read function would then update the user_data through pointer value

    Just to see this, what you can do is print the contents from value before and after the call to the bt_gatt_attr_read() within the read_button() function. You may also print button_state and see how it relates to the before and after values.

    Regards,

    Naeem

  • Hi Naeem,

    thank you very much for the answer.
    this is waht I understand behind the story:

    whenever the button on the eval board is pressed, a callback in the BLE stack is called and the static variable app_button_state is updated.

    whenever the up-arrow button in the App is pressed, the application callback function 'read_button' is called, which copies the value of app_button_state to another static variable button_state.

    As to 'bt_gatt_attr_read', per Zehyr doc, it "reads attribute value from local database storing the result into buffer.", so that on the APP i can see the value  is either "Button Pressed" or "Button Released".

    but I noticed a small "abnormality" in the prints of the content of the variable "value"

    The prints I added is as follows:

    static ssize_t read_button(struct bt_conn *conn,
    			  const struct bt_gatt_attr *attr,
    			  void *buf,
    			  uint16_t len,
    			  uint16_t offset)
    {
    	//get a pointer to button_state which is passed in the BT_GATT_CHARACTERISTIC() and stored in attr->user_data
    	const void *value = attr->user_data;
    	LOG_DBG("Attribute read, handle: %u, conn: %p", attr->handle,
    		(void *)conn);
    	if (lbs_cb.button_cb) {
    		// Call the application callback function to update the get the current value of the button
    		printk("button_state is %d\n", button_state);
    		printk("value is %d\n", *(int*)value);
    
    		button_state = lbs_cb.button_cb();
    
    		printk("Application callback is called.\n");
    		printk("button_state is %d\n", button_state);
    		printk("value is %d\n", *(int*)value);		
    		printk("value is %d\n\n", *(int*)value);				
    		return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
    					 sizeof(*value));			 
    	}
    	return 0;
    }

    The result in the serial connection is:

    There are 3 parts of the print ouputs, as I pressed the up-arrow on the APP 3 times.
    before the 3rd time pressing in the APP, I pressed the button on the eval board.

    I am happy to see the value of "value" is changed, but suprised by the way it changes:

    Before and after the line  , the value of "value" is 65792 and 65793, I don't understand this change as the application call back "button_cb()" don't make chages except copying the value of a static variable to another.

    Do you have any ideas about it? thanks!

  • can you print the value[0] and see if it matches.

Reply Children
No Data
Related