-
Notifications
You must be signed in to change notification settings - Fork 33
Bugfix/fix bluetooth bt tool async adv moudle cb change to const #436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Bugfix/fix bluetooth bt tool async adv moudle cb change to const #436
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>
… 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>
There was a problem hiding this 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 callback and configuration structures from RAM to Flash memory. The change adds the const qualifier to various static structures throughout the Bluetooth stack, which instructs the compiler/linker to place them in read-only memory (Flash) instead of RAM, reducing runtime memory consumption.
Changes:
- Added
constqualifiers to static callback structures, option arrays, and command tables across multiple Bluetooth modules - Added const casts when passing these structures to APIs that don't accept const pointers
- Added two forward declarations in sal_adapter_le_interface.c (appears unrelated to the main optimization)
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 18 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/async/adv.c | Made advertising option arrays, command table, and callback structure const with appropriate casts |
| tools/adv.c | Made advertising option arrays, command table, and callback structure const with appropriate casts |
| tools/a2dp_source.c | Made A2DP command table const with appropriate cast |
| service/stacks/zephyr/sal_spp_interface.c | Made SPP SDP attributes and RFCOMM ops const with appropriate cast |
| service/stacks/zephyr/sal_hid_device_interface.c | Made HID SDP attributes and callback structure const with appropriate cast |
| service/stacks/zephyr/sal_hfp_hf_interface.c | Made HFP HF callback structure const with appropriate cast |
| service/stacks/zephyr/sal_hfp_ag_interface.c | Made HFP AG callback structure const with appropriate cast |
| service/stacks/zephyr/sal_avrcp_interface.c | Made AVRCP target callback structure const |
| service/stacks/zephyr/sal_adapter_le_interface.c | Made LE adapter callback structures const with appropriate casts; added forward declarations |
| service/stacks/zephyr/sal_adapter_interface.c | Made BR/EDR adapter callback structures const with appropriate casts |
| service/stacks/zephyr/sal_a2dp_interface.c | Made A2DP codec info, stream ops, and callback structures const with appropriate 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 callback structure const with appropriate cast |
| service/ipc/socket/src/bt_socket_scan.c | Made scanner socket callback structure const with appropriate cast |
| service/ipc/socket/src/bt_socket_advertiser.c | Made advertiser socket callback structure const with appropriate cast |
| framework/btwrap/async/bt_gatt_feature.c | Made GATT client callback structure const with appropriate cast |
| feature/feature_async/src/bluetooth_ble_impl.c | Made BLE advertiser and GATT client callback structures const with appropriate casts |
💡 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); |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
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. If the bt_hid_device_register function doesn't modify the callback structure, its signature should be updated to accept a const pointer instead. This would eliminate the need for the cast and make the code safer.
| 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); |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
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. If the bt_hfp_hf_register function doesn't modify the callback structure, its signature should be updated to accept a const pointer instead. This would eliminate the need for the cast and make the code safer.
| 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); |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
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. If the bt_hfp_ag_register function doesn't modify the callback structure, its signature should be updated to accept a const pointer instead. This would eliminate the need for the cast and make the code safer.
| 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); | ||
| #endif | ||
| } | ||
|
|
||
| static void zblue_unregister_callback(void) | ||
| { | ||
| bt_conn_cb_unregister(&g_conn_cbs); | ||
| bt_conn_cb_unregister((struct bt_conn_cb *)&g_conn_cbs); | ||
| #ifdef CONFIG_BT_SMP | ||
| bt_conn_le_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); |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
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. If these callback registration functions don't modify the callback structures, their signatures should be updated to accept const pointers instead. This would eliminate the need for the casts and make the code safer.
| @@ -1430,7 +1430,7 @@ static int zblue_on_config_req(struct bt_a2dp* a2dp, struct bt_a2dp_ep* ep, | |||
|
|
|||
| a2dp_info->stream = (struct bt_a2dp_stream*)calloc(1, sizeof(struct bt_a2dp_stream)); | |||
| *stream = a2dp_info->stream; /* The a2dp_stream saved in SAL is assigned a value in zblue. */ | |||
| 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); | |||
| *rsp_err_code = BT_AVDTP_SUCCESS; | |||
| return 0; | |||
| } | |||
| @@ -1516,7 +1516,7 @@ static void zblue_on_suspend_rsp(struct bt_a2dp_stream* stream, uint8_t rsp_err_ | |||
| BT_LOGE("%s, suspend fail: %d", __func__, rsp_err_code); | |||
| } | |||
|
|
|||
| static struct bt_a2dp_cb a2dp_cbks = { | |||
| static const struct bt_a2dp_cb a2dp_cbks = { | |||
| .connected = zblue_on_connected, | |||
| .disconnected = zblue_on_disconnected, | |||
| .config_req = zblue_on_config_req, | |||
| @@ -1565,7 +1565,7 @@ bt_status_t bt_sal_a2dp_source_init(uint8_t max_connections) | |||
| } | |||
| #endif /* CONFIG_BLUETOOTH_A2DP_AAC_CODEC */ | |||
|
|
|||
| SAL_CHECK_RET(bt_a2dp_register_cb(&a2dp_cbks), 0); | |||
| SAL_CHECK_RET(bt_a2dp_register_cb((struct bt_a2dp_cb *)&a2dp_cbks), 0); | |||
|
|
|||
| return BT_STATUS_SUCCESS; | |||
| #else | |||
| @@ -1605,7 +1605,7 @@ bt_status_t bt_sal_a2dp_sink_init(uint8_t max_connections) | |||
| } | |||
| #endif /* CONFIG_BLUETOOTH_A2DP_AAC_CODEC */ | |||
|
|
|||
| SAL_CHECK_RET(bt_a2dp_register_cb(&a2dp_cbks), 0); | |||
| SAL_CHECK_RET(bt_a2dp_register_cb((struct bt_a2dp_cb *)&a2dp_cbks), 0); | |||
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
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. If the bt_a2dp_stream_cb_register and bt_a2dp_register_cb functions don't modify the callback structures, their signatures should be updated to accept const pointers instead. This would eliminate the need for the casts and make the code safer.
| } | ||
|
|
||
| spp_conn->rfcomm_dlc.ops = &g_rfcomm_ops; | ||
| spp_conn->rfcomm_dlc.ops = (struct bt_rfcomm_dlc_ops *)&g_rfcomm_ops; |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
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. If the underlying API doesn't modify the ops structure, its signature should be updated to accept a const pointer instead. This would eliminate the need for the cast and make the code safer.
| scan->ins = ins; | ||
| scan->remote = packet->scan_pl._bt_le_start_scan.remote; | ||
| packet->scan_r.remote = PTR2INT(uint64_t) scanner_start_scan(scan, &g_scanner_socket_cb); | ||
| packet->scan_r.remote = PTR2INT(uint64_t) scanner_start_scan(scan, (scanner_callbacks_t *)&g_scanner_socket_cb); |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
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. If the scanner_start_scan function doesn't modify the callback structure, its signature should be updated to accept a const pointer instead. This would eliminate the need for the cast and make the code safer.
| } 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); |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
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. If the bt_le_start_advertising_async and bt_gattc_feature_create_client_async functions don't modify the callback structures, their signatures should be updated to accept const pointers instead. This would eliminate the need for the casts and make the code safer.
| (bt_gattc_feature_callbacks_t *)&gattc_cbs, (void*)data); | |
| &gattc_cbs, (void*)data); |
| p_adv_data, adv_len, | ||
| p_scan_rsp_data, scan_rsp_len, | ||
| &adv_callback, | ||
| (advertiser_callback_t *)&adv_callback, |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
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. If the bt_le_start_advertising_async function doesn't modify the callback structure (which appears to be the case based on its implementation), its signature should be updated to accept a const pointer instead. This would eliminate the need for the cast and make the code safer.
| p_adv_data, adv_len, | ||
| p_scan_rsp_data, scan_rsp_len, | ||
| &adv_callback); | ||
| (advertiser_callback_t *)&adv_callback); |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
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. If the bt_le_start_advertising function doesn't modify the callback structure, its signature should be updated to accept a const pointer instead. This would eliminate the need for the cast and make the code safer.
Memory Optimization: Change the bluetooth bt_tool async adv module to const for reduce the ram size.
bug:v/84961
Rootcause: To reduce RAM resource consumption, the
adv_options,adv_stop_options,g_adv_async_tablesandadv_callbackin the bt_tools async adv module must be marked with theconstmodifier.This ensures it is compiled and linked into Flash memory instead of RAM.