Skip to content

Conversation

@huangyulong3
Copy link

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

bug:v/84978

Rootcause: To reduce RAM resource consumption, the set_options,
'delete_options', 'g_storage_tables', in the bt_tools storage tool module must be marked with the const modifier.
This ensures it is compiled and linked into Flash memory instead of RAM.

@huangyulong3 huangyulong3 force-pushed the bugfix/fix_bluetooth_bt_tool_storage_tool_moudle_cb_change_to_const branch from 5748998 to 3cbe0ee Compare February 4, 2026 13:22
…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>
… 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.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
…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.

Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
…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/84963

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

bug:v/84969

Rootcause: To reduce RAM resource consumption, the `le_conn_options`,
`g_cmd_tables`, `g_set_cmd_tables` and `g_pair_cmd_tables`
in the bt_tools 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/84970

Rootcause: To reduce RAM resource consumption, the `g_gattc_tables`,
`gattc_cbs`,
in the bt_tools 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>
…to const for reduce the ram size.

bug:v/84971

Rootcause: To reduce RAM resource consumption, the `s_dis_attr_db`,
`s_bas_attr_db`, `s_iot_attr_db`, `g_gatts_tables`
in the bt_tools gatt server 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>
…nst for reduce the ram size.

bug:v/84972

Rootcause: To reduce RAM resource consumption, the `g_hfp_ag_tables`,
in the bt_tools hfp ag 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>
…nst for reduce the ram size.

bug:v/84973

Rootcause: To reduce RAM resource consumption, the `g_hfp_tables`,
in the bt_tools hfp hf 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/84974

Rootcause: To reduce RAM resource consumption, the `g_hidd_tables`,
in the bt_tools hid device 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/84975

