<?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>Updating data length on peripheral/server side</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/106632/updating-data-length-on-peripheral-server-side</link><description>Hello, 
 
 I have followed 2.2 from this blog post to try and get my application to update its data length and MTU size when connecting to the nRF Connect for Mobile app. It&amp;#39;s not clear from the blog post when to call the &amp;#39;request_...&amp;#39; functions, but</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 15 Dec 2023 18:13:03 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/106632/updating-data-length-on-peripheral-server-side" /><item><title>RE: Updating data length on peripheral/server side</title><link>https://devzone.nordicsemi.com/thread/460631?ContentTypeID=1</link><pubDate>Fri, 15 Dec 2023 18:13:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:42f5dc31-39b4-4d12-ada1-c2e44b3cfc05</guid><dc:creator>WoutWG</dc:creator><description>&lt;p&gt;Hi Einar,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks! Seems like I was going too quickly. There was indeed a build warning, seems like I overlooked that. And seems like I misread the blogpost and wrote the wrong macro as parameter for&amp;nbsp;bt_conn_le_data_len_update.&lt;/p&gt;
&lt;p&gt;Works beautifully now!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Updating data length on peripheral/server side</title><link>https://devzone.nordicsemi.com/thread/460519?ContentTypeID=1</link><pubDate>Fri, 15 Dec 2023 11:15:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ae120a13-dcee-44cf-8f35-af6c8a8eebd3</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I see you call&amp;nbsp;&lt;code&gt;bt_conn_le_data_len_update()&lt;/code&gt; like this, which I assume you got a warning about when building?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    err = bt_conn_le_data_len_update(conn, CONFIG_BT_CTLR_DATA_LENGTH_MAX);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;CONFIG_BT_CTLR_DATA_LENGTH_MAX is a number, and you are supposed to pass a struct (bt_conn_le_data_len_param). So it is expected that this fails. You can build the struct using&amp;nbsp;BT_CONN_LE_DATA_LEN_PARAM.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Updating data length on peripheral/server side</title><link>https://devzone.nordicsemi.com/thread/460369?ContentTypeID=1</link><pubDate>Thu, 14 Dec 2023 12:55:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ec1ebeca-16ee-41f2-9624-700e701f6917</guid><dc:creator>WoutWG</dc:creator><description>&lt;p&gt;Hi Einar,&lt;/p&gt;
&lt;p&gt;I am using nRF Connect SDK 2.5.0 on a custom board with a nRF52840-QFAA&lt;/p&gt;
&lt;p&gt;These are my relevant configurations&lt;/p&gt;
&lt;p&gt;custom board defconfig&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;CONFIG_SOC_SERIES_NRF52X=y
CONFIG_SOC_NRF52840_QFAA=y
CONFIG_BOARD_CUSTOM=y

# Enable MPU
CONFIG_ARM_MPU=y

# Enable hardware stack protection
CONFIG_HW_STACK_PROTECTION=y

# Enable RTT
CONFIG_USE_SEGGER_RTT=y

# enable GPIO
CONFIG_GPIO=y

# enable uart driver
CONFIG_SERIAL=n

# enable console
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=n
CONFIG_RTT_CONSOLE=y

CONFIG_PINCTRL=y&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;prj.conf&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DEVICE_NAME=&amp;quot;DLE_sample&amp;quot;

CONFIG_BT_PERIPHERAL_PREF_MIN_INT=160
CONFIG_BT_PERIPHERAL_PREF_MAX_INT=160
CONFIG_BT_PERIPHERAL_PREF_LATENCY=2
CONFIG_BT_PERIPHERAL_PREF_TIMEOUT=800
CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=y

# For requesting ATT_MTU update
CONFIG_BT_GATT_CLIENT=y
# For data length update
CONFIG_BT_USER_DATA_LEN_UPDATE=y 
# Max supported by Nordic Softdevice controller
CONFIG_BT_CTLR_DATA_LENGTH_MAX=251 
CONFIG_BT_BUF_ACL_TX_SIZE=251
CONFIG_BT_BUF_ACL_RX_SIZE=251
# Maximum supported by Nordic Softdevice controller (Data length - 4)
CONFIG_BT_L2CAP_TX_MTU=247

