connectivity/bluetooth/bluetooth-dev.html · connectivity/bluetooth/bluetooth-dev.rst 공식 원문 ↗

Zephyr 3.7.0 · Connectivity · Bluetooth

Bluetooth Application Development

Embedded, 외부 Controller와 simulation 환경에서 Bluetooth application을 개발합니다.

Official pathconnectivity/bluetooth/bluetooth-dev.html
Source fileconnectivity/bluetooth/bluetooth-dev.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

Part 1

요약·해설

Thread safety 원칙부터 Embedded·QEMU·native_sim·BabbleSim 선택, Controller 호환 주의점, bt_enable() 초기화와 Beacon 예제까지 연결합니다.

Setups

Embedded · External · Simulated

Initialize

bt_enable()

Example

BLE Beacon

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 A5A59F5FB9E8342AEB9E5C32035F0FAEE5DE4E804BF964C2CA2842A93E0BDD05
.. _bluetooth-dev:

Application Development
#######################

Bluetooth applications are developed using the common infrastructure and
approach that is described in the :ref:`application` section of the
documentation.

Additional information that is only relevant to Bluetooth applications can be
found on this page.

.. contents::
    :local:
    :depth: 2

Thread safety
*************

Calling into the Bluetooth API is intended to be thread safe, unless otherwise
noted in the documentation of the API function. The effort to ensure that this
is the case for all API calls is an ongoing one, but the overall goal is
formally stated in this paragraph. Bug reports and Pull Requests that move the
subsystem in the direction of such goal are welcome.

.. _bluetooth-hw-setup:

Hardware setup
**************

This section describes the options you have when building and debugging Bluetooth
applications with Zephyr. Depending on the hardware that is available to you,
the requirements you have and the type of development you prefer you may pick
one or another setup to match your needs.

There are 3 possible setups:

#. :ref:`Embedded <bluetooth-hw-setup-embedded>`
#. :ref:`External controller <bluetooth-hw-setup-external-ll>`

   - :ref:`QEMU host <bluetooth-hw-setup-qemu-host>`
   - :ref:`native_sim host <bluetooth-hw-setup-native-sim-host>`

#. :ref:`Simulated nRF5x with BabbleSim <bluetooth-hw-setup-bsim>`

.. _bluetooth-hw-setup-embedded:

Embedded
========

This setup relies on all software running directly on the embedded platform(s)
that the application is targeting.
All the :ref:`bluetooth-configs` and :ref:`bluetooth-build-types` are supported
but you might need to build Zephyr more than once if you are using a dual-chip
configuration or if you have multiple cores in your SoC each running a different
build type (e.g., one running the Host, the other the Controller).

To start developing using this setup follow the :ref:`Getting Started Guide
<getting_started>`, choose one (or more if you are using a dual-chip solution)
boards that support Bluetooth and then :ref:`run the application
<application_run_board>`).

There is a way to access the :ref:`HCI <bluetooth-hci>` traffic between the Host
and Controller, even if there is no physical transport. See :ref:`Embedded HCI
tracing <bluetooth-embedded-hci-tracing>` for instructions.

.. _bluetooth-hw-setup-external-ll:

Host on Linux with an external Controller
=========================================

.. note::
   This is currently only available on GNU/Linux

This setup relies on a "dual-chip" :ref:`configuration <bluetooth-configs>`
which is comprised of the following devices:

#. A :ref:`Host-only <bluetooth-build-types>` application running in the
   :ref:`QEMU <application_run_qemu>` emulator or the :ref:`native_sim <native_sim>` native
   port of Zephyr
#. A Controller, which can be one of the following types:

   * A commercially available Controller
   * A :ref:`Controller-only <bluetooth-build-types>` build of Zephyr
   * A :ref:`Virtual controller <bluetooth_virtual_posix>`

.. warning::
   Certain external Controllers are either unable to accept the Host to
   Controller flow control parameters that Zephyr sets by default (Qualcomm), or
   do not transmit any data from the Controller to the Host (Realtek). If you
   see a message similar to::

     <wrn> bt_hci_core: opcode 0x0c33 status 0x12

   when booting your sample of choice (make sure you have enabled
   :kconfig:option:`CONFIG_LOG` in your :file:`prj.conf` before running the
   sample), or if there is no data flowing from the Controller to the Host, then
   you need to disable Host to Controller flow control. To do so, set
   ``CONFIG_BT_HCI_ACL_FLOW_CONTROL=n`` in your :file:`prj.conf`.

.. _bluetooth-hw-setup-qemu-host:

QEMU
----

You can run the Zephyr Host on the :ref:`QEMU emulator<application_run_qemu>`
and have it interact with a physical external Bluetooth Controller.

Refer to :ref:`bluetooth_qemu_native` for full instructions on how to build and
run an application in this setup.

.. _bluetooth-hw-setup-native-sim-host:

native_sim
----------

.. note::
   This is currently only available on GNU/Linux

The :ref:`native_sim <native_sim>` target builds your Zephyr application
with the Zephyr kernel, and some minimal HW emulation as a native Linux
executable.