Rootcause: To reduce RAM resource consumption, the `g_log_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/84976

Rootcause: To reduce RAM resource consumption, the `scan_options`,
'g_scanner_tables', 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>
… for reduce the ram size.

bug:v/84977

Rootcause: To reduce RAM resource consumption, the `spp_ping_options`,
'g_spp_tables', in the bt_tools 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>
… to const for reduce the ram size.

bug:v/84978

Rootcause: To reduce RAM resource consumption, the `set_options`,
'delete_options', 'g_storage_tables', in the bt_tools storage tool 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>
@huangyulong3 huangyulong3 force-pushed the bugfix/fix_bluetooth_bt_tool_storage_tool_moudle_cb_change_to_const branch from 3cbe0ee to 2334ef7 Compare February 4, 2026 15:57

status = bt_le_start_advertising_async(adv_info->ins, &adv_params,
p_adv_data, adv_len, p_scan_rsp_data, scan_rsp_len, &adv_callback,
p_adv_data, adv_len, p_scan_rsp_data, scan_rsp_len, (advertiser_callback_t*)&adv_callback,
Copy link
Contributor

Choose a reason for hiding this comment

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

why do const cast?

} else {
status = bt_gattc_feature_create_client_async(gattc_info->ins, &gattc_info->gattc->remote_address, gattc_create_cb,
&gattc_cbs, (void*)data);
(bt_gattc_feature_callbacks_t*)&gattc_cbs, (void*)data);
Copy link
Contributor

Choose a reason for hiding this comment

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

dito
Suggest fix async api as well

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 converting various data structures to const to move them from RAM to Flash memory, reducing RAM consumption in the Bluetooth stack. The changes mark command tables, option structures, GATT attribute databases, callback structures, and various other static data structures as const.

Changes:

  • Converted static arrays of bt_command_t, struct option, gatt_attr_db_t, and callback structures to const across tools and service stack files
  • Added explicit casts to remove const qualifier when passing these structures to APIs that expect non-const pointers
  • Applied changes consistently across 20+ files in tools/, service/stacks/, service/profiles/, service/ipc/, framework/, and feature/ directories

Reviewed changes

Copilot reviewed 32 out of 32 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
tools/storage_update/storage_tool.c Added const to set_options, delete_options, g_storage_tables; cast added when calling execute_command_in_table_offset
tools/spp.c Added const to spp_ping_options and g_spp_tables
tools/scan.c Added const to scan_options and g_scanner_tables; cast added when calling execute_command_in_table_offset
tools/log.c Added const to g_log_tables; cast added when calling execute_command_in_table
tools/hid_device.c Added const to g_hidd_tables; cast added when calling execute_command_in_table
tools/hfp_hf.c Added const to g_hfp_tables; cast added when calling execute_command_in_table
tools/hfp_ag.c Added const to g_hfp_ag_tables; cast added when calling execute_command_in_table
tools/gatt_server.c Added const to s_dis_attr_db, s_bas_attr_db, s_iot_attr_db, g_gatts_tables; casts added when initializing service databases
tools/gatt_client.c Added const to g_gattc_tables and gattc_cbs; casts added when calling APIs
tools/bt_tools.c Added const to le_conn_options, g_cmd_tables, g_set_cmd_tables, g_get_cmd_tables, g_pair_cmd_tables; casts added when calling execute_command_in_table
tools/async/scan.c Added const to scan_options and g_scanner_async_tables; cast added when calling execute_command_in_table_offset
tools/async/log.c Added const to g_log_async_tables; cast added when calling execute_command_in_table
tools/async/gatt_client.c Added const to g_gattc_async_tables and gattc_cbs; casts added when calling APIs
tools/async/gap.c Added const to le_conn_options, g_set_cmd_tables, g_get_cmd_tables, g_pair_cmd_tables; casts added when calling execute_command_in_table
tools/async/adv.c Added const to adv_options, adv_stop_options, g_adv_async_tables, adv_callback; casts added when calling APIs
tools/adv.c Added const to adv_options, adv_stop_options, g_adv_tables, adv_callback; casts added when calling APIs
tools/a2dp_source.c Added const to g_a2dp_tables; cast added when calling execute_command_in_table
service/stacks/zephyr/sal_spp_interface.c Added const to spp_attrs_template and g_rfcomm_ops; cast added when assigning to rfcomm_dlc.ops
service/stacks/zephyr/sal_hid_device_interface.c Added const to hid_attrs_template and hid_callback; cast added when calling bt_hid_device_register
service/stacks/zephyr/sal_hfp_hf_interface.c Added const to hf_callbacks; cast added when calling bt_hfp_hf_register
service/stacks/zephyr/sal_hfp_ag_interface.c Added const to g_hfp_ag_cb; cast added when calling bt_hfp_ag_register
service/stacks/zephyr/sal_avrcp_interface.c Added const to avrcp_tg_cbks
service/stacks/zephyr/sal_adapter_le_interface.c Added const to g_conn_cbs, g_conn_auth_info_cbs, g_setting_cbs; casts added when registering callbacks; added forward declarations
service/stacks/zephyr/sal_adapter_interface.c Added const to g_conn_cbs, g_setting_cbs, g_conn_auth_info_cbs, g_br_discovery_cb; casts added when registering callbacks
service/stacks/zephyr/sal_a2dp_interface.c Added const to sbc_src_ie, stream_ops, a2dp_cbks; casts added when calling stream registration functions
service/profiles/spp/spp_service.c Added const to sppInterface
service/profiles/hid/hid_device_service.c Added const to deviceInterface
service/ipc/socket/src/bt_socket_spp.c Added const to g_spp_socket_cb; cast added when calling register_app
service/ipc/socket/src/bt_socket_scan.c Added const to g_scanner_socket_cb; cast added when calling scanner_start_scan
service/ipc/socket/src/bt_socket_advertiser.c Added const to g_advertiser_socket_cb; cast added when calling bt_le_start_advertising
framework/btwrap/async/bt_gatt_feature.c Added const to s_feature_gattc_cbs; cast added when calling bt_gattc_create_connect_async
feature/feature_async/src/bluetooth_ble_impl.c Added const to adv_callback and gattc_cbs; casts added when calling APIs

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

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 from g_conn_cbs violates const-correctness. While callback registration functions typically don't modify the callback structures, casting away const when the API expects a non-const pointer is undefined behavior. The proper solution would be to update the bt_conn_cb_register API signature to accept a const struct bt_conn_cb* parameter.

Copilot uses AI. Check for mistakes.
Comment on lines +222 to 226
static const gatt_attr_db_t s_bas_attr_db[] = {
GATT_H_PRIMARY_SERVICE(BT_UUID_DECLARE_16(0x180F), BAS_SERVICE_ID),
GATT_H_CHARACTERISTIC_AUTO_RSP(BT_UUID_DECLARE_16(0x2A19), GATT_PROP_READ | GATT_PROP_NOTIFY, GATT_PERM_READ, &battery_level, sizeof(battery_level), BAS_BATTERY_LEVEL_CHR_ID),
GATT_H_CCCD(GATT_PERM_READ | GATT_PERM_WRITE, tx_char_ccc_changed, BAS_BATTERY_LEVEL_CHR_CCC_ID),
};
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.

This is a critical const-correctness violation. The s_bas_attr_db array is marked const, but one of its entries contains a pointer to mutable data (battery_level at line 220, which is non-const). The GATT_H_CHARACTERISTIC_AUTO_RSP macro stores the address of battery_level in the attr_value field. When the gatt_attr_db_t array is cast away from const and used, if the GATT stack attempts to read the current battery level value through this pointer, it should work. However, marking the array as const when it contains pointers to mutable data creates a logical inconsistency and violates const-correctness principles. The structure should either remain non-const, or battery_level should be made const if it's never modified.

Copilot uses AI. Check for mistakes.

if (argc > 0)
ret = execute_command_in_table_offset(handle, g_storage_tables, ARRAY_SIZE(g_storage_tables), argc, argv, 0);
ret = execute_command_in_table_offset(handle, (bt_command_t*)g_storage_tables, ARRAY_SIZE(g_storage_tables), argc, argv, 0);
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 potentially problematic. While the execute_command_in_table_offset function only reads from the table and doesn't modify it, casting const to non-const pointer violates const-correctness and is undefined behavior if the data is subsequently modified. The proper solution would be to update the API signature of execute_command_in_table_offset to accept a const bt_command_t* parameter. This same pattern appears throughout the codebase in multiple files.

Copilot uses AI. Check for mistakes.
}

if (bt_gattc_create_connect(handle, &g_gattc_devies[conn_id].handle, &gattc_cbs) != BT_STATUS_SUCCESS)
if (bt_gattc_create_connect(handle, &g_gattc_devies[conn_id].handle, (gattc_callbacks_t*)&gattc_cbs) != BT_STATUS_SUCCESS)
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 from gattc_cbs violates const-correctness. While bt_gattc_create_connect likely only stores and reads the callbacks without modifying them, the API signature expects a non-const pointer. The proper solution would be to update the bt_gattc_create_connect API signature to accept a const gattc_callbacks_t* parameter, or verify that the underlying implementation truly treats this as immutable.

Copilot uses AI. Check for mistakes.

static gatt_srv_db_t s_dis_service_db = {
.attr_db = s_dis_attr_db,
.attr_db = (gatt_attr_db_t*)s_dis_attr_db,
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 from s_dis_attr_db is acceptable in this specific case. The s_dis_attr_db array only contains pointers to const string literals (model_number_str, manufacturer_name_str, pnp_id), which are all marked const. However, the API signature expects a non-const pointer. While this works in practice since the data is never modified, it would be better to update the API signature to accept const pointers for better const-correctness.

Copilot uses AI. Check for mistakes.

static gatt_srv_db_t s_bas_service_db = {
.attr_db = s_bas_attr_db,
.attr_db = (gatt_attr_db_t*)s_bas_attr_db,
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 from s_bas_attr_db violates const-correctness. The same issue as with s_dis_attr_db applies here - the structure being cast contains pointers to mutable data, and casting away const could lead to undefined behavior.

Copilot uses AI. Check for mistakes.

static gatt_srv_db_t s_iot_service_db = {
.attr_db = s_iot_attr_db,
.attr_db = (gatt_attr_db_t*)s_iot_attr_db,
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 from s_iot_attr_db appears acceptable in this specific case, as the attribute values it references (read_only_char_value, NULL, and callback functions) are either read-only or not directly modified through these pointers. However, read_only_char_value is declared as non-const (line 116), which creates a const-correctness inconsistency. Consider declaring read_only_char_value as const to maintain consistency.

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 qualifier from g_rfcomm_ops violates const-correctness. The rfcomm_dlc.ops field expects a non-const pointer, and while the ops callbacks are likely never modified, this cast violates type safety and could lead to undefined behavior if any code path attempts to modify the supposedly immutable callback structure.

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.

2 participants