CONFIG_POWEROFF=y # Allows going to complete power off
CONFIG_PM_DEVICE=y

CONFIG_FPU=y

# Support for sensors
CONFIG_GPIO=y
CONFIG_I2C=y

# Debugging
CONFIG_DEBUG_OPTIMIZATIONS=y
CONFIG_DEBUG_THREAD_INFO=y # Helps to identify stack size issues

CONFIG_MPSL_WORK_STACK_SIZE=2048
# CONFIG_BT_RX_STACK_SIZE=2048

CONFIG_LOG=y
CONFIG_USE_SEGGER_RTT=y
CONFIG_LOG_BACKEND_RTT=y
CONFIG_LOG_PRINTK=n
CONFIG_LOG_PROCESS_THREAD_STACK_SIZE=2048

CONFIG_BT_DEBUG_LOG=y&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I created a ble.c file to take care of all BLE related activity&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/bluetooth/addr.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/conn.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/gap.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/gatt.h&amp;gt;

#include &amp;quot;ble.h&amp;quot;

#include &amp;lt;zephyr/logging/log.h&amp;gt;
LOG_MODULE_REGISTER(ble, CONFIG_APP_LOG_LEVEL);

static struct bt_le_adv_param *adv_param = BT_LE_ADV_PARAM(
    (BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_USE_IDENTITY), // No special advertising options
    800,                                                     // Advertising interval set to about 500ms
    801,
    NULL // Peer address;
);

static const struct bt_data ad[] = {
    BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), // No classic Bluetooth is supported, generally connectable
    BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN)          // Include complete local name in advertising
};

static const struct bt_data sd[] = {}; // Can hold data for a scan response if applicable

static void update_phy(struct bt_conn *conn)
{
    int err;
    const struct bt_conn_le_phy_param preferred_phy = {
        .options = BT_CONN_LE_PHY_OPT_CODED_S8, // Each symbol is represented by 8 symbols, 125kbps datarate
        .pref_rx_phy = BT_GAP_LE_PHY_CODED,     
        .pref_tx_phy = BT_GAP_LE_PHY_CODED,
    };
    err = bt_conn_le_phy_update(conn, &amp;amp;preferred_phy);
    if (err)
    {
        LOG_ERR(&amp;quot;bt_conn_le_phy_update() returned %d&amp;quot;, err);
    }
}

void mtu_exchange_cb(
    struct bt_conn *conn, uint8_t att_err,
    struct bt_gatt_exchange_params *params)
{
    if (att_err)
    {
        LOG_ERR(&amp;quot;MTU exchange returned with error code %d&amp;quot;, att_err);
    }
    else
    {
        LOG_INF(&amp;quot;MTU sucessfully set to %d&amp;quot;, CONFIG_BT_L2CAP_TX_MTU);
    }
}

static void request_mtu_exchange(struct bt_conn *conn)
{
    int err;
    static struct bt_gatt_exchange_params exchange_params;
    exchange_params.func = mtu_exchange_cb;

    err = bt_gatt_exchange_mtu(conn, &amp;amp;exchange_params);
    if (err)
    {
        LOG_ERR(&amp;quot;MTU exchange failed (err %d)&amp;quot;, err);
    }
    else
    {
        LOG_INF(&amp;quot;MTU exchange pending ...&amp;quot;);
    }
}

static void request_data_len_update(struct bt_conn *conn)
{
    int err;

    err = bt_conn_le_data_len_update(conn, CONFIG_BT_CTLR_DATA_LENGTH_MAX);
    if (err)
    {
        LOG_ERR(&amp;quot;Data length update request failed: %d&amp;quot;, err);
        return;
    }

    LOG_INF(&amp;quot;Data length updated to %d&amp;quot;, CONFIG_BT_CTLR_DATA_LENGTH_MAX);
    request_mtu_exchange(conn);
}

