Skip to content

Conversation

@huangyulong3
Copy link

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

bug:v/84968

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

…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>
… const for reduce the ram size.

bug:v/84961

Rootcause: To reduce RAM resource consumption, the `adv_options`, `adv_stop_options`,
`g_adv_async_tables` and `adv_callback` in the bt_tools async 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>
… const for reduce the ram size.

bug:v/84961

Rootcause: To reduce RAM resource consumption, the `le_conn_options`, `g_set_cmd_tables`,
`g_get_cmd_tables` and `g_pair_cmd_tables` in the bt_tools async gap 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
…odule to const for reduce the ram size.

bug:v/84966

Rootcause: To reduce RAM resource consumption, the `g_gattc_async_tables`, `gattc_cbs`,
in the bt_tools async gatt client 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/84967

Rootcause: To reduce RAM resource consumption, the `g_log_async_tables`,
in the bt_tools log 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>
…t for reduce the ram size.

bug:v/84968

Rootcause: To reduce RAM resource consumption, the `scan_options`,
in the bt_tools 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>
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 to reduce RAM usage by marking static data structures as const, causing them to be stored in Flash memory instead of RAM. The changes primarily target Bluetooth tool async modules and service stack interface layers.

Changes:

  • Added const qualifiers to static command tables and callback structures across async tools and service layers
  • Added type casts to maintain compatibility with function signatures that don't accept const pointers
  • Added forward declarations for connection management functions in sal_adapter_le_interface.c

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
tools/async/scan.c Made scan_options and scanner command tables const with cast when passing to execute function
tools/async/log.c Made log command tables const with cast when passing to execute function
tools/async/gatt_client.c Made GATT client command tables and callback structures const with casts
tools/async/gap.c Made GAP command tables and connection options const with casts
tools/async/adv.c Made advertising options, command tables, and callbacks const with casts
tools/adv.c Made advertising structures const (non-async version) with casts
tools/a2dp_source.c Made A2DP command tables const with cast
service/stacks/zephyr/sal_spp_interface.c Made SPP SDP attributes and RFCOMM ops const with cast
service/stacks/zephyr/sal_hid_device_interface.c Made HID SDP attributes and callbacks const with cast
service/stacks/zephyr/sal_hfp_hf_interface.c Made HFP HF callbacks const with cast
service/stacks/zephyr/sal_hfp_ag_interface.c Made HFP AG callbacks const with cast
service/stacks/zephyr/sal_avrcp_interface.c Made AVRCP target callbacks const
service/stacks/zephyr/sal_adapter_le_interface.c Made LE adapter callbacks const, added forward declarations, with casts in registration
service/stacks/zephyr/sal_adapter_interface.c Made BR/EDR adapter callbacks const with casts in registration
service/stacks/zephyr/sal_a2dp_interface.c Made A2DP callbacks and codec info const with casts
service/profiles/spp/spp_service.c Made SPP interface const
service/profiles/hid/hid_device_service.c Made HID device interface const
service/ipc/socket/src/bt_socket_spp.c Made socket SPP callbacks const with cast
service/ipc/socket/src/bt_socket_scan.c Made socket scanner callbacks const with cast
service/ipc/socket/src/bt_socket_advertiser.c Made socket advertiser callbacks const with cast
framework/btwrap/async/bt_gatt_feature.c Made GATT feature callbacks const with cast
feature/feature_async/src/bluetooth_ble_impl.c Made BLE implementation callbacks const with casts

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

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 is unsafe and violates const-correctness. The adv_callback structure is marked const (line 101) but then cast to non-const here. Instead of using this cast, the bt_le_start_advertising function signature should be updated to accept const advertiser_callback_t* as the callback parameter.

Copilot uses AI. Check for mistakes.
g_sal_hf_conn_list = bt_list_new(free_connection);

err = Z_API(bt_hfp_hf_register)(&hf_callbacks);
err = Z_API(bt_hfp_hf_register)((struct bt_hfp_hf_cb *)&hf_callbacks);
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 is unsafe and could mask bugs if the Zephyr registration functions try to modify the callback structures. The hf_callbacks structure is marked const (line 891), but these casts remove that protection. Verify that bt_hfp_hf_register doesn't modify the callback structure. If it truly doesn't modify it, consider submitting a patch to Zephyr to make their API const-correct.

Copilot uses AI. Check for mistakes.

