|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <string.h> |
| 8 | +#include "esp_err.h" |
| 9 | +#include "esp_bt_main.h" |
| 10 | +#include "esp_pbac_api.h" |
| 11 | +#include "btc/btc_manage.h" |
| 12 | +#include "btc_pba_client.h" |
| 13 | + |
| 14 | +#if BTC_PBA_CLIENT_INCLUDED |
| 15 | + |
| 16 | +esp_err_t esp_pbac_init(void) |
| 17 | +{ |
| 18 | + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { |
| 19 | + return ESP_ERR_INVALID_STATE; |
| 20 | + } |
| 21 | + |
| 22 | + btc_msg_t msg; |
| 23 | + |
| 24 | + msg.sig = BTC_SIG_API_CALL; |
| 25 | + msg.pid = BTC_PID_PBA_CLIENT; |
| 26 | + msg.act = BTC_PBA_CLIENT_INIT_EVT; |
| 27 | + |
| 28 | + /* Switch to BTC context */ |
| 29 | + bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL, NULL); |
| 30 | + return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL; |
| 31 | +} |
| 32 | + |
| 33 | +esp_err_t esp_pbac_deinit(void) |
| 34 | +{ |
| 35 | + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { |
| 36 | + return ESP_ERR_INVALID_STATE; |
| 37 | + } |
| 38 | + |
| 39 | + btc_msg_t msg; |
| 40 | + |
| 41 | + msg.sig = BTC_SIG_API_CALL; |
| 42 | + msg.pid = BTC_PID_PBA_CLIENT; |
| 43 | + msg.act = BTC_PBA_CLIENT_DEINIT_EVT; |
| 44 | + |
| 45 | + /* Switch to BTC context */ |
| 46 | + bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL, NULL); |
| 47 | + return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL; |
| 48 | +} |
| 49 | + |
| 50 | +esp_err_t esp_pbac_register_callback(esp_pbac_callback_t callback) |
| 51 | +{ |
| 52 | + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { |
| 53 | + return ESP_ERR_INVALID_STATE; |
| 54 | + } |
| 55 | + |
| 56 | + if (callback == NULL) { |
| 57 | + return ESP_FAIL; |
| 58 | + } |
| 59 | + |
| 60 | + btc_profile_cb_set(BTC_PID_PBA_CLIENT, callback); |
| 61 | + return ESP_OK; |
| 62 | +} |
| 63 | + |
| 64 | +esp_err_t esp_pbac_connect(esp_bd_addr_t bd_addr) |
| 65 | +{ |
| 66 | + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { |
| 67 | + return ESP_ERR_INVALID_STATE; |
| 68 | + } |
| 69 | + |
| 70 | + if (bd_addr == NULL) { |
| 71 | + return ESP_ERR_INVALID_ARG; |
| 72 | + } |
| 73 | + |
| 74 | + btc_msg_t msg; |
| 75 | + msg.sig = BTC_SIG_API_CALL; |
| 76 | + msg.pid = BTC_PID_PBA_CLIENT; |
| 77 | + msg.act = BTC_PBA_CLIENT_CONNECT_EVT; |
| 78 | + |
| 79 | + btc_pba_client_args_t args = {0}; |
| 80 | + memcpy(args.connect.bd_addr.address, bd_addr, sizeof(esp_bd_addr_t)); |
| 81 | + |
| 82 | + /* Switch to BTC context */ |
| 83 | + bt_status_t stat = btc_transfer_context(&msg, &args, sizeof(btc_pba_client_args_t), NULL, NULL); |
| 84 | + return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL; |
| 85 | +} |
| 86 | + |
| 87 | +esp_err_t esp_pbac_disconnect(esp_pbac_conn_hdl_t handle) |
| 88 | +{ |
| 89 | + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { |
| 90 | + return ESP_ERR_INVALID_STATE; |
| 91 | + } |
| 92 | + |
| 93 | + if (handle == ESP_PBAC_INVALID_HANDLE) { |
| 94 | + return ESP_ERR_INVALID_ARG; |
| 95 | + } |
| 96 | + |
| 97 | + btc_msg_t msg; |
| 98 | + msg.sig = BTC_SIG_API_CALL; |
| 99 | + msg.pid = BTC_PID_PBA_CLIENT; |
| 100 | + msg.act = BTC_PBA_CLIENT_DISCONNECT_EVT; |
| 101 | + |
| 102 | + btc_pba_client_args_t args = {0}; |
| 103 | + args.disconnect.handle = handle; |
| 104 | + |
| 105 | + /* Switch to BTC context */ |
| 106 | + bt_status_t stat = btc_transfer_context(&msg, &args, sizeof(btc_pba_client_args_t), NULL, NULL); |
| 107 | + return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL; |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +esp_err_t esp_pbac_pull_phone_book(esp_pbac_conn_hdl_t handle, const char *name, esp_pbac_pull_phone_book_app_param_t *app_param) |
| 112 | +{ |
| 113 | + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { |
| 114 | + return ESP_ERR_INVALID_STATE; |
| 115 | + } |
| 116 | + |
| 117 | + if (handle == ESP_PBAC_INVALID_HANDLE || name == NULL) { |
| 118 | + return ESP_ERR_INVALID_ARG; |
| 119 | + } |
| 120 | + |
| 121 | + btc_msg_t msg; |
| 122 | + msg.sig = BTC_SIG_API_CALL; |
| 123 | + msg.pid = BTC_PID_PBA_CLIENT; |
| 124 | + msg.act = BTC_PBA_CLIENT_PULL_PHONE_BOOK_EVT; |
| 125 | + |
| 126 | + btc_pba_client_args_t args = {0}; |
| 127 | + args.pull_phone_book.handle = handle; |
| 128 | + args.pull_phone_book.name = (char *)name; |
| 129 | + if (app_param != NULL) { |
| 130 | + args.pull_phone_book.include_app_param = true; |
| 131 | + memcpy(&args.pull_phone_book.app_param, app_param, sizeof(esp_pbac_pull_phone_book_app_param_t)); |
| 132 | + } |
| 133 | + else { |
| 134 | + args.pull_phone_book.include_app_param = false; |
| 135 | + } |
| 136 | + |
| 137 | + /* Switch to BTC context */ |
| 138 | + bt_status_t stat = btc_transfer_context(&msg, &args, sizeof(btc_pba_client_args_t), btc_pba_client_args_deep_copy, btc_pba_client_args_deep_free); |
| 139 | + return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL; |
| 140 | +} |
| 141 | + |
| 142 | +esp_err_t esp_pbac_set_phone_book(esp_pbac_conn_hdl_t handle, esp_pbac_set_phone_book_flags_t flags, const char *name) |
| 143 | +{ |
| 144 | + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { |
| 145 | + return ESP_ERR_INVALID_STATE; |
| 146 | + } |
| 147 | + |
| 148 | + /* since ESP_PBAC_SET_PHONE_BOOK_FLAGS_ROOT is equal to ESP_PBAC_SET_PHONE_BOOK_FLAGS_DOWN, we dont check XXX_DOWN */ |
| 149 | + if (handle == ESP_PBAC_INVALID_HANDLE || (flags != ESP_PBAC_SET_PHONE_BOOK_FLAGS_ROOT && flags != ESP_PBAC_SET_PHONE_BOOK_FLAGS_UP)) { |
| 150 | + return ESP_ERR_INVALID_ARG; |
| 151 | + } |
| 152 | + |
| 153 | + btc_msg_t msg; |
| 154 | + msg.sig = BTC_SIG_API_CALL; |
| 155 | + msg.pid = BTC_PID_PBA_CLIENT; |
| 156 | + msg.act = BTC_PBA_CLIENT_SET_PHONE_BOOK_EVT; |
| 157 | + |
| 158 | + btc_pba_client_args_t args = {0}; |
| 159 | + args.set_phone_book.handle = handle; |
| 160 | + args.set_phone_book.flags = flags; |
| 161 | + /* set phone book name is allowed to be NULL */ |
| 162 | + args.set_phone_book.name = (char *)name; |
| 163 | + |
| 164 | + /* Switch to BTC context */ |
| 165 | + bt_status_t stat = btc_transfer_context(&msg, &args, sizeof(btc_pba_client_args_t), btc_pba_client_args_deep_copy, btc_pba_client_args_deep_free); |
| 166 | + return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL; |
| 167 | +} |
| 168 | + |
| 169 | +esp_err_t esp_pbac_set_phone_book2(esp_pbac_conn_hdl_t handle, const char *path) |
| 170 | +{ |
| 171 | + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { |
| 172 | + return ESP_ERR_INVALID_STATE; |
| 173 | + } |
| 174 | + |
| 175 | + btc_msg_t msg; |
| 176 | + msg.sig = BTC_SIG_API_CALL; |
| 177 | + msg.pid = BTC_PID_PBA_CLIENT; |
| 178 | + msg.act = BTC_PBA_CLIENT_SET_PHONE_BOOK2_EVT; |
| 179 | + |
| 180 | + btc_pba_client_args_t args = {0}; |
| 181 | + args.set_phone_book.handle = handle; |
| 182 | + args.set_phone_book.name = (char *)path; |
| 183 | + |
| 184 | + /* Switch to BTC context */ |
| 185 | + bt_status_t stat = btc_transfer_context(&msg, &args, sizeof(btc_pba_client_args_t), btc_pba_client_args_deep_copy, btc_pba_client_args_deep_free); |
| 186 | + return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL; |
| 187 | +} |
| 188 | + |
| 189 | +esp_err_t esp_pbac_pull_vcard_listing(esp_pbac_conn_hdl_t handle, const char *name, esp_pbac_pull_vcard_listing_app_param_t *app_param) |
| 190 | +{ |
| 191 | + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { |
| 192 | + return ESP_ERR_INVALID_STATE; |
| 193 | + } |
| 194 | + |
| 195 | + if (handle == ESP_PBAC_INVALID_HANDLE || name == NULL) { |
| 196 | + return ESP_ERR_INVALID_ARG; |
| 197 | + } |
| 198 | + |
| 199 | + btc_msg_t msg; |
| 200 | + msg.sig = BTC_SIG_API_CALL; |
| 201 | + msg.pid = BTC_PID_PBA_CLIENT; |
| 202 | + msg.act = BTC_PBA_CLIENT_PULL_VCARD_LISTING_EVT; |
| 203 | + |
| 204 | + btc_pba_client_args_t args = {0}; |
| 205 | + args.pull_vcard_listing.handle = handle; |
| 206 | + args.pull_vcard_listing.name = (char *)name; |
| 207 | + if (app_param != NULL) { |
| 208 | + args.pull_vcard_listing.include_app_param = true; |
| 209 | + memcpy(&args.pull_vcard_listing.app_param, app_param, sizeof(esp_pbac_pull_vcard_listing_app_param_t)); |
| 210 | + } |
| 211 | + else { |
| 212 | + args.pull_vcard_listing.include_app_param = false; |
| 213 | + } |
| 214 | + |
| 215 | + /* Switch to BTC context */ |
| 216 | + bt_status_t stat = btc_transfer_context(&msg, &args, sizeof(btc_pba_client_args_t), btc_pba_client_args_deep_copy, btc_pba_client_args_deep_free); |
| 217 | + return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL; |
| 218 | +} |
| 219 | + |
| 220 | +esp_err_t esp_pbac_pull_vcard_entry(esp_pbac_conn_hdl_t handle, const char *name, esp_pbac_pull_vcard_entry_app_param_t *app_param) |
| 221 | +{ |
| 222 | + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { |
| 223 | + return ESP_ERR_INVALID_STATE; |
| 224 | + } |
| 225 | + |
| 226 | + if (handle == ESP_PBAC_INVALID_HANDLE || name == NULL) { |
| 227 | + return ESP_ERR_INVALID_ARG; |
| 228 | + } |
| 229 | + |
| 230 | + btc_msg_t msg; |
| 231 | + msg.sig = BTC_SIG_API_CALL; |
| 232 | + msg.pid = BTC_PID_PBA_CLIENT; |
| 233 | + msg.act = BTC_PBA_CLIENT_PULL_VCARD_ENTRY_EVT; |
| 234 | + |
| 235 | + btc_pba_client_args_t args = {0}; |
| 236 | + args.pull_vcard_entry.handle = handle; |
| 237 | + args.pull_vcard_entry.name = (char *)name; |
| 238 | + if (app_param != NULL) { |
| 239 | + args.pull_vcard_entry.include_app_param = true; |
| 240 | + memcpy(&args.pull_vcard_entry.app_param, app_param, sizeof(esp_pbac_pull_vcard_entry_app_param_t)); |
| 241 | + } |
| 242 | + else { |
| 243 | + args.pull_vcard_entry.include_app_param = false; |
| 244 | + } |
| 245 | + |
| 246 | + /* Switch to BTC context */ |
| 247 | + bt_status_t stat = btc_transfer_context(&msg, &args, sizeof(btc_pba_client_args_t), btc_pba_client_args_deep_copy, btc_pba_client_args_deep_free); |
| 248 | + return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL; |
| 249 | +} |
| 250 | + |
| 251 | +#endif |
0 commit comments