-
Notifications
You must be signed in to change notification settings - Fork 33
Bugfix/fix bluetooth bt tool async gatt client moudle cb change to const #438
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?
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>
… 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>
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 optimizes RAM usage by moving static callback and command table structures from RAM to Flash memory by marking them as const. The changes affect bluetooth tools and service layer implementations across the codebase.
Changes:
- Converted static callback structures and command tables to
constto place them in Flash instead of RAM - 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
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 17 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/async/gatt_client.c | Made g_gattc_async_tables and gattc_cbs const, added casts for API calls |
| tools/async/gap.c | Made le_conn_options, g_set_cmd_tables, g_get_cmd_tables, and g_pair_cmd_tables const, added casts |
| tools/async/adv.c | Made adv_options, adv_stop_options, g_adv_async_tables, and adv_callback const, added casts |
| tools/adv.c | Made adv_options, adv_stop_options, g_adv_tables, and adv_callback const, added casts |
| tools/a2dp_source.c | Made g_a2dp_tables const, added cast |
| service/stacks/zephyr/sal_spp_interface.c | Made spp_attrs_template and g_rfcomm_ops const, added casts |
| service/stacks/zephyr/sal_hid_device_interface.c | Made hid_attrs_template and hid_callback const, added casts |
| service/stacks/zephyr/sal_hfp_hf_interface.c | Made hf_callbacks const, added cast |
| service/stacks/zephyr/sal_hfp_ag_interface.c | Made g_hfp_ag_cb const, added cast |
| 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, g_setting_cbs const, added forward declarations and casts |
| service/stacks/zephyr/sal_adapter_interface.c | Made g_conn_cbs, g_setting_cbs, g_conn_auth_info_cbs, g_br_discovery_cb const, added casts |
| service/stacks/zephyr/sal_a2dp_interface.c | Made sbc_src_ie, stream_ops, and a2dp_cbks const, added casts |
| 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 |
| service/ipc/socket/src/bt_socket_scan.c | Made g_scanner_socket_cb const, added cast |
| service/ipc/socket/src/bt_socket_advertiser.c | Made g_advertiser_socket_cb const, added cast |
| framework/btwrap/async/bt_gatt_feature.c | Made s_feature_gattc_cbs const, added cast |
| feature/feature_async/src/bluetooth_ble_impl.c | Made adv_callback and gattc_cbs const, added casts |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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 qualifier violates const-correctness. The API signature of bt_le_start_advertising_async should be updated to accept const advertiser_callback_t* since callbacks are not modified.
| (advertiser_callback_t *)&adv_callback, | |
| &adv_callback, |
| 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); |
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 qualifier violates const-correctness. These Zephyr callback registration APIs should accept const pointers since the callbacks are only read and stored. This applies to multiple callback types in this file (g_br_discovery_cb, g_conn_cbs, g_conn_auth_cbs, g_conn_auth_info_cbs, g_setting_cbs).
| int* conn_id = (int*)malloc(sizeof(int)); | ||
| *conn_id = index; | ||
| if (bt_gattc_create_connect_async(handle, &g_gattc_devies[index].handle, &gattc_cbs, create_connect_cb, | ||
| if (bt_gattc_create_connect_async(handle, &g_gattc_devies[index].handle, (gattc_callbacks_t *)&gattc_cbs, create_connect_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 qualifier to pass to a non-const parameter is technically undefined behavior in C if the function modifies the data. While the memory optimization goal (moving data from RAM to Flash) is valid, a better approach would be to update the API signature of bt_gattc_create_connect_async to accept const gattc_callbacks_t* since the callbacks are only read, not modified. This pattern is repeated throughout the codebase (in all the files changed in this PR), and while it may work in practice, it violates const-correctness principles and could potentially cause issues with certain compilers or optimization levels.
| 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 qualifier violates const-correctness. The API signature of bt_hfp_ag_register should be updated to accept const struct bt_hfp_ag_cb* since callbacks are only read and stored.
| @@ -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); | |||
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 qualifier violates const-correctness. The API signatures of bt_a2dp_stream_cb_register and bt_a2dp_register_cb should be updated to accept const pointers since callbacks are only read and stored. This pattern is repeated throughout this file for multiple callback registrations.
| 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 qualifier violates const-correctness. The API signature of bt_hfp_hf_register should be updated to accept const struct bt_hfp_hf_cb* since callbacks are only read and stored.
| 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); |
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 qualifier violates const-correctness. These Zephyr callback registration APIs should accept const pointers since the callbacks are only read and stored. This pattern is repeated throughout this file for multiple callback types (g_conn_cbs, g_conn_auth_cbs, g_conn_auth_info_cbs, g_setting_cbs, g_br_discovery_cb).
| 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); |
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 qualifier violates const-correctness. The API signature should be updated to accept const pointer for callbacks that are only read and stored.
| 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 qualifier violates const-correctness. The API signature of bt_le_start_advertising should be updated to accept const advertiser_callback_t* since callbacks are not modified.
| (advertiser_callback_t *)&adv_callback); | |
| &adv_callback); |
| 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 qualifier violates const-correctness. The API signature of bt_hid_device_register should be updated to accept const struct bt_hid_device_cb* since callbacks are only read and stored.
Memory Optimization: Change the bluetooth bt_tool async gatt client module 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
constmodifier.This ensures it is compiled and linked into Flash memory instead of RAM.