1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 /** 39 * @title PBAP Client 40 * 41 */ 42 43 #ifndef PBAP_CLIENT_H 44 #define PBAP_CLIENT_H 45 46 #if defined __cplusplus 47 extern "C" { 48 #endif 49 50 #include "btstack_config.h" 51 #include "yxml.h" 52 #include <stdint.h> 53 54 // max len of phone number used for lookup in pbap_lookup_by_number 55 #define PBAP_MAX_PHONE_NUMBER_LEN 32 56 57 // max len of name reported in PBAP_SUBEVENT_CARD_RESULT 58 #define PBAP_MAX_NAME_LEN 32 59 // max len of vcard handle reported in PBAP_SUBEVENT_CARD_RESULT 60 #define PBAP_MAX_HANDLE_LEN 16 61 62 63 typedef enum { 64 PBAP_CLIENT_INIT = 0, 65 PBAP_CLIENT_W4_GOEP_CONNECTION, 66 PBAP_CLIENT_W2_SEND_CONNECT_REQUEST, 67 PBAP_CLIENT_W4_CONNECT_RESPONSE, 68 PBAP_CLIENT_W4_USER_AUTHENTICATION, 69 PBAP_CLIENT_W2_SEND_AUTHENTICATED_CONNECT, 70 PBAP_CLIENT_CONNECTED, 71 // 72 PBAP_CLIENT_W2_SEND_DISCONNECT_REQUEST, 73 PBAP_CLIENT_W4_DISCONNECT_RESPONSE, 74 // 75 PBAP_CLIENT_W2_PULL_PHONEBOOK, 76 PBAP_CLIENT_W4_PHONEBOOK, 77 PBAP_CLIENT_W2_SET_PATH_ROOT, 78 PBAP_CLIENT_W4_SET_PATH_ROOT_COMPLETE, 79 PBAP_CLIENT_W2_SET_PATH_ELEMENT, 80 PBAP_CLIENT_W4_SET_PATH_ELEMENT_COMPLETE, 81 PBAP_CLIENT_W2_GET_PHONEBOOK_SIZE, 82 PBAP_CLIENT_W4_GET_PHONEBOOK_SIZE_COMPLETE, 83 // - pull vacard liast 84 PBAP_CLIENT_W2_GET_CARD_LIST, 85 PBAP_CLIENT_W4_GET_CARD_LIST_COMPLETE, 86 // - pull vcard entry 87 PBAP_CLIENT_W2_GET_CARD_ENTRY, 88 PBAP_CLIENT_W4_GET_CARD_ENTRY_COMPLETE, 89 // abort operation 90 PBAP_CLIENT_W4_ABORT_COMPLETE, 91 92 } pbap_client_state_t; 93 94 typedef enum { 95 PBAP_CLIENT_PHONEBOOK_SIZE_PARSER_STATE_W4_TYPE = 0, 96 PBAP_CLIENT_PHONEBOOK_SIZE_PARSER_STATE_W4_LEN, 97 PBAP_CLIENT_PHONEBOOK_SIZE_PARSER_STATE_W4_VALUE, 98 PBAP_CLIENT_PHONEBOOK_SIZE_PARSER_STATE_INVALID, 99 } pbap_client_phonebook_size_parser_state_t; 100 101 typedef struct { 102 // parsing 103 pbap_client_phonebook_size_parser_state_t state; 104 uint8_t type; 105 uint8_t len; 106 uint8_t pos; 107 // data 108 bool have_size; 109 uint8_t size_buffer[2]; 110 } pbap_client_phonebook_size_parser_t; 111 112 typedef enum { 113 PBAP_CLIENT_SRM_DISABLED, 114 PBAP_CLIENT_SRM_W4_CONFIRM, 115 PBAP_CLIENT_SRM_ENABLED_BUT_WAITING, 116 PBAP_CLIENT_SRM_ENABLED 117 } pbap_client_obex_srm_state_t; 118 119 typedef struct { 120 uint8_t srm_value; 121 uint8_t srmp_value; 122 } pbap_client_obex_srm_t; 123 124 typedef enum { 125 OBEX_AUTH_PARSER_STATE_W4_TYPE = 0, 126 OBEX_AUTH_PARSER_STATE_W4_LEN, 127 OBEX_AUTH_PARSER_STATE_W4_VALUE, 128 OBEX_AUTH_PARSER_STATE_INVALID, 129 } pbap_client_obex_auth_parser_state_t; 130 131 typedef struct { 132 // parsing 133 pbap_client_obex_auth_parser_state_t state; 134 uint8_t type; 135 uint8_t len; 136 uint8_t pos; 137 // data 138 uint8_t authentication_options; 139 uint16_t authentication_nonce[16]; 140 } pbap_client_obex_auth_parser_t; 141 142 typedef struct pbap_client { 143 // pbap client linked list 144 btstack_linked_item_t item; 145 146 // goep client linked list 147 goep_client_t goep_client; 148 149 pbap_client_state_t state; 150 bd_addr_t bd_addr; 151 hci_con_handle_t con_handle; 152 uint16_t goep_cid; 153 btstack_packet_handler_t client_handler; 154 int request_number; 155 const char * current_folder; 156 const char * phone_number; 157 const char * phonebook_path; 158 const char * vcard_name; 159 uint16_t set_path_offset; 160 /* vcard selector / operator */ 161 uint32_t vcard_selector; 162 uint8_t vcard_selector_operator; 163 uint8_t vcard_selector_supported; 164 /* property selector */ 165 uint32_t property_selector; 166 uint16_t list_start_offset; 167 uint16_t max_list_count; 168 /* abort */ 169 uint8_t abort_operation; 170 /* obex parser */ 171 bool obex_parser_waiting_for_response; 172 obex_parser_t obex_parser; 173 uint8_t obex_header_buffer[4]; 174 /* authentication */ 175 pbap_client_obex_auth_parser_t obex_auth_parser; 176 const char * authentication_password; 177 /* xml parser */ 178 yxml_t xml_parser; 179 uint8_t xml_buffer[50]; 180 /* vcard listing parser */ 181 bool parser_card_found; 182 bool parser_name_found; 183 bool parser_handle_found; 184 char parser_name[PBAP_MAX_NAME_LEN]; 185 char parser_handle[PBAP_MAX_HANDLE_LEN]; 186 /* phonebook size */ 187 pbap_client_phonebook_size_parser_t phonebook_size_parser; 188 /* flow control mode */ 189 uint8_t flow_control_enabled; 190 uint8_t flow_next_triggered; 191 bool flow_wait_for_user; 192 /* srm */ 193 pbap_client_obex_srm_t obex_srm; 194 pbap_client_obex_srm_state_t srm_state; 195 } pbap_client_t; 196 197 /* API_START */ 198 199 /** 200 * Setup PhoneBook Access Client 201 */ 202 void pbap_client_init(void); 203 204 /** 205 * @brief Create PBAP connection to a Phone Book Server (PSE) server on a remote device. 206 * If the server requires authentication, a PBAP_SUBEVENT_AUTHENTICATION_REQUEST is emitted, which 207 * can be answered with pbap_authentication_password(..). 208 * The status of PBAP connection establishment is reported via PBAP_SUBEVENT_CONNECTION_OPENED event, 209 * i.e. on success status field is set to ERROR_CODE_SUCCESS. 210 * 211 * This function allows for multiple parallel connections. 212 * 213 * @param client storage for connection state. Must stay valid until connection closes 214 * @param l2cap_ertm_config 215 * @param l2cap_ertm_buffer_size 216 * @param l2cap_ertm_buffer 217 * @param handler 218 * @param addr 219 * @param out_cid to use for further commands 220 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_MEMORY_ALLOC_FAILED if PBAP or GOEP connection already exists. 221 */ 222 223 uint8_t pbap_client_connect(pbap_client_t * client, l2cap_ertm_config_t *l2cap_ertm_config, uint8_t *l2cap_ertm_buffer, 224 uint16_t l2cap_ertm_buffer_size, btstack_packet_handler_t handler, bd_addr_t addr, uint16_t * out_cid); 225 226 /** 227 * @brief Create PBAP connection to a Phone Book Server (PSE) server on a remote device. 228 * If the server requires authentication, a PBAP_SUBEVENT_AUTHENTICATION_REQUEST is emitted, which 229 * can be answered with pbap_authentication_password(..). 230 * The status of PBAP connection establishment is reported via PBAP_SUBEVENT_CONNECTION_OPENED event, 231 * i.e. on success status field is set to ERROR_CODE_SUCCESS. 232 * 233 * This function uses a single pbap_client_t and l2cap ertm buffer instance and can only be used for a single connection. 234 * Fur multiple parallel connections, use pbap_client_connect. 235 * 236 * @param handler 237 * @param addr 238 * @param out_cid to use for further commands 239 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_MEMORY_ALLOC_FAILED if PBAP or GOEP connection already exists. 240 */ 241 uint8_t pbap_connect(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t * out_cid); 242 243 /** 244 * @brief Provide password for OBEX Authentication after receiving PBAP_SUBEVENT_AUTHENTICATION_REQUEST. 245 * The status of PBAP connection establishment is reported via PBAP_SUBEVENT_CONNECTION_OPENED event, 246 * i.e. on success status field is set to ERROR_CODE_SUCCESS. 247 * 248 * @param pbap_cid 249 * @param password (null terminated string) - not copied, needs to stay valid until connection completed 250 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 251 */ 252 uint8_t pbap_authentication_password(uint16_t pbap_cid, const char * password); 253 254 /** 255 * @brief Disconnects PBAP connection with given identifier. 256 * Event PBAP_SUBEVENT_CONNECTION_CLOSED indicates that PBAP connection is closed. 257 * 258 * @param pbap_cid 259 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 260 */ 261 uint8_t pbap_disconnect(uint16_t pbap_cid); 262 263 /** 264 * @brief Set current folder on PSE. The status is reported via PBAP_SUBEVENT_OPERATION_COMPLETED event. 265 * 266 * @param pbap_cid 267 * @param path - note: path is not copied 268 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 269 */ 270 uint8_t pbap_set_phonebook(uint16_t pbap_cid, const char * path); 271 272 /** 273 * @brief Set vCard Selector for get/pull phonebook. No event is emitted. 274 * 275 * @param pbap_cid 276 * @param vcard_selector - combination of PBAP_PROPERTY_MASK_* properties 277 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 278 */ 279 uint8_t pbap_set_vcard_selector(uint16_t pbap_cid, uint32_t vcard_selector); 280 281 /** 282 * @brief Set vCard Selector for get/pull phonebook. No event is emitted. 283 * 284 * @param pbap_cid 285 * @param vcard_selector_operator - PBAP_VCARD_SELECTOR_OPERATOR_OR (default) or PBAP_VCARD_SELECTOR_OPERATOR_AND 286 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 287 */ 288 uint8_t pbap_set_vcard_selector_operator(uint16_t pbap_cid, int vcard_selector_operator); 289 290 /** 291 * @brief Set Property Selector. No event is emitted. 292 * @param pbap_cid 293 * @param property_selector - combination of PBAP_PROPERTY_MASK_* properties 294 * @return 295 */ 296 uint8_t pbap_set_property_selector(uint16_t pbap_cid, uint32_t property_selector); 297 298 /** 299 * @brief Set number of items returned by pull phonebook. 300 * @param pbap_cid 301 * @param max_list_count 302 * @return 303 */ 304 uint8_t pbap_set_max_list_count(uint16_t pbap_cid, uint16_t max_list_count); 305 306 /** 307 * @bbrief Set start offset for pull phonebook 308 * @param pbap_cid 309 * @param list_start_offset 310 * @return 311 */ 312 uint8_t pbap_set_list_start_offset(uint16_t pbap_cid, uint16_t list_start_offset); 313 314 /** 315 * @brief Get size of phone book from PSE. The result is reported via PBAP_SUBEVENT_PHONEBOOK_SIZE event. 316 * 317 * @param pbap_cid 318 * @param path - note: path is not copied, common path 'telecom/pb.vcf' 319 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 320 */ 321 uint8_t pbap_get_phonebook_size(uint16_t pbap_cid, const char * path); 322 323 /** 324 * @brief Pull phone book from PSE. The result is reported via registered packet handler (see pbap_connect function), 325 * with packet type set to PBAP_DATA_PACKET. Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of the phone book. 326 * 327 * @param pbap_cid 328 * @param path - note: path is not copied, common path 'telecom/pb.vcf' 329 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 330 */ 331 uint8_t pbap_pull_phonebook(uint16_t pbap_cid, const char * path); 332 333 /** 334 * @brief Pull vCard listing. vCard data is emitted via PBAP_SUBEVENT_CARD_RESULT event. 335 * Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of vCard listing. 336 * 337 * @param pbap_cid 338 * @param path 339 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 340 */ 341 uint8_t pbap_pull_vcard_listing(uint16_t pbap_cid, const char * path); 342 343 /** 344 * @brief Pull vCard entry. The result is reported via callback (see pbap_connect function), 345 * with packet type set to PBAP_DATA_PACKET. 346 * Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of the vCard entry. 347 * 348 * @param pbap_cid 349 * @param path 350 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 351 */ 352 uint8_t pbap_pull_vcard_entry(uint16_t pbap_cid, const char * path); 353 354 /** 355 * @brief Lookup contact(s) by phone number. vCard data is emitted via PBAP_SUBEVENT_CARD_RESULT event. 356 * Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of the lookup. 357 * 358 * @param pbap_cid 359 * @param phone_number 360 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 361 */ 362 uint8_t pbap_lookup_by_number(uint16_t pbap_cid, const char * phone_number); 363 364 /** 365 * @brief Abort current operation. No event is emitted. 366 * 367 * @param pbap_cid 368 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 369 */ 370 uint8_t pbap_abort(uint16_t pbap_cid); 371 372 373 /** 374 * @brief Set flow control mode - default is off. No event is emitted. 375 * @note When enabled, pbap_next_packet needs to be called after a packet was processed to receive the next one 376 * 377 * @param pbap_cid 378 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 379 */ 380 uint8_t pbap_set_flow_control_mode(uint16_t pbap_cid, int enable); 381 382 /** 383 * @brief Trigger next packet from PSE when Flow Control Mode is enabled. 384 * @param pbap_cid 385 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 386 */ 387 uint8_t pbap_next_packet(uint16_t pbap_cid); 388 389 /** 390 * @brief De-Init PBAP Client 391 */ 392 void pbap_client_deinit(void); 393 394 /* API_END */ 395 396 #if defined __cplusplus 397 } 398 #endif 399 #endif 400