Bluetooth: Define global Bluetooth address constants

The Bluetooth address constants (BT_ADDR[_LE]_ANY, BT_ADDR[_LE]_NONE)
are currently defined as the address of the local anonymous structs
that are initialised to the corresponding address values, and assigning
them to a variable whose scope is greater than that of a function may
end up creating dangling pointers (for instance, as done in the
`bt_conn_get_info` function).

This commit defines the Bluetooth address constants as global constant
variables that are placed in the read-only data section, and modifies
the Bluetooth address constant macros to use the address of these
variables instead.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
This commit is contained in:
Stephanos Ioannidis 2022-08-24 22:55:04 +09:00 committed by Carles Cufí
commit 074b7f4ecd
3 changed files with 23 additions and 6 deletions

View file

@ -54,16 +54,20 @@ typedef struct {
bt_addr_t a; bt_addr_t a;
} bt_addr_le_t; } bt_addr_le_t;
/* Global Bluetooth address constants defined in bluetooth/common/addr.c */
extern const bt_addr_t bt_addr_any;
extern const bt_addr_t bt_addr_none;
extern const bt_addr_le_t bt_addr_le_any;
extern const bt_addr_le_t bt_addr_le_none;
/** Bluetooth device "any" address, not a valid address */ /** Bluetooth device "any" address, not a valid address */
#define BT_ADDR_ANY ((bt_addr_t[]) { { { 0, 0, 0, 0, 0, 0 } } }) #define BT_ADDR_ANY (&bt_addr_any)
/** Bluetooth device "none" address, not a valid address */ /** Bluetooth device "none" address, not a valid address */
#define BT_ADDR_NONE ((bt_addr_t[]) { { \ #define BT_ADDR_NONE (&bt_addr_none)
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } })
/** Bluetooth LE device "any" address, not a valid address */ /** Bluetooth LE device "any" address, not a valid address */
#define BT_ADDR_LE_ANY ((bt_addr_le_t[]) { { 0, { { 0, 0, 0, 0, 0, 0 } } } }) #define BT_ADDR_LE_ANY (&bt_addr_le_any)
/** Bluetooth LE device "none" address, not a valid address */ /** Bluetooth LE device "none" address, not a valid address */
#define BT_ADDR_LE_NONE ((bt_addr_le_t[]) { { 0, \ #define BT_ADDR_LE_NONE (&bt_addr_le_none)
{ { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } } })
/** @brief Compare Bluetooth device addresses. /** @brief Compare Bluetooth device addresses.
* *

View file

@ -2,6 +2,7 @@
zephyr_library() zephyr_library()
zephyr_library_sources(addr.c)
zephyr_library_sources(dummy.c) zephyr_library_sources(dummy.c)
zephyr_library_sources(log.c) zephyr_library_sources(log.c)

View file

@ -0,0 +1,12 @@
/*
* Copyright (c) 2022 Stephanos Ioannidis <root@stephanos.io>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/bluetooth/addr.h>
const bt_addr_t bt_addr_any = { { 0, 0, 0, 0, 0, 0 } };
const bt_addr_t bt_addr_none = { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
const bt_addr_le_t bt_addr_le_any = { 0, { { 0, 0, 0, 0, 0, 0 } } };
const bt_addr_le_t bt_addr_le_none = { 0, { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };