Skip to content

Conversation

@huangyulong3
Copy link

Memory Optimization: Change the bluetooth bt_tool adv module to const for reduce the ram size.

bug:v/84960

Rootcause: To reduce RAM resource consumption, the adv_options,a dv_stop_options,
g_adv_tables and adv_callback in the bt_tools adv module must be marked with the const modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 huangyulong3@xiaomi.com

…k to const for reduce the ram size.

bug: v/84934

Rootcause: To reduce RAM resource consumption, the `adv_callback`
in the ble impl module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
…re_gattc_cbs to const for reduce the ram size.

bug: v/84935

Rootcause: To reduce RAM resource consumption, the `s_feature_gattc_cbs`
in the gatt feature module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
…v_callback to const for reduce the ram size.

bug: v/84941

Rootcause: To reduce RAM resource consumption, the `g_advertiser_socket_cb`
in the socket advertiser module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com
…back to const for reduce the ram size.

bug: v/84944

Rootcause: To reduce RAM resource consumption, the `g_advertiser_socket_cb`
in the socket scan module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com
…ket_cb to const for reduce the ram size.

bug: v/84945

Rootcause: To reduce RAM resource consumption, the `g_spp_socket_cb`
in the socket spp module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com
…o const for reduce the ram size.

bug:v/84946

Rootcause: To reduce RAM resource consumption, the `deviceInterface`
in the hid device service module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com
… for reduce the ram size.

bug:v/84947

Rootcause: To reduce RAM resource consumption, the `sppInterface`
in the spp service module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com
…o const for reduce the ram size.

bug:v/84950

Rootcause: To reduce RAM resource consumption, the `stream_ops`
in the sal a2dp interface module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com
…e to const for reduce the ram size.

bug:v/84951

Rootcause: To reduce RAM resource consumption, the `g_conn_cbs`, `g_setting_cbs`,
`g_conn_auth_info_cbs`and `g_br_discovery_cb`,
in the sal adapter interface module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com
…dule to const for reduce the ram size.

bug:v/84952

Rootcause: To reduce RAM resource consumption, the `g_conn_auth_info_cbs`, `g_conn_cbs`,
and `g_setting_cbs` in the sal adapter interface module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
…to const for reduce the ram size.

bug:v/84953

Rootcause: To reduce RAM resource consumption, the `avrcp_tg_cbks`,
in the sal avrcp interface module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
… to const for reduce the ram size.

bug:v/84954

Rootcause: To reduce RAM resource consumption, the `g_hfp_ag_cb`,
in the sal hfp ag interface module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
…e to const for reduce the ram size.

bug:v/84956

Rootcause: To reduce RAM resource consumption, the `hf_callbacks`,
in the sal hfp hf interface module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.
…dule to const for reduce the ram size.

bug:v/84957

Rootcause: To reduce RAM resource consumption, the `hid_attrs_template` and `hid_callback`,
in the sal hid device interface module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.
…dule to const for reduce the ram size.

bug:v/84958

Rootcause: To reduce RAM resource consumption, the `spp_attrs_template` and `g_rfcomm_ops`,
in the sal spp interface module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
…to const for reduce the ram size.

bug:v/84959

Rootcause: To reduce RAM resource consumption, the `g_a2dp_tables`,
in the bt_tools a2dp source module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
… for reduce the ram size.

bug:v/84960

Rootcause: To reduce RAM resource consumption, the `adv_options`,a dv_stop_options,
`g_adv_tables` and `adv_callback` in the bt_tools adv module must be marked with the `const` modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements a memory optimization by marking static callback structures, command tables, and interface definitions as const to store them in Flash memory instead of RAM. This reduces RAM consumption for the Bluetooth stack's advertising, SPP, HID, HFP, A2DP, and AVRCP modules.

Changes:

  • Modified static callback structures, command tables, and SDP attribute templates to be const qualified
  • Added type casts to remove const qualifiers when passing these structures to external APIs
  • Applied changes across multiple Bluetooth profile implementations and tool modules

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tools/adv.c Constified advertising options, command tables, and callbacks with casts to external APIs
tools/a2dp_source.c Constified A2DP source command table with cast
service/stacks/zephyr/sal_spp_interface.c Constified SPP SDP attributes template and RFCOMM operations callbacks
service/stacks/zephyr/sal_hid_device_interface.c Constified HID SDP attributes and device callbacks
service/stacks/zephyr/sal_hfp_hf_interface.c Constified HFP hands-free callbacks with cast to Zephyr API
service/stacks/zephyr/sal_hfp_ag_interface.c Constified HFP audio gateway callbacks with cast to Zephyr API
service/stacks/zephyr/sal_avrcp_interface.c Constified AVRCP target callbacks
service/stacks/zephyr/sal_adapter_le_interface.c Constified LE adapter connection and authentication callbacks
service/stacks/zephyr/sal_adapter_interface.c Constified BR/EDR adapter connection and discovery callbacks
service/stacks/zephyr/sal_a2dp_interface.c Constified A2DP codec information and stream operation callbacks
service/profiles/spp/spp_service.c Constified SPP profile interface structure
service/profiles/hid/hid_device_service.c Constified HID device profile interface structure
service/ipc/socket/src/bt_socket_spp.c Constified SPP socket callbacks with cast
service/ipc/socket/src/bt_socket_scan.c Constified scanner socket callbacks with cast
service/ipc/socket/src/bt_socket_advertiser.c Constified advertiser socket callbacks with cast
framework/btwrap/async/bt_gatt_feature.c Constified GATT client feature callbacks with cast
feature/feature_async/src/bluetooth_ble_impl.c Constified advertising and GATT client callbacks with casts

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

static int dump_adv_cmd(void* handle, int argc, char* argv[]);

static struct option adv_options[] = {
static const struct option adv_options[] = {
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description contains a typo: "adv_options, a dv_stop_options" should be "adv_options, adv_stop_options" (missing 'a' in 'adv_stop_options').

Copilot uses AI. Check for mistakes.
p_adv_data, adv_len,
p_scan_rsp_data, scan_rsp_len,
&adv_callback);
(advertiser_callback_t *)&adv_callback);
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting away const qualifier is unsafe and technically undefined behavior. While the callback structure is correctly marked const for Flash storage, passing it to bt_le_start_advertising via a cast removes the const protection. If the underlying Zephyr API or any intermediate layer attempts to modify this structure, it will result in undefined behavior (attempting to write to Flash/read-only memory).

Consider one of these approaches:

  1. Verify that bt_le_start_advertising and all underlying Zephyr APIs accept const pointers and update the API signatures accordingly
  2. If the APIs cannot be changed, document this pattern extensively with comments explaining why the cast is necessary and safe
  3. If there's any uncertainty about whether the structure might be modified, keep it in RAM (non-const) to avoid potential runtime failures

The same concern applies to all similar const casts in this PR.

Suggested change
(advertiser_callback_t *)&adv_callback);
&adv_callback);

Copilot uses AI. Check for mistakes.
static void zblue_register_callback(void)
{
bt_conn_cb_register(&g_conn_cbs);
bt_conn_cb_register((struct bt_conn_cb *)&g_conn_cbs);
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting away const qualifier for callback registration is potentially unsafe. If the Zephyr bt_conn_cb_register function or any underlying implementation stores and later modifies the callback structure, this will result in undefined behavior since g_conn_cbs resides in read-only memory (Flash).

Typically, callback registration functions either:

  1. Store a pointer to the callbacks (safe if they're never modified, but const-casting hides this assumption)
  2. Copy the callbacks internally (safe, and the const-cast is just a signature mismatch)

This pattern appears throughout the Zephyr SAL interface files. Please verify that the Zephyr APIs do not modify these callback structures at runtime. If they do modify them, these structures must remain non-const in RAM. If they don't modify them, consider updating the Zephyr API signatures to accept const pointers to make this safer and more explicit.

Copilot uses AI. Check for mistakes.
}

spp_conn->rfcomm_dlc.ops = &g_rfcomm_ops;
spp_conn->rfcomm_dlc.ops = (struct bt_rfcomm_dlc_ops *)&g_rfcomm_ops;
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direct pointer assignment of const callback structure to a non-const field is unsafe. The rfcomm_dlc.ops field receives a pointer to g_rfcomm_ops which is now const (in Flash). If the Zephyr RFCOMM implementation or any code that accesses spp_conn->rfcomm_dlc.ops attempts to modify the ops structure, it will result in undefined behavior.

This is more concerning than callback registration functions because here you're directly storing a pointer that could be dereferenced and modified elsewhere in the code. Verify that:

  1. The bt_rfcomm_dlc_ops structure is never modified after assignment
  2. All code that accesses rfcomm_dlc.ops only reads from it

If there's any possibility of modification, g_rfcomm_ops must remain non-const in RAM.

Copilot uses AI. Check for mistakes.
scan->ins = ins;
scan->remote = packet->scan_pl._bt_le_start_scan.remote;
packet->scan_r.remote = PTR2INT(uint64_t) scanner_start_scan(scan, &g_scanner_socket_cb);
packet->scan_r.remote = PTR2INT(uint64_t) scanner_start_scan(scan, (scanner_callbacks_t *)&g_scanner_socket_cb);
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cast to non-const pointer is unnecessary. The function signature for scanner_start_scan already accepts const scanner_callbacks_t* as seen in scan_manager.h. Remove the cast and pass &g_scanner_socket_cb directly without casting.

Suggested change
packet->scan_r.remote = PTR2INT(uint64_t) scanner_start_scan(scan, (scanner_callbacks_t *)&g_scanner_socket_cb);
packet->scan_r.remote = PTR2INT(uint64_t) scanner_start_scan(scan, &g_scanner_socket_cb);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant