Skip to content

Conversation

@huangyulong3
Copy link

Memory Optimization: Change the bluetooth bt_tool spp module to const 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.

…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>
… 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>
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 data structures as const to move them from RAM to Flash memory. The changes target Bluetooth tool command tables, option tables, callback structures, and GATT attribute databases across multiple files.

Changes:

  • Static command tables (bt_command_t arrays) and option tables (struct option arrays) made const with explicit casts when passed to APIs expecting non-const pointers
  • Callback structures for various Bluetooth services made const with explicit casts at registration points
  • GATT attribute databases made const with casts when assigned to service database structures

Reviewed changes

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

Show a summary per file
File Description
tools/spp.c Made spp_ping_options and g_spp_tables const
tools/scan.c Made scan_options and g_scanner_tables const, added cast when calling execute_command_in_table_offset
tools/log.c Made g_log_tables const, added cast when calling execute_command_in_table
tools/hid_device.c Made g_hidd_tables const, added cast when calling execute_command_in_table
tools/hfp_hf.c Made g_hfp_tables const, added cast when calling execute_command_in_table
tools/hfp_ag.c Made g_hfp_ag_tables const, added cast when calling execute_command_in_table
tools/gatt_server.c Made g_gatts_tables, s_dis_attr_db, s_bas_attr_db, and s_iot_attr_db const, added casts when assigning to service structures; missing cast when passing g_gatts_tables to API
tools/gatt_client.c Made g_gattc_tables and gattc_cbs const, added casts at usage points
tools/bt_tools.c Made le_conn_options, g_cmd_tables, g_set_cmd_tables, g_get_cmd_tables, and g_pair_cmd_tables const, added casts when calling execute_command_in_table
tools/async/scan.c Made scan_options and g_scanner_async_tables const, added cast when calling execute_command_in_table_offset
tools/async/log.c Made g_log_async_tables const, added cast when calling execute_command_in_table
tools/async/gatt_client.c Made g_gattc_async_tables and gattc_cbs const, added casts at usage points
tools/async/gap.c Made le_conn_options, g_set_cmd_tables, g_get_cmd_tables, and g_pair_cmd_tables const, added casts when calling execute_command_in_table
tools/async/adv.c Made adv_options, adv_stop_options, g_adv_async_tables, and adv_callback const, added casts at usage points
tools/adv.c Made adv_options, adv_stop_options, g_adv_tables, and adv_callback const, added casts at usage points
tools/a2dp_source.c Made g_a2dp_tables const, added cast when calling execute_command_in_table
service/stacks/zephyr/sal_spp_interface.c Made spp_attrs_template and g_rfcomm_ops const, added cast when assigning ops pointer
service/stacks/zephyr/sal_hid_device_interface.c Made hid_attrs_template and hid_callback const, added cast when registering callback
service/stacks/zephyr/sal_hfp_hf_interface.c Made hf_callbacks const, added cast when registering callback
service/stacks/zephyr/sal_hfp_ag_interface.c Made g_hfp_ag_cb const, added cast when registering callback
service/stacks/zephyr/sal_avrcp_interface.c Made avrcp_tg_cbks const
service/stacks/zephyr/sal_adapter_le_interface.c Made g_conn_cbs, g_conn_auth_info_cbs, and g_setting_cbs const, added forward declarations, added casts at registration/unregistration points
service/stacks/zephyr/sal_adapter_interface.c Made g_conn_cbs, g_setting_cbs, g_conn_auth_info_cbs, and g_br_discovery_cb const, added casts at registration/unregistration points
service/stacks/zephyr/sal_a2dp_interface.c Made sbc_src_ie, stream_ops, and a2dp_cbks const, added casts when registering callbacks
service/profiles/spp/spp_service.c Made sppInterface const
service/profiles/hid/hid_device_service.c Made deviceInterface const
service/ipc/socket/src/bt_socket_spp.c Made g_spp_socket_cb const, added cast when registering callback
service/ipc/socket/src/bt_socket_scan.c Made g_scanner_socket_cb const, added cast when starting scan
service/ipc/socket/src/bt_socket_advertiser.c Made g_advertiser_socket_cb const, added cast when starting advertising
framework/btwrap/async/bt_gatt_feature.c Made s_feature_gattc_cbs const, added cast when creating client
feature/feature_async/src/bluetooth_ble_impl.c Made adv_callback and gattc_cbs const, added casts at usage points

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

}

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 when assigning to rfcomm_dlc.ops is potentially unsafe. While making g_rfcomm_ops const moves it from RAM to Flash for memory optimization, the cast indicates that the rfcomm_dlc.ops field expects a non-const pointer. If the Zephyr Bluetooth stack or any code path attempts to modify this structure through the ops pointer, it will cause undefined behavior since the data is now in read-only memory. The proper solution would be to update the bt_rfcomm_dlc structure definition to accept a const ops pointer, which would provide compile-time safety.

Copilot uses AI. Check for mistakes.
static int get_state_cmd(void* handle, int argc, char* argv[]);

static bt_command_t g_a2dp_tables[] = {
static const bt_command_t g_a2dp_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.

Incomplete optimization: Several similar bt_command_t arrays in other tool files were not made const in this PR, such as g_a2dp_sink_tables in tools/a2dp_sink.c, g_avrcp_control_tables in tools/avrcp_control.c, g_l2cap_commands in tools/l2cap.c, and several others in the LEA-related files. For consistency and to maximize the RAM savings, all static command tables that are only read should be marked const. This would provide the same memory optimization benefit across all tool modules.

Copilot uses AI. Check for mistakes.
static transmit_context_t trans_ctx = { 0 };

static struct option spp_ping_options[] = {
static const struct option spp_ping_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 is incomplete and potentially misleading. It only mentions spp_ping_options and g_spp_tables in tools/spp.c, but this PR actually modifies many more structures across multiple files including scan_options, various command tables (g_scanner_tables, g_log_tables, g_hidd_tables, g_hfp_tables, g_hfp_ag_tables, g_gatts_tables, g_gattc_tables, g_adv_tables, g_a2dp_tables, etc.), callback structures (gattc_cbs, adv_callback, g_rfcomm_ops, hf_callbacks, g_hfp_ag_cb, avrcp_tg_cbks, g_conn_cbs, g_conn_auth_info_cbs, g_br_discovery_cb, sbc_src_ie, stream_ops, a2dp_cbks, sppInterface, deviceInterface, etc.), and GATT attribute databases (s_dis_attr_db, s_bas_attr_db, s_iot_attr_db, spp_attrs_template, hid_attrs_template). The description should accurately reflect the scope of changes to help reviewers understand the full impact.

Copilot uses AI. Check for mistakes.
};

static bt_command_t g_gatts_tables[] = {
static const bt_command_t g_gatts_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.

Missing cast to (bt_command_t *) when passing const g_gatts_tables to execute_command_in_table. Since g_gatts_tables was changed to const on line 257, this call site must also be updated with the cast to match the pattern used in other files, otherwise it will cause a compiler warning about discarding const qualifier.

Copilot uses AI. Check for mistakes.

if (argc > 0)
ret = execute_command_in_table_offset(handle, g_scanner_tables,
ret = execute_command_in_table_offset(handle, (bt_command_t *)g_scanner_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 qualifier is potentially unsafe. While this change moves the data from RAM to Flash for memory optimization, the cast indicates that execute_command_in_table_offset expects a non-const pointer. If the API implementation or any code path attempts to modify this data, it will cause undefined behavior since the data is now in read-only memory. The proper solution is to update the API signature of execute_command_in_table and execute_command_in_table_offset to accept const bt_command_t* parameters, which would eliminate the need for casting and provide compile-time safety.

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