<?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>How to implement a write handler in nRF Connect SDK?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/93736/how-to-implement-a-write-handler-in-nrf-connect-sdk</link><description>Hi, 
 Perhaps this question is too basic, but I haven&amp;#39;t been able to find this anywhere. 
 I&amp;#39;ve followed this tutorial successfully with an nRF52832 DK: 
 However, I&amp;#39;m missing the part about how to implement a peripheral write handler. In other words</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 22 Nov 2022 13:08:28 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/93736/how-to-implement-a-write-handler-in-nrf-connect-sdk" /><item><title>RE: How to implement a write handler in nRF Connect SDK?</title><link>https://devzone.nordicsemi.com/thread/396917?ContentTypeID=1</link><pubDate>Tue, 22 Nov 2022 13:08:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:93805003-b5a7-414e-83f9-2ba5e344b570</guid><dc:creator>Edvin</dc:creator><description>[quote user="builder01"]&lt;p&gt;1) Create a structure of your service callbacks in your main.c&lt;/p&gt;
&lt;p&gt;2) In one of them (on_data_received) you&amp;#39;re actually able to receive data to operate on a motor duty cycle, apparently.&lt;/p&gt;
&lt;p&gt;3) In your service .h file, you declare and structure with function prototypes to implement your callbacks&lt;/p&gt;
&lt;p&gt;4) In your service implementation, you log the received information (at least for on_write()). You also use a remote service callbacks structure to execute operate on your peripheral. In order to use them, you implement the bluetooth_init() function that will assign the structure and callbacks you created in the main.c to your service structure.&lt;/p&gt;[/quote]
&lt;p&gt;Yes. Pretty much. Not everything is strictly necessary, but it is good to see how you can pass events back and forth between files. Particularly trigger a callback function in main, even though &amp;quot;main.h&amp;quot; (if it existed) is not included from remote.c.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote user="builder01"]Why is the if case necessary? As far as I see, your data_received has a void return type, not a bool or an int, so how come will this if-statement return a 1 in order to execute your data_received() function.[/quote]
&lt;p&gt;It is correct that it is a void function. But when I call:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    if (remote_service_callbacks.data_received) {&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The remote_service_callbacks.data_received() function is not actually called. It could also be written as this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    if (remote_service_callbacks.data_received != NULL) {&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Which may be more descriptive. It is just checking that the function is actually populated. If a function pointer is NULL, and you call it, it will cause a hardfault. So we just want to ensure that this is not the case.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a write handler in nRF Connect SDK?</title><link>https://devzone.nordicsemi.com/thread/396764?ContentTypeID=1</link><pubDate>Mon, 21 Nov 2022 21:17:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1d5cb3d-7ba7-4303-b163-865f542c510f</guid><dc:creator>builder01</dc:creator><description>&lt;p&gt;Also, why didn&amp;#39;t you register the &amp;quot;button_value&amp;quot; variable into the user_data field when registering your BT_UUID_REMOTE_BUTTON_CHRC characteristic? Sounds like it would&amp;#39;ve been the cleanest thing to do.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a write handler in nRF Connect SDK?</title><link>https://devzone.nordicsemi.com/thread/396758?ContentTypeID=1</link><pubDate>Mon, 21 Nov 2022 20:22:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1e64da5a-1246-4efb-a4a8-4fa7eaf229f2</guid><dc:creator>builder01</dc:creator><description>&lt;p&gt;Hi, thanks for your answer. The 2nd part, in particular. I finally find the time to go over it.&lt;br /&gt;&lt;br /&gt;If I understand correctly, you do the following:&lt;br /&gt;&lt;br /&gt;1) Create a structure of your service callbacks in your main.c&lt;/p&gt;
&lt;p&gt;2) In one of them (on_data_received) you&amp;#39;re actually able to receive data to operate on a motor duty cycle, apparently.&lt;/p&gt;
&lt;p&gt;3) In your service .h file, you declare and structure with function prototypes to implement your callbacks&lt;/p&gt;
&lt;p&gt;4) In your service implementation, you log the received information (at least for on_write()). You also use a remote service callbacks structure to execute operate on your peripheral. In order to use them, you implement the bluetooth_init() function that will assign the structure and callbacks you created in the main.c to your service structure.&lt;br /&gt;&lt;br /&gt;Is that correct? If so, I only have one last doubt.&lt;br /&gt;&lt;br /&gt;On the on_write() function in your remote.c file, you do the following line:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    if (remote_service_callbacks.data_received) {
        remote_service_callbacks.data_received(conn, buf, len);
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Why is the if case necessary? As far as I see, your data_received has a void return type, not a bool or an int, so how come will this if-statement return a 1 in order to execute your data_received() function.&lt;br /&gt;&lt;br /&gt;Perhaps my C knowledge isn&amp;#39;t deep enough.&lt;br /&gt;&lt;br /&gt;If you can answer that, then I&amp;#39;ll mark your answer as the accepted answer :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a write handler in nRF Connect SDK?</title><link>https://devzone.nordicsemi.com/thread/395292?ContentTypeID=1</link><pubDate>Fri, 11 Nov 2022 11:12:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9465340b-9eee-4f35-b8a5-35d7bf0557b8</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;In the guide you refer to, look at Step 4, where you create your services and characteristics:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/* LED Button Service Declaration and Registration */
BT_GATT_SERVICE_DEFINE(my_service,
BT_GATT_PRIMARY_SERVICE(BT_UUID_MY_SERIVCE),
BT_GATT_CHARACTERISTIC(BT_UUID_MY_SERIVCE_RX,
			       BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
			       BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, 
                   NULL, on_receive, NULL),
BT_GATT_CHARACTERISTIC(BT_UUID_MY_SERIVCE_TX,
			       BT_GATT_CHRC_NOTIFY,
			       BT_GATT_PERM_READ,
                   NULL, NULL, NULL),
BT_GATT_CCC(lbslc_ccc_cfg_changed,
        BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;On line 7 in that snippet, there is a parameter: &amp;quot;on_receive&amp;quot;, which is a function pointer to the function that is triggered when the connected device writes to that characteristic.It can be implemented the way it is in the snippet right below that:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/* This function is called whenever the RX Characteristic has been written to by a Client */
static ssize_t on_receive(struct bt_conn *conn,
			  const struct bt_gatt_attr *attr,
			  const void *buf,
			  u16_t len,
			  u16_t offset,
			  u8_t flags)
{
    const u8_t * buffer = buf;
    
	printk(&amp;quot;Received data, handle %d, conn %p, data: 0x&amp;quot;, attr-&amp;gt;handle, conn);
    for(u8_t i = 0; i &amp;lt; len; i++){
        printk(&amp;quot;%02X&amp;quot;, buffer[i]);
    }
    printk(&amp;quot;\n&amp;quot;);

	return len;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;So that function will be triggered whenever a connected device writes to the characteristic. Is that what you were looking for? Or do you need/want the callback to be in your main.c function? In that case, I happen to have a github repository that I used for a course at some point, where I did something similar, except that we forward the interrupt to a callback function in main.c. Feel free to have a look:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/edvinand/OmegaV_BLE_Course/tree/main/remote_controller/src"&gt;https://github.com/edvinand/OmegaV_BLE_Course/tree/main/remote_controller/src&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Look at how I forwarded custom callback functions (remote_callbacks) in to the bluetooth_init() function in main(), and how these are used inside the remote.c file, inside the on_write() callback. In that case, you can use the callback function in main.c to toggle an LED or GPIO.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement a write handler in nRF Connect SDK?</title><link>https://devzone.nordicsemi.com/thread/395280?ContentTypeID=1</link><pubDate>Fri, 11 Nov 2022 10:19:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:17249791-f05a-4bb7-ac78-87ebe79caeb5</guid><dc:creator>paulcardno</dc:creator><description>&lt;p&gt;Hi Builder01 &lt;br /&gt;I&amp;#39;m on a quest to do this as well.&lt;br /&gt;I haven&amp;#39;t followed the example that you have given, I will take a deeper look tomorrow.&lt;br /&gt;I&amp;#39;ve found that the sample zephyr/samples/bluetooth/peripheral has custom service and characteristics that have read/write/notify/indicate, its not a tutorial and I&amp;#39;m currently working on understand to create my own write&amp;nbsp; and then notify characteristics. Not there yet. But it does look promising. Hope its helpful for you and I would also in interesting in a tutorial that does this.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>