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 struct pbap_client { 113 // pbap client linked list 114 btstack_linked_item_t item; 115 116 // goep client linked list 117 goep_client_t goep_client; 118 119 pbap_client_state_t state; 120 bd_addr_t bd_addr; 121 hci_con_handle_t con_handle; 122 uint8_t incoming; 123 uint16_t goep_cid; 124 btstack_packet_handler_t client_handler; 125 int request_number; 126 const char * current_folder; 127 const char * phone_number; 128 const char * phonebook_path; 129 const char * vcard_name; 130 uint16_t set_path_offset; 131 /* vcard selector / operator */ 132 uint32_t vcard_selector; 133 uint8_t vcard_selector_operator; 134 uint8_t vcard_selector_supported; 135 /* property selector */ 136 uint32_t property_selector; 137 uint16_t list_start_offset; 138 uint16_t max_list_count; 139 /* abort */ 140 uint8_t abort_operation; 141 /* obex parser */ 142 bool obex_parser_waiting_for_response; 143 obex_parser_t obex_parser; 144 uint8_t obex_header_buffer[4]; 145 /* authentication */ 146 obex_auth_parser_t obex_auth_parser; 147 const char * authentication_password; 148 /* xml parser */ 149 yxml_t xml_parser; 150 uint8_t xml_buffer[50]; 151 /* vcard listing parser */ 152 bool parser_card_found; 153 bool parser_name_found; 154 bool parser_handle_found; 155 char parser_name[PBAP_MAX_NAME_LEN]; 156 char parser_handle[PBAP_MAX_HANDLE_LEN]; 157 /* phonebook size */ 158 pbap_client_phonebook_size_parser_t phonebook_size_parser; 159 /* flow control mode */ 160 uint8_t flow_control_enabled; 161 uint8_t flow_next_triggered; 162 bool flow_wait_for_user; 163 /* srm */ 164 obex_srm_t obex_srm; 165 obex_srm_state_t srm_state; 166 } pbap_client_t; 167 168 /* API_START */ 169 170 /** 171 * Setup PhoneBook Access Client 172 */ 173 void pbap_client_init(void); 174 175 /** 176 * @brief Create PBAP connection to a Phone Book Server (PSE) server on a remote device. 177 * If the server requires authentication, a PBAP_SUBEVENT_AUTHENTICATION_REQUEST is emitted, which 178 * can be answered with pbap_authentication_password(..). 179 * The status of PBAP connection establishment is reported via PBAP_SUBEVENT_CONNECTION_OPENED event, 180 * i.e. on success status field is set to ERROR_CODE_SUCCESS. 181 * 182 * @param handler 183 * @param addr 184 * @param out_cid to use for further commands 185 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_MEMORY_ALLOC_FAILED if PBAP or GOEP connection already exists. 186 */ 187 uint8_t pbap_connect(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t * out_cid); 188 189 /** 190 * @brief Provide password for OBEX Authentication after receiving PBAP_SUBEVENT_AUTHENTICATION_REQUEST. 191 * The status of PBAP connection establishment is reported via PBAP_SUBEVENT_CONNECTION_OPENED event, 192 * i.e. on success status field is set to ERROR_CODE_SUCCESS. 193 * 194 * @param pbap_cid 195 * @param password (null terminated string) - not copied, needs to stay valid until connection completed 196 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 197 */ 198 uint8_t pbap_authentication_password(uint16_t pbap_cid, const char * password); 199 200 /** 201 * @brief Disconnects PBAP connection with given identifier. 202 * Event PBAP_SUBEVENT_CONNECTION_CLOSED indicates that PBAP connection is closed. 203 * 204 * @param pbap_cid 205 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 206 */ 207 uint8_t pbap_disconnect(uint16_t pbap_cid); 208 209 /** 210 * @brief Set current folder on PSE. The status is reported via PBAP_SUBEVENT_OPERATION_COMPLETED event. 211 * 212 * @param pbap_cid 213 * @param path - note: path is not copied 214 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 215 */ 216 uint8_t pbap_set_phonebook(uint16_t pbap_cid, const char * path); 217 218 /** 219 * @brief Set vCard Selector for get/pull phonebook. No event is emitted. 220 * 221 * @param pbap_cid 222 * @param vcard_selector - combination of PBAP_PROPERTY_MASK_* properties 223 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 224 */ 225 uint8_t pbap_set_vcard_selector(uint16_t pbap_cid, uint32_t vcard_selector); 226 227 /** 228 * @brief Set vCard Selector for get/pull phonebook. No event is emitted. 229 * 230 * @param pbap_cid 231 * @param vcard_selector_operator - PBAP_VCARD_SELECTOR_OPERATOR_OR (default) or PBAP_VCARD_SELECTOR_OPERATOR_AND 232 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 233 */ 234 uint8_t pbap_set_vcard_selector_operator(uint16_t pbap_cid, int vcard_selector_operator); 235 236 /** 237 * @brief Set Property Selector. No event is emitted. 238 * @param pbap_cid 239 * @param property_selector - combination of PBAP_PROPERTY_MASK_* properties 240 * @return 241 */ 242 uint8_t pbap_set_property_selector(uint16_t pbap_cid, uint32_t property_selector); 243 244 /** 245 * @brief Set number of items returned by pull phonebook. 246 * @param pbap_cid 247 * @param max_list_count 248 * @return 249 */ 250 uint8_t pbap_set_max_list_count(uint16_t pbap_cid, uint16_t max_list_count); 251 252 /** 253 * @bbrief Set start offset for pull phonebook 254 * @param pbap_cid 255 * @param list_start_offset 256 * @return 257 */ 258 uint8_t pbap_set_list_start_offset(uint16_t pbap_cid, uint16_t list_start_offset); 259 260 /** 261 * @brief Get size of phone book from PSE. The result is reported via PBAP_SUBEVENT_PHONEBOOK_SIZE event. 262 * 263 * @param pbap_cid 264 * @param path - note: path is not copied, common path 'telecom/pb.vcf' 265 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 266 */ 267 uint8_t pbap_get_phonebook_size(uint16_t pbap_cid, const char * path); 268 269 /** 270 * @brief Pull phone book from PSE. The result is reported via registered packet handler (see pbap_connect function), 271 * with packet type set to PBAP_DATA_PACKET. Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of the phone book. 272 * 273 * @param pbap_cid 274 * @param path - note: path is not copied, common path 'telecom/pb.vcf' 275 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 276 */ 277 uint8_t pbap_pull_phonebook(uint16_t pbap_cid, const char * path); 278 279 /** 280 * @brief Pull vCard listing. vCard data is emitted via PBAP_SUBEVENT_CARD_RESULT event. 281 * Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of vCard listing. 282 * 283 * @param pbap_cid 284 * @param path 285 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 286 */ 287 uint8_t pbap_pull_vcard_listing(uint16_t pbap_cid, const char * path); 288 289 /** 290 * @brief Pull vCard entry. The result is reported via callback (see pbap_connect function), 291 * with packet type set to PBAP_DATA_PACKET. 292 * Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of the vCard entry. 293 * 294 * @param pbap_cid 295 * @param path 296 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 297 */ 298 uint8_t pbap_pull_vcard_entry(uint16_t pbap_cid, const char * path); 299 300 /** 301 * @brief Lookup contact(s) by phone number. vCard data is emitted via PBAP_SUBEVENT_CARD_RESULT event. 302 * Event PBAP_SUBEVENT_OPERATION_COMPLETED marks the end of the lookup. 303 * 304 * @param pbap_cid 305 * @param phone_number 306 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 307 */ 308 uint8_t pbap_lookup_by_number(uint16_t pbap_cid, const char * phone_number); 309 310 /** 311 * @brief Abort current operation. No event is emitted. 312 * 313 * @param pbap_cid 314 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 315 */ 316 uint8_t pbap_abort(uint16_t pbap_cid); 317 318 319 /** 320 * @brief Set flow control mode - default is off. No event is emitted. 321 * @note When enabled, pbap_next_packet needs to be called after a packet was processed to receive the next one 322 * 323 * @param pbap_cid 324 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 325 */ 326 uint8_t pbap_set_flow_control_mode(uint16_t pbap_cid, int enable); 327 328 /** 329 * @brief Trigger next packet from PSE when Flow Control Mode is enabled. 330 * @param pbap_cid 331 * @return status ERROR_CODE_SUCCESS on success, otherwise BTSTACK_BUSY if in a wrong state. 332 */ 333 uint8_t pbap_next_packet(uint16_t pbap_cid); 334 335 /** 336 * @brief De-Init PBAP Client 337 */ 338 void pbap_client_deinit(void); 339 340 /* API_END */ 341 342 #if defined __cplusplus 343 } 344 #endif 345 #endif 346