Skip to content

Conversation

@huangyulong3
Copy link

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

…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>
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 static data structures from RAM to Flash memory by adding the const qualifier. The primary goal is to reduce RAM resource consumption across the Bluetooth tool and service modules.

Changes:

  • Added const qualifier to static command tables (bt_command_t arrays) and option structures (struct option arrays) across multiple tool files
  • Added const qualifier to static callback structures (gattc_callbacks_t, advertiser_callback_t, etc.) in service layer files
  • Added explicit casts (type *) when passing const-qualified structures to APIs that expect non-const pointers
  • Added forward declarations in sal_adapter_le_interface.c

Reviewed changes

Copilot reviewed 28 out of 28 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tools/hid_device.c Made g_hidd_tables const and added cast when passing to execute_command_in_table
tools/hfp_hf.c Made g_hfp_tables const and added cast when passing to execute_command_in_table
tools/hfp_ag.c Made g_hfp_ag_tables const and added cast when passing to execute_command_in_table
tools/gatt_server.c Made attribute database arrays and command table const, added casts in service database structures
tools/gatt_client.c Made g_gattc_tables and gattc_cbs const, added casts when registering callbacks
tools/bt_tools.c Made multiple command tables and option structures const, added casts in execute calls
tools/async/scan.c Made scan option and command tables const, added casts
tools/async/log.c Made log command table const, added cast
tools/async/gatt_client.c Made command table and callbacks const, added casts
tools/async/gap.c Made multiple command tables and option structures const, added casts
tools/async/adv.c Made advertising option and command tables and callbacks const, added casts
tools/adv.c Made advertising option and command tables and callbacks const, added casts
tools/a2dp_source.c Made command table const, added cast
service/stacks/zephyr/sal_spp_interface.c Made SDP attributes and RFCOMM ops const, added casts
service/stacks/zephyr/sal_hid_device_interface.c Made SDP attributes and HID callbacks const, added casts
service/stacks/zephyr/sal_hfp_hf_interface.c Made HFP HF callbacks const, added cast
service/stacks/zephyr/sal_hfp_ag_interface.c Made HFP AG callbacks const, added cast
service/stacks/zephyr/sal_avrcp_interface.c Made AVRCP target callbacks const
service/stacks/zephyr/sal_adapter_le_interface.c Made LE connection callbacks const, added casts and forward declarations
service/stacks/zephyr/sal_adapter_interface.c Made BR/EDR connection callbacks const, added casts
service/stacks/zephyr/sal_a2dp_interface.c Made A2DP codec info and callbacks const, added casts
service/profiles/spp/spp_service.c Made SPP interface structure const
service/profiles/hid/hid_device_service.c Made HID device interface structure const
service/ipc/socket/src/bt_socket_spp.c Made SPP socket callbacks const, added cast
service/ipc/socket/src/bt_socket_scan.c Made scanner socket callbacks const, added cast
service/ipc/socket/src/bt_socket_advertiser.c Made advertiser socket callbacks const, added cast
framework/btwrap/async/bt_gatt_feature.c Made GATT feature callbacks const, added cast
feature/feature_async/src/bluetooth_ble_impl.c Made advertiser and GATT client callbacks const, added casts

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

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

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

The memory optimization is incomplete. Several tool files with the same bt_command_t table pattern were not updated to use const:

  • tools/a2dp_sink.c: g_a2dp_sink_tables
  • tools/avrcp_control.c: g_avrcp_control_tables
  • tools/l2cap.c: g_l2cap_commands
  • tools/lea_*.c: Multiple LEA-related command tables
  • tools/log.c: g_log_tables
  • tools/panu.c: g_pan_tables
  • tools/scan.c: g_scanner_tables
  • tools/spp.c: g_spp_tables
  • tools/storage_update/storage_tool.c: g_storage_tables
  • tools/async/gap.c: g_async_cmd_tables

These should be made const with corresponding casts in execute_command_in_table calls for consistency and to maximize RAM savings.

Copilot uses AI. Check for mistakes.
};

static struct option le_conn_options[] = {
static const struct option le_conn_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 memory optimization is incomplete for option structures. Several files with struct option arrays were not updated to use const:

  • tools/bt_tools.c: main_options
  • tools/scan.c: scan_options
  • tools/spp.c: spp_ping_options
  • tools/storage_update/storage_tool.c: set_options, delete_options

These should be made const for consistency and to maximize RAM savings, similar to the changes made to le_conn_options, adv_options, adv_stop_options, and scan_options in other files.

Copilot uses AI. Check for mistakes.

if (argc > 0)
ret = execute_command_in_table(handle, g_hidd_tables, ARRAY_SIZE(g_hidd_tables), argc, argv);
ret = execute_command_in_table(handle, (bt_command_t *)g_hidd_tables, ARRAY_SIZE(g_hidd_tables), argc, argv);
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 and passing the pointer to a function expecting non-const is technically undefined behavior in C if the function modifies the data. While the current implementation may not modify these structures, this creates a maintenance risk: if the API implementation changes in the future to modify these structures, it would result in undefined behavior.

A safer approach would be to:

  1. Update the API signatures to accept const pointers (e.g., const bt_command_t*, const gattc_callbacks_t*, etc.) if these structures are truly not modified
  2. Or, keep the structures non-const if they need to be modifiable

The current approach trades type safety for memory optimization, which may be acceptable for embedded systems with tight memory constraints, but should be documented clearly.

Suggested change
ret = execute_command_in_table(handle, (bt_command_t *)g_hidd_tables, ARRAY_SIZE(g_hidd_tables), argc, argv);
ret = execute_command_in_table(handle, g_hidd_tables, ARRAY_SIZE(g_hidd_tables), argc, argv);

Copilot uses AI. Check for mistakes.
Comment on lines +110 to +112
static le_conn_info_t* le_conn_add(const bt_address_t* addr);
static le_conn_info_t* le_conn_find(const bt_address_t* addr);

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 forward declarations for le_conn_add and le_conn_find added on lines 110-111 appear unnecessary. These functions are not referenced by the g_conn_cbs structure below, and forward declarations are only needed when there's a dependency. Unless these functions reference the const structures above them in a way that creates a circular dependency, these forward declarations can be removed.

Suggested change
static le_conn_info_t* le_conn_add(const bt_address_t* addr);
static le_conn_info_t* le_conn_find(const bt_address_t* addr);

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

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

The PR title mentions only "hid device module" but the changes affect many more modules including hfp_hf, hfp_ag, gatt_server, gatt_client, bt_tools, adv, scan, log, spp, a2dp, avrcp, and various service stack and IPC files. The title should be updated to reflect the broader scope of changes, for example: "Memory optimization: Make static data structures const across bluetooth tools and services"

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

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

The PR title contains a typo: "moudle" should be "module".

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