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 MATTHIAS 24 * RINGWALD 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 #define __BTSTACK_FILE__ "pbap_client_demo.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(pbap_client_demo): Connect to Phonebook Server and get contacts. 42 * 43 * @text Note: The Bluetooth address of the remote Phonbook server is hardcoded. 44 * Change it before running example, then use the UI to connect to it, to set and 45 * query contacts. 46 */ 47 // ***************************************************************************** 48 49 50 #include "btstack_config.h" 51 52 #include <stdint.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 57 #include "btstack_run_loop.h" 58 #include "l2cap.h" 59 #include "classic/rfcomm.h" 60 #include "btstack_event.h" 61 #include "classic/goep_client.h" 62 #include "classic/pbap_client.h" 63 64 #ifdef HAVE_BTSTACK_STDIN 65 #include "btstack_stdin.h" 66 #endif 67 68 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 69 70 static bd_addr_t remote_addr; 71 // MBP2016 "F4-0F-24-3B-1B-E1" 72 // Nexus 7 "30-85-A9-54-2E-78" 73 // iPhone SE "BC:EC:5D:E6:15:03" 74 // PTS "001BDC080AA5" 75 static char * remote_addr_string = "001BDC080AA5"; 76 77 static char * phone_number = "911"; 78 79 static const char * phonebook_path = "telecom/pb.vcf"; 80 81 static btstack_packet_callback_registration_t hci_event_callback_registration; 82 static uint16_t pbap_cid; 83 84 #ifdef HAVE_BTSTACK_STDIN 85 86 // Testig User Interface 87 static void show_usage(void){ 88 bd_addr_t iut_address; 89 gap_local_bd_addr(iut_address); 90 91 printf("\n--- Bluetooth PBAP Client (HF) Test Console %s ---\n", bd_addr_to_str(iut_address)); 92 printf("\n"); 93 printf("a - establish PBAP connection to %s\n", bd_addr_to_str(remote_addr)); 94 printf("b - set phonebook '/telecom/pb'\n"); 95 printf("c - set phonebook '/SIM1/telecom/pb'\n"); 96 printf("r - set path to '/root/telecom'\n"); 97 printf("d - get size of '%s'\n", phonebook_path); 98 printf("e - pull phonebook '%s'\n", phonebook_path); 99 printf("f - disconnnect\n"); 100 printf("g - Lookup contact with number '%s'\n", phone_number); 101 printf("p - authenticate using password '0000'\n"); 102 printf("r - set path to 'telecom'\n"); 103 printf("x - abort operation\n"); 104 printf("\n"); 105 } 106 107 static void stdin_process(char c){ 108 switch (c){ 109 case 'a': 110 printf("[+] Connecting to %s...\n", bd_addr_to_str(remote_addr)); 111 pbap_connect(&packet_handler, remote_addr, &pbap_cid); 112 break; 113 case 'b': 114 printf("[+] Set Phonebook 'telecom/pb'\n"); 115 pbap_set_phonebook(pbap_cid, "telecom/pb"); 116 break; 117 case 'c': 118 printf("[+] Set Phonebook 'SIM1/telecom/pb'\n"); 119 pbap_set_phonebook(pbap_cid, "SIM1/telecom/pb"); 120 break; 121 case 'd': 122 pbap_get_phonebook_size(pbap_cid, phonebook_path); 123 break; 124 case 'e': 125 pbap_pull_phonebook(pbap_cid, phonebook_path); 126 break; 127 case 'f': 128 pbap_disconnect(pbap_cid); 129 break; 130 case 'g': 131 pbap_lookup_by_number(pbap_cid, phone_number); 132 break; 133 case 'p': 134 pbap_authentication_password(pbap_cid, "0000"); 135 break; 136 case 'r': 137 printf("[+] Set path to 'telecom'\n"); 138 pbap_set_phonebook(pbap_cid, "telecom"); 139 break; 140 case 'x': 141 printf("[+] Abort'\n"); 142 pbap_abort(pbap_cid); 143 break; 144 default: 145 show_usage(); 146 break; 147 } 148 } 149 150 // packet handler for interactive console 151 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 152 UNUSED(channel); 153 UNUSED(size); 154 int i; 155 uint8_t status; 156 char buffer[32]; 157 switch (packet_type){ 158 case HCI_EVENT_PACKET: 159 switch (hci_event_packet_get_type(packet)) { 160 case BTSTACK_EVENT_STATE: 161 // BTstack activated, get started 162 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 163 show_usage(); 164 } 165 break; 166 case HCI_EVENT_PBAP_META: 167 switch (hci_event_pbap_meta_get_subevent_code(packet)){ 168 case PBAP_SUBEVENT_CONNECTION_OPENED: 169 status = pbap_subevent_connection_opened_get_status(packet); 170 if (status){ 171 printf("[!] Connection failed, status 0x%02x\n", status); 172 } else { 173 printf("[+] Connected\n"); 174 } 175 break; 176 case PBAP_SUBEVENT_CONNECTION_CLOSED: 177 printf("[+] Connection closed\n"); 178 break; 179 case PBAP_SUBEVENT_OPERATION_COMPLETED: 180 printf("[+] Operation complete\n"); 181 break; 182 case PBAP_SUBEVENT_AUTHENTICATION_REQUEST: 183 printf("[?] Authentication requested\n"); 184 break; 185 case PBAP_SUBEVENT_PHONEBOOK_SIZE: 186 status = pbap_subevent_phonebook_size_get_status(packet); 187 if (status){ 188 printf("[!] Get Phonebook size error: 0x%x\n", status); 189 } else { 190 printf("[+] Phonebook size: %u\n", pbap_subevent_phonebook_size_get_phoneboook_size(packet)); 191 } 192 break; 193 case PBAP_SUBEVENT_CARD_RESULT: 194 memcpy(buffer, pbap_subevent_card_result_get_name(packet), pbap_subevent_card_result_get_name_len(packet)); 195 buffer[pbap_subevent_card_result_get_name_len(packet)] = 0; 196 printf("[-] Name: '%s'\n", buffer); 197 memcpy(buffer, pbap_subevent_card_result_get_handle(packet), pbap_subevent_card_result_get_handle_len(packet)); 198 buffer[pbap_subevent_card_result_get_handle_len(packet)] = 0; 199 printf("[-] Handle: '%s'\n", buffer); 200 break; 201 default: 202 break; 203 } 204 break; 205 default: 206 break; 207 } 208 break; 209 case PBAP_DATA_PACKET: 210 for (i=0;i<size;i++){ 211 printf("%c", packet[i]); 212 } 213 break; 214 default: 215 break; 216 } 217 } 218 #else 219 220 // packet handler for emdded system with fixed operation sequence 221 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 222 UNUSED(channel); 223 UNUSED(size); 224 int i; 225 switch (packet_type){ 226 case HCI_EVENT_PACKET: 227 switch (hci_event_packet_get_type(packet)) { 228 case BTSTACK_EVENT_STATE: 229 // BTstack activated, get started 230 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 231 printf("[+] Connect to Phone Book Server on %s\n", bd_addr_to_str(remote_addr)); 232 pbap_connect(&packet_handler, remote_addr, &pbap_cid); 233 } 234 break; 235 case HCI_EVENT_PBAP_META: 236 switch (hci_event_pbap_meta_get_subevent_code(packet)){ 237 case PBAP_SUBEVENT_CONNECTION_OPENED: 238 printf("[+] Connected\n"); 239 printf("[+] Pull phonebook\n"); 240 pbap_pull_phonebook(pbap_cid); 241 break; 242 case PBAP_SUBEVENT_CONNECTION_CLOSED: 243 printf("[+] Connection closed\n"); 244 break; 245 case PBAP_SUBEVENT_OPERATION_COMPLETED: 246 printf("[+] Operation complete\n"); 247 printf("[+] Pull Phonebook complete\n"); 248 pbap_disconnect(pbap_cid); 249 break; 250 default: 251 break; 252 } 253 break; 254 default: 255 break; 256 } 257 break; 258 case PBAP_DATA_PACKET: 259 for (i=0;i<size;i++){ 260 printf("%c", packet[i]); 261 } 262 break; 263 default: 264 break; 265 } 266 } 267 #endif 268 269 int btstack_main(int argc, const char * argv[]); 270 int btstack_main(int argc, const char * argv[]){ 271 272 (void)argc; 273 (void)argv; 274 275 // init L2CAP 276 l2cap_init(); 277 278 // init RFCOM 279 rfcomm_init(); 280 281 // init GOEP Client 282 goep_client_init(); 283 284 // init PBAP Client 285 pbap_client_init(); 286 287 // register for HCI events 288 hci_event_callback_registration.callback = &packet_handler; 289 hci_add_event_handler(&hci_event_callback_registration); 290 291 sscanf_bd_addr(remote_addr_string, remote_addr); 292 293 #ifdef HAVE_BTSTACK_STDIN 294 btstack_stdin_setup(stdin_process); 295 #endif 296 297 // turn on! 298 hci_power_control(HCI_POWER_ON); 299 300 return 0; 301 } 302 /* EXAMPLE_END */ 303