a2dp_info->stream = (struct bt_a2dp_stream*)calloc(1, sizeof(struct bt_a2dp_stream));
bt_a2dp_stream_cb_register(a2dp_info->stream, &stream_ops);
bt_a2dp_stream_cb_register(a2dp_info->stream, (struct bt_a2dp_stream_ops *)&stream_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.

Casting away const is unsafe. The stream_ops structure is marked const (line 1107), but this cast removes that protection. Verify that bt_a2dp_stream_cb_register doesn't modify the callback structure. If it truly doesn't modify it, consider submitting a patch to Zephyr to make their API const-correct.

Copilot uses AI. Check for mistakes.
int* conn_id = (int*)malloc(sizeof(int));
*conn_id = index;
if (bt_gattc_create_connect_async(handle, &g_gattc_devies[index].handle, &gattc_cbs, create_connect_cb,
if (bt_gattc_create_connect_async(handle, &g_gattc_devies[index].handle, (gattc_callbacks_t *)&gattc_cbs, create_connect_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.

Casting away const is unsafe and violates const-correctness. The gattc_cbs structure is marked const (line 732) but then cast to non-const here. Instead of using this cast, the bt_gattc_create_connect_async function signature should be updated to accept const gattc_callbacks_t* as the callbacks parameter. This would properly support const callback structures throughout the codebase.

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 is unsafe and violates const-correctness. The adv_callback structure is marked const (line 108) but then cast to non-const here. Instead of using this cast, the bt_le_start_advertising_async function signature should be updated to accept const advertiser_callback_t* as the callback parameter. This would properly support const callback structures and align with similar functions in the service layer that already accept const callback pointers (see service/src/advertising.h).

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.

Casting away const is unsafe and could mask bugs if the underlying Zephyr function tries to modify the structure. The g_rfcomm_ops structure is marked const (line 441), but this cast removes that protection. Verify that the Zephyr bt_rfcomm_dlc.ops field and related functions don't modify the ops structure. If they truly don't modify it, consider submitting a patch to Zephyr to make their API const-correct.

Suggested change
spp_conn->rfcomm_dlc.ops = (struct bt_rfcomm_dlc_ops *)&g_rfcomm_ops;
/* Use a mutable copy of g_rfcomm_ops to avoid casting away const. */
static struct bt_rfcomm_dlc_ops g_rfcomm_ops_mutable;
static bool g_rfcomm_ops_mutable_inited;
if (!g_rfcomm_ops_mutable_inited) {
g_rfcomm_ops_mutable = g_rfcomm_ops;
g_rfcomm_ops_mutable_inited = true;
}
spp_conn->rfcomm_dlc.ops = &g_rfcomm_ops_mutable;

Copilot uses AI. Check for mistakes.
Comment on lines +764 to +770
bt_conn_cb_register((struct bt_conn_cb *)&g_conn_cbs);
#ifdef CONFIG_BT_SMP
bt_conn_le_auth_cb_register(&g_conn_auth_cbs);
bt_conn_auth_info_cb_register(&g_conn_auth_info_cbs);
bt_conn_le_auth_cb_register((struct bt_conn_auth_cb *)&g_conn_auth_cbs);
bt_conn_auth_info_cb_register((struct bt_conn_auth_info_cb *)&g_conn_auth_info_cbs);
#endif
#ifdef CONFIG_SETTINGS_ZBLUE
bt_setting_cb_register(&g_setting_cbs);
bt_setting_cb_register((struct bt_settings_zblue_cb *)&g_setting_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.

Multiple casts removing const qualifiers from callback structures (g_conn_cbs, g_conn_auth_cbs, g_conn_auth_info_cbs, g_setting_cbs) that were marked const (lines 113, 125, 133). These casts are unsafe and violate const-correctness. Verify that the Zephyr registration functions (bt_conn_cb_register, bt_conn_le_auth_cb_register, etc.) don't modify the callback structures. If they truly don't modify them, consider submitting a patch to Zephyr to make their registration APIs const-correct.

Copilot uses AI. Check for mistakes.
bt_br_discovery_cb_unregister((struct bt_br_discovery_cb *)&g_br_discovery_cb);
bt_conn_auth_cb_register(NULL);
bt_conn_auth_info_cb_unregister(&g_conn_auth_info_cbs);
bt_conn_auth_info_cb_unregister((struct bt_conn_auth_info_cb *)&g_conn_auth_info_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.

Multiple casts removing const qualifiers from callback structures (g_br_discovery_cb, g_conn_cbs, g_conn_auth_cbs, g_conn_auth_info_cbs, g_setting_cbs) that were marked const. These casts are unsafe and violate const-correctness. Verify that the Zephyr registration functions don't modify the callback structures. If they truly don't modify them, consider submitting a patch to Zephyr to make their registration APIs const-correct.

Suggested change
bt_conn_auth_info_cb_unregister((struct bt_conn_auth_info_cb *)&g_conn_auth_info_cbs);
bt_conn_auth_info_cb_unregister(NULL);

Copilot uses AI. Check for mistakes.

if (argc > 0)
ret = execute_command_in_table_offset(handle, g_scanner_async_tables,
ret = execute_command_in_table_offset(handle, (bt_command_t *)g_scanner_async_tables,
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 is unsafe and violates const-correctness principles. Instead of casting const pointers to non-const when calling execute_command_in_table_offset, the function signature should be updated to accept const bt_command_t* as the table parameter. This would be safer and more maintainable.

The same issue applies to all similar casts in this PR (in log.c, gatt_client.c, gap.c, adv.c, a2dp_source.c, etc.). Consider updating the function signature in bt_tools.h to: int execute_command_in_table_offset(void* handle, const bt_command_t* table, ...);

Suggested change
ret = execute_command_in_table_offset(handle, (bt_command_t *)g_scanner_async_tables,
ret = execute_command_in_table_offset(handle, g_scanner_async_tables,

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