This executable is a normal Linux program, which can be debugged and
instrumented like any other, and it communicates with a physical or virtual
external Controller. Refer to:

- :ref:`bluetooth_qemu_native` for the physical controller
- :ref:`bluetooth_virtual_posix` for the virtual controller

.. _bluetooth-hw-setup-bsim:

Simulated nRF5x with BabbleSim
==============================

.. note::
   This is currently only available on GNU/Linux

The :ref:`nrf52_bsim <nrf52_bsim>` and :ref:`nrf5340bsim <nrf5340bsim>` boards,
are simulated target boards
which emulate the necessary peripherals of a nRF52/53 SOC to be able to develop
and test BLE applications.
These boards, use:

   * `BabbleSim`_ to simulate the nRF5x modem and the radio environment.
   * The POSIX arch and native simulator to emulate the processor, and run natively on your host.
   * `Models of the nrf5x HW <https://github.com/BabbleSim/ext_NRF_hw_models/>`_

Just like with the :ref:`native_sim <native_sim>` target, the build result is a normal Linux
executable.
You can find more information on how to run simulations with one or several
devices in either of :ref:`these boards's documentation <nrf52bsim_build_and_run>`.

With the :ref:`nrf52_bsim <nrf52_bsim>`, typically you do :ref:`Combined builds
<bluetooth-build-types>`, but it is also possible to build the controller with one of the
:ref:`HCI UART <bluetooth-hci-uart-sample>` samples in one simulated device, and the host with
the H4 driver instead of the integrated controller in another simulated device.

With the :ref:`nrf5340bsim <nrf5340bsim>`, you can build with either, both controller and host
on its network core, or, with the network core running only the controller, the application
core running the host and your application, and the HCI transport over IPC.

Initialization
**************

The Bluetooth subsystem is initialized using the :c:func:`bt_enable`
function. The caller should ensure that function succeeds by checking
the return code for errors. If a function pointer is passed to
:c:func:`bt_enable`, the initialization happens asynchronously, and the
completion is notified through the given function.

Bluetooth Application Example
*****************************

A simple Bluetooth beacon application is shown below. The application
initializes the Bluetooth Subsystem and enables non-connectable
advertising, effectively acting as a Bluetooth Low Energy broadcaster.

.. literalinclude:: ../../../../../samples/bluetooth/beacon/src/main.c
   :language: c
   :lines: 19-
   :linenos:

The key APIs employed by the beacon sample are :c:func:`bt_enable`
that's used to initialize Bluetooth and then :c:func:`bt_le_adv_start`
that's used to start advertising a specific combination of advertising
and scan response data.

More Examples
*************

More :ref:`sample Bluetooth applications <bluetooth-samples>` are available in
``samples/bluetooth/``.

.. _BabbleSim: https://babblesim.github.io/

Part 3

한국어 전문 번역

Application Development

Bluetooth application은 Zephyr 문서의 일반 Application 개발 절에서 설명하는 공통 infrastructure와 개발 절차를 사용합니다. 이 페이지는 그중 Bluetooth application에만 필요한 추가 정보를 모읍니다.

Thread safety

Bluetooth API function 문서에 별도 주의가 없다면 Bluetooth API 호출은 thread-safe인 것이 원칙입니다. 모든 API 호출이 이 원칙을 만족하도록 만드는 작업은 계속 진행 중이지만, subsystem 전체의 공식 목표는 thread-safe입니다. 이 목표에 가까워지도록 돕는 bug report와 Pull Request를 환영합니다.

Hardware setup

Zephyr Bluetooth application을 build하고 debug할 때 선택할 수 있는 구성을 설명합니다. 가진 hardware, 요구 사항과 선호하는 개발 방식에 따라 다음 세 가지 중 하나를 고릅니다.

  1. Embedded
  2. External controller: QEMU host 또는 native_sim host
  3. BabbleSim으로 simulation하는 nRF5x

Embedded

Application이 목표로 하는 embedded platform에서 모든 software를 직접 실행하는 구성입니다. 모든 Bluetooth configuration과 build type을 지원합니다. 다만 dual-chip configuration이거나 SoC의 여러 core가 서로 다른 build type을 실행한다면 Zephyr를 두 번 이상 build해야 할 수 있습니다. 예를 들어 한 core는 Host를, 다른 core는 Controller를 실행합니다.

이 구성으로 개발을 시작하려면 Zephyr Getting Started Guide를 따라 Bluetooth를 지원하는 board를 하나 고릅니다. Dual-chip solution이면 board가 둘 이상 필요할 수 있습니다. 그런 다음 해당 board에서 application을 build하고 실행합니다.

물리 transport가 없어도 Host와 Controller 사이 HCI traffic을 확인할 수 있습니다. Embedded HCI tracing 절차를 사용합니다.

Linux Host와 외부 Controller

이 구성은 dual-chip configuration입니다. Zephyr의 QEMU emulator 또는 native_sim native port에서 Host-only application을 실행하고, 별도 Controller를 연결합니다.

