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 <stdint.h> 51 52 #include "btstack_config.h" 53 #include "yxml.h" 54 #include "classic/obex_srm_client.h" 55 56 // max len of phone number used for lookup in pbap_lookup_by_number 57 #define PBAP_MAX_PHONE_NUMBER_LEN 32 58 59 // max len of name reported in PBAP_SUBEVENT_CARD_RESULT 60 #define PBAP_MAX_NAME_LEN 32 61 // max len of vcard handle reported in PBAP_SUBEVENT_CARD_RESULT 62 #define PBAP_MAX_HANDLE_LEN 16 63 64 65 typedef enum { 66 PBAP_CLIENT_INIT = 0, 67 PBAP_CLIENT_W4_GOEP_CONNECTION, 68 PBAP_CLIENT_W2_SEND_CONNECT_REQUEST, 69 PBAP_CLIENT_W4_CONNECT_RESPONSE, 70 PBAP_CLIENT_W4_USER_AUTHENTICATION, 71 PBAP_CLIENT_W2_SEND_AUTHENTICATED_CONNECT, 72 PBAP_CLIENT_CONNECTED, 73 // 74 PBAP_CLIENT_W2_SEND_DISCONNECT_REQUEST, 75 PBAP_CLIENT_W4_DISCONNECT_RESPONSE, 76 // 77 PBAP_CLIENT_W2_PULL_PHONEBOOK, 78 PBAP_CLIENT_W4_PHONEBOOK, 79 PBAP_CLIENT_W2_SET_PATH_ROOT, 80 PBAP_CLIENT_W4_SET_PATH_ROOT_COMPLETE, 81 PBAP_CLIENT_W2_SET_PATH_ELEMENT, 82 PBAP_CLIENT_W4_SET_PATH_ELEMENT_COMPLETE, 83 PBAP_CLIENT_W2_GET_PHONEBOOK_SIZE, 84 PBAP_CLIENT_W4_GET_PHONEBOOK_SIZE_COMPLETE, 85 // - pull vacard liast 86 PBAP_CLIENT_W2_GET_CARD_LIST, 87 PBAP_CLIENT_W4_GET_CARD_LIST_COMPLETE, 88 // - pull vcard entry 89 PBAP_CLIENT_W2_GET_CARD_ENTRY, 90 PBAP_CLIENT_W4_GET_CARD_ENTRY_COMPLETE, 91 // abort operation 92 PBAP_CLIENT_W4_ABORT_COMPLETE, 93 94 } pbap_client_state_t; 95 96 typedef enum { 97 PBAP_CLIENT_PHONEBOOK_SIZE_PARSER_STATE_W4_TYPE = 0, 98 PBAP_CLIENT_PHONEBOOK_SIZE_PARSER_STATE_W4_LEN, 99 PBAP_CLIENT_PHONEBOOK_SIZE_PARSER_STATE_W4_VALUE, 100 PBAP_CLIENT_PHONEBOOK_SIZE_PARSER_STATE_INVALID, 101 } pbap_client_phonebook_size_parser_state_t; 102 103 typedef struct { 104 // parsing 105 pbap_client_phonebook_size_parser_state_t state; 106 uint8_t type; 107 uint8_t len; 108 uint8_t pos; 109 // data 110 bool have_size; 111 uint8_t size_buffer[2]; 112 } pbap_client_phonebook_size_parser_t; 113 114 typedef enum { 115 OBEX_AUTH_PARSER_STATE_W4_TYPE = 0, 116 OBEX_AUTH_PARSER_STATE_W4_LEN, 117 OBEX_AUTH_PARSER_STATE_W4_VALUE, 118 OBEX_AUTH_PARSER_STATE_INVALID, 119 } pbap_client_obex_auth_parser_state_t; 120 121 typedef struct { 122 // parsing 123 pbap_client_obex_auth_parser_state_t state; 124 uint8_t type; 125 uint8_t len; 126 uint8_t pos; 127 // data 128 uint8_t authentication_options; 129 uint16_t authentication_nonce[16]; 130 } pbap_client_obex_auth_parser_t; 131 132 typedef struct pbap_client { 133 // pbap client linked list 134 btstack_linked_item_t item; 135 136 // goep client linked list 137 goep_client_t goep_client; 138 139 pbap_client_state_t state; 140 bd_addr_t bd_addr; 141 hci_con_handle_t con_handle; 142 uint16_t goep_cid; 143 btstack_packet_handler_t client_handler; 144 int request_number; 145 const char * current_folder; 146 const char * phone_number; 147 const char * phonebook_path; 148 const char * vcard_name; 149 uint16_t set_path_offset; 150 /* vcard selector / operator */ 151 uint32_t vcard_selector; 152 uint8_t vcard_selector_operator; 153 uint8_t vcard_selector_supported; 154 /* property selector */ 155 uint32_t property_selector; 156 uint16_t list_start_offset; 157 uint16_t max_list_count; 158 /* abort */ 159 uint8_t abort_operation; 160 /* obex parser */ 161 bool obex_parser_waiting_for_response; 162 obex_parser_t obex_parser; 163 uint8_t obex_header_buffer[4]; 164 /* authentication */ 165 pbap_client_obex_auth_parser_t obex_auth_parser; 166 const char * authentication_password; 167 /* xml parser */ 168 yxml_t xml_parser; 169 uint8_t xml_buffer[50]; 170 /* vcard listing parser */ 171 bool parser_card_found; 172 bool parser_name_found; 173 bool parser_handle_found; 174 char parser_name[PBAP_MAX_NAME_LEN]; 175 char parser_handle[PBAP_MAX_HANDLE_LEN]; 176 /* phonebook size */ 177 pbap_client_phonebook_size_parser_t phonebook_size_parser; 178 /* flow control mode */ 179 uint8_t flow_control_enabled; 180 uint8_t flow_next_triggered; 181 bool flow_wait_for_user; 182 /* srm */ 183 obex_srm_client_t obex_srm; 184 } pbap_client_t; 185 186 /* API_START */ 187 188 /** 189 * Setup PhoneBook Access Client 190 */ 191 void pbap_client_init(void); 192 193 /** 194 * @brief Create PBAP connection to a Phone Book Server (PSE) server on a remote device. 195 * If the server requires authentication, a PBAP_SUBEVENT_AUTHENTICATION_REQUEST is emitted, which 196 * can be answered with pbap_authentication_password(..). 197 * The status of PBAP connection establishment is reported via PBAP_SUBEVENT_CONNECTION_OPENED event, 198 * i.e. on success status field is set to ERROR_CODE_SUCCESS. 199 * 200 * This function allows for multiple parallel connections. 201 * 202 * @param client storage for connection state. Must stay valid until connection closes 203 * @param l2cap_ertm_config 204 * @param l2cap_ertm_buffer_size 205 * @param l2cap_ertm_buffer 206 * @param handler 207 * @param addr 208 * @param out_cid to use for further commands 209 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_MEMORY_ALLOC_FAILED if PBAP or GOEP connection already exists. 210 */ 211 212 uint8_t pbap_client_connect(pbap_client_t * client, l2cap_ertm_config_t *l2cap_ertm_config, uint8_t *l2cap_ertm_buffer, 213 uint16_t l2cap_ertm_buffer_size, btstack_packet_handler_t handler, bd_addr_t addr, uint16_t * out_cid); 214 215 /** 216 * @brief Create PBAP connection to a Phone Book Server (PSE) server on a remote device. 217 * If the server requires authentication, a PBAP_SUBEVENT_AUTHENTICATION_REQUEST is emitted, which 218 * can be answered with pbap_authentication_password(..). 219 * The status of PBAP connection establishment is reported via PBAP_SUBEVENT_CONNECTION_OPENED event, 220 * i.e. on success status field is set to ERROR_CODE_SUCCESS. 221 * 222 * This function uses a single pbap_client_t and l2cap ertm buffer instance and can only be used for a single connection. 223 * Fur multiple parallel connections, use pbap_client_connect. 224 * 225 * @param handler 226 * @param addr 227 * @param out_cid to use for further commands 228 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_MEMORY_ALLOC_FAILED if PBAP or GOEP connection already exists. 229 */ 230 uint8_t pbap_connect(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t * out_cid); 231 232 /** 233 * @brief Provide password for OBEX Authentication after receiving PBAP_SUBEVENT_AUTHENTICATION_REQUEST. 234 * The status of PBAP connection establishment is reported via PBAP_SUBEVENT_CONNECTION_OPENED event, 235 * i.e. on success status field is set to ERROR_CODE_SUCCESS. 236 * 237 * @param pbap_cid 238 * @param password (null terminated string) - not copied, needs to stay valid until connection completed 239 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 240 */ 241 uint8_t pbap_authentication_password(uint16_t pbap_cid, const char * password); 242 243 /** 244 * @brief Disconnects PBAP connection with given identifier. 245 * Event PBAP_SUBEVENT_CONNECTION_CLOSED indicates that PBAP connection is closed. 246 * 247 * @param pbap_cid 248 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 249 */ 250 uint8_t pbap_disconnect(uint16_t pbap_cid); 251 252 /** 253 * @brief Set current folder on PSE. The status is reported via PBAP_SUBEVENT_OPERATION_COMPLETED event. 254 * 255 * @param pbap_cid 256 * @param path - note: path is not copied 257 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 258 */ 259 uint8_t pbap_set_phonebook(uint16_t pbap_cid, const char * path); 260 261 /** 262 * @brief Set vCard Selector for get/pull phonebook. No event is emitted. 263 * 264 * @param pbap_cid 265 * @param vcard_selector - combination of PBAP_PROPERTY_MASK_* properties 266 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 267 */ 268 uint8_t pbap_set_vcard_selector(uint16_t pbap_cid, uint32_t vcard_selector); 269 270 /** 271 * @brief Set vCard Selector for get/pull phonebook. No event is emitted. 272 * 273 * @param pbap_cid 274 * @param vcard_selector_operator - PBAP_VCARD_SELECTOR_OPERATOR_OR (default) or PBAP_VCARD_SELECTOR_OPERATOR_AND 275 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 276 */ 277 uint8_t pbap_set_vcard_selector_operator(uint16_t pbap_cid, int vcard_selector_operator); 278 279 /** 280 * @brief Set Property Selector. No event is emitted. 281 * @param pbap_cid 282 * @param property_selector - combination of PBAP_PROPERTY_MASK_* properties 283 * @return 284 */ 285 uint8_t pbap_set_property_selector(uint16_t pbap_cid, uint32_t property_selector); 286 287 /** 288 * @brief Set number of items returned by pull phonebook. 289 * @param pbap_cid 290 * @param max_list_count 291 * @return 292 */ 293 uint8_t pbap_set_max_list_count(uint16_t pbap_cid, uint16_t max_list_count); 294 295 /** 296 * @bbrief Set start offset for pull phonebook 297 * @param pbap_cid 298 * @param list_start_offset 299 * @return 300 */ 301 uint8_t pbap_set_list_start_offset(uint16_t pbap_cid, uint16_t list_start_offset); 302 303 /** 304 * @brief Get size of phone book from PSE. The result is reported via PBAP_SUBEVENT_PHONEBOOK_SIZE event. 305 * 306 * @param pbap_cid 307 * @param path - note: path is not copied, common path 'telecom/pb.vcf' 308 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 309 */ 310 uint8_t pbap_get_phonebook_size(uint16_t pbap_cid, const char * path); 311 312 /** 313 * @brief Pull phone book from PSE. The result is reported via registered packet handler (see pbap_connect function), 314 * with packet type set to PBAP_DATA_PACKET. Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of the phone book. 315 * 316 * @param pbap_cid 317 * @param path - note: path is not copied, common path 'telecom/pb.vcf' 318 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 319 */ 320 uint8_t pbap_pull_phonebook(uint16_t pbap_cid, const char * path); 321 322 /** 323 * @brief Pull vCard listing. vCard data is emitted via PBAP_SUBEVENT_CARD_RESULT event. 324 * Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of vCard listing. 325 * 326 * @param pbap_cid 327 * @param path 328 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 329 */ 330 uint8_t pbap_pull_vcard_listing(uint16_t pbap_cid, const char * path); 331 332 /** 333 * @brief Pull vCard entry. The result is reported via callback (see pbap_connect function), 334 * with packet type set to PBAP_DATA_PACKET. 335 * Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of the vCard entry. 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_entry(uint16_t pbap_cid, const char * path); 342 343 /** 344 * @brief Lookup contact(s) by phone number. vCard data is emitted via PBAP_SUBEVENT_CARD_RESULT event. 345 * Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of the lookup. 346 * 347 * @param pbap_cid 348 * @param phone_number 349 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 350 */ 351 uint8_t pbap_lookup_by_number(uint16_t pbap_cid, const char * phone_number); 352 353 /** 354 * @brief Abort current operation. No event is emitted. 355 * 356 * @param pbap_cid 357 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 358 */ 359 uint8_t pbap_abort(uint16_t pbap_cid); 360 361 362 /** 363 * @brief Set flow control mode - default is off. No event is emitted. 364 * @note When enabled, pbap_next_packet needs to be called after a packet was processed to receive the next one 365 * 366 * @param pbap_cid 367 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 368 */ 369 uint8_t pbap_set_flow_control_mode(uint16_t pbap_cid, int enable); 370 371 /** 372 * @brief Trigger next packet from PSE when Flow Control Mode is enabled. 373 * @param pbap_cid 374 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 375 */ 376 uint8_t pbap_next_packet(uint16_t pbap_cid); 377 378 /** 379 * @brief De-Init PBAP Client 380 */ 381 void pbap_client_deinit(void); 382 383 /* API_END */ 384 385 #if defined __cplusplus 386 } 387 #endif 388 #endif 389