void on_connected(struct bt_conn *conn, uint8_t err)
{
    if (err)
    {
        LOG_ERR(&amp;quot;Connection error %d&amp;quot;, err);
        return;
    }
    LOG_INF(&amp;quot;Connected&amp;quot;);

    struct bt_conn_info info;
    err = bt_conn_get_info(conn, &amp;amp;info);
    if (err)
    {
        LOG_ERR(&amp;quot;bt_conn_get_info() returned %d&amp;quot;, err);
        return;
    }

    double connection_interval = info.le.interval * 1.25; // in ms
    uint16_t supervision_timeout = info.le.timeout * 10;  // in ms
    LOG_INF(&amp;quot;Connection parameters: interval %.2f ms, latency %d intervals, timeout %d ms&amp;quot;, connection_interval, info.le.interval, supervision_timeout);

    // update_phy(conn);
    request_data_len_update(conn);
}

void on_disconnected(struct bt_conn *conn, uint8_t reason)
{
    LOG_INF(&amp;quot;Disconnected, reason %d&amp;quot;, reason);
}

void on_le_param_updated(struct bt_conn *conn, uint16_t interval, uint16_t latency, uint16_t timeout)
{
    double connection_interval = interval * 1.25; // in ms
    uint16_t supervision_timeout = timeout * 10;  // in ms
    LOG_INF(&amp;quot;Connection parameters updated: interval %.2f ms, latency %d intervals, timeout %d ms&amp;quot;, connection_interval, latency, supervision_timeout);
}

void on_le_phy_updated(struct bt_conn *conn, struct bt_conn_le_phy_info *param)
{
    if (param-&amp;gt;tx_phy == BT_CONN_LE_TX_POWER_PHY_1M)
    {
        LOG_INF(&amp;quot;PHY updated. New PHY: 1M&amp;quot;);
    }
    else if (param-&amp;gt;tx_phy == BT_CONN_LE_TX_POWER_PHY_2M)
    {
        LOG_INF(&amp;quot;PHY updated. New PHY: 2M&amp;quot;);
    }
    else if (param-&amp;gt;tx_phy == BT_CONN_LE_TX_POWER_PHY_CODED_S2)
    {
        LOG_INF(&amp;quot;PHY updated. New PHY: Long Range (500kbps)&amp;quot;);
    }
    else if (param-&amp;gt;tx_phy == BT_CONN_LE_TX_POWER_PHY_CODED_S8)
    {
        LOG_INF(&amp;quot;PHY updated. New PHY: Long Range (125kbps)&amp;quot;);
    }
}

struct bt_conn_cb connection_callbacks = {
    .connected = on_connected,
    .disconnected = on_disconnected,
    .le_param_updated = on_le_param_updated,
    .le_phy_updated = on_le_phy_updated,
};

/**
 * @brief Initialize the BLE functionality
 *
 * Register the callbacks and enable the BLE stack
 *
 * @return int Error code
 */
int ble_init()
{
    // Register connection callbacks
    bt_conn_cb_register(&amp;amp;connection_callbacks);

    int err = bt_enable(NULL);
    if (err)
    {
        LOG_ERR(&amp;quot;Bluetooth init failed (err %d)\n&amp;quot;, err);
        return err;
    }
    LOG_INF(&amp;quot;Bluetooth initialized\n&amp;quot;);

    return 0;
}

/**
 * @brief Start advertising
 *
 * Start advertising with preset parameters
 *
 * @return int Error code
 */
int ble_adv_start()
{
    int err = bt_le_adv_start(adv_param, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    if (err)
    {
        LOG_ERR(&amp;quot;Advertising failed to start\n&amp;quot;);
        return err;
    }

    return 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The call to update_phy is commented out for now because I&amp;#39;m testing with the app on my phone which doesn&amp;#39;t seem to support the coded PHY, but I intend to eventually connect to another device which does have a Nordic processor that will allow coded PHY as well.&lt;/p&gt;
&lt;p&gt;Hope this helps to clear things up.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Updating data length on peripheral/server side</title><link>https://devzone.nordicsemi.com/thread/460365?ContentTypeID=1</link><pubDate>Thu, 14 Dec 2023 12:44:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:acee8d42-c328-4465-8c55-897a85922d7f</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Can you say more about which device you are using, how you are testing (perhaps show the code and configs) and which nRF Connect SDK version you are using?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>