<?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 integrate BLE Mesh + NUS together in Zephyr</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/118398/how-to-integrate-ble-mesh-nus-together-in-zephyr</link><description>I am using Zephyr to create a program that integrates BLE Mesh and NUS. My goal is to use a sensor_client to read data from a sensor_server, and then send the data, via peripheral_uart, to an external central_uart device after reading it. I referred to</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 01 Feb 2025 14:40:13 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/118398/how-to-integrate-ble-mesh-nus-together-in-zephyr" /><item><title>RE: How to integrate BLE Mesh + NUS together in Zephyr</title><link>https://devzone.nordicsemi.com/thread/520974?ContentTypeID=1</link><pubDate>Sat, 01 Feb 2025 14:40:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:026969cf-7c7b-4294-88d4-19d28d9a63be</guid><dc:creator>Ziyao Zhou</dc:creator><description>&lt;p&gt;I have finish this code and publish in Github, Thank you so much for your help!&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/Gary-zzy/ble_peripheral_uart-sensor_server_coex"&gt;github&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to integrate BLE Mesh + NUS together in Zephyr</title><link>https://devzone.nordicsemi.com/thread/520963?ContentTypeID=1</link><pubDate>Sat, 01 Feb 2025 10:03:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d11a3789-71a1-4e0e-b8e3-3367efa628d9</guid><dc:creator>Ziyao Zhou</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I have successfully intergrate these two together, however, I notice a issue.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background-color:#ffff00;color:#000000;"&gt;1.&lt;/span&gt;When NUS is not connected, the sensor client can get the readings correctly every time it requests.&lt;/p&gt;
&lt;p&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/pastedimage1738403999572v2.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background-color:#ffff00;"&gt;2.&lt;/span&gt;When the nus is connected, the sensor client can make the correct request, but sometimes it cannot receive the correct data, causing the last received data to be used&lt;/p&gt;
&lt;p&gt;&lt;img style="height:75px;" height="115" src="https://devzone.nordicsemi.com/resized-image/__size/628x150/__key/communityserver-discussions-components-files/4/pastedimage1738403879771v1.png_2D00_640x480.png" width="313" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Below are my code:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background-color:#ff0000;"&gt;model handler:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;bluetooth/mesh/models.h&amp;gt;
#include &amp;quot;model_handler.h&amp;quot;
#include &amp;quot;nus_handler.h&amp;quot; 

#define GET_DATA_INTERVAL 9000

static struct k_work_delayable get_data_work;

/* 传感器客户端回调函数 - 只处理People Count */
static void sensor_cli_data_cb(struct bt_mesh_sensor_cli *cli,
                               struct bt_mesh_msg_ctx *ctx,
                               const struct bt_mesh_sensor_type *sensor,
                               const struct bt_mesh_sensor_value *value)
{
    if (sensor-&amp;gt;id == bt_mesh_sensor_people_count.id) {
        //printk(&amp;quot;People count is %s\n&amp;quot;, bt_mesh_sensor_ch_str(value));
         const char *count_str = bt_mesh_sensor_ch_str(value);
        printk(&amp;quot;People count is %s\n&amp;quot;, count_str);

        /* 更新 NUS 模块中的 people count 字符串 */
        nus_update_people_count(count_str);
    }
}

/* 简化的传感器客户端处理程序 */
static const struct bt_mesh_sensor_cli_handlers bt_mesh_sensor_cli_handlers = {
    .data = sensor_cli_data_cb,
};

static struct bt_mesh_sensor_cli sensor_cli =
    BT_MESH_SENSOR_CLI_INIT(&amp;amp;bt_mesh_sensor_cli_handlers);

