<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Quesetion to understand a call back function in a sample</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/108444/quesetion-to-understand-a-call-back-function-in-a-sample</link><description>this may be a very simple question, but I just don&amp;#39;t it. I am able to run the sample Exercise 1 – Nordic Developer Academy (nordicsemi.com) on the nRF52832 eval board, but I don&amp;#39;t understand the following BLE call back function: 
 This line calls the</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 28 Feb 2024 10:38:50 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/108444/quesetion-to-understand-a-call-back-function-in-a-sample" /><item><title>RE: Quesetion to understand a call back function in a sample</title><link>https://devzone.nordicsemi.com/thread/471247?ContentTypeID=1</link><pubDate>Wed, 28 Feb 2024 10:38:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7baf9244-a734-45c6-bc76-a09e10a85c90</guid><dc:creator>Naeem Maroof</dc:creator><description>&lt;p&gt;can you print the value[0] and see if it matches.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Quesetion to understand a call back function in a sample</title><link>https://devzone.nordicsemi.com/thread/470306?ContentTypeID=1</link><pubDate>Thu, 22 Feb 2024 15:14:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:53829003-75c6-4a29-b590-eed82966ebfd</guid><dc:creator>ulyssis</dc:creator><description>&lt;p&gt;Hi Naeem,&lt;/p&gt;
&lt;p&gt;thank you very much for the answer.&lt;br /&gt;this is waht I understand behind the story:&lt;/p&gt;
&lt;p&gt;whenever the button on the eval board is pressed, a callback in the BLE stack is called and the static variable &lt;strong&gt;&lt;em&gt;app_button_state&lt;/em&gt;&lt;/strong&gt;&amp;nbsp;is updated.&lt;/p&gt;
&lt;p&gt;whenever the up-arrow button in the App is pressed, the application callback function &amp;#39;read_button&amp;#39; is called, which copies the value of&amp;nbsp;&lt;strong&gt;&lt;em&gt;app_button_state&lt;/em&gt;&lt;/strong&gt; to another static variable&lt;strong&gt; &lt;em&gt;button_state.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As to &amp;#39;&lt;span&gt;bt_gatt_attr_read&lt;/span&gt;&amp;#39;, &lt;span&gt;per Zehyr doc, it &amp;quot;reads attribute value from local database storing the result into buffer.&amp;quot;, so that on the APP i can see the value&amp;nbsp; is either &amp;quot;Button Pressed&amp;quot; or &amp;quot;Button Released&amp;quot;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;but I noticed a small &amp;quot;abnormality&amp;quot; in the prints of the content of the variable &amp;quot;value&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The prints I added is as follows:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;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-&amp;gt;user_data
	const void *value = attr-&amp;gt;user_data;
	LOG_DBG(&amp;quot;Attribute read, handle: %u, conn: %p&amp;quot;, attr-&amp;gt;handle,
		(void *)conn);
	if (lbs_cb.button_cb) {
		// Call the application callback function to update the get the current value of the button
		printk(&amp;quot;button_state is %d\n&amp;quot;, button_state);
		printk(&amp;quot;value is %d\n&amp;quot;, *(int*)value);

		button_state = lbs_cb.button_cb();

		printk(&amp;quot;Application callback is called.\n&amp;quot;);
		printk(&amp;quot;button_state is %d\n&amp;quot;, button_state);
		printk(&amp;quot;value is %d\n&amp;quot;, *(int*)value);		
		printk(&amp;quot;value is %d\n\n&amp;quot;, *(int*)value);				
		return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
					 sizeof(*value));			 
	}
	return 0;
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The result in the serial connection is:&lt;br /&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1708614537138v1.png" alt=" " /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;There are 3 parts of the print ouputs, as I pressed the up-arrow on the APP 3 times.&lt;br /&gt;before the 3rd time pressing in the APP, I pressed the button on the eval board.&lt;/p&gt;
&lt;p&gt;I am happy to see the value of &amp;quot;value&amp;quot; is changed, but suprised by the way it changes:&lt;/p&gt;
&lt;p&gt;Before and after the line&amp;nbsp;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1708614720741v2.png" alt=" " /&gt;, the value of &amp;quot;value&amp;quot; is 65792 and 65793, I don&amp;#39;t understand this change as the application call back &amp;quot;button_cb()&amp;quot; don&amp;#39;t make chages except copying the value of a static variable to another.&lt;/p&gt;
&lt;p&gt;Do you have any ideas about it? thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Quesetion to understand a call back function in a sample</title><link>https://devzone.nordicsemi.com/thread/470004?ContentTypeID=1</link><pubDate>Wed, 21 Feb 2024 12:08:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c4874329-e0cd-4ff7-aa44-a5c604df98dd</guid><dc:creator>Naeem Maroof</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;The &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;button_state&lt;/span&gt; is a global variable which is used with &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;BT_GATT_CHARACTERISTIC&lt;/span&gt; within the service definition. So here (in the line you have mentioned), this variable is updated.&lt;/p&gt;
&lt;p&gt;This variable kind of holds the current button state as you press the up-arrow button on the application.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;However, the variable &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;value&lt;/span&gt; is a pointer to &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;user_data&lt;/span&gt; in the passed attributes. The &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;bt_gatt_attr_read&lt;/span&gt; function would then update the &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;user_data&lt;/span&gt; through pointer &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;value&lt;/span&gt;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Just to see this, what you can do is print the contents from &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;value&lt;/span&gt; before and after the call to the &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;bt_gatt_attr_read()&lt;/span&gt; within the &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;read_button()&lt;/span&gt; function. You may also print &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;button_state&lt;/span&gt; and see how it relates to the before and after values.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Naeem&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>