Controller는 다음 중 하나일 수 있습니다.

  • 시중에서 구할 수 있는 상용 Controller
  • Zephyr Controller-only build
  • Virtual controller

QEMU

Zephyr Host를 QEMU emulator에서 실행하면서 실제 외부 Bluetooth Controller와 통신하게 할 수 있습니다. 이 구성의 전체 build·실행 절차는 Bluetooth QEMU/native 문서를 따릅니다.

native_sim

native_sim target은 Zephyr kernel과 최소한의 hardware emulation을 포함한 application을 native Linux executable로 build합니다. 결과물은 일반 Linux program이므로 다른 program처럼 debugger와 instrumentation을 사용할 수 있고, 실제 또는 virtual 외부 Controller와 통신합니다.

  • Physical controller: Bluetooth QEMU/native 절차 사용
  • Virtual controller: Bluetooth virtual POSIX 절차 사용

BabbleSim으로 simulation하는 nRF5x

nrf52_bsim과 nrf5340bsim은 BLE application 개발·시험에 필요한 nRF52/53 SoC peripheral을 emulation하는 simulation target board입니다.

이 board들은 다음 구성 요소를 사용합니다.

  • BabbleSim: nRF5x modem과 radio environment simulation
  • POSIX architecture와 native simulator: processor를 emulation하고 host에서 native로 실행
  • nRF5x hardware model

native_sim과 마찬가지로 build 결과는 일반 Linux executable입니다. nrf52_bsim 또는 nrf5340bsim board 문서에서 device 하나 또는 여러 개를 포함한 simulation 실행법을 확인할 수 있습니다.

nrf52_bsim에서는 일반적으로 Combined build를 사용합니다. 그러나 simulation device 하나에 HCI UART sample 중 하나로 Controller를 build하고, 다른 device에는 integrated Controller 대신 H4 driver를 쓰는 Host를 build하는 것도 가능합니다.

nrf5340bsim에서는 Controller와 Host를 network core에 함께 둘 수 있습니다. 또는 network core에는 Controller만 두고 application core에 Host와 application을 배치한 뒤 HCI를 IPC로 전달할 수 있습니다.

Initialization

Bluetooth subsystem은 bt_enable()로 초기화합니다. 호출자는 return code를 검사해 성공 여부와 error를 반드시 확인해야 합니다. bt_enable()에 function pointer를 전달하면 초기화는 asynchronous하게 진행되고, 완료 시 그 function으로 통지됩니다.

Bluetooth Application Example

다음은 단순한 Bluetooth beacon application입니다. Bluetooth subsystem을 초기화하고 non-connectable advertising을 시작해 Bluetooth Low Energy broadcaster로 동작합니다. 원문의 literalinclude는 `samples/bluetooth/beacon/src/main.c` 19번째 줄부터를 그대로 싣습니다.


/*
 * Set Advertisement data. Based on the Eddystone specification:
 * https://github.com/google/eddystone/blob/master/protocol-specification.md
 * https://github.com/google/eddystone/tree/master/eddystone-url
 */
static const struct bt_data ad[] = {
	BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
	BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0xaa, 0xfe),
	BT_DATA_BYTES(BT_DATA_SVC_DATA16,
		      0xaa, 0xfe, /* Eddystone UUID */
		      0x10, /* Eddystone-URL frame type */
		      0x00, /* Calibrated Tx power at 0m */
		      0x00, /* URL Scheme Prefix http://www. */
		      'z', 'e', 'p', 'h', 'y', 'r',
		      'p', 'r', 'o', 'j', 'e', 'c', 't',
		      0x08) /* .org */
};

/* Set Scan Response data */
static const struct bt_data sd[] = {
	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
};

static void bt_ready(int err)
{
	char addr_s[BT_ADDR_LE_STR_LEN];
	bt_addr_le_t addr = {0};
	size_t count = 1;

	if (err) {
		printk("Bluetooth init failed (err %d)\n", err);
		return;
	}

	printk("Bluetooth initialized\n");

	/* Start advertising */
	err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad),
			      sd, ARRAY_SIZE(sd));
	if (err) {
		printk("Advertising failed to start (err %d)\n", err);
		return;
	}


	/* For connectable advertising you would use
	 * bt_le_oob_get_local().  For non-connectable non-identity
	 * advertising an non-resolvable private address is used;
	 * there is no API to retrieve that.
	 */

	bt_id_get(&addr, &count);
	bt_addr_le_to_str(&addr, addr_s, sizeof(addr_s));

	printk("Beacon started, advertising as %s\n", addr_s);
}

int main(void)
{
	int err;

	printk("Starting Beacon Demo\n");

	/* Initialize the Bluetooth Subsystem */
	err = bt_enable(bt_ready);
	if (err) {
		printk("Bluetooth init failed (err %d)\n", err);
	}
	return 0;
}

bt_enable()은 Bluetooth를 초기화하고, bt_le_adv_start()는 지정한 advertising data와 scan-response data 조합의 advertising을 시작합니다.

More Examples

더 많은 Bluetooth sample application은 samples/bluetooth/에서 찾을 수 있습니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.