/* 数据获取工作处理函数 - 只请求People Count */
static void get_data(struct k_work *work)
{
    if (!bt_mesh_is_provisioned()) {
        k_work_schedule(&amp;amp;get_data_work, K_MSEC(GET_DATA_INTERVAL));
        return;
    }

    int err = bt_mesh_sensor_cli_get(&amp;amp;sensor_cli, 
                                   NULL, 
                                   &amp;amp;bt_mesh_sensor_people_count, 
                                   NULL);
    if (err) {
        printk(&amp;quot;Error getting people count (%d)\n&amp;quot;, err);
    }
    
    k_work_schedule(&amp;amp;get_data_work, K_MSEC(GET_DATA_INTERVAL));
}

/* 健康服务回调（保留基本功能） */
static void attention_on(const struct bt_mesh_model *mod) {}
static void attention_off(const struct bt_mesh_model *mod) {}

static const struct bt_mesh_health_srv_cb health_srv_cb = {
    .attn_on = attention_on,
    .attn_off = attention_off,
};

static struct bt_mesh_health_srv health_srv = {
    .cb = &amp;amp;health_srv_cb,
};

BT_MESH_HEALTH_PUB_DEFINE(health_pub, 0);

/* 模型元素配置 */
static struct bt_mesh_elem elements[] = {
    BT_MESH_ELEM(1,
        BT_MESH_MODEL_LIST(
            BT_MESH_MODEL_CFG_SRV,
            BT_MESH_MODEL_HEALTH_SRV(&amp;amp;health_srv, &amp;amp;health_pub),
            BT_MESH_MODEL_SENSOR_CLI(&amp;amp;sensor_cli)
        ),
        BT_MESH_MODEL_NONE
    ),
};

static const struct bt_mesh_comp comp = {
    .cid = CONFIG_BT_COMPANY_ID,
    .elem = elements,
    .elem_count = ARRAY_SIZE(elements),
};

const struct bt_mesh_comp *model_handler_init(void)
{
    k_work_init_delayable(&amp;amp;get_data_work, get_data);
    k_work_schedule(&amp;amp;get_data_work, K_MSEC(GET_DATA_INTERVAL));
    
    return &amp;amp;comp;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background-color:#ff0000;"&gt;nus_handler:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;quot;nus_handler.h&amp;quot;
#include &amp;lt;zephyr/types.h&amp;gt;
#include &amp;lt;zephyr/sys/printk.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/gatt.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;zephyr/sys/byteorder.h&amp;gt;

#include &amp;lt;zephyr/types.h&amp;gt;
#include &amp;lt;stddef.h&amp;gt;
#include &amp;lt;zephyr/sys/printk.h&amp;gt;
#include &amp;lt;zephyr/sys/byteorder.h&amp;gt;
#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/drivers/gpio.h&amp;gt;
#include &amp;lt;soc.h&amp;gt;

#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/hci.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/conn.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/uuid.h&amp;gt;

#include &amp;lt;bluetooth/services/nus.h&amp;gt;
#include &amp;lt;dk_buttons_and_leds.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/conn.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

#define PEOPLE_COUNT_STR_SIZE 32
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(CONFIG_BT_DEVICE_NAME) - 1)

static struct bt_conn *current_conn;
static struct k_work_delayable send_work;
static struct bt_le_ext_adv *nus_adv; // 独立的扩展广告实例
//static const char cmd_str[] = &amp;quot;123456789&amp;quot;;
static uint8_t nus_conn_id = 1;

static int start_extended_advertising(void);
static void send_work_handler(struct k_work *work);
static int init_extended_advertising(void);





static char people_count_str[PEOPLE_COUNT_STR_SIZE] = &amp;quot;0&amp;quot;;
void nus_update_people_count(const char *new_val)
{
    /* 将传入的字符串复制到全局变量中，注意防止溢出 */
    strncpy(people_count_str, new_val, sizeof(people_count_str) - 1);
    people_count_str[sizeof(people_count_str) - 1] = &amp;#39;\0&amp;#39;;
}






/* Advertising配置 */
static const struct bt_data ad[] = {
    BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
};

static const struct bt_data sd[] = {
    BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NUS_VAL),
};

