Bugfix/fix bluetooth bt tool a2dp source moudle cb change to const#434
Conversation
…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>
There was a problem hiding this comment.
Pull request overview
This PR implements a memory optimization to reduce RAM usage by marking callback and command table structures as const, which causes them to be stored in Flash memory instead of RAM. However, the implementation uses const-casting to work around APIs that don't accept const pointers, which is technically unsafe and defeats the purpose of const-correctness.
Changes:
- Marked 20+ static callback and command table structures as
constacross 16 files - Added type casts to remove
constqualifier when passing these structures to APIs that don't accept const pointers - Added unused forward declarations in sal_adapter_le_interface.c
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 16 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/a2dp_source.c | Marked g_a2dp_tables as const with cast when calling execute_command_in_table |
| service/stacks/zephyr/sal_spp_interface.c | Marked spp_attrs_template and g_rfcomm_ops as const with casts for registration |
| service/stacks/zephyr/sal_hid_device_interface.c | Marked hid_attrs_template and hid_callback as const with cast for registration |
| service/stacks/zephyr/sal_hfp_hf_interface.c | Marked hf_callbacks as const with cast for registration |
| service/stacks/zephyr/sal_hfp_ag_interface.c | Marked g_hfp_ag_cb as const with cast for registration |
| service/stacks/zephyr/sal_avrcp_interface.c | Marked avrcp_tg_cbks as const (no cast used, potentially inconsistent) |
| service/stacks/zephyr/sal_adapter_le_interface.c | Marked multiple callback structures as const with casts; added unused forward declarations |
| service/stacks/zephyr/sal_adapter_interface.c | Marked multiple callback structures as const with casts for registration |
| service/stacks/zephyr/sal_a2dp_interface.c | Marked sbc_src_ie, stream_ops, and a2dp_cbks as const with casts |
| service/profiles/spp/spp_service.c | Marked sppInterface as const (returned as void*, acceptable pattern) |
| service/profiles/hid/hid_device_service.c | Marked deviceInterface as const (returned as void*, acceptable pattern) |
| service/ipc/socket/src/bt_socket_spp.c | Marked g_spp_socket_cb as const with cast for registration |
| service/ipc/socket/src/bt_socket_scan.c | Marked g_scanner_socket_cb as const with cast for registration |
| service/ipc/socket/src/bt_socket_advertiser.c | Marked g_advertiser_socket_cb as const with cast for registration |
| framework/btwrap/async/bt_gatt_feature.c | Marked s_feature_gattc_cbs as const with cast for registration |
| feature/feature_async/src/bluetooth_ble_impl.c | Marked adv_callback and gattc_cbs as const with casts for registration |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| sal_bt_hid_device_mgr_t* hid_mgr = &g_hid_device_mgr; | ||
|
|
||
| err = Z_API(bt_hid_device_register)(&hid_callback); | ||
| err = Z_API(bt_hid_device_register)((struct bt_hid_device_cb *)&hid_callback); |
There was a problem hiding this comment.
Casting away const is unsafe and could lead to undefined behavior if the underlying Zephyr API modifies the callback structure. The proper solution is to update the Zephyr API signatures to accept const pointers, or if these structures can be modified, they should not be marked as const.
|
|
||
| status = bt_gattc_create_connect_async( | ||
| ins, &client->conn, &s_feature_gattc_cbs, create_client_cb, client); | ||
| ins, &client->conn, (gattc_callbacks_t *)&s_feature_gattc_cbs, create_client_cb, client); |
There was a problem hiding this comment.
Casting away const is unsafe and could lead to undefined behavior if the underlying API modifies the callback structure. The proper solution is to update the API signatures to accept const pointers, or if these structures can be modified, they should not be marked as const.
| 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[] = { |
There was a problem hiding this comment.
The PR title mentions "fix bluetooth bt_tool a2dp source moudle cb change to const" but the changes affect many more files beyond just the a2dp source module in bt_tools. The PR modifies callback structures in 16 files across multiple components (SPP, HID, HFP HF/AG, AVRCP, adapters, A2DP, GATT, BLE). The title and description should accurately reflect the scope of changes to avoid confusion.
| 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); |
There was a problem hiding this comment.
Casting away const is unsafe and could lead to undefined behavior if the underlying Zephyr API modifies the callback structure. The proper solution is to update the Zephyr API signatures to accept const pointers, or if these structures can be modified, they should not be marked as const.
| @@ -1563,7 +1563,7 @@ static void mtu_updated_callback(gattc_handle_t conn_handle, gatt_status_t statu | |||
| bt_list_remove(gattc_info->userdata_list, data); | |||
| } | |||
|
|
|||
| static bt_gattc_feature_callbacks_t gattc_cbs = { | |||
| static const bt_gattc_feature_callbacks_t gattc_cbs = { | |||
| sizeof(gattc_cbs), | |||
| .on_connected = connect_callback, | |||
| .on_disconnected = disconnect_callback, | |||
| @@ -1761,7 +1761,7 @@ void system_bluetooth_ble_GattClient_interface_gattc_connect(FeatureInterfaceHan | |||
| gattc_info->gattc->addr_type, gattc_connect_cb, (void*)data); | |||
| } 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); | |||
There was a problem hiding this comment.
Casting away const is unsafe and could lead to undefined behavior if the underlying API modifies the callback structure. This pattern is repeated at line 1764. The proper solution is to update the API signatures to accept const pointers, or if these structures can be modified, they should not be marked as const.
| bt_br_discovery_cb_register((struct bt_br_discovery_cb *)&g_br_discovery_cb); | ||
| bt_conn_cb_register((struct bt_conn_cb *)&g_conn_cbs); | ||
| bt_conn_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); | ||
| #ifdef CONFIG_SETTINGS_ZBLUE | ||
| bt_setting_cb_register(&g_setting_cbs); | ||
| bt_setting_cb_register((struct bt_settings_zblue_cb *)&g_setting_cbs); | ||
| #endif | ||
| } | ||
|
|
||
| static void zblue_unregister_callback(void) | ||
| { | ||
| bt_br_discovery_cb_unregister(&g_br_discovery_cb); | ||
| 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); |
There was a problem hiding this comment.
Casting away const is unsafe and could lead to undefined behavior if the underlying Zephyr API modifies the callback structure. This pattern is repeated throughout the file at lines 663-668, 674, 676. The proper solution is to update the Zephyr API signatures to accept const pointers, or if these structures can be modified, they should not be marked as const.
| g_sal_ag_conn_list = bt_list_new(free_connection); | ||
|
|
||
| SAL_CHECK_RET(Z_API(bt_hfp_ag_register)(&g_hfp_ag_cb), 0); | ||
| SAL_CHECK_RET(Z_API(bt_hfp_ag_register)((struct bt_hfp_ag_cb *)&g_hfp_ag_cb), 0); |
There was a problem hiding this comment.
Casting away const is unsafe and could lead to undefined behavior if the underlying Zephyr API modifies the callback structure. The proper solution is to update the Zephyr API signatures to accept const pointers, or if these structures can be modified, they should not be marked as const.
| case BT_SPP_REGISTER_APP: { | ||
| if (ins->spp_cookie == NULL) { | ||
| ins->spp_cookie = profile->register_app(ins, packet->spp_pl._bt_spp_register_app.name_len ? packet->spp_pl._bt_spp_register_app.name : NULL, &g_spp_socket_cb); | ||
| ins->spp_cookie = profile->register_app(ins, packet->spp_pl._bt_spp_register_app.name_len ? packet->spp_pl._bt_spp_register_app.name : NULL, (spp_callbacks_t *)&g_spp_socket_cb); |
There was a problem hiding this comment.
Casting away const is unsafe and could lead to undefined behavior if the underlying API modifies the callback structure. The proper solution is to update the API signatures to accept const pointers, or if these structures can be modified, they should not be marked as const.
| packet->adv_pl._bt_le_start_advertising.scan_rsp_data, | ||
| packet->adv_pl._bt_le_start_advertising.scan_rsp_len, | ||
| &g_advertiser_socket_cb); | ||
| (advertiser_callback_t *)&g_advertiser_socket_cb); |
There was a problem hiding this comment.
Casting away const is unsafe and could lead to undefined behavior if the underlying API modifies the callback structure. The proper solution is to update the API signatures to accept const pointers, or if these structures can be modified, they should not be marked as const.
| 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); | ||
|
|
There was a problem hiding this comment.
These forward declarations for le_conn_add and le_conn_find appear to be unused - no definitions or calls to these functions can be found in the codebase. Consider removing these unused forward declarations to keep the code clean.
| 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); |
Memory Optimization: Change the bluetooth bt_tool a2dp source module 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
constmodifier.This ensures it is compiled and linked into Flash memory instead of RAM.