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 = "BC:EC:5D:E6:15:03"; 76 77 static char * phone_number = "911"; 78 79 static btstack_packet_callback_registration_t hci_event_callback_registration; 80 static uint16_t pbap_cid; 81 82 #ifdef HAVE_BTSTACK_STDIN 83 84 // Testig User Interface 85 static void show_usage(void){ 86 bd_addr_t iut_address; 87 gap_local_bd_addr(iut_address); 88 89 printf("\n--- Bluetooth PBAP Client (HF) Test Console %s ---\n", bd_addr_to_str(iut_address)); 90 printf("\n"); 91 printf("a - establish PBAP connection to %s\n", bd_addr_to_str(remote_addr)); 92 printf("b - set phonebook '/telecom/pb'\n"); 93 printf("c - set phonebook '/SIM1/telecom/pb'\n"); 94 printf("d - get phonebook size\n"); 95 printf("e - pull phonebook\n"); 96 printf("f - disconnnect\n"); 97 printf("g - Lookup contact with number '%s'\n", phone_number); 98 printf("p - authenticate using password '0000'\n"); 99 printf("\n"); 100 } 101 102 static void stdin_process(char c){ 103 switch (c){ 104 case 'a': 105 printf("[+] Connecting to %s...\n", bd_addr_to_str(remote_addr)); 106 pbap_connect(&packet_handler, remote_addr, &pbap_cid); 107 break; 108 case 'b': 109 printf("[+] Set Phonebook 'telecom/pb'\n"); 110 pbap_set_phonebook(pbap_cid, "telecom/pb"); 111 break; 112 case 'c': 113 printf("[+] Set Phonebook 'SIM1/telecom/pb'\n"); 114 pbap_set_phonebook(pbap_cid, "SIM1/telecom/pb"); 115 break; 116 case 'd': 117 pbap_get_phonebook_size(pbap_cid); 118 break; 119 case 'e': 120 pbap_pull_phonebook(pbap_cid); 121 break; 122 case 'f': 123 pbap_disconnect(pbap_cid); 124 break; 125 case 'g': 126 pbap_lookup_by_number(pbap_cid, phone_number); 127 break; 128 case 'p': 129 pbap_authentication_password(pbap_cid, "0000"); 130 break; 131 default: 132 show_usage(); 133 break; 134 } 135 } 136 137 // packet handler for interactive console 138 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 139 UNUSED(channel); 140 UNUSED(size); 141 int i; 142 uint8_t status; 143 char buffer[32]; 144 switch (packet_type){ 145 case HCI_EVENT_PACKET: 146 switch (hci_event_packet_get_type(packet)) { 147 case BTSTACK_EVENT_STATE: 148 // BTstack activated, get started 149 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 150 show_usage(); 151 } 152 break; 153 case HCI_EVENT_PBAP_META: 154 switch (hci_event_pbap_meta_get_subevent_code(packet)){ 155 case PBAP_SUBEVENT_CONNECTION_OPENED: 156 status = pbap_subevent_connection_opened_get_status(packet); 157 if (status){ 158 printf("[!] Connection failed, status 0x%02x\n", status); 159 } else { 160 printf("[+] Connected\n"); 161 } 162 break; 163 case PBAP_SUBEVENT_CONNECTION_CLOSED: 164 printf("[+] Connection closed\n"); 165 break; 166 case PBAP_SUBEVENT_OPERATION_COMPLETED: 167 printf("[+] Operation complete\n"); 168 break; 169 case PBAP_SUBEVENT_AUTHENTICATION_REQUEST: 170 printf("[?] Authentication requested\n"); 171 break; 172 case PBAP_SUBEVENT_PHONEBOOK_SIZE: 173 status = pbap_subevent_phonebook_size_get_status(packet); 174 if (status){ 175 printf("[!] Get Phonebook size error: 0x%x\n", status); 176 } else { 177 printf("[+] Phonebook size: %u\n", pbap_subevent_phonebook_size_get_phoneboook_size(packet)); 178 } 179 break; 180 case PBAP_SUBEVENT_CARD_RESULT: 181 memcpy(buffer, pbap_subevent_card_result_get_name(packet), pbap_subevent_card_result_get_name_len(packet)); 182 buffer[pbap_subevent_card_result_get_name_len(packet)] = 0; 183 printf("[-] Name: '%s'\n", buffer); 184 memcpy(buffer, pbap_subevent_card_result_get_handle(packet), pbap_subevent_card_result_get_handle_len(packet)); 185 buffer[pbap_subevent_card_result_get_handle_len(packet)] = 0; 186 printf("[-] Handle: '%s'\n", buffer); 187 break; 188 default: 189 break; 190 } 191 break; 192 default: 193 break; 194 } 195 break; 196 case PBAP_DATA_PACKET: 197 for (i=0;i<size;i++){ 198 printf("%c", packet[i]); 199 } 200 break; 201 default: 202 break; 203 } 204 } 205 #else 206 207 // packet handler for emdded system with fixed operation sequence 208 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 209 UNUSED(channel); 210 UNUSED(size); 211 int i; 212 switch (packet_type){ 213 case HCI_EVENT_PACKET: 214 switch (hci_event_packet_get_type(packet)) { 215 case BTSTACK_EVENT_STATE: 216 // BTstack activated, get started 217 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 218 printf("[+] Connect to Phone Book Server on %s\n", bd_addr_to_str(remote_addr)); 219 pbap_connect(&packet_handler, remote_addr, &pbap_cid); 220 } 221 break; 222 case HCI_EVENT_PBAP_META: 223 switch (hci_event_pbap_meta_get_subevent_code(packet)){ 224 case PBAP_SUBEVENT_CONNECTION_OPENED: 225 printf("[+] Connected\n"); 226 printf("[+] Pull phonebook\n"); 227 pbap_pull_phonebook(pbap_cid); 228 break; 229 case PBAP_SUBEVENT_CONNECTION_CLOSED: 230 printf("[+] Connection closed\n"); 231 break; 232 case PBAP_SUBEVENT_OPERATION_COMPLETED: 233 printf("[+] Operation complete\n"); 234 printf("[+] Pull Phonebook complete\n"); 235 pbap_disconnect(pbap_cid); 236 break; 237 default: 238 break; 239 } 240 break; 241 default: 242 break; 243 } 244 break; 245 case PBAP_DATA_PACKET: 246 for (i=0;i<size;i++){ 247 printf("%c", packet[i]); 248 } 249 break; 250 default: 251 break; 252 } 253 } 254 #endif 255 256 int btstack_main(int argc, const char * argv[]); 257 int btstack_main(int argc, const char * argv[]){ 258 259 (void)argc; 260 (void)argv; 261 262 // init L2CAP 263 l2cap_init(); 264 265 // init RFCOM 266 rfcomm_init(); 267 268 // init GOEP Client 269 goep_client_init(); 270 271 // init PBAP Client 272 pbap_client_init(); 273 274 // register for HCI events 275 hci_event_callback_registration.callback = &packet_handler; 276 hci_add_event_handler(&hci_event_callback_registration); 277 278 sscanf_bd_addr(remote_addr_string, remote_addr); 279 280 #ifdef HAVE_BTSTACK_STDIN 281 btstack_stdin_setup(stdin_process); 282 #endif 283 284 // turn on! 285 hci_power_control(HCI_POWER_ON); 286 287 return 0; 288 } 289 /* EXAMPLE_END */ 290