/* 蓝牙连接回调函数 */
static void connected(struct bt_conn *conn, uint8_t err)
{
    struct bt_conn_info conn_info;

    if (err) {
        printk(&amp;quot;Connection failed (err 0x%02x)\n&amp;quot;, err);
        return;
    }

    // 获取连接信息
    if (bt_conn_get_info(conn, &amp;amp;conn_info) != 0) {
        printk(&amp;quot;Failed to get connection info\n&amp;quot;);
        return;
    }

    // 检查连接是否属于NUS服务的标识
    if (conn_info.id == nus_conn_id) {
        printk(&amp;quot;NUS Connected\n&amp;quot;);
        current_conn = bt_conn_ref(conn); // 保存连接引用

        // 初始化定时发送任务
        k_work_init_delayable(&amp;amp;send_work, send_work_handler);
        k_work_schedule(&amp;amp;send_work, K_SECONDS(10)); // 每 10 秒发送一次数据
    } else {
        printk(&amp;quot;Connected to another service (Mesh), not handling\n&amp;quot;);
    }
}


static void disconnected(struct bt_conn *conn, uint8_t reason) {
    printk(&amp;quot;Disconnected (reason 0x%02x)\n&amp;quot;, reason);

    if (current_conn) {
        // 强制断开物理连接
        bt_conn_disconnect(current_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
        bt_conn_unref(current_conn);
        current_conn = NULL; 
    }

     k_work_cancel_delayable(&amp;amp;send_work);


        if (nus_adv) {
        bt_le_ext_adv_stop(nus_adv);
        bt_le_ext_adv_delete(nus_adv);
        nus_adv = NULL;
    }
    // 重新启动广告
    init_extended_advertising();
    start_extended_advertising();

}

/* 定义连接回调结构 */
BT_CONN_CB_DEFINE(conn_callbacks) = {
    .connected = connected,
    .disconnected = disconnected,
};

/* 数据发送工作处理 */
static void send_work_handler(struct k_work *work)
{
    if (current_conn) {
        /* 直接发送更新后的 people_count_str */
        int err = bt_nus_send(current_conn, people_count_str, strlen(people_count_str));
        if (err == -ENOMEM) {
            printk(&amp;quot;Failed to send NUS data: Out of memory (err %d)\n&amp;quot;, err);
        } else if (err) {
            printk(&amp;quot;Failed to send NUS data (err %d)\n&amp;quot;, err);
        } else {
            printk(&amp;quot;Sent People Count: %s\n&amp;quot;, people_count_str);
        }
    } else {
        printk(&amp;quot;No active connection, skipping send\n&amp;quot;);
    }

    /* 定时调度下一次发送任务 */
    k_work_schedule(&amp;amp;send_work, K_SECONDS(10));
}

/* NUS回调函数 */
static void bt_receive_cb(struct bt_conn *conn, const uint8_t *const data, uint16_t len)
{
    printk(&amp;quot;Received data: %.*s\n&amp;quot;, len, data);
}

/* NUS回调结构 */
static struct bt_nus_cb nus_cb = {
    .received = bt_receive_cb,
};

/* 初始化扩展广告 */
static int init_extended_advertising(void)
{
    int err;
    size_t id_count = 0xFF;
    //struct bt_le_adv_param adv_params = *BT_LE_ADV_CONN;

	struct bt_le_adv_param adv_params = {
        .id = nus_conn_id,
        .options = BT_LE_ADV_OPT_CONNECTABLE, // 明确启用扩展广告和连接
        .interval_min = BT_GAP_ADV_FAST_INT_MIN_2,
        .interval_max = BT_GAP_ADV_FAST_INT_MAX_2,
        .peer = NULL,
    };
    /* 检查当前标识符数量 */
    (void)bt_id_get(NULL, &amp;amp;id_count);

    if (id_count &amp;lt; CONFIG_BT_ID_MAX) {
        int id = bt_id_create(NULL, NULL);

        if (id &amp;lt; 0) {
            printk(&amp;quot;Unable to create a new identity for NUS (err %d). Using the default one.\n&amp;quot;, id);
            nus_conn_id = BT_ID_DEFAULT;
        } else {
            nus_conn_id = id;
            printk(&amp;quot;Created a new identity for NUS: %d\n&amp;quot;, nus_conn_id);
        }
    } else {
        nus_conn_id = 1;
        printk(&amp;quot;Recovered identity for NUS: %d\n&amp;quot;, nus_conn_id);
    }

     

    adv_params.id = nus_conn_id;


    /* 创建扩展广告实例 */
    err = bt_le_ext_adv_create(&amp;amp;adv_params, NULL, &amp;amp;nus_adv);
    if (err) {
        printk(&amp;quot;Failed to create extended advertising instance (err %d)\n&amp;quot;, err);
        return err;
    }

    /* 设置广告和扫描响应数据 */
    err = bt_le_ext_adv_set_data(nus_adv, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    if (err) {
        printk(&amp;quot;Failed to set extended advertising data (err %d)\n&amp;quot;, err);
        return err;
    }

    printk(&amp;quot;Extended advertising instance created\n&amp;quot;);
    return 0;
}

/* 启动扩展广告 */
static int start_extended_advertising(void)
{
    int err;

    err = bt_le_ext_adv_start(nus_adv, BT_LE_EXT_ADV_START_DEFAULT);
    if (err) {
        printk(&amp;quot;Failed to start extended advertising (err %d)\n&amp;quot;, err);
        return err;
    }

    printk(&amp;quot;Extended advertising started\n&amp;quot;);
    return 0;
}

/* 初始化NUS服务的入口函数 */
int nus_handler_init(void)
{
    int err;

    /* 初始化NUS服务 */
    err = bt_nus_init(&amp;amp;nus_cb);
    if (err) {
        printk(&amp;quot;Failed to initialize NUS (err %d)\n&amp;quot;, err);
        return err;
    }

    printk(&amp;quot;NUS service initialized\n&amp;quot;);

    /* 初始化扩展广告 */
    err = init_extended_advertising();
    if (err) {
        return err;
    }

    /* 启动扩展广告 */
    err = start_extended_advertising();
    if (err) {
        return err;
    }

    

    printk(&amp;quot;NUS Handler initialized\n&amp;quot;);
    return 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background-color:#ff0000;"&gt;main.c&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

/** @file
 *  @brief Nordic mesh sensor observer sample
 */
#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;bluetooth/mesh/models.h&amp;gt;
#include &amp;lt;bluetooth/mesh/dk_prov.h&amp;gt;
#include &amp;lt;dk_buttons_and_leds.h&amp;gt;
#include &amp;quot;model_handler.h&amp;quot;


#include &amp;quot;nus_handler.h&amp;quot;//头文件


static void bt_ready(int err)
{
	if (err) {
		printk(&amp;quot;Bluetooth init failed (err %d)\n&amp;quot;, err);
		return;
	}

	printk(&amp;quot;Bluetooth initialized\n&amp;quot;);

	err = dk_leds_init();
	if (err) {
		printk(&amp;quot;Initializing LEDs failed (err %d)\n&amp;quot;, err);
		return;
	}

	err = dk_buttons_init(NULL);
	if (err) {
		printk(&amp;quot;Initializing buttons failed (err %d)\n&amp;quot;, err);
		return;
	}

	
		


	err = bt_mesh_init(bt_mesh_dk_prov_init(), model_handler_init());
	if (err) {
		printk(&amp;quot;Initializing mesh failed (err %d)\n&amp;quot;, err);
		return;
	}

	if (IS_ENABLED(CONFIG_SETTINGS)) {
		settings_load();
	}

	/* This will be a no-op if settings_load() loaded provisioning info */
	bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT);

	printk(&amp;quot;Mesh initialized\n&amp;quot;);

/* 初始化 NUS handler */
	err = nus_handler_init();
	if (err) {
		printk(&amp;quot;NUS handler initialization failed (err %d)\n&amp;quot;, err);
		return;
	}
	printk(&amp;quot;NUS handler initialized\n&amp;quot;);
	 
}


int main(void)
{
	int err;

	printk(&amp;quot;Initializing...\n&amp;quot;);

	err = bt_enable(bt_ready);
	if (err) {
		printk(&amp;quot;Bluetooth init failed (err %d)\n&amp;quot;, err);
	}

	return 0;
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background-color:#ff0000;"&gt;projconfig&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#
# Copyright (c) 2020 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
CONFIG_NCS_SAMPLES_DEFAULTS=y

# Deferred logging helps improve LPN power consumption
# when friendship is established.
CONFIG_LOG_MODE_DEFERRED=y

# General configuration
CONFIG_NCS_APPLICATION_BOOT_BANNER_STRING=&amp;quot;Mesh Sensor Observer&amp;quot;
#CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048 在后面加了个大的
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
# Flash shell module uses 8k of RAM for testing buffers, disable to save RAM
CONFIG_FLASH_SHELL=n
CONFIG_NVS=y
CONFIG_NVS_LOOKUP_CACHE=y
CONFIG_SETTINGS=y
CONFIG_SETTINGS_NVS_NAME_CACHE=y
CONFIG_HWINFO=y
CONFIG_DK_LIBRARY=y
CONFIG_PM_SINGLE_IMAGE=y
CONFIG_PM_PARTITION_SIZE_SETTINGS_STORAGE=0x8000
CONFIG_SOC_FLASH_NRF_PARTIAL_ERASE=y
CONFIG_CBPRINTF_FP_SUPPORT=y

# Bluetooth configuration
CONFIG_BT=y
CONFIG_BT_DEVICE_NAME=&amp;quot;Mesh Sensor Observer&amp;quot;
CONFIG_BT_L2CAP_TX_BUF_COUNT=8
CONFIG_BT_OBSERVER=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_SETTINGS=y

# Disable unused Bluetooth features
CONFIG_BT_CTLR_LE_ENC=n
CONFIG_BT_PHY_UPDATE=n
CONFIG_BT_CTLR_CHAN_SEL_2=n
CONFIG_BT_CTLR_MIN_USED_CHAN=n
CONFIG_BT_CTLR_PRIVACY=n

# Bluetooth Mesh configuration
CONFIG_BT_MESH=y
CONFIG_BT_MESH_RELAY=y
CONFIG_BT_MESH_FRIEND=y
CONFIG_BT_MESH_TX_SEG_MAX=24
CONFIG_BT_MESH_RX_SEG_MAX=16
CONFIG_BT_MESH_PB_GATT=y
CONFIG_BT_MESH_GATT_PROXY=y
CONFIG_BT_MESH_PROXY_USE_DEVICE_NAME=y
CONFIG_BT_MESH_DK_PROV=y
CONFIG_BT_MESH_SUBNET_COUNT=2
CONFIG_BT_MESH_APP_KEY_COUNT=3
CONFIG_BT_MESH_MODEL_KEY_COUNT=3
CONFIG_BT_MESH_CRPL=32
CONFIG_BT_MESH_MSG_CACHE_SIZE=64
CONFIG_BT_MESH_SHELL=y

# Bluetooth Mesh models
CONFIG_BT_MESH_SENSOR_CLI=y

CONFIG_LOG_BACKEND_RTT=n


#我加的我加的

CONFIG_BT_NUS=y
# CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_BT_EXT_ADV=y
CONFIG_BT_EXT_ADV_MAX_ADV_SET=9
# CONFIG_BT_BUF_ACL_RX_SIZE=27
# CONFIG_BT_BUF_ACL_TX_SIZE=27
# CONFIG_BT_BUF_ACL_TX_COUNT=10
# CONFIG_BT_BUF_EVT_RX_COUNT=10
# CONFIG_BT_BUF_EVT_DISCARDABLE_SIZE=58
# CONFIG_HEAP_MEM_POOL_SIZE=4096
# CONFIG_MAIN_STACK_SIZE=2048
# CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
CONFIG_BT_ID_MAX=9
CONFIG_BT_MAX_CONN=9
CONFIG_BT_BUF_CMD_TX_COUNT=30

#为了调试加的
CONFIG_LOG=y
#CONFIG_LOG_DEFAULT_LEVEL=4&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you so much for your time!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to integrate BLE Mesh + NUS together in Zephyr</title><link>https://devzone.nordicsemi.com/thread/520913?ContentTypeID=1</link><pubDate>Fri, 31 Jan 2025 14:51:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0d65cfc4-0ae4-4dce-95e1-24125b88469e</guid><dc:creator>Ziyao Zhou</dc:creator><description>&lt;p&gt;&lt;span&gt;Dear Andreas,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Today I have tried to change ble_peripheral_lbs_coex &amp;#39;s LBS service to my nus_handler, and I can get the data through NUS central. Though there are still some problems that BLE mesh sometimes is not compatible to NUS, it turn out that the my NUS code is OK. So the problems should be beteween BLE mesh sensor client and BLE mesh onoff. What is the difference between these two, I hope you could help me figure out &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f601.svg" title="Grin"&gt;&amp;#x1f601;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to integrate BLE Mesh + NUS together in Zephyr</title><link>https://devzone.nordicsemi.com/thread/520712?ContentTypeID=1</link><pubDate>Thu, 30 Jan 2025 13:02:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a976ed89-2a99-47f5-a911-f3fd9fa821ca</guid><dc:creator>Ziyao Zhou</dc:creator><description>&lt;div&gt;
&lt;div&gt;Dear Ahaug
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Today I have successfully monitor the varible &amp;quot;err&amp;quot; and I found whenever the code go through this function, it will return err = -105&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&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/pastedimage1738231654351v3.png" alt=" " /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre class="ui-code" data-mode="text"&gt;
err = le_ext_adv_param_set(adv, param, false);
&amp;#160; &amp;#160; if (err) {
&amp;#160; &amp;#160; &amp;#160; &amp;#160; adv_delete(adv);
&amp;#160; &amp;#160; &amp;#160; &amp;#160; return err;
&amp;#160; &amp;#160; }
&lt;/pre&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;However, I cannor go deeper than this function. Sometimes when I go into this function it will return err -105, sometimes cannot. So I currenrly can only say that this function leads the problems.&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;Could you please help me to figure it out?&lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f603.svg" title="Smiley"&gt;&amp;#x1f603;&lt;/span&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to integrate BLE Mesh + NUS together in Zephyr</title><link>https://devzone.nordicsemi.com/thread/520571?ContentTypeID=1</link><pubDate>Wed, 29 Jan 2025 14:48:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c4b16af2-4eb6-402a-9052-bd8c64d2ade6</guid><dc:creator>Ziyao Zhou</dc:creator><description>&lt;p&gt;&lt;span&gt;Dear Andreas,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thank you for your suggestion! I have tried to do the debug and I think the problem is&amp;nbsp;&lt;/span&gt;ENOBUFS.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static int le_ext_adv_param_set(struct bt_le_ext_adv *adv,
				const struct bt_le_adv_param *param,
				bool  has_scan_data)
{
	struct bt_hci_cp_le_set_ext_adv_param *cp;
	bool dir_adv = param-&amp;gt;peer != NULL, scannable;
	struct net_buf *buf, *rsp;
	int err;
	enum adv_name_type name_type;
	uint16_t props = 0;

	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_ADV_PARAM, sizeof(*cp));
	if (!buf) {
		return -ENOBUFS;
	}

	cp = net_buf_add(buf, sizeof(*cp));
	(void)memset(cp, 0, sizeof(*cp));

	adv-&amp;gt;options = param-&amp;gt;options;

	err = bt_id_set_adv_own_addr(adv, param-&amp;gt;options, dir_adv,
				     &amp;amp;cp-&amp;gt;own_addr_type);
	if (err) {
		net_buf_unref(buf);
		return err;
	}

	if (dir_adv) {
		bt_addr_le_copy(&amp;amp;adv-&amp;gt;target_addr, param-&amp;gt;peer);
	} else {
		bt_addr_le_copy(&amp;amp;adv-&amp;gt;target_addr, BT_ADDR_LE_ANY);
	}

	name_type = get_adv_name_type_param(param);

	cp-&amp;gt;handle = adv-&amp;gt;handle;
	sys_put_le24(param-&amp;gt;interval_min, cp-&amp;gt;prim_min_interval);
	sys_put_le24(param-&amp;gt;interval_max, cp-&amp;gt;prim_max_interval);
	cp-&amp;gt;prim_channel_map = get_adv_channel_map(param-&amp;gt;options);
	cp-&amp;gt;filter_policy = get_filter_policy(param-&amp;gt;options);
	cp-&amp;gt;tx_power = BT_HCI_LE_ADV_TX_POWER_NO_PREF;

	cp-&amp;gt;prim_adv_phy = BT_HCI_LE_PHY_1M;
	if ((param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_EXT_ADV) &amp;amp;&amp;amp;
	    !(param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_NO_2M)) {
		cp-&amp;gt;sec_adv_phy = BT_HCI_LE_PHY_2M;
	} else {
		cp-&amp;gt;sec_adv_phy = BT_HCI_LE_PHY_1M;
	}

	if (param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_CODED) {
		cp-&amp;gt;prim_adv_phy = BT_HCI_LE_PHY_CODED;
		cp-&amp;gt;sec_adv_phy = BT_HCI_LE_PHY_CODED;
	}

	if (!(param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_EXT_ADV)) {
		props |= BT_HCI_LE_ADV_PROP_LEGACY;
	}

	if (param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_USE_TX_POWER) {
		props |= BT_HCI_LE_ADV_PROP_TX_POWER;
	}

	if (param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_ANONYMOUS) {
		props |= BT_HCI_LE_ADV_PROP_ANON;
	}

	if (param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_NOTIFY_SCAN_REQ) {
		cp-&amp;gt;scan_req_notify_enable = BT_HCI_LE_ADV_SCAN_REQ_ENABLE;
	}

	if (param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_CONNECTABLE) {
		props |= BT_HCI_LE_ADV_PROP_CONN;
		if (!dir_adv &amp;amp;&amp;amp; !(param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_EXT_ADV)) {
			/* When using non-extended adv packets then undirected
			 * advertising has to be scannable as well.
			 * We didn&amp;#39;t require this option to be set before, so
			 * it is implicitly set instead in this case.
			 */
			props |= BT_HCI_LE_ADV_PROP_SCAN;
		}
	}

	if ((param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_SCANNABLE) || has_scan_data ||
	    (name_type == ADV_NAME_TYPE_SD)) {
		props |= BT_HCI_LE_ADV_PROP_SCAN;
	}

	scannable = !!(props &amp;amp; BT_HCI_LE_ADV_PROP_SCAN);

	if (dir_adv) {
		props |= BT_HCI_LE_ADV_PROP_DIRECT;
		if (!(param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
			props |= BT_HCI_LE_ADV_PROP_HI_DC_CONN;
		}

		bt_addr_le_copy(&amp;amp;cp-&amp;gt;peer_addr, param-&amp;gt;peer);
	}

	cp-&amp;gt;sid = param-&amp;gt;sid;

	cp-&amp;gt;sec_adv_max_skip = param-&amp;gt;secondary_max_skip;

	cp-&amp;gt;props = sys_cpu_to_le16(props);
	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_ADV_PARAM, buf, &amp;amp;rsp);
	if (err) {
		return err;
	}

#if defined(CONFIG_BT_EXT_ADV)
	struct bt_hci_rp_le_set_ext_adv_param *rp = (void *)rsp-&amp;gt;data;

	adv-&amp;gt;tx_power = rp-&amp;gt;tx_power;
#endif /* defined(CONFIG_BT_EXT_ADV) */

	net_buf_unref(rsp);

	atomic_set_bit(adv-&amp;gt;flags, BT_ADV_PARAMS_SET);

	if (atomic_test_and_clear_bit(adv-&amp;gt;flags, BT_ADV_RANDOM_ADDR_PENDING)) {
		err = bt_id_set_adv_random_addr(adv, &amp;amp;adv-&amp;gt;random_addr.a);
		if (err) {
			return err;
		}
	}

	/* Flag only used by bt_le_adv_start API. */
	atomic_set_bit_to(adv-&amp;gt;flags, BT_ADV_PERSIST, false);

	atomic_set_bit_to(adv-&amp;gt;flags, BT_ADV_INCLUDE_NAME_AD,
			  name_type == ADV_NAME_TYPE_AD);

	atomic_set_bit_to(adv-&amp;gt;flags, BT_ADV_INCLUDE_NAME_SD,
			  name_type == ADV_NAME_TYPE_SD);

	atomic_set_bit_to(adv-&amp;gt;flags, BT_ADV_CONNECTABLE,
			  param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_CONNECTABLE);

	atomic_set_bit_to(adv-&amp;gt;flags, BT_ADV_SCANNABLE, scannable);

	atomic_set_bit_to(adv-&amp;gt;flags, BT_ADV_USE_IDENTITY,
			  param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_USE_IDENTITY);

	atomic_set_bit_to(adv-&amp;gt;flags, BT_ADV_EXT_ADV,
			  param-&amp;gt;options &amp;amp; BT_LE_ADV_OPT_EXT_ADV);

	return 0;
}&lt;/pre&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/pastedimage1738161073091v1.png_2D00_640x480.png" alt=" " /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to integrate BLE Mesh + NUS together in Zephyr</title><link>https://devzone.nordicsemi.com/thread/520541?ContentTypeID=1</link><pubDate>Wed, 29 Jan 2025 11:37:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9f08047e-09b1-46e7-bc91-57cc7afe2a2c</guid><dc:creator>AHaug</dc:creator><description>&lt;p&gt;Noted,&lt;/p&gt;
&lt;p&gt;Could you do some debugging and see where&amp;nbsp;bt_le_ext_adv_create(&amp;amp;adv_params, NULL, &amp;amp;nus_adv); fails? If you&amp;#39;re uncertain, then this lesson might be a good resource to follow:&amp;nbsp;&lt;a href="https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate/lessons/lesson-2-debugging/"&gt;https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate/lessons/lesson-2-debugging/&amp;nbsp;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;br /&gt;Andreas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to integrate BLE Mesh + NUS together in Zephyr</title><link>https://devzone.nordicsemi.com/thread/520531?ContentTypeID=1</link><pubDate>Wed, 29 Jan 2025 10:07:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f607baa2-d01f-4e6b-8b31-4fe043558704</guid><dc:creator>Ziyao Zhou</dc:creator><description>&lt;p&gt;Thank you so much for your reply! I just tried the config but still cannot start ADV&lt;/p&gt;
&lt;p&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/pastedimage1738145237650v3.png" alt=" " /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to integrate BLE Mesh + NUS together in Zephyr</title><link>https://devzone.nordicsemi.com/thread/520527?ContentTypeID=1</link><pubDate>Wed, 29 Jan 2025 09:59:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6b92bee0-e43d-4662-912d-32aac9b8451c</guid><dc:creator>AHaug</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This is most likely not the reason for your fault, but could you enable&amp;nbsp;CONFIG_BT_EXT_ADV=y?&amp;nbsp;&lt;a href="https://docs.nordicsemi.com/bundle/ncs-2.8.0/page/kconfig/index.html#CONFIG_BT_EXT_ADV_MAX_ADV_SET"&gt;https://docs.nordicsemi.com/bundle/ncs-2.8.0/page/kconfig/index.html#CONFIG_BT_EXT_ADV_MAX_ADV_SET&lt;/a&gt;&amp;nbsp;depends on that configuration and AFAIK from your prj.conf I can&amp;#39;t see that you&amp;#39;ve enabled extended advertising&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;br /&gt;Andreas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>